arjunek1992 | f6353d4 | 2018-11-20 08:56:29 -0500 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-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.onlab.rest; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import org.apache.commons.io.IOUtils; |
| 21 | import org.slf4j.Logger; |
| 22 | |
| 23 | import javax.ws.rs.container.ContainerRequestContext; |
| 24 | import javax.ws.rs.container.ContainerRequestFilter; |
| 25 | import javax.ws.rs.container.ContainerResponseContext; |
| 26 | import javax.ws.rs.container.ContainerResponseFilter; |
| 27 | import java.io.IOException; |
| 28 | |
| 29 | import static org.onlab.util.Tools.readTreeFromStream; |
| 30 | import static org.slf4j.LoggerFactory.getLogger; |
| 31 | |
| 32 | /** |
| 33 | * FIlter for logging all REST Api http requests and details of request and response. |
| 34 | */ |
| 35 | |
| 36 | public class AuditFilter implements ContainerRequestFilter, ContainerResponseFilter { |
| 37 | |
| 38 | private static Logger log = getLogger(AuditFilter.class); |
| 39 | private ObjectMapper mapper = new ObjectMapper(); |
| 40 | private final String separator = " | "; |
| 41 | |
| 42 | private static boolean disableForTests = false; |
| 43 | private static String auditFile = "all"; |
| 44 | private static boolean auditEnabled = false; |
| 45 | |
| 46 | @Override |
| 47 | public void filter(ContainerRequestContext requestContext) throws IOException { |
| 48 | if (disableForTests) { |
| 49 | return; |
| 50 | } |
| 51 | if (isEnabled()) { |
| 52 | String requestBody = (requestContext.hasEntity() ? |
| 53 | (readTreeFromStream(mapper, requestContext.getEntityStream()).toString()) : ""); |
| 54 | requestContext.setProperty("requestBody", requestBody); |
| 55 | requestContext.setProperty("auditLog", "Path: " + requestContext.getUriInfo().getPath() + separator |
| 56 | + "Method: " + requestContext.getMethod() + separator |
| 57 | + (requestContext.getMethod().equals("PUT") ? |
| 58 | ("Path_Parameters: " + requestContext.getUriInfo().getPathParameters().toString() + separator |
| 59 | + "Query_Parameters: " + requestContext.getUriInfo().getQueryParameters().toString() |
| 60 | + separator + "Request_Body: " + requestBody) : "")); |
| 61 | requestContext.setEntityStream(IOUtils.toInputStream(requestBody)); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public void filter(ContainerRequestContext containerRequestContext, |
| 67 | ContainerResponseContext containerResponseContext) throws IOException { |
| 68 | if (disableForTests) { |
| 69 | return; |
| 70 | } |
| 71 | if (isEnabled()) { |
| 72 | containerRequestContext.setProperty("auditLog", containerRequestContext.getProperty("auditLog") + separator |
| 73 | + "Status: " + containerResponseContext.getStatusInfo().toString()); |
| 74 | saveAuditLog(containerRequestContext.getProperty("auditLog").toString()); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * To disable unit testing for this class. |
| 80 | */ |
| 81 | public static void disableForTests() { |
| 82 | disableForTests = true; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * To save audit logs into the log file. |
| 87 | * |
| 88 | * @param msg audit message. |
| 89 | */ |
| 90 | private void saveAuditLog(String msg) { |
| 91 | if (isEnabled()) { |
| 92 | if (auditFile.equals("all")) { |
| 93 | log.info(msg); |
| 94 | log.info("AuditLog : " + msg); |
| 95 | } else if (auditFile.equals("karaf")) { |
| 96 | log.info(msg); |
| 97 | } else if (auditFile.equals("audit")) { |
| 98 | log.info("AuditLog : " + msg); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * To check if REST API Audit is enabled. |
| 105 | * |
| 106 | * @return true if the REST API Audit is enabled. |
| 107 | */ |
| 108 | private static boolean isEnabled() { |
| 109 | return auditEnabled; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * To enable REST API Audit. |
| 114 | */ |
| 115 | public static void enableAudit() { |
| 116 | auditEnabled = true; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * To disable REST API Audit. |
| 121 | */ |
| 122 | public static void disableAudit() { |
| 123 | auditEnabled = false; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * To set audit file type which REST API Audit logs must be saved. |
| 128 | * |
| 129 | * @param auditFile file that REST API Audit logs are saved. |
| 130 | */ |
| 131 | public static void setAuditFile(String auditFile) { |
| 132 | AuditFilter.auditFile = auditFile; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | } |