Update css file name in conf.py
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / admin / RESTfulPAPEngine.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd.
7  * Modifications Copyright (C) 2019 Bell Canada
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.admin;
24
25 import com.att.research.xacml.api.pap.PAPException;
26 import com.att.research.xacml.api.pap.PDPPolicy;
27 import com.att.research.xacml.api.pap.PDPStatus;
28 import com.att.research.xacml.util.XACMLProperties;
29 import com.fasterxml.jackson.databind.DeserializationFeature;
30 import com.fasterxml.jackson.databind.ObjectMapper;
31 import com.fasterxml.jackson.databind.type.CollectionType;
32
33 import java.io.File;
34 import java.io.FileInputStream;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.io.OutputStream;
38 import java.io.UnsupportedEncodingException;
39 import java.net.HttpURLConnection;
40 import java.net.URL;
41 import java.net.URLConnection;
42 import java.net.URLEncoder;
43 import java.nio.charset.StandardCharsets;
44 import java.util.Arrays;
45 import java.util.Base64;
46 import java.util.Collections;
47 import java.util.HashMap;
48 import java.util.Map;
49 import java.util.Set;
50
51 import org.apache.commons.io.IOUtils;
52 import org.apache.commons.lang.StringUtils;
53 import org.onap.policy.common.logging.flexlogger.FlexLogger;
54 import org.onap.policy.common.logging.flexlogger.Logger;
55 import org.onap.policy.rest.XacmlRestProperties;
56 import org.onap.policy.rest.adapter.PolicyRestAdapter;
57 import org.onap.policy.utils.PeCryptoUtils;
58 import org.onap.policy.xacml.api.XACMLErrorConstants;
59 import org.onap.policy.xacml.api.pap.OnapPDP;
60 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
61 import org.onap.policy.xacml.api.pap.PAPPolicyEngine;
62 import org.onap.policy.xacml.std.pap.StdPAPPolicy;
63 import org.onap.policy.xacml.std.pap.StdPDP;
64 import org.onap.policy.xacml.std.pap.StdPDPGroup;
65 import org.onap.policy.xacml.std.pap.StdPDPItemSetChangeNotifier;
66 import org.onap.policy.xacml.std.pap.StdPDPPolicy;
67 import org.onap.policy.xacml.std.pap.StdPDPStatus;
68
69 /**
70  * Implementation of the PAPEngine interface that communicates with a PAP engine in a remote servlet through a RESTful
71  * interface.
72  *
73  *
74  */
75 public class RESTfulPAPEngine extends StdPDPItemSetChangeNotifier implements PAPPolicyEngine {
76     private static final Logger LOGGER = FlexLogger.getLogger(RESTfulPAPEngine.class);
77
78     private static final String GROUP_ID = "groupId=";
79     private static final String UNABLE_MSG = "Unable to PUT policy '";
80     private static final String EXCEPTION_MSG = "', e:";
81     private static final String ERROR_MSG = "error";
82     private static final String PDPID_MSG = "pdpId=";
83
84     //
85     // URL of the PAP Servlet that this Admin Console talks to
86     //
87     private String papServletUrlString;
88
89     /**
90      * Set up link with PAP Servlet and get our initial set of Groups.
91      *
92      * @throws PAPException When failing to register with PAP
93      */
94     public RESTfulPAPEngine(String myUrlString) throws PAPException {
95         //
96         // Get our URL to the PAP servlet
97         //
98         this.papServletUrlString = XACMLProperties.getProperty(XacmlRestProperties.PROP_PAP_URL);
99         if (StringUtils.isBlank(this.papServletUrlString)) {
100             String message = "The property 'POLICYENGINE_ADMIN_ACTIVE' was not set during installation. "
101                     + "Admin Console cannot call PAP.";
102             LOGGER.error(message);
103             throw new PAPException(message);
104         }
105
106         //
107         // register this Admin Console with the PAP Servlet to get updates
108         //
109         Object newUrl = sendToPap("PUT", null, null, null, "adminConsoleURL=" + myUrlString);
110         if (newUrl != null) {
111             // assume this was a re-direct and try again
112             LOGGER.warn("Redirecting to '" + newUrl + "'");
113             this.papServletUrlString = (String) newUrl;
114             newUrl = sendToPap("PUT", null, null, null, "adminConsoleURL=" + myUrlString);
115             if (newUrl != null) {
116                 LOGGER.error("Failed to redirect to " + this.papServletUrlString);
117                 throw new PAPException("Failed to register with PAP");
118             }
119         }
120     }
121
122     //
123     // High-level commands used by the Admin Console code through the PAPEngine Interface
124     //
125
126     @Override
127     public OnapPDPGroup getDefaultGroup() throws PAPException {
128         return (OnapPDPGroup) sendToPap("GET", null, null, StdPDPGroup.class, GROUP_ID, "default=");
129     }
130
131     @Override
132     public void setDefaultGroup(OnapPDPGroup group) throws PAPException {
133         sendToPap("POST", null, null, null, GROUP_ID + group.getId(), "default=true");
134     }
135
136     @SuppressWarnings("unchecked")
137     @Override
138     public Set<OnapPDPGroup> getOnapPDPGroups() throws PAPException {
139         Set<OnapPDPGroup> newGroupSet;
140         newGroupSet = (Set<OnapPDPGroup>) this.sendToPap("GET", null, Set.class, StdPDPGroup.class, GROUP_ID);
141         return Collections.unmodifiableSet(newGroupSet);
142     }
143
144     @Override
145     public OnapPDPGroup getGroup(String id) throws PAPException {
146         return (OnapPDPGroup) sendToPap("GET", null, null, StdPDPGroup.class, GROUP_ID + id);
147     }
148
149     @Override
150     public void newGroup(String name, String description) throws PAPException {
151         String escapedName;
152         String escapedDescription;
153         try {
154             escapedName = URLEncoder.encode(name, "UTF-8");
155             escapedDescription = URLEncoder.encode(description, "UTF-8");
156         } catch (UnsupportedEncodingException e) {
157             throw new PAPException("Unable to send name or description to PAP: " + e.getMessage() + e);
158         }
159
160         this.sendToPap("POST", null, null, null, GROUP_ID, "groupName=" + escapedName,
161                 "groupDescription=" + escapedDescription);
162     }
163
164     @Override
165     public void updateGroup(OnapPDPGroup group) throws PAPException {
166         try {
167             //
168             // ASSUME that all of the policies mentioned in this group are already located in the correct directory on
169             // the PAP!
170             //
171             // Whenever a Policy is added to the group, that file must be automatically copied to the PAP from the
172             // Workspace.
173             //
174             // Copy all policies from the local machine's workspace to the PAP's PDPGroup directory.
175             // This is not efficient since most of the policies will already exist there.
176             // However, the policy files are (probably!) not too huge, and this is a good way to ensure that any
177             // corrupted files on the PAP get refreshed.
178             // now update the group object on the PAP
179             sendToPap("PUT", group, null, null, GROUP_ID + group.getId());
180         } catch (Exception e) {
181             String message = UNABLE_MSG + group.getId() + EXCEPTION_MSG + e;
182             LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
183             throw new PAPException(message);
184         }
185     }
186
187     /*
188      * Passing UserName to PAP Rest for Audit Logging.
189      *
190      * @see org.onap.policy.xacml.api.pap.PAPPolicyEngine#updateGroup(org.onap.policy.xacml.api.pap.OnapPDPGroup,
191      * java.lang.String)
192      */
193     @Override
194     public void updateGroup(OnapPDPGroup group, String userName) throws PAPException {
195         try {
196             sendToPap("PUT", group, null, null, GROUP_ID + group.getId(), "userId=" + userName);
197         } catch (Exception e) {
198             String message = UNABLE_MSG + group.getId() + EXCEPTION_MSG + e;
199             LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
200             throw new PAPException(message);
201         }
202     }
203
204     @Override
205     public void removeGroup(OnapPDPGroup group, OnapPDPGroup newGroup) throws PAPException {
206         String moveToGroupString = null;
207         if (newGroup != null) {
208             moveToGroupString = "movePDPsToGroupId=" + newGroup.getId();
209         }
210         sendToPap("DELETE", null, null, null, GROUP_ID + group.getId(), moveToGroupString);
211     }
212
213     @Override
214     public OnapPDPGroup getPDPGroup(OnapPDP pdp) throws PAPException {
215         return getPDPGroup(pdp.getId());
216     }
217
218     public OnapPDPGroup getPDPGroup(String pdpId) throws PAPException {
219         return (OnapPDPGroup) sendToPap("GET", null, null, StdPDPGroup.class, GROUP_ID, PDPID_MSG + pdpId,
220                 "getPDPGroup=");
221     }
222
223     @Override
224     public OnapPDP getPDP(String pdpId) throws PAPException {
225         return (OnapPDP) sendToPap("GET", null, null, StdPDP.class, GROUP_ID, PDPID_MSG + pdpId);
226     }
227
228     @Override
229     public void newPDP(String id, OnapPDPGroup group, String name, String description, int jmxport)
230             throws PAPException {
231         StdPDP newPdp = new StdPDP(id, name, description, jmxport);
232         sendToPap("PUT", newPdp, null, null, GROUP_ID + group.getId(), PDPID_MSG + id);
233     }
234
235     @Override
236     public void movePDP(OnapPDP pdp, OnapPDPGroup newGroup) throws PAPException {
237         sendToPap("POST", null, null, null, GROUP_ID + newGroup.getId(), PDPID_MSG + pdp.getId());
238     }
239
240     @Override
241     public void updatePDP(OnapPDP pdp) throws PAPException {
242         OnapPDPGroup group = getPDPGroup(pdp);
243         sendToPap("PUT", pdp, null, null, GROUP_ID + group.getId(), PDPID_MSG + pdp.getId());
244     }
245
246     @Override
247     public void removePDP(OnapPDP pdp) throws PAPException {
248         OnapPDPGroup group = getPDPGroup(pdp);
249         sendToPap("DELETE", null, null, null, GROUP_ID + group.getId(), PDPID_MSG + pdp.getId());
250     }
251
252     /**
253      * validatePolicyRequest Creates a pap policy and then send to pap.
254      *
255      * @param policyAdapter Input Adapter
256      * @param policyType Type of Policy
257      * @return true if validated
258      * @throws PAPException exception if invalid
259      */
260     public boolean validatePolicyRequest(PolicyRestAdapter policyAdapter, String policyType) throws PAPException {
261         StdPAPPolicy newPapPolicy = new StdPAPPolicy(policyAdapter.getPolicyName(), policyAdapter.getConfigBodyData(),
262                 policyAdapter.getConfigType(), "Base");
263
264         // send JSON object to PAP
265         return (Boolean) sendToPap("PUT", newPapPolicy, null, null, "operation=validate", "apiflag=admin",
266                 "policyType=" + policyType);
267     }
268
269     @Override
270     public void publishPolicy(String id, String name, boolean isRoot, InputStream policy, OnapPDPGroup group)
271             throws PAPException {
272         // copy the (one) file into the target directory on the PAP servlet
273         copyFile(id, group, policy, null);
274
275         // adjust the local copy of the group to include the new policy
276         PDPPolicy pdpPolicy = new StdPDPPolicy(id, isRoot, name);
277         group.getPolicies().add(pdpPolicy);
278
279         // tell the PAP servlet to include the policy in the configuration
280         updateGroup(group);
281     }
282
283     /**
284      * Copy a single Policy file from the input stream to the PAP Servlet. Either this works (silently) or it throws an
285      * exception.
286      *
287      * @param policyId ID of policy
288      * @param group PDP Group
289      * @param policy Input stream of policy
290      * @throws PAPException exception
291      */
292     public void copyFile(String policyId, OnapPDPGroup group, InputStream policy, String usrId) throws PAPException {
293         // send the policy file to the PAP Servlet
294         try {
295             sendToPap("POST", policy, null, null, GROUP_ID + group.getId(), "policyId=" + policyId, "userId=" + usrId );
296         } catch (Exception e) {
297             String message = UNABLE_MSG + policyId + EXCEPTION_MSG + e;
298             LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
299             throw new PAPException(message);
300         }
301     }
302
303     @Override
304     public void copyPolicy(PDPPolicy policy, OnapPDPGroup group, String userId) throws PAPException {
305         if (policy == null || group == null) {
306             throw new PAPException("Null input policy=" + policy + "  group=" + group);
307         }
308         try (InputStream is = new FileInputStream(new File(policy.getLocation()))) {
309             copyFile(policy.getId(), group, is, userId);
310         } catch (Exception e) {
311             String message = UNABLE_MSG + policy.getId() + EXCEPTION_MSG + e;
312             LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
313             throw new PAPException(message);
314         }
315     }
316
317     @Override
318     public void removePolicy(PDPPolicy policy, OnapPDPGroup group) throws PAPException {
319         throw new PAPException("NOT IMPLEMENTED");
320     }
321
322     /**
323      * Special operation - Similar to the normal PAP operations but this one contacts the PDP directly to get detailed
324      * status info.
325      *
326      * @param pdp PDP to get status
327      * @return PDPStatus object
328      * @throws PAPException Exception
329      */
330     @Override
331     public PDPStatus getStatus(OnapPDP pdp) throws PAPException {
332         return (StdPDPStatus) sendToPap("GET", pdp, null, StdPDPStatus.class);
333     }
334
335     //
336     // Internal Operations called by the PAPEngine Interface methods
337     //
338
339     /**
340      * Send a request to the PAP Servlet and get the response.
341      *
342      * <p>The content is either an InputStream to be copied to the Request OutputStream OR it is an object that is to be
343      * encoded into JSON and pushed into the Request OutputStream.
344      *
345      * <p>The Request parameters may be encoded in multiple "name=value" sets, or parameters may be
346      * combined by the caller.
347      *
348      * @param method method
349      * @param content - EITHER an InputStream OR an Object to be encoded in JSON
350      * @param collectionTypeClass Collection
351      * @param responseContentClass Response Content
352      * @param parameters List of parameters
353      * @return Object
354      * @throws PAPException exception
355      */
356     @SuppressWarnings({"rawtypes", "unchecked"})
357     private Object sendToPap(String method, Object content, Class collectionTypeClass, Class responseContentClass,
358             String... parameters) throws PAPException {
359         HttpURLConnection connection = null;
360         String papID = XACMLProperties.getProperty(XacmlRestProperties.PROP_PAP_USERID);
361         LOGGER.info("User Id is " + papID);
362         PeCryptoUtils.initAesKey(XACMLProperties.getProperty(XacmlRestProperties.PROP_AES_KEY));
363         String papPass = PeCryptoUtils
364                 .decrypt(PeCryptoUtils.decrypt(XACMLProperties.getProperty(XacmlRestProperties.PROP_PAP_PASS)));
365         Base64.Encoder encoder = Base64.getEncoder();
366         String encoding = encoder.encodeToString((papID + ":" + papPass).getBytes(StandardCharsets.UTF_8));
367         Object contentObj = content;
368         LOGGER.info("Encoding for the PAP is: " + encoding);
369         try {
370             String fullUrl = papServletUrlString;
371             if (parameters != null && parameters.length > 0) {
372                 StringBuilder queryString = new StringBuilder();
373                 Arrays.stream(parameters).map(p -> "&" + p).forEach(queryString::append);
374                 fullUrl += "?" + queryString.substring(1);
375             }
376
377             // special case - Status (actually the detailed status) comes from the PDP directly, not the PAP
378             if ("GET".equals(method) && (contentObj instanceof OnapPDP) && responseContentClass == StdPDPStatus.class) {
379                 // Adjust the url and properties appropriately
380                 String pdpID = ((OnapPDP) contentObj).getId();
381                 fullUrl = pdpID + "?type=Status";
382                 contentObj = null;
383                 if (CheckPdpProperties.validateId(pdpID)) {
384                     encoding = CheckPdpProperties.getEncoding(pdpID);
385                 }
386             }
387
388             //
389             // Open up the connection
390             //
391             connection = (HttpURLConnection) makeConnection(fullUrl);
392             //
393             // Setup our method and headers
394             //
395             connection.setRequestMethod(method);
396             connection.setUseCaches(false);
397             //
398             // Adding this in. It seems the HttpUrlConnection class does NOT
399             // properly forward our headers for POST re-direction. It does so
400             // for a GET re-direction.
401             //
402             // So we need to handle this ourselves.
403             //
404             connection.setInstanceFollowRedirects(false);
405             connection.setRequestProperty("Authorization", "Basic " + encoding);
406             connection.setDoOutput(true);
407             connection.setDoInput(true);
408
409             if (contentObj != null) {
410                 if (contentObj instanceof InputStream) {
411                     sendCurrPolicyConfig(method, connection, (InputStream) contentObj);
412                 } else {
413                     // The contentObj is an object to be encoded in JSON
414                     ObjectMapper mapper = new ObjectMapper();
415                     mapper.writeValue(connection.getOutputStream(), contentObj);
416                 }
417             }
418             //
419             // Do the connect
420             //
421             connection.connect();
422             if (connection.getResponseCode() == 204) {
423                 LOGGER.info("Success - no content.");
424                 return null;
425             } else if (connection.getResponseCode() == 200) {
426                 LOGGER.info("Success. We have a return object.");
427                 String isValidData = connection.getHeaderField("isValidData");
428                 String isSuccess = connection.getHeaderField("successMapKey");
429                 Map<String, String> successMap = new HashMap<>();
430                 if ("true".equalsIgnoreCase(isValidData)) {
431                     LOGGER.info("Policy Data is valid.");
432                     return true;
433                 } else if ("false".equalsIgnoreCase(isValidData)) {
434                     LOGGER.info("Policy Data is invalid.");
435                     return false;
436                 } else if ("success".equalsIgnoreCase(isSuccess)) {
437                     LOGGER.info("Policy Created Successfully!");
438                     String finalPolicyPath = connection.getHeaderField("finalPolicyPath");
439                     successMap.put("success", finalPolicyPath);
440                     return successMap;
441                 } else if (ERROR_MSG.equalsIgnoreCase(isSuccess)) {
442                     LOGGER.info("There was an error while creating the policy!");
443                     successMap.put(ERROR_MSG, ERROR_MSG);
444                     return successMap;
445                 } else {
446                     // get the response content into a String
447                     String json = getJsonString(connection);
448
449                     // convert Object sent as JSON into local object
450                     ObjectMapper mapper = new ObjectMapper();
451                     mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
452                     if (collectionTypeClass != null) {
453                         // collection of objects expected
454                         final CollectionType javaType = mapper.getTypeFactory()
455                                 .constructCollectionType(collectionTypeClass, responseContentClass);
456                         return mapper.readValue(json, javaType);
457                     } else {
458                         // single value object expected
459                         return mapper.readValue(json, responseContentClass);
460                     }
461                 }
462             } else if (connection.getResponseCode() >= 300 && connection.getResponseCode() <= 399) {
463                 // redirection
464                 String newUrl = connection.getHeaderField("Location");
465                 if (newUrl == null) {
466                     LOGGER.error(
467                             "No Location header to redirect to when response code=" + connection.getResponseCode());
468                     throw new IOException(
469                             "No redirect Location header when response code=" + connection.getResponseCode());
470                 }
471                 int qindex = newUrl.indexOf('?');
472                 if (qindex > 0) {
473                     newUrl = newUrl.substring(0, qindex);
474                 }
475                 LOGGER.info("Redirect seen.  Redirecting " + fullUrl + " to " + newUrl);
476                 return newUrl;
477             } else {
478                 LOGGER.warn("Unexpected response code: " + connection.getResponseCode() + "  message: "
479                         + connection.getResponseMessage());
480                 throw new IOException(
481                         "Server Response: " + connection.getResponseCode() + ": " + connection.getResponseMessage());
482             }
483         } catch (Exception e) {
484             LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "HTTP Request/Response to PAP: " + e, e);
485             throw new PAPException("Request/Response threw :" + e);
486         } finally {
487             // cleanup the connection
488             if (connection != null) {
489                 try {
490                     // For some reason trying to get the inputStream from the connection
491                     // throws an exception rather than returning null when the InputStream does not exist.
492                     InputStream is = connection.getInputStream();
493                     if (is != null) {
494                         is.close();
495                     }
496                 } catch (IOException ex) {
497                     LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to close connection: " + ex, ex);
498                 }
499                 connection.disconnect();
500             }
501         }
502     }
503
504     private void sendCurrPolicyConfig(String method, final HttpURLConnection connection, InputStream contentObj) {
505         try {
506             //
507             // Send our current policy configuration
508             //
509             try (OutputStream os = connection.getOutputStream()) {
510                 int count = IOUtils.copy(contentObj, os);
511                 if (LOGGER.isDebugEnabled()) {
512                     LOGGER.debug("copied to output, bytes=" + count);
513                 }
514             }
515         } catch (Exception e) {
516             LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to write content in '" + method + "'", e);
517         }
518     }
519
520     private String getJsonString(final HttpURLConnection connection) throws IOException {
521         String json;
522         // read the inputStream into a buffer (trick found online scans entire input looking for end-of-file)
523         try (java.util.Scanner scanner = new java.util.Scanner(connection.getInputStream())) {
524             scanner.useDelimiter("\\A");
525             json = scanner.hasNext() ? scanner.next() : "";
526         } catch (Exception e) {
527             LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to read inputStream from connection: " + e,
528                     e);
529             throw e;
530         }
531         LOGGER.info("JSON response from PAP: " + json);
532         return json;
533     }
534
535     // these may be overridden by junit tests
536
537     protected URLConnection makeConnection(String fullUrl) throws IOException {
538         return new URL(fullUrl).openConnection();
539     }
540 }