Initial OpenECOMP policy/engine commit
[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 org.openecomp.policy.common.logging.eelf.MessageCodes;
24 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
25
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;
41
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44 import org.openecomp.policy.pap.xacml.rest.XACMLPapServlet;
45
46 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
47
48 import org.openecomp.policy.common.logging.flexlogger.FlexLogger; 
49 import org.openecomp.policy.common.logging.flexlogger.Logger; 
50
51 public class CheckPDP {
52
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);
59
60         public static boolean validateID(String id) {
61                 // ReadFile
62                 try {
63                         readFile();
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");
68                         return false;
69                 }
70                 // Check ID
71                 if (pdpMap.containsKey(id)) {
72                         return true;
73                 }
74                 return false;
75         }
76
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);
84                 }
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());
92                         } 
93                         if (pdpPath.toString().endsWith(".properties")) {
94                                 readProps();
95                         } else {
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");
100                         }
101                 }
102                 // Check if File is updated recently
103                 else {
104                         newModified = pdpPath.toFile().lastModified();
105                         if (newModified != oldModified) {
106                                 // File has been updated.
107                                 readProps();
108                         }
109                 }
110         }
111
112         private static void readProps() throws Exception {
113                 InputStream in;
114                 pdpProp = new Properties();
115                 try {
116                         in = new FileInputStream(pdpPath.toFile());
117                         oldModified = pdpPath.toFile().lastModified();
118                         pdpProp.load(in);
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);
124                 }
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");
136                                 }
137                                 if (check_val.contains(";")) {
138                                         List<String> pdp_default = new ArrayList<String>(Arrays.asList(check_val.split("\\s*;\\s*")));
139                                         int pdpCount = 0;
140                                         while (pdpCount < pdp_default.size()) {
141                                                 String pdpVal = pdp_default.get(pdpCount);
142                                                 readPDPParam(pdpVal);
143                                                 pdpCount++;
144                                         }
145                                 } else {
146                                         readPDPParam(check_val);
147                                 }
148                         }
149                 }
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");
153                 }
154         }
155
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();
164                                 // 0 - PDPURL
165                                 pdpMap.put(pdpValues.get(0), encoder.encodeToString((userID+":"+pass).getBytes(StandardCharsets.UTF_8)));
166                         }else{
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);
171                         }
172                 }else{
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.");
177                 }
178         }
179         
180         public static String getEncoding(String pdpID){
181                 try {
182                         readFile();
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");
187                 }
188                 String encoding = null;
189                 if(pdpMap!=null && (!pdpMap.isEmpty())){
190                         try{
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");
196                         }
197                         return encoding;
198                 }else{
199                         return null;
200                 }
201         }
202
203 }