Commit includes ControlLoopPolicy API and bugfixes
[policy/engine.git] / ECOMP-PAP-REST / src / main / java / org / openecomp / policy / pap / xacml / restAuth / CheckPDP.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-PAP-REST
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.policy.pap.xacml.restAuth;
22
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.HashMap;
36 import java.util.List;
37 import java.util.Properties;
38
39 import org.openecomp.policy.common.logging.eelf.MessageCodes;
40 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
41 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
42 import org.openecomp.policy.common.logging.flexlogger.Logger;
43 import org.openecomp.policy.pap.xacml.rest.XACMLPapServlet;
44 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
45
46 public class CheckPDP {
47
48         private static Path pdpPath = null;
49         private static Properties pdpProp = null;
50         private static Long oldModified = null;
51         private static Long newModified = null;
52         private static HashMap<String, String> pdpMap = null;
53         private static final Logger logger = FlexLogger.getLogger(CheckPDP.class);
54
55         public static boolean validateID(String id) {
56                 // ReadFile
57                 try {
58                         readFile();
59                 } catch (Exception e) {
60                         PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, "CheckPDP", "Exception reading file");
61                         return false;
62                 }
63                 // Check ID
64                 if (pdpMap.containsKey(id)) {
65                         return true;
66                 }
67                 return false;
68         }
69
70         private static void readFile() throws Exception {
71                 String pdpFile = XACMLPapServlet.getPDPFile();
72                 if (pdpFile == null) {
73                         PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR + "PDP File name is undefined");
74                         throw new Exception(XACMLErrorConstants.ERROR_SYSTEM_ERROR +"PDP File name not Valid : " + pdpFile);
75                 }
76                 if (pdpPath == null) {
77                         pdpPath = Paths.get(pdpFile);
78                         if (Files.notExists(pdpPath)) {
79                                 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR + "File doesn't exist in the specified Path");
80                                 throw new Exception(XACMLErrorConstants.ERROR_SYSTEM_ERROR +"File doesn't exist in the specified Path : "+ pdpPath.toString());
81                         } 
82                         if (pdpPath.toString().endsWith(".properties")) {
83                                 readProps();
84                         } else {
85                                 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR + "Not a .properties file");
86                                 throw new Exception(XACMLErrorConstants.ERROR_SYSTEM_ERROR +"Not a .properties file");
87                         }
88                 }
89                 // Check if File is updated recently
90                 else {
91                         newModified = pdpPath.toFile().lastModified();
92                         if (newModified != oldModified) {
93                                 // File has been updated.
94                                 readProps();
95                         }
96                 }
97         }
98
99         @SuppressWarnings({ "rawtypes", "unchecked" })
100         private static void readProps() throws Exception {
101                 InputStream in;
102                 pdpProp = new Properties();
103                 try {
104                         in = new FileInputStream(pdpPath.toFile());
105                         oldModified = pdpPath.toFile().lastModified();
106                         pdpProp.load(in);
107                 } catch (IOException e) {
108                         PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, "CheckPDP", "Cannot load the Properties file");
109                         throw new Exception("Cannot Load the Properties file", e);
110                 }
111                 // Read the Properties and Load the PDPs and encoding.
112                 pdpMap = new HashMap<>();
113                 // Check the Keys for PDP_URLs
114                 Collection<Object> unsorted = pdpProp.keySet();
115                 List<String> sorted = new ArrayList(unsorted);
116                 Collections.sort(sorted);
117                 for (String propKey : sorted) {
118                         if (propKey.startsWith("PDP_URL")) {
119                                 String check_val = pdpProp.getProperty(propKey);
120                                 if (check_val == null) {
121                                         throw new Exception("Properties file doesn't have the PDP_URL parameter");
122                                 }
123                                 if (check_val.contains(";")) {
124                                         List<String> pdp_default = new ArrayList<>(Arrays.asList(check_val.split("\\s*;\\s*")));
125                                         int pdpCount = 0;
126                                         while (pdpCount < pdp_default.size()) {
127                                                 String pdpVal = pdp_default.get(pdpCount);
128                                                 readPDPParam(pdpVal);
129                                                 pdpCount++;
130                                         }
131                                 } else {
132                                         readPDPParam(check_val);
133                                 }
134                         }
135                 }
136                 if (pdpMap == null || pdpMap.isEmpty()) {
137                         logger.debug(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Cannot Proceed without PDP_URLs");
138                         throw new Exception(XACMLErrorConstants.ERROR_SYSTEM_ERROR +"Cannot Proceed without PDP_URLs");
139                 }
140         }
141
142         private static void readPDPParam(String pdpVal) throws Exception{
143                 if(pdpVal.contains(",")){
144                         List<String> pdpValues = new ArrayList<>(Arrays.asList(pdpVal.split("\\s*,\\s*")));
145                         if(pdpValues.size()==3){
146                                 // 1:2 will be UserID:Password
147                                 String userID = pdpValues.get(1);
148                                 String pass = pdpValues.get(2);
149                                 Base64.Encoder encoder = Base64.getEncoder();
150                                 // 0 - PDPURL
151                                 pdpMap.put(pdpValues.get(0), encoder.encodeToString((userID+":"+pass).getBytes(StandardCharsets.UTF_8)));
152                         }else{
153                                 PolicyLogger.error(MessageCodes.ERROR_PERMISSIONS + "No Credentials to send Request");
154                                 throw new Exception(XACMLErrorConstants.ERROR_PERMISSIONS + "No enough Credentials to send Request. " + pdpValues);
155                         }
156                 }else{
157                         PolicyLogger.error(MessageCodes.ERROR_PERMISSIONS + "No Credentials to send Request: " + pdpVal);
158                         throw new Exception(XACMLErrorConstants.ERROR_PERMISSIONS +"No enough Credentials to send Request.");
159                 }
160         }
161         
162         public static String getEncoding(String pdpID){
163                 try {
164                         readFile();
165                 } catch (Exception e) {
166                         PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, "CheckPDP", "Exeption reading Properties file");
167                 }
168                 String encoding = null;
169                 if(pdpMap!=null && (!pdpMap.isEmpty())){
170                         try{
171                                 encoding = pdpMap.get(pdpID);
172                         } catch(Exception e){
173                                 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, "CheckPDP", "Exception encoding");
174                         }
175                         return encoding;
176                 }else{
177                         return null;
178                 }
179         }
180
181 }