[
https://issues.apache.org/jira/browse/KNOX-3580?focusedWorklogId=1055396&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-1055396
]
ASF GitHub Bot logged work on KNOX-3580:
----------------------------------------
Author: ASF GitHub Bot
Created on: 17/Nov/26 18:08
Start Date: 17/Nov/26 18:08
Worklog Time Spent: 10m
Work Description: pzampino commented on code in PR #1518:
URL: https://github.com/apache/knox/pull/1518#discussion_r4603703067
##########
gateway-util-common/src/main/java/org/apache/knox/gateway/audit/log4j/appender/AuditGatewayAppender.java:
##########
@@ -0,0 +1,259 @@
+/*
+ * 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.knox.gateway.audit.log4j.appender;
+
+import java.io.IOException;
+import java.net.URI;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.ByteArrayEntity;
+import org.apache.http.entity.ContentType;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.Appender;
+import org.apache.logging.log4j.core.Core;
+import org.apache.logging.log4j.core.Filter;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.appender.AbstractAppender;
+import org.apache.logging.log4j.core.config.Property;
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
+import org.apache.logging.log4j.core.config.plugins.PluginElement;
+import org.apache.logging.log4j.core.config.plugins.PluginFactory;
+
+@Plugin(
+ name = "AuditGateway",
+ category = Core.CATEGORY_NAME,
+ elementType = Appender.ELEMENT_TYPE)
+public class AuditGatewayAppender extends AbstractAppender {
+ private static final Logger LOG =
LogManager.getLogger(AuditGatewayAppender.class);
+ private static final long DROP_WARN_INTERVAL_MS = 60_000L;
+ private static final long POLL_MS = 250L;
+ private static final ContentType JSON =
ContentType.create("application/json");
+ static final int MAX_WORKERS = 4;
+
+ private final URI uri;
+ private final String sourceSystem;
+ private final String podName;
+ private final BlockingQueue<String> queue;
+ private final int requestTimeoutMs;
+ private final long drainTimeoutMs;
+ private final int workerCount;
+ private final List<Thread> workers = new ArrayList<>();
+ private final AtomicBoolean running = new AtomicBoolean();
+ private final AtomicLong dropped = new AtomicLong();
+ private final AtomicLong lastDropWarn = new AtomicLong();
+ private CloseableHttpClient client;
+ private Thread shutdownHook;
+
+ AuditGatewayAppender(String name, Filter filter, URI uri, String
sourceSystem, String podName,
+ int queueSize, long requestTimeoutMs, long
drainTimeoutMs, int workers) {
+ super(name, filter, null, true, Property.EMPTY_ARRAY);
+ this.uri = uri;
+ this.sourceSystem = sourceSystem;
+ this.podName = podName;
+ this.queue = new ArrayBlockingQueue<>(Math.max(1, queueSize));
+ this.requestTimeoutMs = (int) Math.min(Integer.MAX_VALUE, Math.max(0L,
requestTimeoutMs));
Review Comment:
Does this effectively default the timeout to zero?
##########
gateway-util-common/src/main/java/org/apache/knox/gateway/audit/log4j/appender/AuditGatewayAppender.java:
##########
@@ -0,0 +1,259 @@
+/*
+ * 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.knox.gateway.audit.log4j.appender;
+
+import java.io.IOException;
+import java.net.URI;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.ByteArrayEntity;
+import org.apache.http.entity.ContentType;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.Appender;
+import org.apache.logging.log4j.core.Core;
+import org.apache.logging.log4j.core.Filter;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.appender.AbstractAppender;
+import org.apache.logging.log4j.core.config.Property;
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
+import org.apache.logging.log4j.core.config.plugins.PluginElement;
+import org.apache.logging.log4j.core.config.plugins.PluginFactory;
+
+@Plugin(
+ name = "AuditGateway",
+ category = Core.CATEGORY_NAME,
+ elementType = Appender.ELEMENT_TYPE)
+public class AuditGatewayAppender extends AbstractAppender {
+ private static final Logger LOG =
LogManager.getLogger(AuditGatewayAppender.class);
+ private static final long DROP_WARN_INTERVAL_MS = 60_000L;
+ private static final long POLL_MS = 250L;
+ private static final ContentType JSON =
ContentType.create("application/json");
+ static final int MAX_WORKERS = 4;
+
+ private final URI uri;
+ private final String sourceSystem;
+ private final String podName;
+ private final BlockingQueue<String> queue;
+ private final int requestTimeoutMs;
+ private final long drainTimeoutMs;
+ private final int workerCount;
+ private final List<Thread> workers = new ArrayList<>();
+ private final AtomicBoolean running = new AtomicBoolean();
+ private final AtomicLong dropped = new AtomicLong();
+ private final AtomicLong lastDropWarn = new AtomicLong();
+ private CloseableHttpClient client;
+ private Thread shutdownHook;
+
+ AuditGatewayAppender(String name, Filter filter, URI uri, String
sourceSystem, String podName,
+ int queueSize, long requestTimeoutMs, long
drainTimeoutMs, int workers) {
+ super(name, filter, null, true, Property.EMPTY_ARRAY);
+ this.uri = uri;
+ this.sourceSystem = sourceSystem;
+ this.podName = podName;
+ this.queue = new ArrayBlockingQueue<>(Math.max(1, queueSize));
+ this.requestTimeoutMs = (int) Math.min(Integer.MAX_VALUE, Math.max(0L,
requestTimeoutMs));
+ this.drainTimeoutMs = Math.max(0L, drainTimeoutMs);
+ this.workerCount = Math.min(MAX_WORKERS, Math.max(1, workers));
+ }
+
+ @PluginFactory
+ public static AuditGatewayAppender createAppender(
+ @PluginAttribute("name") String name,
+ @PluginElement("Filter") Filter filter,
+ @PluginAttribute("endpoint") String endpoint,
+ @PluginAttribute("path") String path,
+ @PluginAttribute("sourceSystem") String sourceSystem,
+ @PluginAttribute("podName") String podName,
+ @PluginAttribute(value = "queueSize", defaultInt = 2000) int queueSize,
+ @PluginAttribute(value = "requestTimeoutMs", defaultLong = 2500L) long
requestTimeoutMs,
+ @PluginAttribute(value = "drainTimeoutMs", defaultLong = 5000L) long
drainTimeoutMs,
+ @PluginAttribute(value = "workers", defaultInt = 1) int workers) {
+ return new AuditGatewayAppender(name, filter, resolveUri(endpoint, path),
sourceSystem, podName,
+ queueSize, requestTimeoutMs, drainTimeoutMs, workers);
+ }
+
+ static URI resolveUri(String endpoint, String path) {
+ if (StringUtils.isBlank(endpoint)) {
+ return null;
+ }
+ String value = endpoint.trim();
+ if (!value.contains("://")) {
+ value = "http://" + value;
Review Comment:
Should the default be https?
Issue Time Tracking
-------------------
Worklog Id: (was: 1055396)
Time Spent: 0.5h (was: 20m)
> Add log4j2 appender to POST audit events as JSON to an HTTP endpoint
> ---------------------------------------------------------------------
>
> Key: KNOX-3580
> URL: https://issues.apache.org/jira/browse/KNOX-3580
> Project: Apache Knox
> Issue Type: Task
> Components: Server
> Affects Versions: 3.0.0
> Reporter: Tamás Hanicz
> Assignee: Tamás Hanicz
> Priority: Major
> Time Spent: 0.5h
> Remaining Estimate: 0h
>
> Add an AuditGateway log4j2 appender that sends each Knox audit record as a
> JSON event to an HTTP endpoint. It is enabled only when the AUDIT_GATEWAY env
> var is set; otherwise it does nothing.
> - Maps Knox action and outcome to a category/verb/outcome event, including
> actor (act), target resource and correlation id.
> - Sends asynchronously from a bounded queue, with retries and a circuit
> breaker.
> - Drains queued events on shutdown.
> - gateway-audit.log is unchanged.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)