1616
1717package  org .springframework .boot .autoconfigure .elasticsearch ;
1818
19+ import  java .net .URI ;
20+ import  java .net .URISyntaxException ;
1921import  java .time .Duration ;
2022
2123import  org .apache .http .HttpHost ;
2224import  org .apache .http .auth .AuthScope ;
2325import  org .apache .http .auth .Credentials ;
2426import  org .apache .http .auth .UsernamePasswordCredentials ;
25- import  org .apache .http .client .CredentialsProvider ;
2627import  org .apache .http .client .config .RequestConfig ;
2728import  org .apache .http .impl .client .BasicCredentialsProvider ;
2829import  org .apache .http .impl .nio .client .HttpAsyncClientBuilder ;
3637import  org .springframework .boot .context .properties .PropertyMapper ;
3738import  org .springframework .context .annotation .Bean ;
3839import  org .springframework .context .annotation .Configuration ;
40+ import  org .springframework .util .StringUtils ;
3941
4042/** 
4143 * Elasticsearch rest client infrastructure configurations. 
4244 * 
4345 * @author Brian Clozel 
4446 * @author Stephane Nicoll 
4547 * @author Vedran Pavic 
48+  * @author Evgeniy Cheban 
4649 */ 
4750class  ElasticsearchRestClientConfigurations  {
4851
@@ -58,7 +61,7 @@ RestClientBuilderCustomizer defaultRestClientBuilderCustomizer(ElasticsearchRest
5861		@ Bean 
5962		RestClientBuilder  elasticsearchRestClientBuilder (ElasticsearchRestClientProperties  properties ,
6063				ObjectProvider <RestClientBuilderCustomizer > builderCustomizers ) {
61- 			HttpHost [] hosts  = properties .getUris ().stream ().map (HttpHost :: create ).toArray (HttpHost []::new );
64+ 			HttpHost [] hosts  = properties .getUris ().stream ().map (this :: createHttpHost ).toArray (HttpHost []::new );
6265			RestClientBuilder  builder  = RestClient .builder (hosts );
6366			builder .setHttpClientConfigCallback ((httpClientBuilder ) -> {
6467				builderCustomizers .orderedStream ().forEach ((customizer ) -> customizer .customize (httpClientBuilder ));
@@ -72,6 +75,28 @@ RestClientBuilder elasticsearchRestClientBuilder(ElasticsearchRestClientProperti
7275			return  builder ;
7376		}
7477
78+ 		private  HttpHost  createHttpHost (String  uri ) {
79+ 			try  {
80+ 				return  createHttpHost (URI .create (uri ));
81+ 			}
82+ 			catch  (IllegalArgumentException  ex ) {
83+ 				return  HttpHost .create (uri );
84+ 			}
85+ 		}
86+ 
87+ 		private  HttpHost  createHttpHost (URI  uri ) {
88+ 			if  (!StringUtils .hasLength (uri .getUserInfo ())) {
89+ 				return  HttpHost .create (uri .toString ());
90+ 			}
91+ 			try  {
92+ 				return  HttpHost .create (new  URI (uri .getScheme (), null , uri .getHost (), uri .getPort (), uri .getPath (),
93+ 						uri .getQuery (), uri .getFragment ()).toString ());
94+ 			}
95+ 			catch  (URISyntaxException  ex ) {
96+ 				throw  new  IllegalStateException (ex );
97+ 			}
98+ 		}
99+ 
75100	}
76101
77102	@ Configuration (proxyBeanMethods  = false )
@@ -124,13 +149,7 @@ public void customize(RestClientBuilder builder) {
124149
125150		@ Override 
126151		public  void  customize (HttpAsyncClientBuilder  builder ) {
127- 			map .from (this .properties ::getUsername ).whenHasText ().to ((username ) -> {
128- 				CredentialsProvider  credentialsProvider  = new  BasicCredentialsProvider ();
129- 				Credentials  credentials  = new  UsernamePasswordCredentials (this .properties .getUsername (),
130- 						this .properties .getPassword ());
131- 				credentialsProvider .setCredentials (AuthScope .ANY , credentials );
132- 				builder .setDefaultCredentialsProvider (credentialsProvider );
133- 			});
152+ 			builder .setDefaultCredentialsProvider (new  PropertiesCredentialsProvider (this .properties ));
134153		}
135154
136155		@ Override 
@@ -143,4 +162,47 @@ public void customize(RequestConfig.Builder builder) {
143162
144163	}
145164
165+ 	private  static  class  PropertiesCredentialsProvider  extends  BasicCredentialsProvider  {
166+ 
167+ 		PropertiesCredentialsProvider (ElasticsearchRestClientProperties  properties ) {
168+ 			if  (StringUtils .hasText (properties .getUsername ())) {
169+ 				Credentials  credentials  = new  UsernamePasswordCredentials (properties .getUsername (),
170+ 						properties .getPassword ());
171+ 				setCredentials (AuthScope .ANY , credentials );
172+ 			}
173+ 			properties .getUris ().stream ().map (this ::toUri ).filter (this ::hasUserInfo )
174+ 					.forEach (this ::addUserInfoCredentials );
175+ 		}
176+ 
177+ 		private  URI  toUri (String  uri ) {
178+ 			try  {
179+ 				return  URI .create (uri );
180+ 			}
181+ 			catch  (IllegalArgumentException  ex ) {
182+ 				return  null ;
183+ 			}
184+ 		}
185+ 
186+ 		private  boolean  hasUserInfo (URI  uri ) {
187+ 			return  uri  != null  && StringUtils .hasLength (uri .getUserInfo ());
188+ 		}
189+ 
190+ 		private  void  addUserInfoCredentials (URI  uri ) {
191+ 			AuthScope  authScope  = new  AuthScope (uri .getHost (), uri .getPort ());
192+ 			Credentials  credentials  = createUserInfoCredentials (uri .getUserInfo ());
193+ 			setCredentials (authScope , credentials );
194+ 		}
195+ 
196+ 		private  Credentials  createUserInfoCredentials (String  userInfo ) {
197+ 			int  delimiter  = userInfo .indexOf (":" );
198+ 			if  (delimiter  == -1 ) {
199+ 				return  new  UsernamePasswordCredentials (userInfo , null );
200+ 			}
201+ 			String  username  = userInfo .substring (0 , delimiter );
202+ 			String  password  = userInfo .substring (delimiter  + 1 );
203+ 			return  new  UsernamePasswordCredentials (username , password );
204+ 		}
205+ 
206+ 	}
207+ 
146208}
0 commit comments