1313import co .elastic .clients .transport .endpoints .BooleanResponse ;
1414import co .elastic .clients .transport .rest_client .RestClientTransport ;
1515import com .fasterxml .jackson .databind .module .SimpleModule ;
16+ import org .apache .http .Header ;
1617import org .apache .http .HttpHost ;
18+ import org .apache .http .auth .AuthScope ;
19+ import org .apache .http .auth .UsernamePasswordCredentials ;
20+ import org .apache .http .client .CredentialsProvider ;
21+ import org .apache .http .impl .client .BasicCredentialsProvider ;
22+ import org .apache .http .message .BasicHeader ;
1723import org .elasticsearch .client .RestClient ;
24+ import org .elasticsearch .client .RestClientBuilder ;
1825import org .epics .vtype .VType ;
1926import org .phoebus .applications .saveandrestore .model .Node ;
2027import org .phoebus .applications .saveandrestore .model .NodeType ;
@@ -75,6 +82,16 @@ public class ElasticConfig {
7582 @ Value ("${elasticsearch.http.port:9200}" )
7683 private int port ;
7784
85+ @ Value ("${elasticsearch.authorization.header:}" )
86+ private String authorizationHeader ;
87+
88+ @ Value ("${elasticsearch.authorization.username:}" )
89+ private String username ;
90+
91+ @ Value ("${elasticsearch.authorization.password}" )
92+ private String password ;
93+
94+
7895 private ElasticsearchClient client ;
7996 private static final AtomicBoolean esInitialized = new AtomicBoolean ();
8097
@@ -95,8 +112,20 @@ public class ElasticConfig {
95112 public ElasticsearchClient getClient () {
96113 if (client == null ) {
97114 // Create the low-level client
98- RestClient httpClient = RestClient .builder (new HttpHost (host , port )). build ( );
115+ RestClientBuilder clientBuilder = RestClient .builder (new HttpHost (host , port ));
99116
117+ // Configure authentication
118+ if (!authorizationHeader .isEmpty ()) {
119+ clientBuilder .setDefaultHeaders (new Header [] {new BasicHeader ("Authorization" , authorizationHeader )});
120+ if (!username .isEmpty () || !password .isEmpty ()) {
121+ logger .warning ("elasticsearch.authorization_header is set, ignoring elasticsearch.username and elasticsearch.password." );
122+ }
123+ } else if (!username .isEmpty () || !password .isEmpty ()) {
124+ final CredentialsProvider credentialsProvider = new BasicCredentialsProvider ();
125+ credentialsProvider .setCredentials (AuthScope .ANY , new UsernamePasswordCredentials (username , password ));
126+ clientBuilder .setHttpClientConfigCallback (httpClientBuilder -> httpClientBuilder .setDefaultCredentialsProvider (credentialsProvider ));
127+ }
128+ RestClient httpClient = clientBuilder .build ();
100129 JacksonJsonpMapper jacksonJsonpMapper = new JacksonJsonpMapper ();
101130 SimpleModule module = new SimpleModule ();
102131 module .addSerializer (VType .class , new VTypeSerializer ());
0 commit comments