2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.mso.properties;
24 import java.io.FileReader;
25 import java.io.IOException;
26 import java.security.GeneralSecurityException;
27 import java.util.HashMap;
29 import java.util.Properties;
31 import org.apache.commons.codec.binary.Base64;
32 import org.openecomp.mso.utils.CryptoUtils;
34 public class MsoJavaProperties extends AbstractMsoProperties {
37 private Properties msoProperties = new Properties();
40 public MsoJavaProperties() {
44 public synchronized void setProperty(String key,String value) {
45 msoProperties.setProperty(key, value);
48 public synchronized String getProperty(String key, String defaultValue) {
49 if (msoProperties.containsKey(key)) {
50 return msoProperties.getProperty(key);
56 public synchronized int getIntProperty(String key, int defaultValue) {
58 int value = defaultValue;
59 if (msoProperties.containsKey(key)) {
61 value = Integer.parseInt(msoProperties.getProperty(key));
62 } catch (NumberFormatException e) {
63 LOGGER.debug("Exception while parsing integer: " + msoProperties.getProperty(key), e);
70 public synchronized boolean getBooleanProperty(String key, boolean defaultValue) {
72 if (msoProperties.containsKey(key)) {
73 return Boolean.parseBoolean(msoProperties.getProperty(key));
80 public synchronized String getEncryptedProperty(String key, String defaultValue, String encryptionKey) {
82 if (msoProperties.containsKey(key)) {
84 return CryptoUtils.decrypt(msoProperties.getProperty(key), encryptionKey);
85 } catch (GeneralSecurityException e) {
86 LOGGER.debug("Exception while decrypting property: " + msoProperties.getProperty(key), e);
94 * @param encryptedAuth: encrypted credentials from properties
95 * @param msoKey: key to use to decrypt from properties
96 * @return base 64 encoded basic auth credentials
98 public synchronized String getBasicAuth(String encryptedAuth, String msoKey){
99 String encodedString = null;
100 if ((encryptedAuth == null || encryptedAuth.isEmpty()) || (msoKey == null || msoKey.isEmpty()))
103 String auth = decrypt(encryptedAuth, msoKey);
104 byte[] encoded = Base64.encodeBase64(auth.getBytes());
105 encodedString = new String(encoded);
106 encodedString = "Basic " + encodedString;
108 } catch (Exception ex) {
109 LOGGER.debug("Exception while getBasicAuth " + encryptedAuth, ex);
111 return encodedString;
114 public synchronized int size() {
115 return this.msoProperties.size();
118 public synchronized String decrypt(String toDecrypt, String msokey){
119 String result = null;
121 result = CryptoUtils.decrypt(toDecrypt, msokey);
124 catch (Exception e) {
125 LOGGER.debug("Failed to decrypt credentials: " + toDecrypt, e);
131 protected synchronized void reloadPropertiesFile() throws IOException {
132 this.loadPropertiesFile(this.propertiesFileName);
136 * This method load a properties file from a source path.
138 * @param propertiesPath The path to the file
139 * @throws IOException In case of issues during the opening
142 protected synchronized void loadPropertiesFile(String propertiesPath) throws IOException {
144 FileReader reader = null;
146 propertiesFileName = propertiesPath;
148 msoProperties.clear();
149 reader = new FileReader(propertiesPath);
150 msoProperties.load(reader);
153 this.automaticRefreshInMinutes = this.getIntProperty(RELOAD_TIME_PROPERTY, DEFAULT_RELOAD_TIME_MIN);
155 if (reader != null) {
158 } catch (IOException e) {
159 LOGGER.debug("Exception while closing reader for file:" + propertiesPath, e);
165 public synchronized MsoJavaProperties clone() {
166 MsoJavaProperties msoCopy = new MsoJavaProperties();
167 msoCopy.msoProperties.putAll(msoProperties);
168 msoCopy.propertiesFileName = this.propertiesFileName;
169 msoCopy.automaticRefreshInMinutes = this.automaticRefreshInMinutes;
174 public int hashCode() {
175 final int prime = 31;
177 result = prime * result + ((msoProperties == null) ? 0 : msoProperties.hashCode());
182 public boolean equals(Object obj) {
189 if (getClass() != obj.getClass()) {
192 MsoJavaProperties other = (MsoJavaProperties) obj;
194 return msoProperties.equals(other.msoProperties);
198 public String toString() {
200 StringBuilder response = new StringBuilder();
201 response.append("Config file ")
202 .append(propertiesFileName)
204 .append(automaticRefreshInMinutes)
206 .append(System.lineSeparator());
208 for (Object key : this.msoProperties.keySet()) {
209 String propertyName = (String) key;
210 response.append(propertyName);
211 response.append("=");
212 response.append(this.msoProperties.getProperty(propertyName));
213 response.append(System.lineSeparator());
215 response.append(System.lineSeparator());
216 response.append(System.lineSeparator());
218 return response.toString();
221 public Map<String, String> asMap() {
222 final Map<String, String> result = new HashMap<>();
223 msoProperties.forEach((key, value) -> result.put(key.toString(), value.toString()));