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.admin;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.nio.charset.StandardCharsets;
28 import java.nio.file.Files;
29 import java.nio.file.Path;
30 import java.nio.file.Paths;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.Base64;
34 import java.util.Collection;
35 import java.util.Collections;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Properties;
40 import org.openecomp.policy.rest.XACMLRestProperties;
42 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
43 import com.att.research.xacml.util.XACMLProperties;
45 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
46 import org.openecomp.policy.common.logging.flexlogger.Logger;
48 public class CheckPDP {
49 private static Path pdpPath = null;
50 private static Properties pdpProp = null;
51 private static Long oldModified = null;
52 private static Long newModified = null;
53 private static HashMap<String, String> pdpMap = null;
54 private static final Logger LOGGER = FlexLogger.getLogger(CheckPDP.class);
56 public static boolean validateID(String id) {
60 } catch (Exception e) {
61 LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
65 if (pdpMap.containsKey(id)) {
71 private static void readFile() throws Exception {
72 String pdpFile = null;
74 pdpFile = XACMLProperties.getProperty(XACMLRestProperties.PROP_PDP_IDFILE);
76 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Cannot read the PDP ID File");
79 if (pdpFile == null) {
80 LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "PDP File name not Valid : " + pdpFile);
81 throw new Exception(XACMLErrorConstants.ERROR_SYSTEM_ERROR +"PDP File name not Valid : " + pdpFile);
83 if (pdpPath == null) {
84 pdpPath = Paths.get(pdpFile);
85 if (Files.notExists(pdpPath)) {
86 LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "File doesn't exist in the specified Path : " + pdpPath.toString());
87 throw new Exception(XACMLErrorConstants.ERROR_SYSTEM_ERROR +"File doesn't exist in the specified Path : "+ pdpPath.toString());
89 if (pdpPath.toString().endsWith(".properties")) {
92 LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Not a .properties file " + pdpFile);
93 throw new Exception(XACMLErrorConstants.ERROR_SYSTEM_ERROR +"Not a .properties file");
96 // Check if File is updated recently
98 newModified = pdpPath.toFile().lastModified();
99 if (newModified != oldModified) {
100 // File has been updated.
106 @SuppressWarnings({ "unchecked", "rawtypes" })
107 private static void readProps() throws Exception {
109 pdpProp = new Properties();
111 in = new FileInputStream(pdpPath.toFile());
112 oldModified = pdpPath.toFile().lastModified();
114 } catch (IOException e) {
115 LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
116 throw new Exception("Cannot Load the Properties file", e);
118 // Read the Properties and Load the PDPs and encoding.
119 pdpMap = new HashMap<String, String>();
120 // Check the Keys for PDP_URLs
121 Collection<Object> unsorted = pdpProp.keySet();
122 List<String> sorted = new ArrayList(unsorted);
123 Collections.sort(sorted);
124 for (String propKey : sorted) {
125 if (propKey.startsWith("PDP_URL")) {
126 String check_val = pdpProp.getProperty(propKey);
127 if (check_val == null) {
128 throw new Exception("Properties file doesn't have the PDP_URL parameter");
130 if (check_val.contains(";")) {
131 List<String> pdp_default = new ArrayList<String>(Arrays.asList(check_val.split("\\s*;\\s*")));
133 while (pdpCount < pdp_default.size()) {
134 String pdpVal = pdp_default.get(pdpCount);
135 readPDPParam(pdpVal);
139 readPDPParam(check_val);
143 if (pdpMap == null || pdpMap.isEmpty()) {
144 LOGGER.debug(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Cannot Proceed without PDP_URLs");
145 throw new Exception(XACMLErrorConstants.ERROR_SYSTEM_ERROR +"Cannot Proceed without PDP_URLs");
149 private static void readPDPParam(String pdpVal) throws Exception{
150 if(pdpVal.contains(",")){
151 List<String> pdpValues = new ArrayList<String>(Arrays.asList(pdpVal.split("\\s*,\\s*")));
152 if(pdpValues.size()==3){
153 // 1:2 will be UserID:Password
154 String userID = pdpValues.get(1);
155 String pass = pdpValues.get(2);
156 Base64.Encoder encoder = Base64.getEncoder();
158 pdpMap.put(pdpValues.get(0), encoder.encodeToString((userID+":"+pass).getBytes(StandardCharsets.UTF_8)));
160 LOGGER.error(XACMLErrorConstants.ERROR_PERMISSIONS + "No Credentials to send Request: " + pdpValues);
161 throw new Exception(XACMLErrorConstants.ERROR_PERMISSIONS + "No enough Credentials to send Request. " + pdpValues);
164 LOGGER.error(XACMLErrorConstants.ERROR_PERMISSIONS + "No Credentials to send Request: " + pdpVal);
165 throw new Exception(XACMLErrorConstants.ERROR_PERMISSIONS +"No enough Credentials to send Request.");
169 public static String getEncoding(String pdpID){
172 } catch (Exception e) {
173 LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
175 String encoding = null;
176 if(pdpMap!=null && (!pdpMap.isEmpty())){
178 encoding = pdpMap.get(pdpID);
179 } catch(Exception e){
180 LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);