2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd.
7 * Modifications Copyright (C) 2019 Bell Canada
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.admin;
26 import com.att.research.xacml.util.XACMLProperties;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.nio.charset.StandardCharsets;
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;
41 import java.util.Properties;
42 import org.onap.policy.common.logging.flexlogger.FlexLogger;
43 import org.onap.policy.common.logging.flexlogger.Logger;
44 import org.onap.policy.rest.XACMLRestProperties;
45 import org.onap.policy.utils.PeCryptoUtils;
46 import org.onap.policy.xacml.api.XACMLErrorConstants;
49 * What is not good about this class is that once a value has been set for pdpProperties path you cannot change it. That
50 * may be ok for a highly controlled production environment in which nothing changes, but not a very good
53 * The reset() method has been added to assist with the above problem in order to acquire >80% JUnit code coverage.
55 * This static class doesn't really check a PDP, it simply loads a properties file and tried to ensure that a valid URL
56 * exists for a PDP along with user/password.
58 public class CheckPDP {
60 private static Path pdpPath = null;
61 private static Long oldModified = null;
62 private static HashMap<String, String> pdpMap = null;
63 private static final Logger LOGGER = FlexLogger.getLogger(CheckPDP.class);
69 public static Map<String, String> getPdpMap() {
73 private static void reset() {
79 public static boolean validateID(String id) {
83 } catch (Exception e) {
84 LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
91 return pdpMap.containsKey(id);
94 private static void readFile() {
97 pdpFile = XACMLProperties.getProperty(XACMLRestProperties.PROP_PDP_IDFILE);
98 } catch (Exception e) {
99 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Cannot read the PDP ID File" + e);
102 if (pdpFile == null) {
103 LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "PDP File name not Valid : " + pdpFile);
104 } else if (pdpPath == null) {
105 pdpPath = Paths.get(pdpFile);
106 if (!pdpPath.toString().endsWith(".properties") || !pdpPath.toFile().exists()) {
108 XACMLErrorConstants.ERROR_SYSTEM_ERROR + "File doesn't exist in the specified Path : " + pdpPath
114 } else { // Check if File is updated recently
115 Long newModified = pdpPath.toFile().lastModified();
116 if (!newModified.equals(oldModified)) {
117 // File has been updated.
123 @SuppressWarnings({"unchecked", "rawtypes"})
124 private static void readProps() {
126 pdpProp = new Properties();
127 try (InputStream in = new FileInputStream(pdpPath.toFile())) {
128 oldModified = pdpPath.toFile().lastModified();
130 // Read the Properties and Load the PDPs and encoding.
131 pdpMap = new HashMap<>();
132 // Check the Keys for PDP_URLs
133 Collection<Object> unsorted = pdpProp.keySet();
134 List<String> sorted = new ArrayList(unsorted);
135 Collections.sort(sorted);
136 for (String propKey : sorted) {
137 loadPDPProperties(propKey, pdpProp);
139 } catch (IOException e) {
140 LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
142 if (pdpMap == null || pdpMap.isEmpty()) {
143 LOGGER.debug(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Cannot Proceed without PDP_URLs");
148 private static void loadPDPProperties(String propKey, Properties pdpProp) {
149 if (propKey.startsWith("PDP_URL")) {
150 String checkVal = pdpProp.getProperty(propKey);
151 if (checkVal == null) {
152 LOGGER.error("Properties file doesn't have the PDP_URL parameter");
154 if (checkVal != null && checkVal.contains(";")) {
155 List<String> pdpDefault = new ArrayList<>(Arrays.asList(checkVal.split("\\s*;\\s*")));
157 while (pdpCount < pdpDefault.size()) {
158 String pdpVal = pdpDefault.get(pdpCount);
159 readPDPParam(pdpVal);
166 private static void readPDPParam(String pdpVal) {
167 if (pdpVal.contains(",")) {
168 List<String> pdpValues = new ArrayList<>(Arrays.asList(pdpVal.split("\\s*,\\s*")));
169 if (pdpValues.size() == 3) {
170 // 1:2 will be UserID:Password
171 String userID = pdpValues.get(1);
172 String pass = PeCryptoUtils.decrypt(pdpValues.get(2));
173 Base64.Encoder encoder = Base64.getEncoder();
175 pdpMap.put(pdpValues.get(0),
176 encoder.encodeToString((userID + ":" + pass).getBytes(StandardCharsets.UTF_8)));
178 LOGGER.error(XACMLErrorConstants.ERROR_PERMISSIONS + "No Credentials to send Request: " + pdpValues);
181 LOGGER.error(XACMLErrorConstants.ERROR_PERMISSIONS + "No Credentials to send Request: " + pdpVal);
185 public static String getEncoding(String pdpID) {
188 } catch (Exception e) {
189 LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
191 String encoding = null;
192 if (pdpMap != null && (!pdpMap.isEmpty())) {
194 encoding = pdpMap.get(pdpID);
195 } catch (Exception e) {
196 LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);