|
| 1 | +/* |
| 2 | + * Janssen Project software is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text. |
| 3 | + * |
| 4 | + * Copyright (c) 2020, Janssen Project |
| 5 | + */ |
| 6 | + |
| 7 | +package io.jans.configapi.interceptor; |
| 8 | + |
| 9 | +import io.jans.configapi.core.interceptor.RequestAuditInterceptor; |
| 10 | +import io.jans.configapi.model.configuration.AuditLogConf; |
| 11 | +import io.jans.configapi.util.AuthUtil; |
| 12 | +import jakarta.annotation.Priority; |
| 13 | +import jakarta.inject.Inject; |
| 14 | +import jakarta.servlet.http.HttpServletRequest; |
| 15 | +import jakarta.ws.rs.WebApplicationException; |
| 16 | +import jakarta.ws.rs.core.Context; |
| 17 | +import jakarta.ws.rs.core.HttpHeaders; |
| 18 | +import jakarta.ws.rs.core.UriInfo; |
| 19 | + |
| 20 | +import java.lang.reflect.Method; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +import org.slf4j.Logger; |
| 27 | +import org.slf4j.LoggerFactory; |
| 28 | + |
| 29 | +import jakarta.interceptor.AroundInvoke; |
| 30 | +import jakarta.interceptor.Interceptor; |
| 31 | +import jakarta.interceptor.InvocationContext; |
| 32 | + |
| 33 | +@Interceptor |
| 34 | +@RequestAuditInterceptor |
| 35 | +@Priority(Interceptor.Priority.APPLICATION) |
| 36 | +public class AuditLogInterceptor { |
| 37 | + |
| 38 | + private static final Logger AUDIT_LOG = LoggerFactory.getLogger("audit"); |
| 39 | + |
| 40 | + @Context |
| 41 | + UriInfo info; |
| 42 | + |
| 43 | + @Context |
| 44 | + HttpServletRequest request; |
| 45 | + |
| 46 | + @Context |
| 47 | + private HttpHeaders httpHeaders; |
| 48 | + |
| 49 | + @Inject |
| 50 | + AuthUtil authUtil; |
| 51 | + |
| 52 | + @SuppressWarnings({ "all" }) |
| 53 | + @AroundInvoke |
| 54 | + public Object aroundReadFrom(InvocationContext context) throws Exception { |
| 55 | + |
| 56 | + try { |
| 57 | + processRequest(context); |
| 58 | + |
| 59 | + } catch (Exception ex) { |
| 60 | + throw new WebApplicationException(ex); |
| 61 | + } |
| 62 | + return context.proceed(); |
| 63 | + } |
| 64 | + |
| 65 | + private void processRequest(InvocationContext context) { |
| 66 | + |
| 67 | + Object[] ctxParameters = context.getParameters(); |
| 68 | + Method method = context.getMethod(); |
| 69 | + Class[] clazzArray = method.getParameterTypes(); |
| 70 | + |
| 71 | + if (clazzArray != null && clazzArray.length > 0) { |
| 72 | + for (int i = 0; i < clazzArray.length; i++) { |
| 73 | + |
| 74 | + Object obj = ctxParameters[i]; |
| 75 | + // Audit log |
| 76 | + logAuditData(context, obj); |
| 77 | + |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private <T> void logAuditData(InvocationContext context, T obj) { |
| 83 | + try { |
| 84 | + AuditLogConf auditLogConf = getAuditLogConf(); |
| 85 | + if (auditLogConf != null && auditLogConf.isEnabled()) { |
| 86 | + AUDIT_LOG.info("====== Request for endpoint:{}, method:{}, from:{}, user:{}, data:{} ", info.getPath(), |
| 87 | + context.getMethod(), request.getRemoteAddr(), httpHeaders.getHeaderString("User-inum"), obj); |
| 88 | + Map<String, String> attributeMap = getAuditHeaderAttributes(auditLogConf); |
| 89 | + AUDIT_LOG.info("attributeMap:{} ", attributeMap); |
| 90 | + } |
| 91 | + |
| 92 | + } catch (Exception ex) { |
| 93 | + ex.printStackTrace(); |
| 94 | + } |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + private AuditLogConf getAuditLogConf() { |
| 99 | + return this.authUtil.getAuditLogConf(); |
| 100 | + } |
| 101 | + |
| 102 | + private Map<String, String> getAuditHeaderAttributes(AuditLogConf auditLogConf) { |
| 103 | + |
| 104 | + if (auditLogConf == null) { |
| 105 | + return Collections.emptyMap(); |
| 106 | + } |
| 107 | + List<String> attributes = auditLogConf.getHeaderAttributes(); |
| 108 | + |
| 109 | + Map<String, String> attributeMap = null; |
| 110 | + if (attributes != null && !attributes.isEmpty()) { |
| 111 | + attributeMap = new HashMap<>(); |
| 112 | + for (String attributeName : attributes) { |
| 113 | + |
| 114 | + String attributeValue = httpHeaders.getHeaderString(attributeName); |
| 115 | + attributeMap.put(attributeName, attributeValue); |
| 116 | + } |
| 117 | + } |
| 118 | + return attributeMap; |
| 119 | + } |
| 120 | + |
| 121 | +} |
0 commit comments