blob: 40749f3d4cba40c6d21d54c5db05931f4eb81561 [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();
Arjun E K53a20082018-12-18 05:13:05 -050039 private final String separator = "\", \"";
40 private final String logCompSeperator = "\" : \"";
Thomas Vachuskabd8ddfe2018-12-13 12:58:48 -080041
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 K53a20082018-12-18 05:13:05 -050059 requestContext.setProperty("auditMessage", "{\"Path" + logCompSeperator
60 + requestContext.getUriInfo().getPath() + separator + "Method"
61 + logCompSeperator + requestContext.getMethod() + separator
Thomas Vachuskabd8ddfe2018-12-13 12:58:48 -080062 + (requestContext.getMethod().equals("PUT") ?
63 // FIXME: is there really a need to differentiate based on method?
Arjun E K53a20082018-12-18 05:13:05 -050064 ("Path_Parameters" + logCompSeperator + requestContext.getUriInfo().getPathParameters().toString()
65 + separator + "Query_Parameters" + logCompSeperator
66 + requestContext.getUriInfo().getQueryParameters().toString()
67 + separator + "Request_Body" + logCompSeperator + requestBody) : ""));
Thomas Vachuskabd8ddfe2018-12-13 12:58:48 -080068 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 K53a20082018-12-18 05:13:05 -050078 + separator + "Status" + logCompSeperator + containerResponseContext.getStatusInfo().toString()
79 + "\"}");
Thomas Vachuskabd8ddfe2018-12-13 12:58:48 -080080 // 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 Kfb5843e2019-02-08 07:47:23 -050088 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 Vachuskabd8ddfe2018-12-13 12:58:48 -080094 return auditService != null && auditService.isAuditing() ? auditService : null;
95 }
96}