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.policy.pypdp.authorization;
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;
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;
43 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
45 import org.openecomp.policy.common.im.IntegrityMonitor;
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;
62 private static IntegrityMonitor im;
63 private static String resourceName = null;
65 public static String getProperty(String propertyKey) {
66 return prop.getProperty(propertyKey);
70 * Set Property by reading the properties File.
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());
80 prop = new Properties();
82 in = new FileInputStream(file.toFile());
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");
90 // Initializing the values.
91 pdps = new ArrayList<String>();
92 paps = new ArrayList<String>();
93 encoding = new ArrayList<String>();
94 encodingPAP = new ArrayList<String>();
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");
109 if (check_val.contains(";")) {
110 List<String> pdp_default = new ArrayList<String>(Arrays.asList(check_val.split("\\s*;\\s*")));
112 while (pdpCount < pdp_default.size()) {
113 String pdpVal = pdp_default.get(pdpCount);
114 readPDPParam(pdpVal);
118 readPDPParam(check_val);
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");
128 if (check_val.contains(";")) {
129 List<String> pap_default = new ArrayList<String>(Arrays.asList(check_val.split("\\s*;\\s*")));
131 while (papCount < pap_default.size()) {
132 String papVal = pap_default.get(papCount);
133 readPAPParam(papVal);
137 readPAPParam(check_val);
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");
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 + "\"");
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");
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");
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 + "\"");
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");
169 Config.pyPDPPass = pass;
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");
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")){
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.");
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.");
194 logger.info("Trying to set up IntegrityMonitor");
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";
202 im = IntegrityMonitor.getInstance(resourceName, prop);
203 } catch (Exception e) {
204 logger.error("Error starting Integerity Monitor: " + e);
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) {
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)));
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);
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.");
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) {
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)));
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);
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.");
256 public static List<String> getPDPs() {
261 public static List<String> getPAPs() {
266 public static List<String> getEncoding() {
267 return Config.encoding;
270 public static List<String> getEncodingPAP() {
271 return Config.encodingPAP;
274 public static String getPYPDPID() {
275 return Config.pyPDPID;
278 public static String getPYPDPPass() {
279 return Config.pyPDPPass;
282 public static String getEnvironment(){
283 return Config.environment;
286 public static IntegrityMonitor getIntegrityMonitor(){
293 public static String getClientFile() {
294 return Config.clientFile;
297 public static Boolean isTest() {