zookeeper-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ars...@apache.org
Subject [zookeeper] branch master updated: ZOOKEEPER-3653: Audit Log feature fails in a stand alone zookeeper setup
Date Thu, 19 Dec 2019 08:52:39 GMT
This is an automated email from the ASF dual-hosted git repository.

arshad pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/zookeeper.git


The following commit(s) were added to refs/heads/master by this push:
     new 7c9a1e4  ZOOKEEPER-3653: Audit Log feature fails in a stand alone zookeeper setup
7c9a1e4 is described below

commit 7c9a1e4e9ae9e3a17c4766b34787e39ca1e42794
Author: Sujith Simon <sujith.abraham.simon@huawei.com>
AuthorDate: Thu Dec 19 14:21:41 2019 +0530

    ZOOKEEPER-3653: Audit Log feature fails in a stand alone zookeeper setup
    
    Author: sujithsimon22 <sujith.abraham.simon@huawei.com>
    
    Reviewers: Enrico Olivelli <eolivelli@apache.org>,Mohammad Arshad <arshad@apache.org>
    
    Closes #1185 from sujithsimon22/master
---
 .../org/apache/zookeeper/audit/AuditHelper.java    | 10 +--
 .../zookeeper/audit/StandaloneServerAuditTest.java | 90 ++++++++++++++++++++++
 2 files changed, 92 insertions(+), 8 deletions(-)

diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/audit/AuditHelper.java b/zookeeper-server/src/main/java/org/apache/zookeeper/audit/AuditHelper.java
index c399801..ce0d58a 100644
--- a/zookeeper-server/src/main/java/org/apache/zookeeper/audit/AuditHelper.java
+++ b/zookeeper-server/src/main/java/org/apache/zookeeper/audit/AuditHelper.java
@@ -18,7 +18,6 @@
 package org.apache.zookeeper.audit;
 
 import java.io.IOException;
-import java.nio.ByteBuffer;
 import java.util.HashMap;
 import java.util.Map;
 import org.apache.jute.Record;
@@ -133,13 +132,8 @@ public final class AuditHelper {
     }
 
     private static void deserialize(Request request, Record record) throws IOException {
-        ByteBufferInputStream.byteBuffer2Record(getRequestData(request), record);
-    }
-
-    private static ByteBuffer getRequestData(Request request) {
-        ByteBuffer reqData = request.request.slice();
-        reqData.rewind();
-        return reqData;
+        request.request.rewind();
+        ByteBufferInputStream.byteBuffer2Record(request.request.slice(), record);
     }
 
     private static Result getResult(ProcessTxnResult rc, boolean failedTxn) {
diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/audit/StandaloneServerAuditTest.java
b/zookeeper-server/src/test/java/org/apache/zookeeper/audit/StandaloneServerAuditTest.java
new file mode 100644
index 0000000..7aed4c6
--- /dev/null
+++ b/zookeeper-server/src/test/java/org/apache/zookeeper/audit/StandaloneServerAuditTest.java
@@ -0,0 +1,90 @@
+/*
+ * 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.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.LineNumberReader;
+import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.log4j.Layout;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.apache.log4j.SimpleLayout;
+import org.apache.log4j.WriterAppender;
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.ZooDefs;
+import org.apache.zookeeper.ZooKeeper;
+import org.apache.zookeeper.test.ClientBase;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+
+
+public class StandaloneServerAuditTest extends ClientBase {
+    private static ByteArrayOutputStream os;
+
+    @BeforeClass
+    public static void setup() {
+        System.setProperty(ZKAuditProvider.AUDIT_ENABLE, "true");
+        // setup the logger to capture all the logs
+        Layout layout = new SimpleLayout();
+        os = new ByteArrayOutputStream();
+        WriterAppender appender = new WriterAppender(layout, os);
+        appender.setImmediateFlush(true);
+        appender.setThreshold(Level.INFO);
+        Logger zLogger = Logger.getLogger(Log4jAuditLogger.class);
+        zLogger.addAppender(appender);
+    }
+
+    @AfterClass
+    public static void teardown() {
+        System.clearProperty(ZKAuditProvider.AUDIT_ENABLE);
+    }
+
+    @Test
+    public void testCreateAuditLog() throws KeeperException, InterruptedException, IOException
{
+        final ZooKeeper zk = createClient();
+        String path = "/createPath";
+        zk.create(path, "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
+                CreateMode.PERSISTENT);
+        List<String> logs = readAuditLog(os);
+        assertEquals(1, logs.size());
+        assertTrue(logs.get(0).endsWith("operation=create\tznode=/createPath\tznode_type=persistent\tresult=success"));
+    }
+
+    private static List<String> readAuditLog(ByteArrayOutputStream os) throws IOException
{
+        List<String> logs = new ArrayList<>();
+        LineNumberReader r = new LineNumberReader(
+                new StringReader(os.toString()));
+        String line;
+        while ((line = r.readLine()) != null) {
+            logs.add(line);
+        }
+        os.reset();
+        return logs;
+    }
+}
+


Mime
View raw message