From 38b01921c22fd4c4f98a4a1d3c2116938ac8258c Mon Sep 17 00:00:00 2001 From: "Wang, Fei" Date: Thu, 14 Mar 2024 19:11:18 -0700 Subject: [PATCH] op audit log4j user --- conf/log4j2.xml.template | 13 +++++++ .../kyuubi/operation/AbstractOperation.scala | 2 + .../operation/OperationAuditLogger.scala | 38 +++++++++++++++++++ .../src/test/resources/log4j2-test.xml | 7 ++++ 4 files changed, 60 insertions(+) create mode 100644 kyuubi-common/src/main/scala/org/apache/kyuubi/operation/OperationAuditLogger.scala diff --git a/conf/log4j2.xml.template b/conf/log4j2.xml.template index 215fddf47f4..f1c86b0f5cc 100644 --- a/conf/log4j2.xml.template +++ b/conf/log4j2.xml.template @@ -26,6 +26,8 @@ rest-audit-%d{yyyy-MM-dd}-%i.log k8s-audit.log k8s-audit-%d{yyyy-MM-dd}-%i.log + operation-audit.log + operation-audit-%d{yyyy-MM-dd}-%i.log @@ -50,6 +52,14 @@ + + + + + + + @@ -72,5 +82,8 @@ + + + diff --git a/kyuubi-common/src/main/scala/org/apache/kyuubi/operation/AbstractOperation.scala b/kyuubi-common/src/main/scala/org/apache/kyuubi/operation/AbstractOperation.scala index 05dd7fda907..3681e98f784 100644 --- a/kyuubi-common/src/main/scala/org/apache/kyuubi/operation/AbstractOperation.scala +++ b/kyuubi-common/src/main/scala/org/apache/kyuubi/operation/AbstractOperation.scala @@ -74,6 +74,7 @@ abstract class AbstractOperation(session: Session) extends Operation with Loggin override def getOperationLog: Option[OperationLog] = None + OperationAuditLogger.audit(this, OperationState.INITIALIZED) @volatile protected var state: OperationState = INITIALIZED @volatile protected var startTime: Long = _ @volatile protected var completedTime: Long = _ @@ -126,6 +127,7 @@ abstract class AbstractOperation(session: Session) extends Operation with Loggin } state = newState lastAccessTime = System.currentTimeMillis() + OperationAuditLogger.audit(this, state) } protected def isClosedOrCanceled: Boolean = { diff --git a/kyuubi-common/src/main/scala/org/apache/kyuubi/operation/OperationAuditLogger.scala b/kyuubi-common/src/main/scala/org/apache/kyuubi/operation/OperationAuditLogger.scala new file mode 100644 index 00000000000..2187d22510e --- /dev/null +++ b/kyuubi-common/src/main/scala/org/apache/kyuubi/operation/OperationAuditLogger.scala @@ -0,0 +1,38 @@ +/* + * 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.kyuubi.operation + +import org.apache.kyuubi.Logging +import org.apache.kyuubi.operation.OperationState.OperationState + +object OperationAuditLogger extends Logging { + final private val AUDIT_BUFFER = new ThreadLocal[StringBuilder]() { + override protected def initialValue: StringBuilder = new StringBuilder() + } + + def audit(operation: Operation, state: OperationState): Unit = { + val sb = AUDIT_BUFFER.get() + sb.setLength(0) + sb.append(s"operation=${operation.getHandle.identifier}").append("\t") + sb.append(s"opType=${operation.getClass.getSimpleName}").append("\t") + sb.append(s"state=$state").append("\t") + sb.append(s"user=${operation.getSession.user}").append("\t") + sb.append(s"session=${operation.getSession.handle.identifier}") + info(sb.toString()) + } +} diff --git a/kyuubi-server/src/test/resources/log4j2-test.xml b/kyuubi-server/src/test/resources/log4j2-test.xml index bccbf1b0d84..343da22bd3e 100644 --- a/kyuubi-server/src/test/resources/log4j2-test.xml +++ b/kyuubi-server/src/test/resources/log4j2-test.xml @@ -21,6 +21,7 @@ target/rest-audit.log + target/operation-audit.log @@ -39,6 +40,9 @@ + + + @@ -48,6 +52,9 @@ + + +