2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.aai.sa.searchdbabstraction.elasticsearch.config;
23 import java.nio.charset.StandardCharsets;
24 import java.util.Base64;
25 import java.util.Optional;
26 import java.util.Properties;
27 import org.eclipse.jetty.util.security.Password;
28 import org.onap.aai.sa.searchdbabstraction.util.SearchDbConstants;
30 public class ElasticSearchConfig {
32 private String uriScheme;
33 private String trustStore;
34 private String trustStorePassword;
35 private String keyStore;
36 private String keyStorePassword;
37 private String authUser;
38 private String authPassword;
39 private String ipAddress;
40 private String httpPort;
41 private String javaApiPort;
42 private String clusterName;
44 public static final String ES_CLUSTER_NAME = "es.cluster-name";
45 public static final String ES_IP_ADDRESS = "es.ip-address";
46 public static final String ES_HTTP_PORT = "es.http-port";
47 public static final String ES_URI_SCHEME = "es.uri-scheme";
48 public static final String ES_TRUST_STORE = "es.trust-store";
49 public static final String ES_TRUST_STORE_ENC = "es.trust-store-password";
50 public static final String ES_KEY_STORE = "es.key-store";
51 public static final String ES_KEY_STORE_ENC = "es.key-store-password";
52 public static final String ES_AUTH_USER = "es.auth-user";
53 public static final String ES_AUTH_ENC = "es.auth-password";
55 private static final String DEFAULT_URI_SCHEME = "http";
56 private static final String JAVA_API_PORT_DEFAULT = "9300";
57 private String authValue;
59 public ElasticSearchConfig(Properties props) {
60 setUriScheme(props.getProperty(ES_URI_SCHEME));
61 if (getUriScheme().equals("https")) {
62 initializeHttpsProperties(props);
64 setClusterName(props.getProperty(ES_CLUSTER_NAME));
65 setIpAddress(props.getProperty(ES_IP_ADDRESS));
66 setHttpPort(props.getProperty(ES_HTTP_PORT));
67 setJavaApiPort(JAVA_API_PORT_DEFAULT);
68 initializeAuthValues(props);
72 public String getUriScheme() {
73 return this.uriScheme;
76 public String getIpAddress() {
80 public void setIpAddress(String ipAddress) {
81 this.ipAddress = ipAddress;
84 public String getHttpPort() {
88 public void setHttpPort(String httpPort) {
89 this.httpPort = httpPort;
92 public String getJavaApiPort() {
96 public void setJavaApiPort(String javaApiPort) {
97 this.javaApiPort = javaApiPort;
100 public String getClusterName() {
104 public void setClusterName(String clusterName) {
105 this.clusterName = clusterName;
108 public void setKeyStore(String keyStore) {
109 this.keyStore = keyStore;
112 public void setKeyStorePassword(String keyStorePassword) {
113 this.keyStorePassword = keyStorePassword;
116 public String getKeyStorePath() {
120 public String getKeyStorePassword() {
121 return keyStorePassword;
124 public String getTrustStorePath() {
128 public void setTrustStore(String trustStore) {
129 this.trustStore = trustStore;
132 public void setTrustStorePassword(String trustStorePassword) {
133 this.trustStorePassword = trustStorePassword;
136 public String getTrustStorePassword() {
137 return trustStorePassword;
140 public void setAuthUser(String authUser) {
141 this.authUser = authUser;
144 public String getAuthUser() {
148 public void setAuthPassword(String authPassword) {
149 this.authPassword = authPassword;
152 public String getAuthPassword() {
156 public boolean useAuth() {
157 return getAuthUser() != null || getAuthPassword() != null;
160 public String getAuthValue() {
165 public String toString() {
166 return String.format(
167 "%s://%s:%s (cluster=%s) (API port=%s)%nauth=%s%ntrustStore=%s (passwd %s)%nkeyStore=%s (passwd %s)",
168 uriScheme, ipAddress, httpPort, clusterName, javaApiPort, useAuth(), trustStore,
169 trustStorePassword != null, keyStore, keyStorePassword != null);
172 private void initializeAuthValues(Properties props) {
173 setAuthUser(props.getProperty(ES_AUTH_USER));
174 Optional<String> passwordValue = Optional.ofNullable(props.getProperty(ES_AUTH_ENC));
175 if (passwordValue.isPresent()) {
176 setAuthPassword(Password.deobfuscate(passwordValue.get()));
179 authValue = "Basic " + Base64.getEncoder()
180 .encodeToString((getAuthUser() + ":" + getAuthPassword()).getBytes(StandardCharsets.UTF_8));
184 private void initializeHttpsProperties(Properties props) {
185 Optional<String> trustStoreFile = Optional.ofNullable(props.getProperty(ES_TRUST_STORE));
186 if (trustStoreFile.isPresent()) {
187 setTrustStore(SearchDbConstants.SDB_SPECIFIC_CONFIG + trustStoreFile.get());
190 Optional<String> passwordValue = Optional.ofNullable(props.getProperty(ES_TRUST_STORE_ENC));
191 if (passwordValue.isPresent()) {
192 if(passwordValue.get().startsWith("OBF:")){
193 setTrustStorePassword(Password.deobfuscate(passwordValue.get()));
195 setTrustStorePassword(passwordValue.get());
199 Optional<String> keyStoreFile = Optional.ofNullable(props.getProperty(ES_KEY_STORE));
200 if (keyStoreFile.isPresent()) {
201 setKeyStore(SearchDbConstants.SDB_SPECIFIC_CONFIG + keyStoreFile.get());
204 passwordValue = Optional.ofNullable(props.getProperty(ES_KEY_STORE_ENC));
205 if (passwordValue.isPresent()) {
206 if(passwordValue.get().startsWith("OBF:")){
207 setKeyStorePassword(Password.deobfuscate(passwordValue.get()));
209 setKeyStorePassword(passwordValue.get());
214 private void setUriScheme(String uriScheme) {
215 this.uriScheme = Optional.ofNullable(uriScheme).orElse(DEFAULT_URI_SCHEME);