remove printing a stacktrace
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / security / portal / config / PortalAuthenticationConfig.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.onap.aai.sparky.security.portal.config;
24
25
26 import java.util.Properties;
27
28 import org.onap.aai.cl.api.Logger;
29 import org.onap.aai.cl.eelf.LoggerFactory;
30 import org.onap.aai.sparky.logging.AaiUiMsgs;
31 import org.onap.aai.sparky.security.CookieDecryptor;
32 import org.onap.aai.sparky.util.ConfigHelper;
33 import org.onap.aai.sparky.util.Encryptor;
34 import org.onap.aai.sparky.viewandinspect.config.SparkyConstants;
35
36
37 /**
38  * Provides Portal authentication configuration.
39  */
40 public class PortalAuthenticationConfig {
41
42   private String username;
43   private String password;
44   private boolean isOnapEnabled;
45   private CookieDecryptor cookieDecryptor;
46   private String cookieDecryptorClassName;
47
48   public static final String PROP_USERNAME = "username";
49   public static final String PROP_PASSWORD = "password"; // NOSONAR
50   public static final String PROP_IS_ONAP_ENABLED = "onap_enabled"; // NOSONAR
51   private static final String AUTHENTICATION_CONFIG_FILE = SparkyConstants.PORTAL_AUTHENTICATION_FILE_LOCATION;
52   public static final String PROP_COOKIEDECRYPTORCLASSNAME = "cookie_decryptor_classname";
53   private static final Logger LOG = LoggerFactory.getInstance().getLogger(PortalAuthenticationConfig.class);
54
55   private PortalAuthenticationConfig() {
56     // Prevent instantiation
57   }
58
59   private static class PortalAuthenticationConfigHelper {
60     private static final PortalAuthenticationConfig INSTANCE = new PortalAuthenticationConfig();
61
62     private PortalAuthenticationConfigHelper() {
63       // Deliberately empty
64     }
65   }
66
67   /**
68    * Get a singleton instance of the configuration.
69    *
70    * @return
71    */
72   public static PortalAuthenticationConfig getInstance() {
73     PortalAuthenticationConfigHelper.INSTANCE.load();
74     return PortalAuthenticationConfigHelper.INSTANCE;
75   }
76
77   public String getUsername() {
78     return username;
79   }
80
81   public String getPassword() {
82     Encryptor encryptor = new Encryptor();
83     return encryptor.decryptValue(password);
84   }
85
86   public boolean getIsOnapEnabled() {
87     return isOnapEnabled;
88   }
89   public String getcookieDecryptorClassName() {
90             return cookieDecryptorClassName;
91           }
92
93   /**
94    * Reload the Portal authentication properties from the classpath.
95    */
96   public void reload() {
97     load();
98   }
99
100   /**
101    * Load the Portal authentication properties from the classpath.
102    */
103   private void load() {
104     Properties props = ConfigHelper.loadConfigFromExplicitPath(AUTHENTICATION_CONFIG_FILE);
105     username = props.getProperty(PROP_USERNAME);
106     password = props.getProperty(PROP_PASSWORD);
107     isOnapEnabled = Boolean.parseBoolean(props.getProperty(PROP_IS_ONAP_ENABLED, "true"));
108     cookieDecryptorClassName= props.getProperty(PROP_COOKIEDECRYPTORCLASSNAME);
109   }
110   
111   public CookieDecryptor getCookieDecryptor() throws ClassNotFoundException{
112           
113           Class cookieDecrypterClass = Class.forName(cookieDecryptorClassName);
114           try {
115                 cookieDecryptor = (CookieDecryptor) cookieDecrypterClass.newInstance();
116         } catch (InstantiationException | IllegalAccessException e) {
117                  LOG.error(AaiUiMsgs.DECRYPTION_ERROR,"Unable to instantiate Cookie Decryptor Class");
118         }
119           return cookieDecryptor;
120   }
121   
122 }