mirror of
				https://github.com/open-metadata/OpenMetadata.git
				synced 2025-10-31 18:48:35 +00:00 
			
		
		
		
	ISSUE-861: add elasticsearch username & password (#894)
* ISSUE-861: add elasticsearch username & password * ISSUE-861: python elasticsearch sink add username & password * ISSUE-861: bugfix * format code * format code
This commit is contained in:
		
							parent
							
								
									e2782168e0
								
							
						
					
					
						commit
						8e06fa25b6
					
				| @ -26,6 +26,10 @@ public class ElasticSearchConfiguration { | ||||
|     @NotEmpty | ||||
|     private Integer port; | ||||
| 
 | ||||
|     private String username; | ||||
| 
 | ||||
|     private String password; | ||||
| 
 | ||||
|     public String getHost() { | ||||
|         return host; | ||||
|     } | ||||
| @ -42,11 +46,29 @@ public class ElasticSearchConfiguration { | ||||
|         this.port = port; | ||||
|     } | ||||
| 
 | ||||
|     public String getUsername() { | ||||
|         return username; | ||||
|     } | ||||
| 
 | ||||
|     public void setUsername(String username) { | ||||
|         this.username = username; | ||||
|     } | ||||
| 
 | ||||
|     public String getPassword() { | ||||
|         return password; | ||||
|     } | ||||
| 
 | ||||
|     public void setPassword(String password) { | ||||
|         this.password = password; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public String toString() { | ||||
|         return "ElasticSearchConfiguration {" + | ||||
|                 ", host=" +  host + | ||||
|         return "ElasticSearchConfiguration{" + | ||||
|                 "host='" + host + '\'' + | ||||
|                 ", port=" + port + | ||||
|                 "}"; | ||||
|                 ", username='" + username + '\'' + | ||||
|                 ", password='" + password + '\'' + | ||||
|                 '}'; | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -17,6 +17,12 @@ | ||||
| package org.openmetadata.catalog.resources.search; | ||||
| 
 | ||||
| 
 | ||||
| import org.apache.commons.lang3.StringUtils; | ||||
| import org.apache.http.auth.AuthScope; | ||||
| import org.apache.http.auth.UsernamePasswordCredentials; | ||||
| import org.apache.http.client.CredentialsProvider; | ||||
| import org.apache.http.impl.client.BasicCredentialsProvider; | ||||
| import org.elasticsearch.client.RestClientBuilder; | ||||
| import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; | ||||
| import org.elasticsearch.search.suggest.Suggest; | ||||
| import org.elasticsearch.search.suggest.SuggestBuilder; | ||||
| @ -66,9 +72,16 @@ public class SearchResource { | ||||
|   private static final Logger LOG = LoggerFactory.getLogger(SearchResource.class); | ||||
| 
 | ||||
|   public SearchResource(ElasticSearchConfiguration esConfig) { | ||||
|     this.client = new RestHighLevelClient( | ||||
|             RestClient.builder(new HttpHost(esConfig.getHost(), esConfig.getPort(), "http")) | ||||
|     ); | ||||
|     RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(esConfig.getHost(), esConfig.getPort(), "http")); | ||||
|     if(StringUtils.isNotEmpty(esConfig.getUsername())){ | ||||
|       CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); | ||||
|       credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(esConfig.getUsername(), esConfig.getPassword())); | ||||
|       restClientBuilder.setHttpClientConfigCallback(httpAsyncClientBuilder -> { | ||||
|         httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider); | ||||
|         return  httpAsyncClientBuilder; | ||||
|       }); | ||||
|     } | ||||
|     this.client = new RestHighLevelClient(restClientBuilder); | ||||
|   } | ||||
| 
 | ||||
|   @GET | ||||
|  | ||||
| @ -52,6 +52,8 @@ logger = logging.getLogger(__name__) | ||||
| class ElasticSearchConfig(ConfigModel): | ||||
|     es_host: str | ||||
|     es_port: int = 9200 | ||||
|     es_username: str | ||||
|     es_password: str | ||||
|     index_tables: Optional[bool] = True | ||||
|     index_topics: Optional[bool] = True | ||||
|     index_dashboards: Optional[bool] = True | ||||
| @ -88,10 +90,14 @@ class ElasticsearchSink(Sink): | ||||
|         self.status = SinkStatus() | ||||
|         self.rest = OpenMetadataAPIClient(self.metadata_config) | ||||
|         self.elasticsearch_doc_type = "_doc" | ||||
|         http_auth = None | ||||
|         if self.config.es_username: | ||||
|             http_auth = (self.config.es_username, self.config.es_password) | ||||
|         self.elasticsearch_client = Elasticsearch( | ||||
|             [ | ||||
|                 {"host": self.config.es_host, "port": self.config.es_port}, | ||||
|             ] | ||||
|             ], | ||||
|             http_auth=http_auth, | ||||
|         ) | ||||
|         if self.config.index_tables: | ||||
|             self._check_or_create_index( | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 rong fengliang
						rong fengliang