Merge "Upgrade log4j to clear security issue"
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / components / ActionPolicy.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2017-2018 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.onap.policy.pap.xacml.rest.components;
22
23 import java.io.BufferedWriter;
24 import java.io.File;
25 import java.io.FileWriter;
26 import java.io.IOException;
27 import java.nio.file.Path;
28 import java.nio.file.Paths;
29 import java.util.HashMap;
30 import java.util.LinkedList;
31 import java.util.List;
32 import java.util.Map;
33
34 import org.onap.policy.common.logging.eelf.MessageCodes;
35 import org.onap.policy.common.logging.eelf.PolicyLogger;
36 import org.onap.policy.common.logging.flexlogger.FlexLogger;
37 import org.onap.policy.common.logging.flexlogger.Logger;
38 import org.onap.policy.rest.adapter.PolicyRestAdapter;
39 import org.onap.policy.rest.dao.CommonClassDao;
40 import org.onap.policy.rest.jpa.FunctionDefinition;
41 import org.onap.policy.xacml.api.XACMLErrorConstants;
42
43 import com.att.research.xacml.api.pap.PAPException;
44
45 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
46 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
47 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ApplyType;
48 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeAssignmentExpressionType;
49 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
50 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
51 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ConditionType;
52 import oasis.names.tc.xacml._3_0.core.schema.wd_17.EffectType;
53 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
54 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObjectFactory;
55 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObligationExpressionType;
56 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObligationExpressionsType;
57 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
58 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
59 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType; 
60
61 public class ActionPolicy extends Policy {
62     
63     /**
64      * ActionPolicy Fields
65      */
66     private static final Logger LOGGER = FlexLogger.getLogger(ActionPolicy.class);
67     
68     public static final String JSON_CONFIG = "JSON";
69     
70     public static final String PDP_ACTION = "PDP";
71     public static final String PEP_ACTION = "PEP";
72     public static final String TYPE_ACTION = "REST";
73
74     public static final String GET_METHOD = "GET";
75     public static final String PUT_METHOD = "PUT";
76     public static final String POST_METHOD = "POST";
77
78     public static final String PERFORMER_ATTRIBUTEID = "performer";
79     public static final String TYPE_ATTRIBUTEID = "type";
80     public static final String METHOD_ATTRIBUTEID = "method";
81     public static final String HEADERS_ATTRIBUTEID = "headers";
82     public static final String URL_ATTRIBUTEID = "url";
83     public static final String BODY_ATTRIBUTEID = "body";
84     
85     List<String> dynamicLabelRuleAlgorithms = new LinkedList<>();
86     List<String> dynamicFieldFunctionRuleAlgorithms = new LinkedList<>();
87     List<String> dynamicFieldOneRuleAlgorithms = new LinkedList<>();
88     List<String> dynamicFieldTwoRuleAlgorithms = new LinkedList<>();
89     
90
91     
92     private CommonClassDao commonClassDao;
93     
94     private static boolean isAttribute = false;
95     private synchronized static boolean getAttribute () {
96         return isAttribute;
97
98     }
99     
100     public ActionPolicy() {
101         super();
102     }
103     
104     public ActionPolicy(PolicyRestAdapter policyAdapter, CommonClassDao commonClassDao){
105         this.policyAdapter = policyAdapter;
106         this.commonClassDao = commonClassDao;
107     }
108
109         @Override
110     public Map<String, String> savePolicies() throws PAPException {
111         
112         Map<String, String> successMap = new HashMap<>();
113         if(isPolicyExists()){
114             successMap.put("EXISTS", "This Policy already exist on the PAP");
115             return successMap;
116         }
117         
118         if(!ActionPolicy.getAttribute()) {
119             successMap.put("invalidAttribute", "Action Attrbute was not in the database.");
120             return successMap;
121         }
122         
123         if(!isPreparedToSave()){
124             //Prep and configure the policy for saving
125             prepareToSave();
126         }
127
128         // Until here we prepared the data and here calling the method to create xml.
129         Path newPolicyPath = null;
130         newPolicyPath = Paths.get(policyAdapter.getNewFileName());
131         successMap = createPolicy(newPolicyPath,getCorrectPolicyDataObject() );     
132         return successMap;      
133     }
134     
135     //This is the method for preparing the policy for saving.  We have broken it out
136     //separately because the fully configured policy is used for multiple things
137     @Override
138     public boolean prepareToSave() throws PAPException{
139
140         if(isPreparedToSave()){
141             //we have already done this
142             return true;
143         }
144         
145         int version = 0;
146         String policyID = policyAdapter.getPolicyID();
147         version = policyAdapter.getHighestVersion();
148         
149         // Create the Instance for pojo, PolicyType object is used in marshalling.
150         if (policyAdapter.getPolicyType().equals("Action")) {
151             PolicyType policyConfig = new PolicyType();
152
153             policyConfig.setVersion(Integer.toString(version));
154             policyConfig.setPolicyId(policyID);
155             policyConfig.setTarget(new TargetType());
156             policyAdapter.setData(policyConfig);
157         }
158         
159         policyName = policyAdapter.getNewFileName();
160         
161         if (policyAdapter.getData() != null) {
162             // Action body is optional so checking value provided or not
163             String comboDictValue = policyAdapter.getActionAttribute();
164             String actionBody = policyAdapter.getActionBody();
165             setAttribute(false);
166
167             //if actionBody is null or empty then we know the ActionAttribute in the request does not exist in the dictionary
168             if(!(actionBody==null || "".equals(actionBody))){   
169                 saveActionBody(policyName, actionBody);
170                 setAttribute(true);
171             } else {
172                 if(!getAttribute()){
173                     LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Could not find " + comboDictValue + " in the ActionPolicyDict table.");
174                     return false;
175                 }
176             }
177             
178             PolicyType actionPolicy = (PolicyType) policyAdapter.getData();
179             actionPolicy.setDescription(policyAdapter.getPolicyDescription());
180             actionPolicy.setRuleCombiningAlgId(policyAdapter.getRuleCombiningAlgId());
181
182             AllOfType allOf = new AllOfType();
183             
184             Map<String, String> dynamicFieldComponentAttributes = policyAdapter.getDynamicFieldConfigAttributes();
185             
186             // If there is any dynamic field attributes create the matches here
187             for (String keyField : dynamicFieldComponentAttributes.keySet()) {
188                 String key = keyField;
189                 String value = dynamicFieldComponentAttributes.get(key);
190                 MatchType dynamicMatch = createDynamicMatch(key, value);
191                 allOf.getMatch().add(dynamicMatch);
192             }
193
194             AnyOfType anyOf = new AnyOfType();
195             anyOf.getAllOf().add(allOf);
196
197             TargetType target = new TargetType();
198             target.getAnyOf().add(anyOf);
199             
200             // Adding the target to the policy element
201             actionPolicy.setTarget(target);
202             
203             RuleType rule = new RuleType();
204             rule.setRuleId(policyAdapter.getRuleID());
205
206             rule.setEffect(EffectType.PERMIT);
207             rule.setTarget(new TargetType());
208             
209             dynamicLabelRuleAlgorithms = policyAdapter.getDynamicRuleAlgorithmLabels();
210             dynamicFieldFunctionRuleAlgorithms = policyAdapter.getDynamicRuleAlgorithmCombo();
211             dynamicFieldOneRuleAlgorithms = policyAdapter.getDynamicRuleAlgorithmField1();
212             dynamicFieldTwoRuleAlgorithms = policyAdapter.getDynamicRuleAlgorithmField2();
213                         
214             // Rule attributes are optional and dynamic so check and add them to condition.
215             if (dynamicLabelRuleAlgorithms != null && !dynamicLabelRuleAlgorithms.isEmpty()) {
216                 boolean isCompound = false;
217                 ConditionType condition = new ConditionType();
218                 int index = dynamicFieldOneRuleAlgorithms.size() - 1;
219
220                 for (String labelAttr : dynamicLabelRuleAlgorithms) {
221                     // if the rule algorithm as a label means it is a compound
222                     if (dynamicFieldOneRuleAlgorithms.get(index).equals(labelAttr)) {
223                         ApplyType actionApply = new ApplyType();
224
225                         String selectedFunction = dynamicFieldFunctionRuleAlgorithms.get(index).toString();
226                         String value1 = dynamicFieldOneRuleAlgorithms.get(index);
227                         String value2 = dynamicFieldTwoRuleAlgorithms.get(index);
228                         actionApply.setFunctionId(getFunctionDefinitionId(selectedFunction));
229                         actionApply.getExpression().add(new ObjectFactory().createApply(getInnerActionApply(value1)));
230                         actionApply.getExpression().add(new ObjectFactory().createApply(getInnerActionApply(value2)));
231                         condition.setExpression(new ObjectFactory().createApply(actionApply));
232                         isCompound = true;
233                     }
234                 }
235                 // if rule algorithm not a compound
236                 if (!isCompound) {
237                     condition.setExpression(new ObjectFactory().createApply(getInnerActionApply(dynamicLabelRuleAlgorithms.get(index).toString())));
238                 }
239                 rule.setCondition(condition);
240             }
241             // set the obligations to rule
242             rule.setObligationExpressions(getObligationExpressions());
243             actionPolicy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(rule);
244             policyAdapter.setPolicyData(actionPolicy);
245         }  else {
246             PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + "Unsupported data object." + policyAdapter.getData().getClass().getCanonicalName());
247         }   
248
249         setPreparedToSave(true);
250         return true;
251     }
252     
253     private static synchronized void setAttribute(boolean b) {
254         isAttribute = b;
255     }
256
257     // Saving the json Configurations file if exists at server location for action policy.
258     private void saveActionBody(String policyName, String actionBodyData) {
259             if(policyName.endsWith(".xml")){
260                 policyName = policyName.replace(".xml", "");
261             }
262             File file = new File(ACTION_HOME+ File.separator + policyName + ".json");
263                         try(BufferedWriter bw = new BufferedWriter(new FileWriter(file.getAbsoluteFile()))) {
264             bw.write(actionBodyData);
265             if (LOGGER.isInfoEnabled()) {
266                 LOGGER.info("Action Body is succesfully saved at " + file.getAbsolutePath());
267             }
268         } catch (IOException e) {
269             LOGGER.error("Exception Occured"+e);
270         }
271     }
272     
273     // Data required for obligation part is setting here.
274     private ObligationExpressionsType getObligationExpressions() {
275         ObligationExpressionsType obligations = new ObligationExpressionsType();
276
277         ObligationExpressionType obligation = new ObligationExpressionType();
278         String comboDictValue = policyAdapter.getActionAttribute();
279         obligation.setObligationId(comboDictValue);
280         obligation.setFulfillOn(EffectType.PERMIT);
281
282         // Add Action Assignment:
283         AttributeAssignmentExpressionType assignment1 = new AttributeAssignmentExpressionType();
284         assignment1.setAttributeId(PERFORMER_ATTRIBUTEID);
285         assignment1.setCategory(CATEGORY_RECIPIENT_SUBJECT);
286
287         AttributeValueType actionNameAttributeValue = new AttributeValueType();
288         actionNameAttributeValue.setDataType(STRING_DATATYPE);
289         actionNameAttributeValue.getContent().add(performer.get(policyAdapter.getActionPerformer()));
290
291         assignment1.setExpression(new ObjectFactory().createAttributeValue(actionNameAttributeValue));
292         obligation.getAttributeAssignmentExpression().add(assignment1);
293
294         // Add Type Assignment:
295         AttributeAssignmentExpressionType assignmentType = new AttributeAssignmentExpressionType();
296         assignmentType.setAttributeId(TYPE_ATTRIBUTEID);
297         assignmentType.setCategory(CATEGORY_RESOURCE);
298
299         AttributeValueType typeAttributeValue = new AttributeValueType();
300         typeAttributeValue.setDataType(STRING_DATATYPE);
301         String actionDictType = policyAdapter.getActionDictType();
302         typeAttributeValue.getContent().add(actionDictType);
303
304         assignmentType.setExpression(new ObjectFactory().createAttributeValue(typeAttributeValue));
305         obligation.getAttributeAssignmentExpression().add(assignmentType);
306
307         // Add Rest_URL Assignment:
308         AttributeAssignmentExpressionType assignmentURL = new AttributeAssignmentExpressionType();
309         assignmentURL.setAttributeId(URL_ATTRIBUTEID);
310         assignmentURL.setCategory(CATEGORY_RESOURCE);
311
312         AttributeValueType actionURLAttributeValue = new AttributeValueType();
313         actionURLAttributeValue.setDataType(URI_DATATYPE);
314         String actionDictUrl = policyAdapter.getActionDictUrl();
315         actionURLAttributeValue.getContent().add(actionDictUrl);
316
317         assignmentURL.setExpression(new ObjectFactory().createAttributeValue(actionURLAttributeValue));
318         obligation.getAttributeAssignmentExpression().add(assignmentURL);
319
320         // Add Method Assignment:
321         AttributeAssignmentExpressionType assignmentMethod = new AttributeAssignmentExpressionType();
322         assignmentMethod.setAttributeId(METHOD_ATTRIBUTEID);
323         assignmentMethod.setCategory(CATEGORY_RESOURCE);
324
325         AttributeValueType methodAttributeValue = new AttributeValueType();
326         methodAttributeValue.setDataType(STRING_DATATYPE);
327         String actionDictMethod = policyAdapter.getActionDictMethod();
328         methodAttributeValue.getContent().add(actionDictMethod);
329
330         assignmentMethod.setExpression(new ObjectFactory().createAttributeValue(methodAttributeValue));
331         obligation.getAttributeAssignmentExpression().add(assignmentMethod);
332
333         // Add JSON_URL Assignment:
334         String actionBody = policyAdapter.getActionBody();      
335         if (actionBody != null) {
336             AttributeAssignmentExpressionType assignmentJsonURL = new AttributeAssignmentExpressionType();
337             assignmentJsonURL.setAttributeId(BODY_ATTRIBUTEID);
338             assignmentJsonURL.setCategory(CATEGORY_RESOURCE);
339
340             AttributeValueType jsonURLAttributeValue = new AttributeValueType();
341             jsonURLAttributeValue.setDataType(URI_DATATYPE);
342             jsonURLAttributeValue.getContent().add(CONFIG_URL + "/Action/"  + policyName + ".json");
343
344             assignmentJsonURL.setExpression(new ObjectFactory().createAttributeValue(jsonURLAttributeValue));
345             obligation.getAttributeAssignmentExpression().add(assignmentJsonURL);
346         }
347
348         String headerVal = policyAdapter.getActionDictHeader();
349         if(headerVal != null && !headerVal.trim().isEmpty()){
350             // parse it on : to get number of headers
351             String[] result = headerVal.split(":");
352             for (String eachString : result){
353                 // parse each value on =
354                 String[] textFieldVals = eachString.split("=");
355                 obligation.getAttributeAssignmentExpression().add(addDynamicHeaders(textFieldVals[0], textFieldVals[1]));
356             }
357         }
358             
359         obligations.getObligationExpression().add(obligation);
360         return obligations;
361     }
362
363     
364     // if compound setting the inner apply here
365     protected ApplyType getInnerActionApply(String value1Label) {
366         ApplyType actionApply = new ApplyType();
367         int index = 0;
368         // check the index for the label.
369         for (String labelAttr : dynamicLabelRuleAlgorithms) {
370             if (labelAttr.equals(value1Label)) {
371                 String value1 = dynamicFieldOneRuleAlgorithms.get(index).toString();
372                 // check if the row contains label again
373                 for (String labelValue : dynamicLabelRuleAlgorithms) {
374                     if (labelValue.equals(value1)) {
375                         return getCompoundApply(index);
376                     }
377                 }
378
379                 // Getting the values from the form.
380                 String functionKey = dynamicFieldFunctionRuleAlgorithms.get(index);
381                 String value2 = dynamicFieldTwoRuleAlgorithms.get(index);
382                 actionApply.setFunctionId(getFunctionDefinitionId(functionKey));
383                 // if two text field are rule attributes.
384                 if ((value1.contains(RULE_VARIABLE)) && (value2.contains(RULE_VARIABLE))) {
385                     ApplyType innerActionApply1 = new ApplyType();
386                     ApplyType innerActionApply2 = new ApplyType();
387                     AttributeDesignatorType attributeDesignator1 = new AttributeDesignatorType();
388                     AttributeDesignatorType attributeDesignator2 = new AttributeDesignatorType();
389                     // If selected function is Integer function set integer functionID
390                     if (functionKey.toLowerCase().contains("integer")) {
391                         innerActionApply1.setFunctionId(FUNTION_INTEGER_ONE_AND_ONLY);
392                         innerActionApply2.setFunctionId(FUNTION_INTEGER_ONE_AND_ONLY);
393                         attributeDesignator1.setDataType(INTEGER_DATATYPE);
394                         attributeDesignator2.setDataType(INTEGER_DATATYPE);
395                     } else {
396                         // If selected function is not a Integer function
397                         // set String functionID
398                         innerActionApply1.setFunctionId(FUNCTION_STRING_ONE_AND_ONLY);
399                         innerActionApply2.setFunctionId(FUNCTION_STRING_ONE_AND_ONLY);
400                         attributeDesignator1.setDataType(STRING_DATATYPE);
401                         attributeDesignator2.setDataType(STRING_DATATYPE);
402                     }
403                     attributeDesignator1.setCategory(CATEGORY_RESOURCE);
404                     attributeDesignator2.setCategory(CATEGORY_RESOURCE);
405
406                     // Here set actual field values
407                     attributeDesignator1.setAttributeId(value1.contains("resource:") ? value1.substring(9): value1.substring(8));
408                     attributeDesignator2.setAttributeId(value1.contains("resource:") ? value1.substring(9): value1.substring(8));
409
410                     innerActionApply1.getExpression().add(new ObjectFactory().createAttributeDesignator(attributeDesignator1));
411                     innerActionApply2.getExpression().add(new ObjectFactory().createAttributeDesignator(attributeDesignator2));
412
413                     actionApply.getExpression().add(new ObjectFactory().createApply(innerActionApply1));
414                     actionApply.getExpression().add(new ObjectFactory().createApply(innerActionApply2));
415
416                 } else {// if either of one text field is rule attribute.
417                     ApplyType innerActionApply = new ApplyType();
418                     AttributeDesignatorType attributeDesignator = new AttributeDesignatorType();
419                     AttributeValueType actionConditionAttributeValue = new AttributeValueType();
420
421                     if (functionKey.toLowerCase().contains("integer")) {
422                         innerActionApply.setFunctionId(FUNTION_INTEGER_ONE_AND_ONLY);
423                         actionConditionAttributeValue.setDataType(INTEGER_DATATYPE);
424                         attributeDesignator.setDataType(INTEGER_DATATYPE);
425                     } else {
426                         innerActionApply.setFunctionId(FUNCTION_STRING_ONE_AND_ONLY);
427                         actionConditionAttributeValue.setDataType(STRING_DATATYPE);
428                         attributeDesignator.setDataType(STRING_DATATYPE);
429                     }
430
431                     String attributeId = null;
432                     String attributeValue = null;
433
434                     // Find which textField has rule attribute and set it as
435                     attributeId = value1;
436                     attributeValue = value2;
437
438                     if (attributeId != null) {
439                         attributeDesignator.setCategory(CATEGORY_RESOURCE);
440                         attributeDesignator.setAttributeId(attributeId);
441                     }
442                     actionConditionAttributeValue.getContent().add(attributeValue);
443                     innerActionApply.getExpression().add(new ObjectFactory().createAttributeDesignator(attributeDesignator));
444                     // Decide the order of element based the values.
445                     if (attributeId.equals(value1)) {
446                         actionApply.getExpression().add(new ObjectFactory().createApply(innerActionApply));
447                         actionApply.getExpression().add(new ObjectFactory().createAttributeValue(actionConditionAttributeValue));
448                     } else {
449                         actionApply.getExpression().add(new ObjectFactory().createAttributeValue(actionConditionAttributeValue));
450                         actionApply.getExpression().add(new ObjectFactory().createApply(innerActionApply));
451                     }
452                 }
453             }
454             index++;
455         }
456         return actionApply;
457     }
458
459     // if the rule algorithm is multiple compound one setting the apply
460     protected ApplyType getCompoundApply(int index) {
461         ApplyType actionApply = new ApplyType();
462         String selectedFunction = dynamicFieldFunctionRuleAlgorithms.get(index);
463         String value1 = dynamicFieldOneRuleAlgorithms.get(index);
464         String value2 = dynamicFieldTwoRuleAlgorithms.get(index);
465         actionApply.setFunctionId(getFunctionDefinitionId(selectedFunction));
466         actionApply.getExpression().add(new ObjectFactory().createApply(getInnerActionApply(value1)));
467         actionApply.getExpression().add(new ObjectFactory().createApply(getInnerActionApply(value2)));
468         return actionApply;
469     }
470         
471     // Adding the dynamic headers if any
472     private AttributeAssignmentExpressionType addDynamicHeaders(String header, String value) {
473         AttributeAssignmentExpressionType assignmentHeaders = new AttributeAssignmentExpressionType();
474         assignmentHeaders.setAttributeId("headers:" + header);
475         assignmentHeaders.setCategory(CATEGORY_RESOURCE);
476
477         AttributeValueType headersAttributeValue = new AttributeValueType();
478         headersAttributeValue.setDataType(STRING_DATATYPE);
479         headersAttributeValue.getContent().add(value);
480
481         assignmentHeaders.setExpression(new ObjectFactory().createAttributeValue(headersAttributeValue));
482         return assignmentHeaders;
483     }
484
485     @Override
486     public Object getCorrectPolicyDataObject() {
487         return policyAdapter.getPolicyData();
488     }
489     
490     public String getFunctionDefinitionId(String key){
491         FunctionDefinition object = (FunctionDefinition) commonClassDao.getDataById(FunctionDefinition.class, "short_name", key);
492         if(object != null){
493                 return object.getXacmlid();
494         }
495         return null;
496     }
497
498 }