Thomas Vachuska | bd8ddfe | 2018-12-13 12:58:48 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.rest.resources; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import org.apache.commons.io.IOUtils; |
| 21 | import org.onlab.osgi.DefaultServiceDirectory; |
| 22 | import org.onlab.osgi.ServiceDirectory; |
| 23 | import org.onosproject.security.AuditService; |
| 24 | |
| 25 | import javax.ws.rs.container.ContainerRequestContext; |
| 26 | import javax.ws.rs.container.ContainerRequestFilter; |
| 27 | import javax.ws.rs.container.ContainerResponseContext; |
| 28 | import javax.ws.rs.container.ContainerResponseFilter; |
| 29 | import java.io.IOException; |
| 30 | |
| 31 | import static org.onlab.util.Tools.readTreeFromStream; |
| 32 | |
| 33 | /** |
| 34 | * HTTP Filter for auditing REST API requests. |
| 35 | */ |
| 36 | public class AuditFilter implements ContainerRequestFilter, ContainerResponseFilter { |
| 37 | |
| 38 | private ObjectMapper mapper = new ObjectMapper(); |
Arjun E K | 53a2008 | 2018-12-18 05:13:05 -0500 | [diff] [blame] | 39 | private final String separator = "\", \""; |
| 40 | private final String logCompSeperator = "\" : \""; |
Thomas Vachuska | bd8ddfe | 2018-12-13 12:58:48 -0800 | [diff] [blame] | 41 | |
| 42 | private static boolean disableForTests = false; |
| 43 | private static ServiceDirectory services = new DefaultServiceDirectory(); |
| 44 | |
| 45 | /** |
| 46 | * Disables functionality for unit tests. |
| 47 | */ |
| 48 | public static void disableForTests() { |
| 49 | disableForTests = true; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public void filter(ContainerRequestContext requestContext) throws IOException { |
| 54 | if (auditService() != null) { |
| 55 | String requestBody = (requestContext.hasEntity() ? |
| 56 | (readTreeFromStream(mapper, requestContext.getEntityStream()).toString()) : ""); |
| 57 | requestContext.setProperty("requestBody", requestBody); |
| 58 | // FIXME: audit message should be better structured |
Arjun E K | 53a2008 | 2018-12-18 05:13:05 -0500 | [diff] [blame] | 59 | requestContext.setProperty("auditMessage", "{\"Path" + logCompSeperator |
| 60 | + requestContext.getUriInfo().getPath() + separator + "Method" |
| 61 | + logCompSeperator + requestContext.getMethod() + separator |
Thomas Vachuska | bd8ddfe | 2018-12-13 12:58:48 -0800 | [diff] [blame] | 62 | + (requestContext.getMethod().equals("PUT") ? |
| 63 | // FIXME: is there really a need to differentiate based on method? |
Arjun E K | 53a2008 | 2018-12-18 05:13:05 -0500 | [diff] [blame] | 64 | ("Path_Parameters" + logCompSeperator + requestContext.getUriInfo().getPathParameters().toString() |
| 65 | + separator + "Query_Parameters" + logCompSeperator |
| 66 | + requestContext.getUriInfo().getQueryParameters().toString() |
| 67 | + separator + "Request_Body" + logCompSeperator + requestBody) : "")); |
Thomas Vachuska | bd8ddfe | 2018-12-13 12:58:48 -0800 | [diff] [blame] | 68 | requestContext.setEntityStream(IOUtils.toInputStream(requestBody)); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void filter(ContainerRequestContext containerRequestContext, |
| 74 | ContainerResponseContext containerResponseContext) throws IOException { |
| 75 | AuditService auditService = auditService(); |
| 76 | if (auditService != null) { |
| 77 | containerRequestContext.setProperty("auditMessage", containerRequestContext.getProperty("auditMessage") |
Arjun E K | 53a2008 | 2018-12-18 05:13:05 -0500 | [diff] [blame] | 78 | + separator + "Status" + logCompSeperator + containerResponseContext.getStatusInfo().toString() |
| 79 | + "\"}"); |
Thomas Vachuska | bd8ddfe | 2018-12-13 12:58:48 -0800 | [diff] [blame] | 80 | // FIXME: Audit record should indicate who did it, not just what was done and when |
| 81 | String user = containerRequestContext.getSecurityContext().getUserPrincipal().getName(); |
| 82 | String action = containerRequestContext.getProperty("auditMessage").toString(); |
| 83 | auditService.logUserAction(user, action); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | private AuditService auditService() { |
Arjun E K | fb5843e | 2019-02-08 07:47:23 -0500 | [diff] [blame] | 88 | AuditService auditService = null; |
| 89 | try { |
| 90 | auditService = disableForTests ? null : services.get(AuditService.class); |
| 91 | } catch (org.onlab.osgi.ServiceNotFoundException e) { |
| 92 | return null; |
| 93 | } |
Thomas Vachuska | bd8ddfe | 2018-12-13 12:58:48 -0800 | [diff] [blame] | 94 | return auditService != null && auditService.isAuditing() ? auditService : null; |
| 95 | } |
| 96 | } |