Search in sources :

Example 1 with AuditDetail

use of io.cdap.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(io.cdap.cdap.common.internal.guava.ClassPath) ClassPath(io.cdap.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(io.cdap.cdap.common.logging.AuditLogConfig) AuditPolicy(io.cdap.cdap.common.security.AuditPolicy) AuditDetail(io.cdap.cdap.common.security.AuditDetail) HttpMethod(io.netty.handler.codec.http.HttpMethod)

Aggregations

ClassPath (io.cdap.cdap.common.internal.guava.ClassPath)1 AuditLogConfig (io.cdap.cdap.common.logging.AuditLogConfig)1 AuditDetail (io.cdap.cdap.common.security.AuditDetail)1 AuditPolicy (io.cdap.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