From 91d04c64771832a0b8815ffbe1f0f9920320d94d Mon Sep 17 00:00:00 2001 From: Pamela Dragosh Date: Tue, 14 Feb 2017 19:41:00 -0500 Subject: Initial OpenECOMP policy/engine commit Change-Id: I7dbff37733b661643dd4d1caefa3d7dccc361b6e Signed-off-by: Pamela Dragosh --- .../policy/pypdp/authorization/Config.java | 300 +++++++++++++++++++++ 1 file changed, 300 insertions(+) create mode 100644 PyPDPServer/src/main/java/org/openecomp/policy/pypdp/authorization/Config.java (limited to 'PyPDPServer/src/main/java/org/openecomp/policy/pypdp/authorization/Config.java') diff --git a/PyPDPServer/src/main/java/org/openecomp/policy/pypdp/authorization/Config.java b/PyPDPServer/src/main/java/org/openecomp/policy/pypdp/authorization/Config.java new file mode 100644 index 000000000..388909ecf --- /dev/null +++ b/PyPDPServer/src/main/java/org/openecomp/policy/pypdp/authorization/Config.java @@ -0,0 +1,300 @@ +/*- + * ============LICENSE_START======================================================= + * ECOMP Policy Engine + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.policy.pypdp.authorization; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Base64; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Properties; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.openecomp.policy.common.logging.eelf.MessageCodes; +import org.openecomp.policy.common.logging.eelf.PolicyLogger; + +import org.openecomp.policy.xacml.api.XACMLErrorConstants; + +import org.openecomp.policy.common.im.IntegrityMonitor; + + +public class Config { + private static final String propertyFilePath = "config.properties"; + private static Properties prop = new Properties(); + private static List pdps = null; + private static List paps = null; + private static List encoding = null; + private static List encodingPAP = null; + private static String pyPDPPass = null; + private static String pyPDPID = null; + private static String environment = null; + private static final Log logger = LogFactory.getLog(Config.class); + private static String clientFile = null; + private static boolean test = false; + + private static IntegrityMonitor im; + private static String resourceName = null; + + public static String getProperty(String propertyKey) { + return prop.getProperty(propertyKey); + } + + /* + * Set Property by reading the properties File. + */ + public static void setProperty() { + Path file = Paths.get(propertyFilePath); + if (Files.notExists(file)) { + logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+ "File doesn't exist in the specified Path "+ file.toString()); + // TODO:EELF Cleanup - Remove logger + PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "File doesn't exist in the specified Path "+ file.toString()); + } else { + InputStream in; + prop = new Properties(); + try { + in = new FileInputStream(file.toFile()); + prop.load(in); + } catch (IOException e) { + logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Cannot Load the Properties file" + e); + // TODO:EELF Cleanup - Remove logger + PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "Cannot Load the Properties file"); + } + } + // Initializing the values. + pdps = new ArrayList(); + paps = new ArrayList(); + encoding = new ArrayList(); + encodingPAP = new ArrayList(); + + // Check the Keys for PDP_URLs + Collection unsorted = prop.keySet(); + List sorted = new ArrayList(unsorted); + Collections.sort(sorted); + for (String propKey : sorted) { + if (propKey.startsWith("PDP_URL")) { + String check_val = prop.getProperty(propKey); + logger.debug("Property file value for Key : \"" + propKey + "\" Value is : \"" + check_val + "\""); + if (check_val == null) { + logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Properties file doesn't have the PDP_URL parameter"); + // TODO:EELF Cleanup - Remove logger + PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "Properties file doesn't have the PDP_URL parameter"); + } + if (check_val.contains(";")) { + List pdp_default = new ArrayList(Arrays.asList(check_val.split("\\s*;\\s*"))); + int pdpCount = 0; + while (pdpCount < pdp_default.size()) { + String pdpVal = pdp_default.get(pdpCount); + readPDPParam(pdpVal); + pdpCount++; + } + } else { + readPDPParam(check_val); + } + } else if (propKey.startsWith("PAP_URL")) { + String check_val = prop.getProperty(propKey); + logger.debug("Property file value for Key : \"" + propKey + "\" Value is : \"" + check_val + "\""); + if (check_val == null) { + logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Properties file doesn't have the PAP_URL parameter"); + // TODO:EELF Cleanup - Remove logger + PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "Properties file doesn't have the PAP_URL parameter"); + } + if (check_val.contains(";")) { + List pap_default = new ArrayList(Arrays.asList(check_val.split("\\s*;\\s*"))); + int papCount=0; + while (papCount < pap_default.size()) { + String papVal = pap_default.get(papCount); + readPAPParam(papVal); + papCount++; + } + } else { + readPAPParam(check_val); + } + } + } + if (pdps == null || pdps.isEmpty()) { + logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Cannot Proceed without PDP_URLs"); + // TODO:EELF Cleanup - Remove logger + PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "Cannot Proceed without PDP_URLs"); + } + + if (prop.containsKey("PYPDP_ID")) { + String id = prop.getProperty("PYPDP_ID"); + logger.debug("Property file value key: \"PYPDP_ID\" Value is : \"" + id + "\""); + if (id == null) { + logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Properties file doesn't have PYPDP_ID parameter"); + // TODO:EELF Cleanup - Remove logger + PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "Properties file doesn't have PYPDP_ID parameter"); + } + Config.pyPDPID = id; + } else { + logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Properties file doesn't have PYPDP_ID parameter"); + // TODO:EELF Cleanup - Remove logger + PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "Properties file doesn't have PYPDP_ID parameter"); + } + if (prop.containsKey("PYPDP_PASSWORD")) { + String pass = prop.getProperty("PYPDP_PASSWORD"); + logger.debug("Property file value key: \"PYPDP_PASSWORD\" Value is : \"" + pass + "\""); + if (pass == null) { + logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Properties file doesn't have PYPDP_PASSWORD parameter"); + // TODO:EELF Cleanup - Remove logger + PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "Properties file doesn't have PYPDP_PASSWORD parameter"); + } + Config.pyPDPPass = pass; + } else { + logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Properties file doesn't have PYPDP_PASSWORD parameter"); + // TODO:EELF Cleanup - Remove logger + PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "Properties file doesn't have PYPDP_PASSWORD parameter"); + } + environment = prop.getProperty("ENVIRONMENT", "DEVL"); + logger.info("Property value for Environment " + environment); + String value = prop.getProperty("Test"); + if(value!= null && value.equalsIgnoreCase("true")){ + test = true; + } + if(prop.containsKey("CLIENT_FILE")){ + clientFile = prop.getProperty("CLIENT_FILE"); + logger.debug("Property file value key: \"CLIENT_FILE\" Value is : \"" + clientFile + "\""); + if(clientFile == null){ + logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"CLIENT_FILE value is missing."); + // TODO:EELF Cleanup - Remove logger + PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "CLIENT_FILE value is missing."); + } + }else{ + logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"CLIENT_FILE paramter is missing from the property file."); + // TODO:EELF Cleanup - Remove logger + PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "CLIENT_FILE paramter is missing from the property file."); + } + logger.info("Trying to set up IntegrityMonitor"); + try { + logger.info("Trying to set up IntegrityMonitor"); + resourceName = prop.getProperty("RESOURCE_NAME").replaceAll(" ", "");; + if(resourceName==null){ + logger.warn("RESOURCE_NAME is missing setting default value. "); + resourceName = "pypdp_pdp01"; + } + im = IntegrityMonitor.getInstance(resourceName, prop); + } catch (Exception e) { + logger.error("Error starting Integerity Monitor: " + e); + } + } + + private static void readPDPParam(String pdpVal) { + if (pdpVal.contains(",")) { + List pdpValues = new ArrayList(Arrays.asList(pdpVal.split("\\s*,\\s*"))); + if (pdpValues.size() == 3) { + // 0 - PDPURL + pdps.add(pdpValues.get(0)); + // 1:2 will be UserID:Password + String userID = pdpValues.get(1); + String pass = pdpValues.get(2); + Base64.Encoder encoder = Base64.getEncoder(); + encoding.add(encoder.encodeToString((userID + ":" + pass) + .getBytes(StandardCharsets.UTF_8))); + } else { + logger.error(XACMLErrorConstants.ERROR_PERMISSIONS+"No enough Credentials to send Request. "+ pdpValues); + // TODO:EELF Cleanup - Remove logger + PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "No enough Credentials to send Request. "+ pdpValues); + } + } else { + logger.error(XACMLErrorConstants.ERROR_PERMISSIONS+"No enough Credentials to send Request."); + // TODO:EELF Cleanup - Remove logger + PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "No enough Credentials to send Request."); + } + } + + private static void readPAPParam(String papVal) { + if (papVal.contains(",")) { + List papValues = new ArrayList(Arrays.asList(papVal.split("\\s*,\\s*"))); + if (papValues.size() == 3) { + // 0 - PAPURL + paps.add(papValues.get(0)); + // 1:2 will be UserID:Password + String userID = papValues.get(1); + String pass = papValues.get(2); + Base64.Encoder encoder = Base64.getEncoder(); + encodingPAP.add(encoder.encodeToString((userID + ":" + pass) + .getBytes(StandardCharsets.UTF_8))); + } else { + logger.error(XACMLErrorConstants.ERROR_PERMISSIONS+"Not enough Credentials to send Request. "+ papValues); + // TODO:EELF Cleanup - Remove logger + PolicyLogger.error(MessageCodes.ERROR_PERMISSIONS, "Not enough Credentials to send Request. "+ papValues); + } + } else { + logger.error(XACMLErrorConstants.ERROR_PERMISSIONS+"Not enough Credentials to send Request."); + // TODO:EELF Cleanup - Remove logger + PolicyLogger.error(MessageCodes.ERROR_PERMISSIONS, "Not enough Credentials to send Request."); + } + } + + public static List getPDPs() { + setProperty(); + return Config.pdps; + } + + public static List getPAPs() { + setProperty(); + return Config.paps; + } + + public static List getEncoding() { + return Config.encoding; + } + + public static List getEncodingPAP() { + return Config.encodingPAP; + } + + public static String getPYPDPID() { + return Config.pyPDPID; + } + + public static String getPYPDPPass() { + return Config.pyPDPPass; + } + + public static String getEnvironment(){ + return Config.environment; + } + + public static IntegrityMonitor getIntegrityMonitor(){ + if(im==null){ + setProperty(); + } + return im; + } + + public static String getClientFile() { + return Config.clientFile; + } + + public static Boolean isTest() { + return Config.test; + } +} -- cgit 1.2.3-korg