Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
protector1990 opened this issue Nov 22, 2023 · 0 comments · Fixed by #2780
Closed

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

protector1990 opened this issue Nov 22, 2023 · 0 comments · Fixed by #2780
Assignees
Labels
type: enhancement A general enhancement

Comments

@protector1990
Copy link

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 22, 2023
@sothawo sothawo added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Nov 22, 2023
@sothawo sothawo added this to the 5.3 M1 (2024.0.0) milestone Nov 24, 2023
@sothawo sothawo self-assigned this Nov 24, 2023
sothawo added a commit to sothawo/spring-data-elasticsearch that referenced this issue Nov 24, 2023
sothawo added a commit that referenced this issue Nov 24, 2023
Original Pull Request #2780
Closes #2778
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants