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.pap.xacml.restAuth;
23 import org.openecomp.policy.common.logging.eelf.MessageCodes;
24 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
26 import java.io.FileInputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.nio.charset.StandardCharsets;
30 import java.nio.file.Files;
31 import java.nio.file.Path;
32 import java.nio.file.Paths;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.Base64;
36 import java.util.Collection;
37 import java.util.Collections;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Properties;
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44 import org.openecomp.policy.pap.xacml.rest.XACMLPapServlet;
46 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
48 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
49 import org.openecomp.policy.common.logging.flexlogger.Logger;
51 public class CheckPDP {
53 private static Path pdpPath = null;
54 private static Properties pdpProp = null;
55 private static Long oldModified = null;
56 private static Long newModified = null;
57 private static HashMap<String, String> pdpMap = null;
58 private static final Logger logger = FlexLogger.getLogger(CheckPDP.class);
60 public static boolean validateID(String id) {
64 } catch (Exception e) {
65 //TODO:EELF Cleanup - Remove logger
66 //logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
67 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, "CheckPDP", "Exception reading file");
71 if (pdpMap.containsKey(id)) {
77 private static void readFile() throws Exception {
78 String pdpFile = XACMLPapServlet.getPDPFile();
79 if (pdpFile == null) {
80 //TODO:EELF Cleanup - Remove logger
81 //logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "PDP File name not Valid : " + pdpFile);
82 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR + "PDP File name is undefined");
83 throw new Exception(XACMLErrorConstants.ERROR_SYSTEM_ERROR +"PDP File name not Valid : " + pdpFile);
85 if (pdpPath == null) {
86 pdpPath = Paths.get(pdpFile);
87 if (Files.notExists(pdpPath)) {
88 //TODO:EELF Cleanup - Remove logger
89 //logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "File doesn't exist in the specified Path : " + pdpPath.toString());
90 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR + "File doesn't exist in the specified Path");
91 throw new Exception(XACMLErrorConstants.ERROR_SYSTEM_ERROR +"File doesn't exist in the specified Path : "+ pdpPath.toString());
93 if (pdpPath.toString().endsWith(".properties")) {
96 //TODO:EELF Cleanup - Remove logger
97 //logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Not a .properties file " + pdpFile);
98 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR + "Not a .properties file");
99 throw new Exception(XACMLErrorConstants.ERROR_SYSTEM_ERROR +"Not a .properties file");
102 // Check if File is updated recently
104 newModified = pdpPath.toFile().lastModified();
105 if (newModified != oldModified) {
106 // File has been updated.
112 private static void readProps() throws Exception {
114 pdpProp = new Properties();
116 in = new FileInputStream(pdpPath.toFile());
117 oldModified = pdpPath.toFile().lastModified();
119 } catch (IOException e) {
120 //TODO:EELF Cleanup - Remove logger
121 //logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
122 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, "CheckPDP", "Cannot load the Properties file");
123 throw new Exception("Cannot Load the Properties file", e);
125 // Read the Properties and Load the PDPs and encoding.
126 pdpMap = new HashMap<String, String>();
127 // Check the Keys for PDP_URLs
128 Collection<Object> unsorted = pdpProp.keySet();
129 List<String> sorted = new ArrayList(unsorted);
130 Collections.sort(sorted);
131 for (String propKey : sorted) {
132 if (propKey.startsWith("PDP_URL")) {
133 String check_val = pdpProp.getProperty(propKey);
134 if (check_val == null) {
135 throw new Exception("Properties file doesn't have the PDP_URL parameter");
137 if (check_val.contains(";")) {
138 List<String> pdp_default = new ArrayList<String>(Arrays.asList(check_val.split("\\s*;\\s*")));
140 while (pdpCount < pdp_default.size()) {
141 String pdpVal = pdp_default.get(pdpCount);
142 readPDPParam(pdpVal);
146 readPDPParam(check_val);
150 if (pdpMap == null || pdpMap.isEmpty()) {
151 logger.debug(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Cannot Proceed without PDP_URLs");
152 throw new Exception(XACMLErrorConstants.ERROR_SYSTEM_ERROR +"Cannot Proceed without PDP_URLs");
156 private static void readPDPParam(String pdpVal) throws Exception{
157 if(pdpVal.contains(",")){
158 List<String> pdpValues = new ArrayList<String>(Arrays.asList(pdpVal.split("\\s*,\\s*")));
159 if(pdpValues.size()==3){
160 // 1:2 will be UserID:Password
161 String userID = pdpValues.get(1);
162 String pass = pdpValues.get(2);
163 Base64.Encoder encoder = Base64.getEncoder();
165 pdpMap.put(pdpValues.get(0), encoder.encodeToString((userID+":"+pass).getBytes(StandardCharsets.UTF_8)));
167 //TODO:EELF Cleanup - Remove logger
168 //logger.error(XACMLErrorConstants.ERROR_PERMISSIONS + "No Credentials to send Request: " + pdpValues);
169 PolicyLogger.error(MessageCodes.ERROR_PERMISSIONS + "No Credentials to send Request");
170 throw new Exception(XACMLErrorConstants.ERROR_PERMISSIONS + "No enough Credentials to send Request. " + pdpValues);
173 //TODO:EELF Cleanup - Remove logger
174 //logger.error(XACMLErrorConstants.ERROR_PERMISSIONS + "No Credentials to send Request: " + pdpVal);
175 PolicyLogger.error(MessageCodes.ERROR_PERMISSIONS + "No Credentials to send Request: " + pdpVal);
176 throw new Exception(XACMLErrorConstants.ERROR_PERMISSIONS +"No enough Credentials to send Request.");
180 public static String getEncoding(String pdpID){
183 } catch (Exception e) {
184 //TODO:EELF Cleanup - Remove logger
185 //logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
186 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, "CheckPDP", "Exeption reading Properties file");
188 String encoding = null;
189 if(pdpMap!=null && (!pdpMap.isEmpty())){
191 encoding = pdpMap.get(pdpID);
192 } catch(Exception e){
193 //TODO:EELF Cleanup - Remove logger
194 //logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
195 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, "CheckPDP", "Exception encoding");