Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / dal / aai / config / ActiveInventorySslConfig.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25
26 package org.openecomp.sparky.dal.aai.config;
27
28 import java.util.Properties;
29
30 import org.eclipse.jetty.util.security.Password;
31 import org.openecomp.sparky.util.ConfigHelper;
32 import org.openecomp.sparky.util.Encryptor;
33 import org.openecomp.sparky.viewandinspect.config.TierSupportUiConstants;
34
35 /**
36  * The Class ActiveInventorySslConfig.
37  */
38 public class ActiveInventorySslConfig {
39
40   private Encryptor encryptor;
41
42   private boolean enableSslDebug;
43   private boolean validateServerHostName;
44   private boolean validateServerCertificateChain;
45
46   private String keystoreType;
47   private String keystoreFilename;
48   private String keystorePassword;
49   private String truststoreType;
50   private String truststoreFilename;
51
52   private String basicAuthUsername;
53   private String basicAuthPassword;
54   
55   /**
56    * Instantiates a new active inventory ssl config.
57    *
58    * @param props the props
59    */
60   public ActiveInventorySslConfig(Properties props, Encryptor encryptor) {
61
62     if (props == null) {
63       return;
64     }
65
66     Properties sslProps = ConfigHelper.getConfigWithPrefix("aai.ssl", props);
67
68     enableSslDebug = Boolean.parseBoolean(sslProps.getProperty("enableDebug", "false"));
69     validateServerHostName =
70         Boolean.parseBoolean(sslProps.getProperty("validateServerHostName", "false"));
71     validateServerCertificateChain =
72         Boolean.parseBoolean(sslProps.getProperty("validateServerCertificateChain", "false"));
73
74     if (enableSslDebug) {
75       System.setProperty("javax.net.debug", "ssl");
76     } else {
77       System.setProperty("javax.net.debug", "");
78     }
79
80     this.encryptor = encryptor;
81
82
83     keystoreType = sslProps.getProperty("keystore.type", "pkcs12");
84
85     keystoreFilename =
86         TierSupportUiConstants.CONFIG_AUTH_LOCATION + sslProps.getProperty("keystore.filename");
87     keystorePassword = encryptor.decryptValue(sslProps.getProperty("keystore.pass", ""));
88     truststoreType = sslProps.getProperty("truststore.type", "jks");
89
90     truststoreFilename =
91         TierSupportUiConstants.CONFIG_AUTH_LOCATION + sslProps.getProperty("truststore.filename");
92     
93     basicAuthUsername = sslProps.getProperty("basicAuth.username");
94     basicAuthPassword = decryptPassword(sslProps.getProperty("basicAuth.password"));
95     
96     }
97   
98   private String decryptPassword(String encryptedPassword) {
99
100     try {
101
102       if (encryptedPassword == null) {
103         return null;
104       }
105
106       return Password.deobfuscate(encryptedPassword);
107
108     } catch (Exception exc) {
109
110       return encryptedPassword;
111
112     }
113
114   }
115   
116   public String getBasicAuthUsername() {
117     return basicAuthUsername;
118   }
119
120   public void setBasicAuthUsername(String basicAuthUsername) {
121     this.basicAuthUsername = basicAuthUsername;
122   }
123
124   public String getBasicAuthPassword() {
125     return basicAuthPassword;
126   }
127
128   public void setBasicAuthPassword(String basicAuthPassword) {
129     this.basicAuthPassword = basicAuthPassword;
130   }
131
132
133   public Encryptor getEncryptor() {
134     return encryptor;
135   }
136
137   public void setEncryptor(Encryptor encryptor) {
138     this.encryptor = encryptor;
139   }
140
141   public String getKeystoreType() {
142     return keystoreType;
143   }
144
145   public void setKeystoreType(String keystoreType) {
146     this.keystoreType = keystoreType;
147   }
148
149   public String getKeystoreFilename() {
150     return keystoreFilename;
151   }
152
153   public void setKeystoreFilename(String keystoreFilename) {
154     this.keystoreFilename = keystoreFilename;
155   }
156
157   public String getKeystorePassword() {
158     return keystorePassword;
159   }
160
161   public void setKeystorePassword(String keystorePassword) {
162     this.keystorePassword = keystorePassword;
163   }
164
165   public String getTruststoreType() {
166     return truststoreType;
167   }
168
169   public void setTruststoreType(String truststoreType) {
170     this.truststoreType = truststoreType;
171   }
172
173   public String getTruststoreFilename() {
174     return truststoreFilename;
175   }
176
177   public void setTruststoreFilename(String truststoreFilename) {
178     this.truststoreFilename = truststoreFilename;
179   }
180
181   public boolean isValidateServerHostName() {
182     return validateServerHostName;
183   }
184
185   public void setValidateServerHostName(boolean validateServerHostName) {
186     this.validateServerHostName = validateServerHostName;
187   }
188
189   public boolean isValidateServerCertificateChain() {
190     return validateServerCertificateChain;
191   }
192
193   public void setValidateServerCertificateChain(boolean validateServerCertificateChain) {
194     this.validateServerCertificateChain = validateServerCertificateChain;
195   }
196   
197   public String getBasicAuthenticationCredentials() {
198
199     String usernameAndPassword = getBasicAuthUsername() + ":"
200         + getBasicAuthPassword();
201     return "Basic " + java.util.Base64.getEncoder().encodeToString(usernameAndPassword.getBytes());
202   }
203
204   /* (non-Javadoc)
205    * @see java.lang.Object#toString()
206    */
207   @Override
208   public String toString() {
209     return "ActiveInventorySslConfig [enableSslDebug=" + enableSslDebug
210         + ", validateServerHostName=" + validateServerHostName + ", validateServerCertificateChain="
211         + validateServerCertificateChain + ", keystoreType=" + keystoreType + ", keystoreFilename="
212         + keystoreFilename + ", truststoreType=" + truststoreType + ", truststoreFilename="
213         + truststoreFilename + "]";
214   }
215
216
217 }