Add missing distributionManagement section to poms
[aai/search-data-service.git] / search-data-service / 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
28 import org.apache.commons.lang.StringUtils;
29 import org.eclipse.jetty.util.security.Password;
30 import org.onap.aai.sa.searchdbabstraction.util.SearchDbConstants;
31
32 public class ElasticSearchConfig {
33
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;
46
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";
58
59     private static final String DEFAULT_URI_SCHEME = "http";
60     private static final String JAVA_API_PORT_DEFAULT = "9300";
61     private String authValue;
62
63     public ElasticSearchConfig(Properties props) {
64         setUriScheme(props.getProperty(ES_URI_SCHEME));
65         if (getUriScheme().equals("https")) {
66             initializeHttpsProperties(props);
67         }
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));
74     }
75
76
77     public String getUriScheme() {
78         return this.uriScheme;
79     }
80
81     public String getIpAddress() {
82         return ipAddress;
83     }
84
85     public void setIpAddress(String ipAddress) {
86         this.ipAddress = ipAddress;
87     }
88
89     public String getHttpPort() {
90         return httpPort;
91     }
92
93     public void setHttpPort(String httpPort) {
94         this.httpPort = httpPort;
95     }
96
97     public String getJavaApiPort() {
98         return javaApiPort;
99     }
100
101     public void setJavaApiPort(String javaApiPort) {
102         this.javaApiPort = javaApiPort;
103     }
104
105     public String getClusterName() {
106         return clusterName;
107     }
108
109     public void setClusterName(String clusterName) {
110         this.clusterName = clusterName;
111     }
112
113     public void setKeyStore(String keyStore) {
114         this.keyStore = keyStore;
115     }
116
117     public void setKeyStorePassword(String keyStorePassword) {
118         this.keyStorePassword = keyStorePassword;
119     }
120
121     public String getKeyStorePath() {
122         return keyStore;
123     }
124
125     public String getKeyStorePassword() {
126         return keyStorePassword;
127     }
128
129     public String getTrustStorePath() {
130         return trustStore;
131     }
132
133     public void setTrustStore(String trustStore) {
134         this.trustStore = trustStore;
135     }
136
137     public void setTrustStorePassword(String trustStorePassword) {
138         this.trustStorePassword = trustStorePassword;
139     }
140
141     public String getTrustStorePassword() {
142         return trustStorePassword;
143     }
144
145     public void setAuthUser(String authUser) {
146         this.authUser = authUser;
147     }
148
149     public String getAuthUser() {
150         return authUser;
151     }
152
153     public void setAuthPassword(String authPassword) {
154         this.authPassword = authPassword;
155     }
156
157     public String getAuthPassword() {
158         return authPassword;
159     }
160
161     public boolean useAuth() {
162         return getAuthUser() != null || getAuthPassword() != null;
163     }
164
165     public String getAuthValue() {
166         return authValue;
167     }
168
169     public String getAuthorizationEnabled() {
170         return authorizationEnabled;
171     }
172
173     public void setAuthorizationEnabled(String authorizationEnabled) {
174         this.authorizationEnabled = authorizationEnabled;
175     }
176
177     public boolean useAuthorizationUser() {
178         return getAuthorizationEnabled()== null? true : Boolean.parseBoolean(getAuthorizationEnabled());
179     }
180
181     @Override
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());
187     }
188
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()));
194         }
195         if (useAuth()) {
196             authValue = "Basic " + Base64.getEncoder()
197                     .encodeToString((getAuthUser() + ":" + getAuthPassword()).getBytes(StandardCharsets.UTF_8));
198         }
199     }
200
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());
205         }
206
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.getenv(StringUtils.removeStart(passwordValue.get(), "ENV:")));
213           }
214           else{
215             setTrustStorePassword(passwordValue.get());
216           }
217         }
218
219         Optional<String> keyStoreFile = Optional.ofNullable(props.getProperty(ES_KEY_STORE));
220         if (keyStoreFile.isPresent()) {
221             setKeyStore(SearchDbConstants.SDB_SPECIFIC_CONFIG + keyStoreFile.get());
222         }
223
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.getenv(StringUtils.removeStart(passwordValue.get(), "ENV:")));
230            }
231           else{
232             setKeyStorePassword(passwordValue.get());
233           }
234         }
235     }
236
237     private void setUriScheme(String uriScheme) {
238         this.uriScheme = Optional.ofNullable(uriScheme).orElse(DEFAULT_URI_SCHEME);
239     }
240 }