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=========================================================
20 package org.openecomp.policy.xacml.action;
22 import java.io.BufferedReader;
23 import java.io.IOException;
24 import java.io.InputStreamReader;
26 import java.net.URLConnection;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.HashMap;
31 import java.util.logging.Logger;
32 import java.util.regex.Matcher;
33 import java.util.regex.Pattern;
35 import javax.json.Json;
36 import javax.json.JsonReader;
38 import org.apache.http.HttpResponse;
39 import org.apache.http.client.ClientProtocolException;
40 import org.apache.http.client.methods.HttpGet;
41 import org.apache.http.client.methods.HttpPost;
42 import org.apache.http.client.methods.HttpPut;
43 import org.apache.http.entity.StringEntity;
44 import org.apache.http.impl.client.DefaultHttpClient;
45 import org.openecomp.policy.rest.XACMLRestProperties;
47 import com.att.research.xacml.api.Advice;
48 import com.att.research.xacml.api.Attribute;
49 import com.att.research.xacml.api.AttributeAssignment;
50 import com.att.research.xacml.api.AttributeValue;
51 import com.att.research.xacml.api.Identifier;
52 import com.att.research.xacml.api.Obligation;
53 import com.att.research.xacml.api.Request;
54 import com.att.research.xacml.api.RequestAttributes;
55 import com.att.research.xacml.api.Result;
56 import com.att.research.xacml.std.IdentifierImpl;
57 import com.att.research.xacml.std.StdAdvice;
58 import com.att.research.xacml.std.StdAttributeAssignment;
59 import com.att.research.xacml.std.StdAttributeValue;
60 import com.att.research.xacml.std.StdMutableResponse;
61 import com.att.research.xacml.std.StdMutableResult;
62 import com.att.research.xacml.std.StdObligation;
63 import com.att.research.xacml.util.XACMLProperties;
65 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
67 public class FindAction {
68 private Logger logger = (Logger) FlexLogger.getLogger(this.getClass());
69 private Boolean changeIt = false;
70 private String configURL = null;
71 private StdMutableResponse newResponse = new StdMutableResponse();
72 private StdMutableResult addResult = new StdMutableResult();
74 public StdMutableResponse run(StdMutableResponse stdResponse, Request pepRequest) {
76 boolean config = false;
77 boolean decide = false;
78 Collection<RequestAttributes> requestAttributes = pepRequest.getRequestAttributes();
79 for(RequestAttributes requestAttribute : requestAttributes){
80 Collection<Attribute> attributes = requestAttribute.getAttributes();
81 for(Attribute attribute : attributes){
82 if(attribute.getAttributeId().stringValue().equals("urn:oasis:names:tc:xacml:1.0:action:action-id")){
83 for(AttributeValue<?> attributeValue : attribute.getValues()){
84 if(attributeValue.getValue().toString().equalsIgnoreCase("ACCESS")){
87 if(attributeValue.getValue().toString().equalsIgnoreCase("DECIDE")){
92 if(attribute.getAttributeId().stringValue().equals("urn:oasis:names:tc:xacml:1.0:resource:resource-id")){
93 for(AttributeValue<?> attributeValue : attribute.getValues()){
94 if(attributeValue.getValue().toString().equalsIgnoreCase("Config")){
107 addResults(stdResponse, config , decide);
108 logger.info("Original Result is " + stdResponse.toString());
109 logger.info("Generated Result is " + addResult.toString());
113 private Collection<Obligation> obligations = new ArrayList<Obligation>();
114 private Map<String, String> matchValues = new HashMap<String, String>();
115 private Map<String, String> headers = new HashMap<String, String>();
116 private boolean header = false;
118 private void search(StdMutableResponse stdResponse) {
119 for (Result result : stdResponse.getResults()) {
120 if (!result.getObligations().isEmpty()) {
121 System.out.println("Obligation Received");
122 // Is there any action that PDP needs to take
123 for (Obligation obligation : result.getObligations()) {
124 int count = 0, uri = 0, PEP = 0;
127 Collection<AttributeAssignment> afterRemoveAssignments = new ArrayList<AttributeAssignment>();
128 Identifier oblId = new IdentifierImpl(obligation.getId().stringValue());
129 StdAttributeAssignment attributeURI = null;
130 for (AttributeAssignment attribute : obligation.getAttributeAssignments()) {
131 matchValues.put(attribute.getAttributeId().stringValue(), attribute.getAttributeValue().getValue().toString());
132 if (attribute.getAttributeId().stringValue().equalsIgnoreCase("performer")) {
133 if (attribute.getAttributeValue().getValue().toString().equalsIgnoreCase("PEPACTION")) {
135 } else if (attribute.getAttributeValue().getValue().toString().equalsIgnoreCase("PDPACTION")) {
138 } else if (attribute.getAttributeId().stringValue().equalsIgnoreCase("URL")) {
141 configURL = attribute.getAttributeValue().getValue().toString();
142 attributeURI = new StdAttributeAssignment(attribute);
144 } else if (attribute.getAttributeId().stringValue().startsWith("headers")) {
145 logger.info("Headers are : "+ attribute.getAttributeValue().getValue().toString());
147 headers.put(attribute.getAttributeId().stringValue().replaceFirst("(headers).", ""),
148 attribute.getAttributeValue().getValue().toString());
149 afterRemoveAssignments.add(attribute);
150 } else if (attribute.getAttributeId().stringValue().equalsIgnoreCase("body")) {
151 String papPath = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_URL);
152 papPath= papPath.replace("/pap", "");
153 matchValues.put("body",attribute.getAttributeValue().getValue().toString().replace("$URL", papPath));
155 * else if (attribute.getAttributeId().stringValue().
156 * equalsIgnoreCase("type")){ requestAction.put("Type",
157 * attribute.getAttributeValue().getValue().toString());
158 * afterRemoveAssignments.add(attribute); } else
160 * .getAttributeId().stringValue().equalsIgnoreCase
161 * ("method")) { requestAction.put("Method",
162 * attribute.getAttributeValue().getValue().toString());
163 * afterRemoveAssignments.add(attribute); } else
165 * .getAttributeId().stringValue().equalsIgnoreCase
166 * ("body")) { requestAction.put("Body",
167 * attribute.getAttributeValue().getValue().toString());
168 * afterRemoveAssignments.add(attribute); }
170 StdAttributeAssignment attributeObligation = new StdAttributeAssignment(attribute);
171 afterRemoveAssignments.add(attributeObligation);
174 if (count == 1 && uri == 1 && PEP == 0) {
175 // Remove Obligation and add Advice
177 TakeAction(stdResponse, oblId, afterRemoveAssignments);
178 } else if (PEP == 1 && count == 0) {
179 // Strip the PEPACTION if available
181 afterRemoveAssignments.add(attributeURI);
183 Obligation afterRemoveObligation = new StdObligation(
184 oblId, afterRemoveAssignments);
185 obligations.add(afterRemoveObligation);
187 obligations.add(obligation);
194 private void TakeAction(StdMutableResponse stdResponse, Identifier advId,
195 Collection<AttributeAssignment> afterRemoveAssignments) {
197 logger.info("the URL is :" + configURL);
198 // Calling Rest URL..
200 // Including the Results in an Advice
201 Identifier id = new IdentifierImpl(
202 "com:att:labs:ecomp:policy:pdp:reply");
203 Identifier statId = new IdentifierImpl(
204 "com:att:labs:ecomp:policy:pdp:reply:status");
205 Identifier statCategory = new IdentifierImpl(
206 "urn:oasis:names:tc:xacml:1.0:subject-category:recipient-subject");
207 Identifier strId = new IdentifierImpl(
208 "http://www.w3.org/2001/XMLSchema#string");
209 Identifier resId = new IdentifierImpl(
210 "com:att:labs:ecomp:policy:pdp:reply:resource");
211 Identifier resCategory = new IdentifierImpl(
212 "urn:oasis:names:tc:xacml:3.0:attribute-category:resource");
213 Identifier urlId = new IdentifierImpl(
214 "http://www.w3.org/2001/XMLSchema#anyURI");
215 // Collection<AttributeAssignment> attributes = new
216 // ArrayList<AttributeAssignment>();
217 AttributeValue<String> attributeStatusValue = new StdAttributeValue<String>(
218 strId, status + response);
219 AttributeValue<String> attributeResourceValue = new StdAttributeValue<String>(
221 StdAttributeAssignment attributeStatus = new StdAttributeAssignment(
222 statCategory, statId, "PDP", attributeStatusValue);
223 StdAttributeAssignment attributeResouce = new StdAttributeAssignment(
224 resCategory, resId, "PDP", attributeResourceValue);
225 afterRemoveAssignments.add(attributeStatus);
226 afterRemoveAssignments.add(attributeResouce);
227 Advice advice = new StdAdvice(id, afterRemoveAssignments);
228 addResult.addAdvice(advice);
232 private void addResults(StdMutableResponse stdResponse, boolean config, boolean decide) {
234 newResponse = stdResponse;
237 for (Result result : stdResponse.getResults()) {
239 addResult.addAdvice(result.getAssociatedAdvice());
241 addResult.addAttributeCategories(result.getAttributes());
242 addResult.addPolicyIdentifiers(result.getPolicyIdentifiers());
243 addResult.addPolicySetIdentifiers(result.getPolicySetIdentifiers());
244 addResult.setStatus(result.getStatus());
245 addResult.setDecision(result.getDecision());
247 addResult.addObligations(obligations);
250 newResponse.add(addResult);
254 private String response;
256 private void callRest() {
257 // Finding the Macros in the URL..
258 Pattern pattern = Pattern.compile("\\$([a-zA-Z0-9.:]*)");
259 Matcher match = pattern.matcher(configURL);
260 StringBuffer sb = new StringBuffer();
261 while (match.find()) {
262 logger.info("Found Macro : " + match.group(1));
263 String replaceValue = matchValues.get(match.group(1));
264 logger.info("Replacing with :" + replaceValue);
265 match.appendReplacement(sb, replaceValue);
267 match.appendTail(sb);
268 logger.info("URL is : " + sb.toString());
269 configURL = sb.toString();
270 // Calling the Requested service.
271 if (matchValues.get("method").equalsIgnoreCase("GET")) {
272 DefaultHttpClient httpClient = new DefaultHttpClient();
274 HttpGet getRequest = new HttpGet(configURL);
275 // Adding Headers here
277 for (String key : headers.keySet()) {
278 getRequest.addHeader(key, headers.get(key));
281 HttpResponse result = httpClient.execute(getRequest);
282 status = result.getStatusLine().getStatusCode();
283 BufferedReader br = new BufferedReader(new InputStreamReader(
284 (result.getEntity().getContent())));
287 while ((out = br.readLine()) != null) {
288 output = output + out;
291 } catch (ClientProtocolException e) {
292 response = e.getMessage();
293 } catch (IOException e) {
294 response = e.getMessage();
296 httpClient.getConnectionManager().shutdown();
298 } else if(matchValues.get("method").equalsIgnoreCase("POST")) {
299 DefaultHttpClient httpClient = new DefaultHttpClient();
301 HttpPost postRequest = new HttpPost(configURL);
302 // Adding Headers here
304 for (String key : headers.keySet()) {
305 postRequest.addHeader(key, headers.get(key));
309 URL configURL = new URL(matchValues.get("body"));
310 URLConnection connection = null;
311 connection = configURL.openConnection();
312 // InputStream in = connection.getInputStrem();
313 // logger.info("The Body Content is : " + IOUtils.toString(in));
314 JsonReader jsonReader = Json.createReader(connection.getInputStream());
315 StringEntity input = new StringEntity(jsonReader.readObject().toString());
316 input.setContentType("application/json");
317 postRequest.setEntity(input);
318 // Executing the Request.
319 HttpResponse result = httpClient.execute(postRequest);
320 logger.info("Result Headers are : " + result.getAllHeaders());
321 status = result.getStatusLine().getStatusCode();
322 BufferedReader br = new BufferedReader(new InputStreamReader(
323 (result.getEntity().getContent())));
326 while ((out = br.readLine()) != null) {
327 output = output + out;
330 } catch (ClientProtocolException e) {
331 response = e.getMessage();
332 } catch (IOException e) {
333 response = e.getMessage();
335 httpClient.getConnectionManager().shutdown();
337 } else if(matchValues.get("method").equalsIgnoreCase("PUT")) {
338 DefaultHttpClient httpClient = new DefaultHttpClient();
340 HttpPut putRequest = new HttpPut(configURL);
341 // Adding Headers here
343 for (String key : headers.keySet()) {
344 putRequest.addHeader(key, headers.get(key));
348 URL configURL = new URL(matchValues.get("body"));
349 URLConnection connection = null;
350 connection = configURL.openConnection();
351 //InputStream in = connection.getInputStream();
352 //logger.info("The Body Content is : " + IOUtils.toString(in));
353 JsonReader jsonReader = Json.createReader(connection.getInputStream());
354 StringEntity input = new StringEntity(jsonReader.readObject().toString());
355 input.setContentType("application/json");
356 putRequest.setEntity(input);
357 // Executing the Request.
358 HttpResponse result = httpClient.execute(putRequest);
359 status = result.getStatusLine().getStatusCode();
360 BufferedReader br = new BufferedReader(new InputStreamReader(
361 (result.getEntity().getContent())));
364 while ((out = br.readLine()) != null) {
365 output = output + out;
368 } catch (ClientProtocolException e) {
369 response = e.getMessage();
370 } catch (IOException e) {
371 response = e.getMessage();
373 httpClient.getConnectionManager().shutdown();