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(); |
| 39 | private final String separator = " | "; |
| 40 | |
| 41 | private static boolean disableForTests = false; |
| 42 | private static ServiceDirectory services = new DefaultServiceDirectory(); |
| 43 | |
| 44 | /** |
| 45 | * Disables functionality for unit tests. |
| 46 | */ |
| 47 | public static void disableForTests() { |
| 48 | disableForTests = true; |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public void filter(ContainerRequestContext requestContext) throws IOException { |
| 53 | if (auditService() != null) { |
| 54 | String requestBody = (requestContext.hasEntity() ? |
| 55 | (readTreeFromStream(mapper, requestContext.getEntityStream()).toString()) : ""); |
| 56 | requestContext.setProperty("requestBody", requestBody); |
| 57 | // FIXME: audit message should be better structured |
| 58 | requestContext.setProperty("auditMessage", "Path: " + requestContext.getUriInfo().getPath() + separator |
| 59 | + "Method: " + requestContext.getMethod() + separator |
| 60 | + (requestContext.getMethod().equals("PUT") ? |
| 61 | // FIXME: is there really a need to differentiate based on method? |
| 62 | ("Path_Parameters: " + requestContext.getUriInfo().getPathParameters().toString() + separator |
| 63 | + "Query_Parameters: " + requestContext.getUriInfo().getQueryParameters().toString() |
| 64 | + separator + "Request_Body: " + requestBody) : "")); |
| 65 | requestContext.setEntityStream(IOUtils.toInputStream(requestBody)); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public void filter(ContainerRequestContext containerRequestContext, |
| 71 | ContainerResponseContext containerResponseContext) throws IOException { |
| 72 | AuditService auditService = auditService(); |
| 73 | if (auditService != null) { |
| 74 | containerRequestContext.setProperty("auditMessage", containerRequestContext.getProperty("auditMessage") |
| 75 | + separator + "Status: " + containerResponseContext.getStatusInfo().toString()); |
| 76 | // FIXME: Audit record should indicate who did it, not just what was done and when |
| 77 | String user = containerRequestContext.getSecurityContext().getUserPrincipal().getName(); |
| 78 | String action = containerRequestContext.getProperty("auditMessage").toString(); |
| 79 | auditService.logUserAction(user, action); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | private AuditService auditService() { |
| 84 | AuditService auditService = disableForTests ? null : services.get(AuditService.class); |
| 85 | return auditService != null && auditService.isAuditing() ? auditService : null; |
| 86 | } |
| 87 | } |