Skip to content

Commit bc31151

Browse files
committed
Save and Restore: Add authentication for Elasticsearch.
Signed-off-by: Mariana González Velarde <[email protected]>
1 parent 9c0f55d commit bc31151

File tree

1 file changed

+30
-1
lines changed
  • services/save-and-restore/src/main/java/org/phoebus/service/saveandrestore/persistence/config

1 file changed

+30
-1
lines changed

services/save-and-restore/src/main/java/org/phoebus/service/saveandrestore/persistence/config/ElasticConfig.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,15 @@
1313
import co.elastic.clients.transport.endpoints.BooleanResponse;
1414
import co.elastic.clients.transport.rest_client.RestClientTransport;
1515
import com.fasterxml.jackson.databind.module.SimpleModule;
16+
import org.apache.http.Header;
1617
import 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;
1723
import org.elasticsearch.client.RestClient;
24+
import org.elasticsearch.client.RestClientBuilder;
1825
import org.epics.vtype.VType;
1926
import org.phoebus.applications.saveandrestore.model.Node;
2027
import 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

Comments
 (0)