blob: 38471abcd69af0f963a66b1271fe7375dffdc11e [file] [log] [blame]
Thomas Vachuskabd8ddfe2018-12-13 12:58:48 -08001/*
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
17package org.onosproject.rest.resources;
18
19import com.fasterxml.jackson.databind.ObjectMapper;
20import org.apache.commons.io.IOUtils;
21import org.onlab.osgi.DefaultServiceDirectory;
22import org.onlab.osgi.ServiceDirectory;
23import org.onosproject.security.AuditService;
24
25import javax.ws.rs.container.ContainerRequestContext;
26import javax.ws.rs.container.ContainerRequestFilter;
27import javax.ws.rs.container.ContainerResponseContext;
28import javax.ws.rs.container.ContainerResponseFilter;
29import java.io.IOException;
30
31import static org.onlab.util.Tools.readTreeFromStream;
32
33/**
34 * HTTP Filter for auditing REST API requests.
35 */
36public 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}