Initial OpenECOMP policy/engine commit
[policy/engine.git] / PyPDPServer / src / main / java / org / openecomp / policy / pypdp / authorization / Config.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP Policy Engine
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.policy.pypdp.authorization;
22
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.nio.charset.StandardCharsets;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Base64;
33 import java.util.Collection;
34 import java.util.Collections;
35 import java.util.List;
36 import java.util.Properties;
37
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40 import org.openecomp.policy.common.logging.eelf.MessageCodes;
41 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
42
43 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
44
45 import org.openecomp.policy.common.im.IntegrityMonitor;
46
47
48 public class Config {
49         private static final String propertyFilePath = "config.properties";
50         private static Properties prop = new Properties();
51         private static List<String> pdps = null;
52         private static List<String> paps = null;
53         private static List<String> encoding = null;
54         private static List<String> encodingPAP = null;
55         private static String pyPDPPass = null;
56         private static String pyPDPID = null;
57         private static String environment = null;
58         private static final Log logger = LogFactory.getLog(Config.class);
59         private static String clientFile = null;
60         private static boolean test = false;
61         
62         private static IntegrityMonitor im;
63         private static String resourceName = null;
64         
65         public static String getProperty(String propertyKey) {
66                 return prop.getProperty(propertyKey);
67         }
68
69         /*
70          * Set Property by reading the properties File.
71          */
72         public static void setProperty() {
73                 Path file = Paths.get(propertyFilePath);
74                 if (Files.notExists(file)) {
75                         logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+ "File doesn't exist in the specified Path "+ file.toString());
76                         // TODO:EELF Cleanup - Remove logger
77                         PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "File doesn't exist in the specified Path "+ file.toString());
78                 } else {
79                         InputStream in;
80                         prop = new Properties();
81                         try {
82                                 in = new FileInputStream(file.toFile());
83                                 prop.load(in);
84                         } catch (IOException e) {
85                                 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Cannot Load the Properties file" + e);
86                                 // TODO:EELF Cleanup - Remove logger
87                                 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "Cannot Load the Properties file");
88                         }
89                 }
90                 // Initializing the values.
91                 pdps = new ArrayList<String>();
92                 paps = new ArrayList<String>();
93                 encoding = new ArrayList<String>();
94                 encodingPAP = new ArrayList<String>();
95                 
96                 // Check the Keys for PDP_URLs
97                 Collection<Object> unsorted = prop.keySet();
98                 List<String> sorted = new ArrayList(unsorted);
99                 Collections.sort(sorted);
100                 for (String propKey : sorted) {
101                         if (propKey.startsWith("PDP_URL")) {
102                                 String check_val = prop.getProperty(propKey);
103                                 logger.debug("Property file value for Key : \"" + propKey + "\" Value is : \"" + check_val + "\"");
104                                 if (check_val == null) {
105                                         logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Properties file doesn't have the PDP_URL parameter");
106                                         // TODO:EELF Cleanup - Remove logger
107                                         PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "Properties file doesn't have the PDP_URL parameter");
108                                 }       
109                                 if (check_val.contains(";")) {
110                                         List<String> pdp_default = new ArrayList<String>(Arrays.asList(check_val.split("\\s*;\\s*")));
111                                         int pdpCount = 0;
112                                         while (pdpCount < pdp_default.size()) {
113                                                 String pdpVal = pdp_default.get(pdpCount);
114                                                 readPDPParam(pdpVal);
115                                                 pdpCount++;
116                                         }
117                                 } else {
118                                         readPDPParam(check_val);
119                                 }
120                         } else if (propKey.startsWith("PAP_URL")) {
121                                 String check_val = prop.getProperty(propKey);
122                                 logger.debug("Property file value for Key : \"" + propKey + "\" Value is : \"" + check_val + "\"");
123                                 if (check_val == null) {
124                                         logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Properties file doesn't have the PAP_URL parameter");
125                                         // TODO:EELF Cleanup - Remove logger
126                                         PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "Properties file doesn't have the PAP_URL parameter");
127                                 }
128                                 if (check_val.contains(";")) {
129                                         List<String> pap_default = new ArrayList<String>(Arrays.asList(check_val.split("\\s*;\\s*")));
130                                         int papCount=0;
131                                         while (papCount < pap_default.size()) {
132                                                 String papVal = pap_default.get(papCount);
133                                                 readPAPParam(papVal);
134                                                 papCount++;
135                                         }
136                                 } else {
137                                         readPAPParam(check_val);
138                                 }
139                         }
140                 }
141                 if (pdps == null || pdps.isEmpty()) {
142                         logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Cannot Proceed without PDP_URLs");
143                         // TODO:EELF Cleanup - Remove logger
144                         PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "Cannot Proceed without PDP_URLs");
145                 }
146                 
147                 if (prop.containsKey("PYPDP_ID")) {
148                         String id = prop.getProperty("PYPDP_ID");
149                         logger.debug("Property file value key: \"PYPDP_ID\" Value is : \"" + id + "\"");
150                         if (id == null) {
151                                 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Properties file doesn't have PYPDP_ID parameter");
152                                 // TODO:EELF Cleanup - Remove logger
153                                 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "Properties file doesn't have PYPDP_ID parameter");
154                         }
155                         Config.pyPDPID = id;
156                 } else {
157                         logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Properties file doesn't have PYPDP_ID parameter");
158                         // TODO:EELF Cleanup - Remove logger
159                         PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "Properties file doesn't have PYPDP_ID parameter");
160                 }
161                 if (prop.containsKey("PYPDP_PASSWORD")) {
162                         String pass = prop.getProperty("PYPDP_PASSWORD");
163                         logger.debug("Property file value key: \"PYPDP_PASSWORD\" Value is : \"" + pass + "\"");
164                         if (pass == null) {
165                                 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Properties file doesn't have PYPDP_PASSWORD parameter");
166                                 // TODO:EELF Cleanup - Remove logger
167                                 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "Properties file doesn't have PYPDP_PASSWORD parameter");
168                         }
169                         Config.pyPDPPass = pass;
170                 } else {
171                         logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Properties file doesn't have PYPDP_PASSWORD parameter");
172                         // TODO:EELF Cleanup - Remove logger
173                         PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "Properties file doesn't have PYPDP_PASSWORD parameter");
174                 }
175                 environment = prop.getProperty("ENVIRONMENT", "DEVL");
176                 logger.info("Property value for Environment " + environment);
177                 String value = prop.getProperty("Test");
178                 if(value!= null && value.equalsIgnoreCase("true")){
179                         test = true;
180                 }
181                 if(prop.containsKey("CLIENT_FILE")){
182                         clientFile = prop.getProperty("CLIENT_FILE");
183                         logger.debug("Property file value key: \"CLIENT_FILE\" Value is : \"" + clientFile + "\"");
184                         if(clientFile == null){
185                                 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"CLIENT_FILE value is missing.");
186                                 // TODO:EELF Cleanup - Remove logger
187                                 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "CLIENT_FILE value is missing.");
188                         }
189                 }else{
190                         logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"CLIENT_FILE paramter is missing from the property file.");
191                         // TODO:EELF Cleanup - Remove logger
192                         PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "CLIENT_FILE paramter is missing from the property file.");
193                 }
194                 logger.info("Trying to set up IntegrityMonitor");
195                 try {
196                         logger.info("Trying to set up IntegrityMonitor");
197                         resourceName = prop.getProperty("RESOURCE_NAME").replaceAll(" ", "");;
198                         if(resourceName==null){
199                                 logger.warn("RESOURCE_NAME is missing setting default value. ");
200                                 resourceName = "pypdp_pdp01";
201                         }
202                         im = IntegrityMonitor.getInstance(resourceName, prop);
203                 } catch (Exception e) {
204                         logger.error("Error starting Integerity Monitor: " + e);
205                 }
206         }
207
208         private static void readPDPParam(String pdpVal) {
209                 if (pdpVal.contains(",")) {
210                         List<String> pdpValues = new ArrayList<String>(Arrays.asList(pdpVal.split("\\s*,\\s*")));
211                         if (pdpValues.size() == 3) {
212                                 // 0 - PDPURL
213                                 pdps.add(pdpValues.get(0));
214                                 // 1:2 will be UserID:Password
215                                 String userID = pdpValues.get(1);
216                                 String pass = pdpValues.get(2);
217                                 Base64.Encoder encoder = Base64.getEncoder();
218                                 encoding.add(encoder.encodeToString((userID + ":" + pass)
219                                                 .getBytes(StandardCharsets.UTF_8)));
220                         } else {
221                                 logger.error(XACMLErrorConstants.ERROR_PERMISSIONS+"No enough Credentials to send Request. "+ pdpValues);
222                                 // TODO:EELF Cleanup - Remove logger
223                                 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "No enough Credentials to send Request. "+ pdpValues);
224                         }
225                 } else {
226                         logger.error(XACMLErrorConstants.ERROR_PERMISSIONS+"No enough Credentials to send Request.");
227                         // TODO:EELF Cleanup - Remove logger
228                         PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "No enough Credentials to send Request.");
229                 }
230         }
231         
232         private static void readPAPParam(String papVal) {
233                 if (papVal.contains(",")) {
234                         List<String> papValues = new ArrayList<String>(Arrays.asList(papVal.split("\\s*,\\s*")));
235                         if (papValues.size() == 3) {
236                                 // 0 - PAPURL
237                                 paps.add(papValues.get(0));
238                                 // 1:2 will be UserID:Password
239                                 String userID = papValues.get(1);
240                                 String pass = papValues.get(2);
241                                 Base64.Encoder encoder = Base64.getEncoder();
242                                 encodingPAP.add(encoder.encodeToString((userID + ":" + pass)
243                                                 .getBytes(StandardCharsets.UTF_8)));
244                         } else {
245                                 logger.error(XACMLErrorConstants.ERROR_PERMISSIONS+"Not enough Credentials to send Request. "+ papValues);
246                                 // TODO:EELF Cleanup - Remove logger
247                                 PolicyLogger.error(MessageCodes.ERROR_PERMISSIONS, "Not enough Credentials to send Request. "+ papValues);
248                         }
249                 } else {
250                         logger.error(XACMLErrorConstants.ERROR_PERMISSIONS+"Not enough Credentials to send Request.");
251                         // TODO:EELF Cleanup - Remove logger
252                         PolicyLogger.error(MessageCodes.ERROR_PERMISSIONS, "Not enough Credentials to send Request.");
253                 }
254         }
255
256         public static List<String> getPDPs() {
257                 setProperty();
258                 return Config.pdps;
259         }
260         
261         public static List<String> getPAPs() {
262                 setProperty();
263                 return Config.paps;
264         }
265
266         public static List<String> getEncoding() {
267                 return Config.encoding;
268         }
269         
270         public static List<String> getEncodingPAP() {
271                 return Config.encodingPAP;
272         }
273
274         public static String getPYPDPID() {
275                 return Config.pyPDPID;
276         }
277
278         public static String getPYPDPPass() {
279                 return Config.pyPDPPass;
280         }
281         
282         public static String getEnvironment(){
283                 return Config.environment;
284         }
285         
286         public static IntegrityMonitor getIntegrityMonitor(){
287                 if(im==null){
288                         setProperty();
289                 }
290                 return im;
291         }
292         
293         public static String getClientFile() {
294                 return Config.clientFile;
295         }
296
297         public static Boolean isTest() {
298                 return Config.test;
299         }
300 }