Search in sources :

Example 1 with AuditDetail

use of co.cask.cdap.common.security.AuditDetail in project cdap by caskdata.

the class RouterAuditLookUp method createMatcher.

private int createMatcher() {
    List<ClassPath.ClassInfo> handlerClasses;
    try {
        handlerClasses = getAllHandlerClasses();
    } catch (IOException e) {
        LOG.error("Failed to get all handler classes for audit logging: {}", e.getCause());
        return -1;
    }
    int count = 0;
    for (ClassPath.ClassInfo classInfo : handlerClasses) {
        Class<?> handlerClass = classInfo.load();
        Path classPath = handlerClass.getAnnotation(Path.class);
        String classPathStr = classPath == null ? "" : classPath.value();
        for (Method method : handlerClass.getMethods()) {
            Path methodPath = method.getAnnotation(Path.class);
            AuditPolicy auditPolicy = method.getAnnotation(AuditPolicy.class);
            HttpMethod httpMethod = getHttpMethod(method);
            if (methodPath == null || auditPolicy == null || httpMethod == null) {
                continue;
            }
            String methodPathStr = methodPath.value();
            String completePath = classPathStr.endsWith("/") || methodPathStr.startsWith("/") ? classPathStr + methodPathStr : classPathStr + "/" + methodPathStr;
            List<AuditDetail> auditContents = Arrays.asList(auditPolicy.value());
            List<String> headerNames = new ArrayList<>();
            if (auditContents.contains(AuditDetail.HEADERS)) {
                Annotation[][] annotations = method.getParameterAnnotations();
                for (Annotation[] annotationArr : annotations) {
                    if (annotationArr.length > 0) {
                        for (Annotation annotation : annotationArr) {
                            if (annotation instanceof HeaderParam) {
                                headerNames.add(((HeaderParam) annotation).value());
                            }
                        }
                    }
                }
            }
            AuditLogConfig auditLogConfig = new AuditLogConfig(httpMethod, auditContents.contains(AuditDetail.REQUEST_BODY), auditContents.contains(AuditDetail.RESPONSE_BODY), headerNames);
            LOG.trace("Audit log lookup: bootstrapped with path: {}", completePath);
            patternMatcher.add(completePath, auditLogConfig);
            // Don't count classes in unit-tests
            if (!isTestClass(classInfo)) {
                count++;
            }
        }
    }
    LOG.debug("Audit log lookup: bootstrapped with {} paths", count);
    return count;
}
Also used : Path(javax.ws.rs.Path) ClassPath(co.cask.cdap.common.internal.guava.ClassPath) ClassPath(co.cask.cdap.common.internal.guava.ClassPath) HeaderParam(javax.ws.rs.HeaderParam) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Method(java.lang.reflect.Method) HttpMethod(io.netty.handler.codec.http.HttpMethod) Annotation(java.lang.annotation.Annotation) AuditLogConfig(co.cask.cdap.common.logging.AuditLogConfig) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) AuditDetail(co.cask.cdap.common.security.AuditDetail) HttpMethod(io.netty.handler.codec.http.HttpMethod)

Aggregations

ClassPath (co.cask.cdap.common.internal.guava.ClassPath)1 AuditLogConfig (co.cask.cdap.common.logging.AuditLogConfig)1 AuditDetail (co.cask.cdap.common.security.AuditDetail)1 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)1 HttpMethod (io.netty.handler.codec.http.HttpMethod)1 IOException (java.io.IOException)1 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 HeaderParam (javax.ws.rs.HeaderParam)1 Path (javax.ws.rs.Path)1