You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@zookeeper.apache.org by GitBox <gi...@apache.org> on 2019/11/06 14:28:11 UTC

[GitHub] [zookeeper] anmolnar commented on a change in pull request #1133: ZOOKEEPER-1260:Audit logging in ZooKeeper servers.

anmolnar commented on a change in pull request #1133: ZOOKEEPER-1260:Audit logging in ZooKeeper servers.
URL: https://github.com/apache/zookeeper/pull/1133#discussion_r343106196
 
 

 ##########
 File path: zookeeper-server/src/main/java/org/apache/zookeeper/audit/ZKAuditLogger.java
 ##########
 @@ -0,0 +1,163 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.zookeeper.audit;
+
+import static org.apache.zookeeper.audit.AuditEvent.FieldName;
+import java.lang.reflect.Constructor;
+import org.apache.zookeeper.server.ServerCnxnFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ZKAuditLogger {
+    public static final String SYSPROP_AUDIT_ENABLE = "zookeeper.audit.enable";
+    public static final String SYSPROP_AUDIT_LOGGER_IMPL = "zookeeper.audit.impl.class";
+    private static final Logger LOG = LoggerFactory.getLogger(ZKAuditLogger.class);
+    // By default audit logging is disabled
+    private static boolean auditEnabled = Boolean.getBoolean(SYSPROP_AUDIT_ENABLE);
+    private static AuditLogger auditLogger;
+
+    static {
+        if (auditEnabled) {
+            //initialise only when audit logging is enabled
+            auditLogger = getAuditLogger();
+            LOG.info("ZooKeeper audit is enabled.");
+        } else {
+            LOG.info("ZooKeeper audit is disabled.");
+        }
+    }
+
+    private static AuditLogger getAuditLogger() {
+        String auditLoggerClass = System.getProperty(SYSPROP_AUDIT_LOGGER_IMPL);
+        if (auditLoggerClass == null) {
+            auditLoggerClass = Log4jAuditLogger.class.getName();
+        }
+        try {
+            Constructor<?> clientCxnConstructor = Class.forName(auditLoggerClass)
+                    .getDeclaredConstructor();
+            AuditLogger auditLogger = (AuditLogger) clientCxnConstructor.newInstance();
+            auditLogger.initialize();
+            return auditLogger;
+        } catch (Exception e) {
+            throw new RuntimeException("Couldn't instantiate " + auditLoggerClass, e);
+        }
+    }
+
+    /**
+     * @return true if audit log is enabled
+     */
+    public static boolean isAuditEnabled() {
+        return auditEnabled;
+    }
+
+    // @VisibleForTesting
+    public static void setAuditEnabled(boolean auditEnabled) {
+        ZKAuditLogger.auditEnabled = auditEnabled;
+    }
+
+    public static void logSuccess(String user, String operation) {
+        log(user, operation, AuditConstants.SUCCESS);
+    }
+
+    public static void logInvoked(String user, String operation) {
+        log(user, operation, AuditConstants.INVOKED);
 
 Review comment:
   Both could be private.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services