Skip to content

Add the overload of usingSsl in ClientConfiguration that would accept boolean #2778

Closed
@protector1990

Description

@protector1990

Hi,

Current way of telling elastic client whether to use https or not is by calling usingSssl on the builder like this:

@Configuration
@RequiredArgsConstructor
@EnableElasticsearchRepositories
@Profile({"staging", "prod"})
public class ElasticsearchConfig extends ElasticsearchConfiguration {

  private final ElasticsearchConfigProperties properties;

  @NotNull
  @Override
  @Bean(name = {"elasticsearchClientConfiguration"})
  public ClientConfiguration clientConfiguration() {
    return ClientConfiguration.builder()
        .connectedTo(properties.getHost() + ":" + properties.getPort())
        .usingSsl()
        .withBasicAuth(properties.getUsername(), properties.getPassword())
        .build();
  }
}

The problem with that is that in different profiles you may or may not need https. With this way of configuring it, you either need to create a separate config class for a profile where you don't need https, and omit .usingSsl() there, or do something like this:

@NotNull
  @Override
  @Bean(name = {"elasticsearchClientConfiguration"})
  public ClientConfiguration clientConfiguration() {
    var clientBuilder = ClientConfiguration.builder()
        .connectedTo(properties.getHost() + ":" + properties.getPort())
        .withBasicAuth(properties.getUsername(), properties.getPassword());
       
    if (properties.useHttps()) {
        clientBuilder.usingSsl()
    }

    return clientBuilder.build();
  }
}

I suggest adding an overload of usingSsl that would accept a boolean, so you could do something like this:

@NotNull
  @Override
  @Bean(name = {"elasticsearchClientConfiguration"})
  public ClientConfiguration clientConfiguration() {
    return ClientConfiguration.builder()
        .connectedTo(properties.getHost() + ":" + properties.getPort())
        .usingSsl(properties.useHttps())
        .withBasicAuth(properties.getUsername(), properties.getPassword())
        .build();
  }

Note that ElasticsearchConfigProperties is my configuration properties class. With this approach you could configure using ssl or not with .properties files.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions