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;
28 import org.apache.commons.lang.StringUtils;
29 import org.eclipse.jetty.util.security.Password;
30 import org.onap.aai.sa.searchdbabstraction.util.SearchDbConstants;
32 public class ElasticSearchConfig {
34 private String uriScheme;
35 private String trustStore;
36 private String trustStorePassword;
37 private String keyStore;
38 private String keyStorePassword;
39 private String authUser;
40 private String authPassword;
41 private String ipAddress;
42 private String httpPort;
43 private String javaApiPort;
44 private String clusterName;
45 private String authorizationEnabled;
47 public static final String ES_CLUSTER_NAME = "es.cluster-name";
48 public static final String ES_IP_ADDRESS = "es.ip-address";
49 public static final String ES_HTTP_PORT = "es.http-port";
50 public static final String ES_URI_SCHEME = "es.uri-scheme";
51 public static final String ES_TRUST_STORE = "es.trust-store";
52 public static final String ES_TRUST_STORE_ENC = "es.trust-store-password";
53 public static final String ES_KEY_STORE = "es.key-store";
54 public static final String ES_KEY_STORE_ENC = "es.key-store-password";
55 public static final String ES_AUTH_USER = "es.auth-user";
56 public static final String ES_AUTH_ENC = "es.auth-password";
57 public static final String ES_AUTH_ENABLED = "es.auth.authorization.enabled";
59 private static final String DEFAULT_URI_SCHEME = "http";
60 private static final String JAVA_API_PORT_DEFAULT = "9300";
61 private String authValue;
63 public ElasticSearchConfig(Properties props) {
64 setUriScheme(props.getProperty(ES_URI_SCHEME));
65 if (getUriScheme().equals("https")) {
66 initializeHttpsProperties(props);
68 setClusterName(props.getProperty(ES_CLUSTER_NAME));
69 setIpAddress(props.getProperty(ES_IP_ADDRESS));
70 setHttpPort(props.getProperty(ES_HTTP_PORT));
71 setJavaApiPort(JAVA_API_PORT_DEFAULT);
72 initializeAuthValues(props);
73 setAuthorizationEnabled(props.getProperty(ES_AUTH_ENABLED));
77 public String getUriScheme() {
78 return this.uriScheme;
81 public String getIpAddress() {
85 public void setIpAddress(String ipAddress) {
86 this.ipAddress = ipAddress;
89 public String getHttpPort() {
93 public void setHttpPort(String httpPort) {
94 this.httpPort = httpPort;
97 public String getJavaApiPort() {
101 public void setJavaApiPort(String javaApiPort) {
102 this.javaApiPort = javaApiPort;
105 public String getClusterName() {
109 public void setClusterName(String clusterName) {
110 this.clusterName = clusterName;
113 public void setKeyStore(String keyStore) {
114 this.keyStore = keyStore;
117 public void setKeyStorePassword(String keyStorePassword) {
118 this.keyStorePassword = keyStorePassword;
121 public String getKeyStorePath() {
125 public String getKeyStorePassword() {
126 return keyStorePassword;
129 public String getTrustStorePath() {
133 public void setTrustStore(String trustStore) {
134 this.trustStore = trustStore;
137 public void setTrustStorePassword(String trustStorePassword) {
138 this.trustStorePassword = trustStorePassword;
141 public String getTrustStorePassword() {
142 return trustStorePassword;
145 public void setAuthUser(String authUser) {
146 this.authUser = authUser;
149 public String getAuthUser() {
153 public void setAuthPassword(String authPassword) {
154 this.authPassword = authPassword;
157 public String getAuthPassword() {
161 public boolean useAuth() {
162 return getAuthUser() != null || getAuthPassword() != null;
165 public String getAuthValue() {
169 public String getAuthorizationEnabled() {
170 return authorizationEnabled;
173 public void setAuthorizationEnabled(String authorizationEnabled) {
174 this.authorizationEnabled = authorizationEnabled;
177 public boolean useAuthorizationUser() {
178 return getAuthorizationEnabled()== null? true : Boolean.parseBoolean(getAuthorizationEnabled());
182 public String toString() {
183 return String.format(
184 "%s://%s:%s (cluster=%s) (API port=%s)%nauth=%s%ntrustStore=%s (passwd %s)%nkeyStore=%s (passwd %s)%nauthorizationUser=%s",
185 uriScheme, ipAddress, httpPort, clusterName, javaApiPort, useAuth(), trustStore,
186 trustStorePassword != null, keyStore, keyStorePassword != null, useAuthorizationUser());
189 private void initializeAuthValues(Properties props) {
190 setAuthUser(props.getProperty(ES_AUTH_USER));
191 Optional<String> passwordValue = Optional.ofNullable(props.getProperty(ES_AUTH_ENC));
192 if (passwordValue.isPresent()) {
193 setAuthPassword(Password.deobfuscate(passwordValue.get()));
196 authValue = "Basic " + Base64.getEncoder()
197 .encodeToString((getAuthUser() + ":" + getAuthPassword()).getBytes(StandardCharsets.UTF_8));
201 private void initializeHttpsProperties(Properties props) {
202 Optional<String> trustStoreFile = Optional.ofNullable(props.getProperty(ES_TRUST_STORE));
203 if (trustStoreFile.isPresent()) {
204 setTrustStore(SearchDbConstants.SDB_SPECIFIC_CONFIG + trustStoreFile.get());
207 Optional<String> passwordValue = Optional.ofNullable(props.getProperty(ES_TRUST_STORE_ENC));
208 if (passwordValue.isPresent()) {
209 if(passwordValue.get().startsWith("OBF:")){
210 setTrustStorePassword(Password.deobfuscate(passwordValue.get()));
211 }else if(passwordValue.get().startsWith("ENV:")){
212 setTrustStorePassword(System.getProperty(StringUtils.removeStart(passwordValue.get(), "ENV:")));
215 setTrustStorePassword(passwordValue.get());
219 Optional<String> keyStoreFile = Optional.ofNullable(props.getProperty(ES_KEY_STORE));
220 if (keyStoreFile.isPresent()) {
221 setKeyStore(SearchDbConstants.SDB_SPECIFIC_CONFIG + keyStoreFile.get());
224 passwordValue = Optional.ofNullable(props.getProperty(ES_KEY_STORE_ENC));
225 if (passwordValue.isPresent()) {
226 if(passwordValue.get().startsWith("OBF:")){
227 setKeyStorePassword(Password.deobfuscate(passwordValue.get()));
228 }else if(passwordValue.get().startsWith("ENV:")){
229 setKeyStorePassword(System.getProperty(StringUtils.removeStart(passwordValue.get(), "ENV:")));
232 setKeyStorePassword(passwordValue.get());
237 private void setUriScheme(String uriScheme) {
238 this.uriScheme = Optional.ofNullable(uriScheme).orElse(DEFAULT_URI_SCHEME);