|  | 
| 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; | 
| 24 | 28 | import org.apache.http.impl.nio.client.HttpAsyncClientBuilder; | 
|  | 29 | +import org.assertj.core.api.InstanceOfAssertFactories; | 
| 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,70 @@ void restClientCanQueryElasticsearchNode() { | 
| 156 | 163 | 				}); | 
| 157 | 164 | 	} | 
| 158 | 165 | 
 | 
|  | 166 | +	@Test | 
|  | 167 | +	void configureUriWithUsernameOnly() { | 
|  | 168 | +		this.contextRunner.withPropertyValues("spring.elasticsearch.rest.uris=http://user@localhost:9200") | 
|  | 169 | +				.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 | +					assertThat(client).extracting("client") | 
|  | 174 | +							.extracting("credentialsProvider", | 
|  | 175 | +									InstanceOfAssertFactories.type(CredentialsProvider.class)) | 
|  | 176 | +							.satisfies((credentialsProvider) -> { | 
|  | 177 | +								Credentials credentials = credentialsProvider | 
|  | 178 | +										.getCredentials(new AuthScope("localhost", 9200)); | 
|  | 179 | +								assertThat(credentials.getUserPrincipal().getName()).isEqualTo("user"); | 
|  | 180 | +								assertThat(credentials.getPassword()).isNull(); | 
|  | 181 | +							}); | 
|  | 182 | +				}); | 
|  | 183 | +	} | 
|  | 184 | + | 
|  | 185 | +	@Test | 
|  | 186 | +	void configureUriWithUsernameAndEmptyPassword() { | 
|  | 187 | +		this.contextRunner.withPropertyValues("spring.elasticsearch.rest.uris=http://user:@localhost:9200") | 
|  | 188 | +				.run((context) -> { | 
|  | 189 | +					RestClient client = context.getBean(RestClient.class); | 
|  | 190 | +					assertThat(client.getNodes().stream().map(Node::getHost).map(HttpHost::toString)) | 
|  | 191 | +							.containsExactly("http://localhost:9200"); | 
|  | 192 | +					assertThat(client).extracting("client") | 
|  | 193 | +							.extracting("credentialsProvider", | 
|  | 194 | +									InstanceOfAssertFactories.type(CredentialsProvider.class)) | 
|  | 195 | +							.satisfies((credentialsProvider) -> { | 
|  | 196 | +								Credentials credentials = credentialsProvider | 
|  | 197 | +										.getCredentials(new AuthScope("localhost", 9200)); | 
|  | 198 | +								assertThat(credentials.getUserPrincipal().getName()).isEqualTo("user"); | 
|  | 199 | +								assertThat(credentials.getPassword()).isEmpty(); | 
|  | 200 | +							}); | 
|  | 201 | +				}); | 
|  | 202 | +	} | 
|  | 203 | + | 
|  | 204 | +	@Test | 
|  | 205 | +	void configureUriWithUsernameAndPasswordWhenUsernameAndPasswordPropertiesSet() { | 
|  | 206 | +		this.contextRunner | 
|  | 207 | +				.withPropertyValues("spring.elasticsearch.rest.uris=http://user:password@localhost:9200,localhost:9201", | 
|  | 208 | +						"spring.elasticsearch.rest.username=admin", "spring.elasticsearch.rest.password=admin") | 
|  | 209 | +				.run((context) -> { | 
|  | 210 | +					RestClient client = context.getBean(RestClient.class); | 
|  | 211 | +					assertThat(client.getNodes().stream().map(Node::getHost).map(HttpHost::toString)) | 
|  | 212 | +							.containsExactly("http://localhost:9200", "http://localhost:9201"); | 
|  | 213 | +					assertThat(client).extracting("client") | 
|  | 214 | +							.extracting("credentialsProvider", | 
|  | 215 | +									InstanceOfAssertFactories.type(CredentialsProvider.class)) | 
|  | 216 | +							.satisfies((credentialsProvider) -> { | 
|  | 217 | +								Credentials uriCredentials = credentialsProvider | 
|  | 218 | +										.getCredentials(new AuthScope("localhost", 9200)); | 
|  | 219 | +								assertThat(uriCredentials.getUserPrincipal().getName()).isEqualTo("user"); | 
|  | 220 | +								assertThat(uriCredentials.getPassword()).isEqualTo("password"); | 
|  | 221 | + | 
|  | 222 | +								Credentials defaultCredentials = credentialsProvider | 
|  | 223 | +										.getCredentials(new AuthScope("localhost", 9201)); | 
|  | 224 | +								assertThat(defaultCredentials.getUserPrincipal().getName()).isEqualTo("admin"); | 
|  | 225 | +								assertThat(defaultCredentials.getPassword()).isEqualTo("admin"); | 
|  | 226 | +							}); | 
|  | 227 | +				}); | 
|  | 228 | +	} | 
|  | 229 | + | 
| 159 | 230 | 	@Configuration(proxyBeanMethods = false) | 
| 160 | 231 | 	static class CustomRestClientConfiguration { | 
| 161 | 232 | 
 | 
|  | 
0 commit comments