0d116f85129133f951ecf7d075aff3a8e15921e8
[aai/search-data-service.git] / src / main / java / org / onap / aai / sa / searchdbabstraction / elasticsearch / config / ElasticSearchConfig.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.aai.sa.searchdbabstraction.elasticsearch.config;
22
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;
29
30 public class ElasticSearchConfig {
31
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
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";
54
55     private static final String DEFAULT_URI_SCHEME = "http";
56     private static final String JAVA_API_PORT_DEFAULT = "9300";
57     private String authValue;
58
59     public ElasticSearchConfig(Properties props) {
60         setUriScheme(props.getProperty(ES_URI_SCHEME));
61         if (getUriScheme().equals("https")) {
62             initializeHttpsProperties(props);
63         }
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);
69     }
70
71
72     public String getUriScheme() {
73         return this.uriScheme;
74     }
75
76     public String getIpAddress() {
77         return ipAddress;
78     }
79
80     public void setIpAddress(String ipAddress) {
81         this.ipAddress = ipAddress;
82     }
83
84     public String getHttpPort() {
85         return httpPort;
86     }
87
88     public void setHttpPort(String httpPort) {
89         this.httpPort = httpPort;
90     }
91
92     public String getJavaApiPort() {
93         return javaApiPort;
94     }
95
96     public void setJavaApiPort(String javaApiPort) {
97         this.javaApiPort = javaApiPort;
98     }
99
100     public String getClusterName() {
101         return clusterName;
102     }
103
104     public void setClusterName(String clusterName) {
105         this.clusterName = clusterName;
106     }
107
108     public void setKeyStore(String keyStore) {
109         this.keyStore = keyStore;
110     }
111
112     public void setKeyStorePassword(String keyStorePassword) {
113         this.keyStorePassword = keyStorePassword;
114     }
115
116     public String getKeyStorePath() {
117         return keyStore;
118     }
119
120     public String getKeyStorePassword() {
121         return keyStorePassword;
122     }
123
124     public String getTrustStorePath() {
125         return trustStore;
126     }
127
128     public void setTrustStore(String trustStore) {
129         this.trustStore = trustStore;
130     }
131
132     public void setTrustStorePassword(String trustStorePassword) {
133         this.trustStorePassword = trustStorePassword;
134     }
135
136     public String getTrustStorePassword() {
137         return trustStorePassword;
138     }
139
140     public void setAuthUser(String authUser) {
141         this.authUser = authUser;
142     }
143
144     public String getAuthUser() {
145         return authUser;
146     }
147
148     public void setAuthPassword(String authPassword) {
149         this.authPassword = authPassword;
150     }
151
152     public String getAuthPassword() {
153         return authPassword;
154     }
155
156     public boolean useAuth() {
157         return getAuthUser() != null || getAuthPassword() != null;
158     }
159
160     public String getAuthValue() {
161         return authValue;
162     }
163
164     @Override
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);
170     }
171
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()));
177         }
178         if (useAuth()) {
179             authValue = "Basic " + Base64.getEncoder()
180                     .encodeToString((getAuthUser() + ":" + getAuthPassword()).getBytes(StandardCharsets.UTF_8));
181         }
182     }
183
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());
188         }
189
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()));
194           }else{
195             setTrustStorePassword(passwordValue.get());
196           }
197         }
198
199         Optional<String> keyStoreFile = Optional.ofNullable(props.getProperty(ES_KEY_STORE));
200         if (keyStoreFile.isPresent()) {
201             setKeyStore(SearchDbConstants.SDB_SPECIFIC_CONFIG + keyStoreFile.get());
202         }
203
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()));
208           }else{
209             setKeyStorePassword(passwordValue.get());
210           }
211         }
212     }
213
214     private void setUriScheme(String uriScheme) {
215         this.uriScheme = Optional.ofNullable(uriScheme).orElse(DEFAULT_URI_SCHEME);
216     }
217 }