Add option to bypass user authorization
[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     private String authorizationEnabled;
44
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";
56
57     private static final String DEFAULT_URI_SCHEME = "http";
58     private static final String JAVA_API_PORT_DEFAULT = "9300";
59     private String authValue;
60
61     public ElasticSearchConfig(Properties props) {
62         setUriScheme(props.getProperty(ES_URI_SCHEME));
63         if (getUriScheme().equals("https")) {
64             initializeHttpsProperties(props);
65         }
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));
72     }
73
74
75     public String getUriScheme() {
76         return this.uriScheme;
77     }
78
79     public String getIpAddress() {
80         return ipAddress;
81     }
82
83     public void setIpAddress(String ipAddress) {
84         this.ipAddress = ipAddress;
85     }
86
87     public String getHttpPort() {
88         return httpPort;
89     }
90
91     public void setHttpPort(String httpPort) {
92         this.httpPort = httpPort;
93     }
94
95     public String getJavaApiPort() {
96         return javaApiPort;
97     }
98
99     public void setJavaApiPort(String javaApiPort) {
100         this.javaApiPort = javaApiPort;
101     }
102
103     public String getClusterName() {
104         return clusterName;
105     }
106
107     public void setClusterName(String clusterName) {
108         this.clusterName = clusterName;
109     }
110
111     public void setKeyStore(String keyStore) {
112         this.keyStore = keyStore;
113     }
114
115     public void setKeyStorePassword(String keyStorePassword) {
116         this.keyStorePassword = keyStorePassword;
117     }
118
119     public String getKeyStorePath() {
120         return keyStore;
121     }
122
123     public String getKeyStorePassword() {
124         return keyStorePassword;
125     }
126
127     public String getTrustStorePath() {
128         return trustStore;
129     }
130
131     public void setTrustStore(String trustStore) {
132         this.trustStore = trustStore;
133     }
134
135     public void setTrustStorePassword(String trustStorePassword) {
136         this.trustStorePassword = trustStorePassword;
137     }
138
139     public String getTrustStorePassword() {
140         return trustStorePassword;
141     }
142
143     public void setAuthUser(String authUser) {
144         this.authUser = authUser;
145     }
146
147     public String getAuthUser() {
148         return authUser;
149     }
150
151     public void setAuthPassword(String authPassword) {
152         this.authPassword = authPassword;
153     }
154
155     public String getAuthPassword() {
156         return authPassword;
157     }
158
159     public boolean useAuth() {
160         return getAuthUser() != null || getAuthPassword() != null;
161     }
162
163     public String getAuthValue() {
164         return authValue;
165     }
166
167     public String getAuthorizationEnabled() {
168         return authorizationEnabled;
169     }
170
171     public void setAuthorizationEnabled(String authorizationEnabled) {
172         this.authorizationEnabled = authorizationEnabled;
173     }
174
175     public boolean useAuthorizationUser() {
176         return getAuthorizationEnabled()== null? true : Boolean.parseBoolean(getAuthorizationEnabled());
177     }
178
179     @Override
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());
185     }
186
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()));
192         }
193         if (useAuth()) {
194             authValue = "Basic " + Base64.getEncoder()
195                     .encodeToString((getAuthUser() + ":" + getAuthPassword()).getBytes(StandardCharsets.UTF_8));
196         }
197     }
198
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());
203         }
204
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()));
209           }else{
210             setTrustStorePassword(passwordValue.get());
211           }
212         }
213
214         Optional<String> keyStoreFile = Optional.ofNullable(props.getProperty(ES_KEY_STORE));
215         if (keyStoreFile.isPresent()) {
216             setKeyStore(SearchDbConstants.SDB_SPECIFIC_CONFIG + keyStoreFile.get());
217         }
218
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()));
223           }else{
224             setKeyStorePassword(passwordValue.get());
225           }
226         }
227     }
228
229     private void setUriScheme(String uriScheme) {
230         this.uriScheme = Optional.ofNullable(uriScheme).orElse(DEFAULT_URI_SCHEME);
231     }
232 }