aboutsummaryrefslogtreecommitdiffstats
path: root/PyPDPServer/src/main/java/org/openecomp/policy/pypdp/authorization/Config.java
blob: 388909ecfa4e5a14936c1d218055add8f1ad8d9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*-
 * ============LICENSE_START=======================================================
 * ECOMP Policy Engine
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.policy.pypdp.authorization;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openecomp.policy.common.logging.eelf.MessageCodes;
import org.openecomp.policy.common.logging.eelf.PolicyLogger;

import org.openecomp.policy.xacml.api.XACMLErrorConstants;

import org.openecomp.policy.common.im.IntegrityMonitor;


public class Config {
	private static final String propertyFilePath = "config.properties";
	private static Properties prop = new Properties();
	private static List<String> pdps = null;
	private static List<String> paps = null;
	private static List<String> encoding = null;
	private static List<String> encodingPAP = null;
	private static String pyPDPPass = null;
	private static String pyPDPID = null;
	private static String environment = null;
	private static final Log logger = LogFactory.getLog(Config.class);
	private static String clientFile = null;
	private static boolean test = false;
	
	private static IntegrityMonitor im;
	private static String resourceName = null;
	
	public static String getProperty(String propertyKey) {
		return prop.getProperty(propertyKey);
	}

	/*
	 * Set Property by reading the properties File.
	 */
	public static void setProperty() {
		Path file = Paths.get(propertyFilePath);
		if (Files.notExists(file)) {
			logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+ "File doesn't exist in the specified Path "+ file.toString());
			// TODO:EELF Cleanup - Remove logger
			PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "File doesn't exist in the specified Path "+ file.toString());
		} else {
			InputStream in;
			prop = new Properties();
			try {
				in = new FileInputStream(file.toFile());
				prop.load(in);
			} catch (IOException e) {
				logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Cannot Load the Properties file" + e);
				// TODO:EELF Cleanup - Remove logger
				PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "Cannot Load the Properties file");
			}
		}
		// Initializing the values.
		pdps = new ArrayList<String>();
		paps = new ArrayList<String>();
		encoding = new ArrayList<String>();
		encodingPAP = new ArrayList<String>();
		
		// Check the Keys for PDP_URLs
		Collection<Object> unsorted = prop.keySet();
		List<String> sorted = new ArrayList(unsorted);
		Collections.sort(sorted);
		for (String propKey : sorted) {
			if (propKey.startsWith("PDP_URL")) {
				String check_val = prop.getProperty(propKey);
				logger.debug("Property file value for Key : \"" + propKey + "\" Value is : \"" + check_val + "\"");
				if (check_val == null) {
					logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Properties file doesn't have the PDP_URL parameter");
					// TODO:EELF Cleanup - Remove logger
					PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "Properties file doesn't have the PDP_URL parameter");
				}	
				if (check_val.contains(";")) {
					List<String> pdp_default = new ArrayList<String>(Arrays.asList(check_val.split("\\s*;\\s*")));
					int pdpCount = 0;
					while (pdpCount < pdp_default.size()) {
						String pdpVal = pdp_default.get(pdpCount);
						readPDPParam(pdpVal);
						pdpCount++;
					}
				} else {
					readPDPParam(check_val);
				}
			} else if (propKey.startsWith("PAP_URL")) {
				String check_val = prop.getProperty(propKey);
				logger.debug("Property file value for Key : \"" + propKey + "\" Value is : \"" + check_val + "\"");
				if (check_val == null) {
					logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Properties file doesn't have the PAP_URL parameter");
					// TODO:EELF Cleanup - Remove logger
					PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "Properties file doesn't have the PAP_URL parameter");
				}
				if (check_val.contains(";")) {
					List<String> pap_default = new ArrayList<String>(Arrays.asList(check_val.split("\\s*;\\s*")));
					int papCount=0;
					while (papCount < pap_default.size()) {
						String papVal = pap_default.get(papCount);
						readPAPParam(papVal);
						papCount++;
					}
				} else {
					readPAPParam(check_val);
				}
			}
		}
		if (pdps == null || pdps.isEmpty()) {
			logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Cannot Proceed without PDP_URLs");
			// TODO:EELF Cleanup - Remove logger
			PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "Cannot Proceed without PDP_URLs");
		}
		
		if (prop.containsKey("PYPDP_ID")) {
			String id = prop.getProperty("PYPDP_ID");
			logger.debug("Property file value key: \"PYPDP_ID\" Value is : \"" + id + "\"");
			if (id == null) {
				logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Properties file doesn't have PYPDP_ID parameter");
				// TODO:EELF Cleanup - Remove logger
				PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "Properties file doesn't have PYPDP_ID parameter");
			}
			Config.pyPDPID = id;
		} else {
			logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Properties file doesn't have PYPDP_ID parameter");
			// TODO:EELF Cleanup - Remove logger
			PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "Properties file doesn't have PYPDP_ID parameter");
		}
		if (prop.containsKey("PYPDP_PASSWORD")) {
			String pass = prop.getProperty("PYPDP_PASSWORD");
			logger.debug("Property file value key: \"PYPDP_PASSWORD\" Value is : \"" + pass + "\"");
			if (pass == null) {
				logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Properties file doesn't have PYPDP_PASSWORD parameter");
				// TODO:EELF Cleanup - Remove logger
				PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "Properties file doesn't have PYPDP_PASSWORD parameter");
			}
			Config.pyPDPPass = pass;
		} else {
			logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Properties file doesn't have PYPDP_PASSWORD parameter");
			// TODO:EELF Cleanup - Remove logger
			PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "Properties file doesn't have PYPDP_PASSWORD parameter");
		}
		environment = prop.getProperty("ENVIRONMENT", "DEVL");
		logger.info("Property value for Environment " + environment);
		String value = prop.getProperty("Test");
		if(value!= null && value.equalsIgnoreCase("true")){
			test = true;
		}
		if(prop.containsKey("CLIENT_FILE")){
			clientFile = prop.getProperty("CLIENT_FILE");
			logger.debug("Property file value key: \"CLIENT_FILE\" Value is : \"" + clientFile + "\"");
			if(clientFile == null){
				logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"CLIENT_FILE value is missing.");
				// TODO:EELF Cleanup - Remove logger
				PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "CLIENT_FILE value is missing.");
			}
		}else{
			logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"CLIENT_FILE paramter is missing from the property file.");
			// TODO:EELF Cleanup - Remove logger
			PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "CLIENT_FILE paramter is missing from the property file.");
		}
		logger.info("Trying to set up IntegrityMonitor");
		try {
			logger.info("Trying to set up IntegrityMonitor");
			resourceName = prop.getProperty("RESOURCE_NAME").replaceAll(" ", "");;
			if(resourceName==null){
				logger.warn("RESOURCE_NAME is missing setting default value. ");
				resourceName = "pypdp_pdp01";
			}
			im = IntegrityMonitor.getInstance(resourceName, prop);
		} catch (Exception e) {
			logger.error("Error starting Integerity Monitor: " + e);
		}
	}

	private static void readPDPParam(String pdpVal) {
		if (pdpVal.contains(",")) {
			List<String> pdpValues = new ArrayList<String>(Arrays.asList(pdpVal.split("\\s*,\\s*")));
			if (pdpValues.size() == 3) {
				// 0 - PDPURL
				pdps.add(pdpValues.get(0));
				// 1:2 will be UserID:Password
				String userID = pdpValues.get(1);
				String pass = pdpValues.get(2);
				Base64.Encoder encoder = Base64.getEncoder();
				encoding.add(encoder.encodeToString((userID + ":" + pass)
						.getBytes(StandardCharsets.UTF_8)));
			} else {
				logger.error(XACMLErrorConstants.ERROR_PERMISSIONS+"No enough Credentials to send Request. "+ pdpValues);
				// TODO:EELF Cleanup - Remove logger
				PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "No enough Credentials to send Request. "+ pdpValues);
			}
		} else {
			logger.error(XACMLErrorConstants.ERROR_PERMISSIONS+"No enough Credentials to send Request.");
			// TODO:EELF Cleanup - Remove logger
			PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "No enough Credentials to send Request.");
		}
	}
	
	private static void readPAPParam(String papVal) {
		if (papVal.contains(",")) {
			List<String> papValues = new ArrayList<String>(Arrays.asList(papVal.split("\\s*,\\s*")));
			if (papValues.size() == 3) {
				// 0 - PAPURL
				paps.add(papValues.get(0));
				// 1:2 will be UserID:Password
				String userID = papValues.get(1);
				String pass = papValues.get(2);
				Base64.Encoder encoder = Base64.getEncoder();
				encodingPAP.add(encoder.encodeToString((userID + ":" + pass)
						.getBytes(StandardCharsets.UTF_8)));
			} else {
				logger.error(XACMLErrorConstants.ERROR_PERMISSIONS+"Not enough Credentials to send Request. "+ papValues);
				// TODO:EELF Cleanup - Remove logger
				PolicyLogger.error(MessageCodes.ERROR_PERMISSIONS, "Not enough Credentials to send Request. "+ papValues);
			}
		} else {
			logger.error(XACMLErrorConstants.ERROR_PERMISSIONS+"Not enough Credentials to send Request.");
			// TODO:EELF Cleanup - Remove logger
			PolicyLogger.error(MessageCodes.ERROR_PERMISSIONS, "Not enough Credentials to send Request.");
		}
	}

	public static List<String> getPDPs() {
		setProperty();
		return Config.pdps;
	}
	
	public static List<String> getPAPs() {
		setProperty();
		return Config.paps;
	}

	public static List<String> getEncoding() {
		return Config.encoding;
	}
	
	public static List<String> getEncodingPAP() {
		return Config.encodingPAP;
	}

	public static String getPYPDPID() {
		return Config.pyPDPID;
	}

	public static String getPYPDPPass() {
		return Config.pyPDPPass;
	}
	
	public static String getEnvironment(){
		return Config.environment;
	}
	
	public static IntegrityMonitor getIntegrityMonitor(){
		if(im==null){
			setProperty();
		}
		return im;
	}
	
	public static String getClientFile() {
		return Config.clientFile;
	}

	public static Boolean isTest() {
		return Config.test;
	}
}