|
20 | 20 | import java.util.HashMap;
|
21 | 21 | import java.util.Map;
|
22 | 22 |
|
| 23 | +import org.apache.http.HttpHost; |
| 24 | +import org.apache.http.auth.AuthScope; |
| 25 | +import org.apache.http.auth.Credentials; |
| 26 | +import org.apache.http.client.CredentialsProvider; |
23 | 27 | import org.apache.http.client.config.RequestConfig;
|
| 28 | +import org.apache.http.impl.client.BasicCredentialsProvider; |
24 | 29 | import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
|
25 | 30 | import org.elasticsearch.action.get.GetRequest;
|
26 | 31 | import org.elasticsearch.action.index.IndexRequest;
|
| 32 | +import org.elasticsearch.client.Node; |
27 | 33 | import org.elasticsearch.client.RequestOptions;
|
28 | 34 | import org.elasticsearch.client.RestClient;
|
29 | 35 | import org.elasticsearch.client.RestClientBuilder;
|
|
47 | 53 | *
|
48 | 54 | * @author Brian Clozel
|
49 | 55 | * @author Vedran Pavic
|
| 56 | + * @author Evgeniy Cheban |
50 | 57 | */
|
51 | 58 | @Testcontainers(disabledWithoutDocker = true)
|
52 | 59 | class ElasticsearchRestClientAutoConfigurationTests {
|
@@ -156,6 +163,69 @@ void restClientCanQueryElasticsearchNode() {
|
156 | 163 | });
|
157 | 164 | }
|
158 | 165 |
|
| 166 | + @Test |
| 167 | + void configureUriWithUsernameOnly() { |
| 168 | + this.contextRunner.withUserConfiguration(CredentialsProviderConfiguration.class) |
| 169 | + .withPropertyValues("spring.elasticsearch.rest.uris=http://user@localhost:9200").run((context) -> { |
| 170 | + RestClient client = context.getBean(RestClient.class); |
| 171 | + assertThat(client.getNodes().stream().map(Node::getHost).map(HttpHost::toString)) |
| 172 | + .containsExactly("http://localhost:9200"); |
| 173 | + |
| 174 | + CredentialsProvider credentialsProvider = context.getBean(CredentialsProvider.class); |
| 175 | + Credentials credentials = credentialsProvider.getCredentials(new AuthScope("localhost", 9200)); |
| 176 | + assertThat(credentials.getUserPrincipal().getName()).isEqualTo("user"); |
| 177 | + assertThat(credentials.getPassword()).isNull(); |
| 178 | + }); |
| 179 | + } |
| 180 | + |
| 181 | + @Test |
| 182 | + void configureUriWithUsernameAndEmptyPassword() { |
| 183 | + this.contextRunner.withUserConfiguration(CredentialsProviderConfiguration.class) |
| 184 | + .withPropertyValues("spring.elasticsearch.rest.uris=http://user:@localhost:9200").run((context) -> { |
| 185 | + RestClient client = context.getBean(RestClient.class); |
| 186 | + assertThat(client.getNodes().stream().map(Node::getHost).map(HttpHost::toString)) |
| 187 | + .containsExactly("http://localhost:9200"); |
| 188 | + |
| 189 | + CredentialsProvider credentialsProvider = context.getBean(CredentialsProvider.class); |
| 190 | + Credentials credentials = credentialsProvider.getCredentials(new AuthScope("localhost", 9200)); |
| 191 | + assertThat(credentials.getUserPrincipal().getName()).isEqualTo("user"); |
| 192 | + assertThat(credentials.getPassword()).isEmpty(); |
| 193 | + }); |
| 194 | + } |
| 195 | + |
| 196 | + @Test |
| 197 | + void configureUriWithUsernameAndPasswordWhenUsernameAndPasswordPropertiesSet() { |
| 198 | + this.contextRunner.withUserConfiguration(CredentialsProviderConfiguration.class) |
| 199 | + .withPropertyValues("spring.elasticsearch.rest.uris=http://user:password@localhost:9200,localhost:9201", |
| 200 | + "spring.elasticsearch.rest.username=admin", "spring.elasticsearch.rest.password=admin") |
| 201 | + .run((context) -> { |
| 202 | + RestClient client = context.getBean(RestClient.class); |
| 203 | + assertThat(client.getNodes().stream().map(Node::getHost).map(HttpHost::toString)) |
| 204 | + .containsExactly("http://localhost:9200", "http://localhost:9201"); |
| 205 | + |
| 206 | + CredentialsProvider credentialsProvider = context.getBean(CredentialsProvider.class); |
| 207 | + |
| 208 | + Credentials uriCredentials = credentialsProvider.getCredentials(new AuthScope("localhost", 9200)); |
| 209 | + assertThat(uriCredentials.getUserPrincipal().getName()).isEqualTo("user"); |
| 210 | + assertThat(uriCredentials.getPassword()).isEqualTo("password"); |
| 211 | + |
| 212 | + Credentials defaultCredentials = credentialsProvider |
| 213 | + .getCredentials(new AuthScope("localhost", 9201)); |
| 214 | + assertThat(defaultCredentials.getUserPrincipal().getName()).isEqualTo("admin"); |
| 215 | + assertThat(defaultCredentials.getPassword()).isEqualTo("admin"); |
| 216 | + }); |
| 217 | + } |
| 218 | + |
| 219 | + @Configuration(proxyBeanMethods = false) |
| 220 | + static class CredentialsProviderConfiguration { |
| 221 | + |
| 222 | + @Bean |
| 223 | + CredentialsProvider credentialsProvider() { |
| 224 | + return new BasicCredentialsProvider(); |
| 225 | + } |
| 226 | + |
| 227 | + } |
| 228 | + |
159 | 229 | @Configuration(proxyBeanMethods = false)
|
160 | 230 | static class CustomRestClientConfiguration {
|
161 | 231 |
|
|
0 commit comments