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;
43 private String authorizationEnabled;
45 public static final String ES_CLUSTER_NAME = "es.cluster-name";
46 public static final String ES_IP_ADDRESS = "es.ip-address";
47 public static final String ES_HTTP_PORT = "es.http-port";
48 public static final String ES_URI_SCHEME = "es.uri-scheme";
49 public static final String ES_TRUST_STORE = "es.trust-store";
50 public static final String ES_TRUST_STORE_ENC = "es.trust-store-password";
51 public static final String ES_KEY_STORE = "es.key-store";
52 public static final String ES_KEY_STORE_ENC = "es.key-store-password";
53 public static final String ES_AUTH_USER = "es.auth-user";
54 public static final String ES_AUTH_ENC = "es.auth-password";
55 public static final String ES_AUTH_ENABLED = "es.auth.authorization.enabled";
57 private static final String DEFAULT_URI_SCHEME = "http";
58 private static final String JAVA_API_PORT_DEFAULT = "9300";
59 private String authValue;
61 public ElasticSearchConfig(Properties props) {
62 setUriScheme(props.getProperty(ES_URI_SCHEME));
63 if (getUriScheme().equals("https")) {
64 initializeHttpsProperties(props);
66 setClusterName(props.getProperty(ES_CLUSTER_NAME));
67 setIpAddress(props.getProperty(ES_IP_ADDRESS));
68 setHttpPort(props.getProperty(ES_HTTP_PORT));
69 setJavaApiPort(JAVA_API_PORT_DEFAULT);
70 initializeAuthValues(props);
71 setAuthorizationEnabled(props.getProperty(ES_AUTH_ENABLED));
75 public String getUriScheme() {
76 return this.uriScheme;
79 public String getIpAddress() {
83 public void setIpAddress(String ipAddress) {
84 this.ipAddress = ipAddress;
87 public String getHttpPort() {
91 public void setHttpPort(String httpPort) {
92 this.httpPort = httpPort;
95 public String getJavaApiPort() {
99 public void setJavaApiPort(String javaApiPort) {
100 this.javaApiPort = javaApiPort;
103 public String getClusterName() {
107 public void setClusterName(String clusterName) {
108 this.clusterName = clusterName;
111 public void setKeyStore(String keyStore) {
112 this.keyStore = keyStore;
115 public void setKeyStorePassword(String keyStorePassword) {
116 this.keyStorePassword = keyStorePassword;
119 public String getKeyStorePath() {
123 public String getKeyStorePassword() {
124 return keyStorePassword;
127 public String getTrustStorePath() {
131 public void setTrustStore(String trustStore) {
132 this.trustStore = trustStore;
135 public void setTrustStorePassword(String trustStorePassword) {
136 this.trustStorePassword = trustStorePassword;
139 public String getTrustStorePassword() {
140 return trustStorePassword;
143 public void setAuthUser(String authUser) {
144 this.authUser = authUser;
147 public String getAuthUser() {
151 public void setAuthPassword(String authPassword) {
152 this.authPassword = authPassword;
155 public String getAuthPassword() {
159 public boolean useAuth() {
160 return getAuthUser() != null || getAuthPassword() != null;
163 public String getAuthValue() {
167 public String getAuthorizationEnabled() {
168 return authorizationEnabled;
171 public void setAuthorizationEnabled(String authorizationEnabled) {
172 this.authorizationEnabled = authorizationEnabled;
175 public boolean useAuthorizationUser() {
176 return getAuthorizationEnabled()== null? true : Boolean.parseBoolean(getAuthorizationEnabled());
180 public String toString() {
181 return String.format(
182 "%s://%s:%s (cluster=%s) (API port=%s)%nauth=%s%ntrustStore=%s (passwd %s)%nkeyStore=%s (passwd %s)%nauthorizationUser=%s",
183 uriScheme, ipAddress, httpPort, clusterName, javaApiPort, useAuth(), trustStore,
184 trustStorePassword != null, keyStore, keyStorePassword != null, useAuthorizationUser());
187 private void initializeAuthValues(Properties props) {
188 setAuthUser(props.getProperty(ES_AUTH_USER));
189 Optional<String> passwordValue = Optional.ofNullable(props.getProperty(ES_AUTH_ENC));
190 if (passwordValue.isPresent()) {
191 setAuthPassword(Password.deobfuscate(passwordValue.get()));
194 authValue = "Basic " + Base64.getEncoder()
195 .encodeToString((getAuthUser() + ":" + getAuthPassword()).getBytes(StandardCharsets.UTF_8));
199 private void initializeHttpsProperties(Properties props) {
200 Optional<String> trustStoreFile = Optional.ofNullable(props.getProperty(ES_TRUST_STORE));
201 if (trustStoreFile.isPresent()) {
202 setTrustStore(SearchDbConstants.SDB_SPECIFIC_CONFIG + trustStoreFile.get());
205 Optional<String> passwordValue = Optional.ofNullable(props.getProperty(ES_TRUST_STORE_ENC));
206 if (passwordValue.isPresent()) {
207 if(passwordValue.get().startsWith("OBF:")){
208 setTrustStorePassword(Password.deobfuscate(passwordValue.get()));
210 setTrustStorePassword(passwordValue.get());
214 Optional<String> keyStoreFile = Optional.ofNullable(props.getProperty(ES_KEY_STORE));
215 if (keyStoreFile.isPresent()) {
216 setKeyStore(SearchDbConstants.SDB_SPECIFIC_CONFIG + keyStoreFile.get());
219 passwordValue = Optional.ofNullable(props.getProperty(ES_KEY_STORE_ENC));
220 if (passwordValue.isPresent()) {
221 if(passwordValue.get().startsWith("OBF:")){
222 setKeyStorePassword(Password.deobfuscate(passwordValue.get()));
224 setKeyStorePassword(passwordValue.get());
229 private void setUriScheme(String uriScheme) {
230 this.uriScheme = Optional.ofNullable(uriScheme).orElse(DEFAULT_URI_SCHEME);