Skip to content

Commit f549079

Browse files
authored
Merge pull request #3206 from mgonzal1/saveandrestore-auth
Save and Restore: Add authentication for Elasticsearch.
2 parents cd82745 + da01367 commit f549079

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-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());

services/save-and-restore/src/main/resources/application.properties

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ server.port=8080
99
elasticsearch.network.host=localhost
1010
elasticsearch.http.port=9200
1111

12+
# The value for the `Authorization` header used in requests to the Elasticsearch server.
13+
# This header supports token-based or API key-based authentication.
14+
# See https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/8.12/_other_authentication_methods.html
15+
elasticsearch.authorization.header =
16+
17+
# Username and password for basic authentication with the Elasticsearch server.
18+
# These credentials are only used if `elasticsearch.authorization.header` is not set.
19+
elasticsearch.authorization.username =
20+
elasticsearch.authorization.password =
21+
22+
1223
# Do not change this!
1324
spring.jackson.serialization.write-dates-as-timestamps=false
1425

0 commit comments

Comments
 (0)