0d897992e48da2179f34c98c96848efeb6695334
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / dal / aai / config / ActiveInventorySslConfig.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.openecomp.sparky.dal.aai.config;
24
25 import java.util.Properties;
26
27 import org.eclipse.jetty.util.security.Password;
28 import org.openecomp.sparky.util.ConfigHelper;
29 import org.openecomp.sparky.util.Encryptor;
30 import org.openecomp.sparky.viewandinspect.config.TierSupportUiConstants;
31
32 /**
33  * The Class ActiveInventorySslConfig.
34  */
35 public class ActiveInventorySslConfig {
36
37   private Encryptor encryptor;
38
39   private boolean enableSslDebug;
40   private boolean validateServerHostName;
41   private boolean validateServerCertificateChain;
42
43   private String keystoreType;
44   private String keystoreFilename;
45   private String keystorePassword;
46   private String truststoreType;
47   private String truststoreFilename;
48
49   private String basicAuthUsername;
50   private String basicAuthPassword;
51   
52   /**
53    * Instantiates a new active inventory ssl config.
54    *
55    * @param props the props
56    */
57   public ActiveInventorySslConfig(Properties props, Encryptor encryptor) {
58
59     if (props == null) {
60       return;
61     }
62
63     Properties sslProps = ConfigHelper.getConfigWithPrefix("aai.ssl", props);
64
65     enableSslDebug = Boolean.parseBoolean(sslProps.getProperty("enableDebug", "false"));
66     validateServerHostName =
67         Boolean.parseBoolean(sslProps.getProperty("validateServerHostName", "false"));
68     validateServerCertificateChain =
69         Boolean.parseBoolean(sslProps.getProperty("validateServerCertificateChain", "false"));
70
71     if (enableSslDebug) {
72       System.setProperty("javax.net.debug", "ssl");
73     } else {
74       System.setProperty("javax.net.debug", "");
75     }
76
77     this.encryptor = encryptor;
78
79
80     keystoreType = sslProps.getProperty("keystore.type", "pkcs12");
81
82     keystoreFilename =
83         TierSupportUiConstants.CONFIG_AUTH_LOCATION + sslProps.getProperty("keystore.filename");
84     keystorePassword = encryptor.decryptValue(sslProps.getProperty("keystore.pass", ""));
85     truststoreType = sslProps.getProperty("truststore.type", "jks");
86
87     truststoreFilename =
88         TierSupportUiConstants.CONFIG_AUTH_LOCATION + sslProps.getProperty("truststore.filename");
89     
90     basicAuthUsername = sslProps.getProperty("basicAuth.username");
91     basicAuthPassword = decryptPassword(sslProps.getProperty("basicAuth.password"));
92     
93     }
94   
95   private String decryptPassword(String encryptedPassword) {
96
97     try {
98
99       if (encryptedPassword == null) {
100         return null;
101       }
102
103       return Password.deobfuscate(encryptedPassword);
104
105     } catch (Exception exc) {
106
107       return encryptedPassword;
108
109     }
110
111   }
112   
113   public String getBasicAuthUsername() {
114     return basicAuthUsername;
115   }
116
117   public void setBasicAuthUsername(String basicAuthUsername) {
118     this.basicAuthUsername = basicAuthUsername;
119   }
120
121   public String getBasicAuthPassword() {
122     return basicAuthPassword;
123   }
124
125   public void setBasicAuthPassword(String basicAuthPassword) {
126     this.basicAuthPassword = basicAuthPassword;
127   }
128
129
130   public Encryptor getEncryptor() {
131     return encryptor;
132   }
133
134   public void setEncryptor(Encryptor encryptor) {
135     this.encryptor = encryptor;
136   }
137
138   public String getKeystoreType() {
139     return keystoreType;
140   }
141
142   public void setKeystoreType(String keystoreType) {
143     this.keystoreType = keystoreType;
144   }
145
146   public String getKeystoreFilename() {
147     return keystoreFilename;
148   }
149
150   public void setKeystoreFilename(String keystoreFilename) {
151     this.keystoreFilename = keystoreFilename;
152   }
153
154   public String getKeystorePassword() {
155     return keystorePassword;
156   }
157
158   public void setKeystorePassword(String keystorePassword) {
159     this.keystorePassword = keystorePassword;
160   }
161
162   public String getTruststoreType() {
163     return truststoreType;
164   }
165
166   public void setTruststoreType(String truststoreType) {
167     this.truststoreType = truststoreType;
168   }
169
170   public String getTruststoreFilename() {
171     return truststoreFilename;
172   }
173
174   public void setTruststoreFilename(String truststoreFilename) {
175     this.truststoreFilename = truststoreFilename;
176   }
177
178   public boolean isValidateServerHostName() {
179     return validateServerHostName;
180   }
181
182   public void setValidateServerHostName(boolean validateServerHostName) {
183     this.validateServerHostName = validateServerHostName;
184   }
185
186   public boolean isValidateServerCertificateChain() {
187     return validateServerCertificateChain;
188   }
189
190   public void setValidateServerCertificateChain(boolean validateServerCertificateChain) {
191     this.validateServerCertificateChain = validateServerCertificateChain;
192   }
193   
194   public String getBasicAuthenticationCredentials() {
195
196     String usernameAndPassword = getBasicAuthUsername() + ":"
197         + getBasicAuthPassword();
198     return "Basic " + java.util.Base64.getEncoder().encodeToString(usernameAndPassword.getBytes());
199   }
200
201   /* (non-Javadoc)
202    * @see java.lang.Object#toString()
203    */
204   @Override
205   public String toString() {
206     return "ActiveInventorySslConfig [enableSslDebug=" + enableSslDebug
207         + ", validateServerHostName=" + validateServerHostName + ", validateServerCertificateChain="
208         + validateServerCertificateChain + ", keystoreType=" + keystoreType + ", keystoreFilename="
209         + keystoreFilename + ", truststoreType=" + truststoreType + ", truststoreFilename="
210         + truststoreFilename + "]";
211   }
212
213
214 }