FANNG1 commented on code in PR #4575: URL: https://github.com/apache/gravitino/pull/4575#discussion_r1808181506
########## core/src/main/java/org/apache/gravitino/audit/FileAuditWriter.java: ########## @@ -0,0 +1,126 @@ +/* + * 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.gravitino.audit; + +import com.google.common.annotations.VisibleForTesting; +import java.io.BufferedWriter; +import java.io.FileOutputStream; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.nio.charset.StandardCharsets; +import java.util.Map; +import org.apache.gravitino.exceptions.GravitinoRuntimeException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * DefaultFileAuditWriter is the default implementation of AuditLogWriter, which writes audit logs + * to a file. + */ +public class FileAuditWriter implements AuditLogWriter { + private static final Logger Log = LoggerFactory.getLogger(FileAuditWriter.class); + + public static final String LOG_FILE_NAME_CONFIG = "fileName"; + + public static final String APPEND = "append"; + + public static final String FILE_IMMEDIATE_FLUSH_CONFIG = "immediateFlush"; Review Comment: use `AUDIT_LOG_FILE_IMMEDIATE_FLUSH`? config seems redundant ########## core/src/main/java/org/apache/gravitino/audit/FileAuditWriter.java: ########## @@ -0,0 +1,126 @@ +/* + * 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.gravitino.audit; + +import com.google.common.annotations.VisibleForTesting; +import java.io.BufferedWriter; +import java.io.FileOutputStream; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.nio.charset.StandardCharsets; +import java.util.Map; +import org.apache.gravitino.exceptions.GravitinoRuntimeException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * DefaultFileAuditWriter is the default implementation of AuditLogWriter, which writes audit logs + * to a file. + */ +public class FileAuditWriter implements AuditLogWriter { + private static final Logger Log = LoggerFactory.getLogger(FileAuditWriter.class); + + public static final String LOG_FILE_NAME_CONFIG = "fileName"; Review Comment: please use `AUDIT_LOG_FILE_NAME` ########## core/src/main/java/org/apache/gravitino/audit/FileAuditWriter.java: ########## @@ -0,0 +1,126 @@ +/* + * 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.gravitino.audit; + +import com.google.common.annotations.VisibleForTesting; +import java.io.BufferedWriter; +import java.io.FileOutputStream; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.nio.charset.StandardCharsets; +import java.util.Map; +import org.apache.gravitino.exceptions.GravitinoRuntimeException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * DefaultFileAuditWriter is the default implementation of AuditLogWriter, which writes audit logs + * to a file. + */ +public class FileAuditWriter implements AuditLogWriter { + private static final Logger Log = LoggerFactory.getLogger(FileAuditWriter.class); + + public static final String LOG_FILE_NAME_CONFIG = "fileName"; + + public static final String APPEND = "append"; + + public static final String FILE_IMMEDIATE_FLUSH_CONFIG = "immediateFlush"; + + public static final String BUFFERED = "buffered"; + + public static final String BUFFER_SIZE = "bufferSize"; + + public static final String LINE_SEPARATOR = "lineSeparator"; + + private Formatter formatter; + private Writer outWriter; + @VisibleForTesting String fileName; + + boolean immediateFlush; + + String lineSeparator; + + boolean append; + + boolean buffered; + + int bufferSize; + + @Override + public Formatter getFormatter() { + return formatter; + } + + @Override + public void init(Formatter formatter, Map<String, String> properties) { + this.formatter = formatter; + this.fileName = + System.getProperty("gravitino.log.path") + + "/" + + properties.getOrDefault(LOG_FILE_NAME_CONFIG, "gravitino_audit.log"); + this.immediateFlush = + Boolean.parseBoolean(properties.getOrDefault(FILE_IMMEDIATE_FLUSH_CONFIG, "false")); + this.lineSeparator = properties.getOrDefault(LINE_SEPARATOR, "\n"); + this.append = Boolean.parseBoolean(properties.getOrDefault(APPEND, "true")); + this.buffered = Boolean.parseBoolean(properties.getOrDefault(BUFFERED, "true")); Review Comment: seems confusing to export buffer and immediateFlush configuration to user ########## docs/gravitino-server-config.md: ########## @@ -138,6 +138,36 @@ The plugin provides several operational modes for how to process event, supporti For more details, please refer to the definition of the plugin. +### Audit log configuration + +The audit log framework defines how audit logs are formatted and written to various storages. The formatter defines an interface that transforms different `Event` types into a unified `AuditLog`. The writer defines an interface to writing AuditLog to different storages. + +Gravitino provides a default implement to log basic audit information to a file, you could extend the audit system by implementation corresponding interfaces. + +| Property name | Description | Default value | Required | Since Version | +|---------------------------------------|----------------------------------------|---------------------------------------------|----------|---------------| +| `gravitino.audit.enabled` | The audit log enable flag. | false | NO | 0.7.0 | +| `gravitino.audit.writer.className` | The class name of audit log writer. | org.apache.gravitino.audit.FileAuditWriter | NO | 0.7.0 | +| `gravitino.audit.formatter.className` | The class name of audit log formatter. | org.apache.gravitino.audit.SimpleFormatter | NO | 0.7.0 | + +#### Audit log writer + +The `AuditLogWriter` defines an interface that enables the writing of metadata audit logs to different storage mediums such as files, databases, etc. + +Writer configuration begins with `gravitino.audit.writer.${name}`, where ${name} is replaced with the actual writer name defined in method `name()`. `FileAuditWriter` is a default implement to log audit information, whose name is `file`. + +| Property name | Description | Default value | Required | Since Version | +|-----------------------------------------------------|-----------------------------------------------------------------------------------------|---------------------|----------|---------------| +| `gravitino.audit.writer.file.fileName` | The audit log file name, the path is ${sys:gravitino.log.path}/${fileName} | gravitino_audit.log | NO | 0.7.0 | +| `gravitino.audit.writer.file.immediateFlush` | whether the writer will flush at the end of each write operation. | false | NO | 0.7.0 | +| `gravitino.audit.writer.file.append` | whether the log will be written to the end or the beginning of the file. | true | NO | 0.7.0 | +| `gravitino.audit.writer.file.buffered` | whether the writer use an internal `ByteBuffer` to store audit log before writing them. | true | NO | 0.7.0 | +| `gravitino.audit.writer.file.bufferSize` | The size of the `ByteBuffer`. | 8192 | NO | 0.7.0 | +| `gravitino.audit.writer.file.lineSeparator` | The audit log line separator. | \n | NO | 0.7.0 | + +#### Audit log formatter Review Comment: please add an empty line after the title ########## docs/gravitino-server-config.md: ########## @@ -138,6 +138,36 @@ The plugin provides several operational modes for how to process event, supporti For more details, please refer to the definition of the plugin. +### Audit log configuration + +The audit log framework defines how audit logs are formatted and written to various storages. The formatter defines an interface that transforms different `Event` types into a unified `AuditLog`. The writer defines an interface to writing AuditLog to different storages. + +Gravitino provides a default implement to log basic audit information to a file, you could extend the audit system by implementation corresponding interfaces. + +| Property name | Description | Default value | Required | Since Version | +|---------------------------------------|----------------------------------------|---------------------------------------------|----------|---------------| +| `gravitino.audit.enabled` | The audit log enable flag. | false | NO | 0.7.0 | +| `gravitino.audit.writer.className` | The class name of audit log writer. | org.apache.gravitino.audit.FileAuditWriter | NO | 0.7.0 | +| `gravitino.audit.formatter.className` | The class name of audit log formatter. | org.apache.gravitino.audit.SimpleFormatter | NO | 0.7.0 | + +#### Audit log writer + +The `AuditLogWriter` defines an interface that enables the writing of metadata audit logs to different storage mediums such as files, databases, etc. + +Writer configuration begins with `gravitino.audit.writer.${name}`, where ${name} is replaced with the actual writer name defined in method `name()`. `FileAuditWriter` is a default implement to log audit information, whose name is `file`. + +| Property name | Description | Default value | Required | Since Version | +|-----------------------------------------------------|-----------------------------------------------------------------------------------------|---------------------|----------|---------------| +| `gravitino.audit.writer.file.fileName` | The audit log file name, the path is ${sys:gravitino.log.path}/${fileName} | gravitino_audit.log | NO | 0.7.0 | +| `gravitino.audit.writer.file.immediateFlush` | whether the writer will flush at the end of each write operation. | false | NO | 0.7.0 | +| `gravitino.audit.writer.file.append` | whether the log will be written to the end or the beginning of the file. | true | NO | 0.7.0 | +| `gravitino.audit.writer.file.buffered` | whether the writer use an internal `ByteBuffer` to store audit log before writing them. | true | NO | 0.7.0 | +| `gravitino.audit.writer.file.bufferSize` | The size of the `ByteBuffer`. | 8192 | NO | 0.7.0 | +| `gravitino.audit.writer.file.lineSeparator` | The audit log line separator. | \n | NO | 0.7.0 | + +#### Audit log formatter Review Comment: could you more `formatter` section before `writer`? ########## docs/gravitino-server-config.md: ########## @@ -138,6 +138,36 @@ The plugin provides several operational modes for how to process event, supporti For more details, please refer to the definition of the plugin. +### Audit log configuration + +The audit log framework defines how audit logs are formatted and written to various storages. The formatter defines an interface that transforms different `Event` types into a unified `AuditLog`. The writer defines an interface to writing AuditLog to different storages. + +Gravitino provides a default implement to log basic audit information to a file, you could extend the audit system by implementation corresponding interfaces. + +| Property name | Description | Default value | Required | Since Version | +|---------------------------------------|----------------------------------------|---------------------------------------------|----------|---------------| +| `gravitino.audit.enabled` | The audit log enable flag. | false | NO | 0.7.0 | Review Comment: please use `0.7.0-incubating` ########## core/src/main/java/org/apache/gravitino/audit/AuditLogManager.java: ########## @@ -0,0 +1,124 @@ +/* + * 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.gravitino.audit; + +import com.google.common.annotations.VisibleForTesting; +import java.io.IOException; +import java.util.Map; +import org.apache.gravitino.Config; +import org.apache.gravitino.Configs; +import org.apache.gravitino.exceptions.GravitinoRuntimeException; +import org.apache.gravitino.listener.EventListenerManager; +import org.apache.gravitino.listener.api.EventListenerPlugin; +import org.apache.gravitino.listener.api.event.Event; +import org.apache.gravitino.utils.MapUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/* + * AuditLogManager is responsible for initializing the audit log writer and formatter, + * which are used to write metadata audit logs. + * */ +public class AuditLogManager { + + private static final Logger LOG = LoggerFactory.getLogger(AuditLogManager.class); + + @VisibleForTesting private AuditLogWriter auditLogWriter; + + public void init(Config config, EventListenerManager eventBusManager) { + if (!config.get(Configs.AUDIT_LOG_ENABLED_CONF)) { + LOG.info("Audit log is not enabled"); + return; + } + + // If formatter class name is not config, use default formatter class name + String formatterClassName = config.get(Configs.AUDIT_LOG_FORMATTER_CLASS_NAME); + Formatter formatter = loadFormatter(formatterClassName); + LOG.info("Audit log formatter class name:{}", formatterClassName); + + String writerClassName = config.get(Configs.AUDIT_LOG_WRITER_CLASS_NAME); + // If writer class name is not config, use default writer class name Review Comment: please remove this comment ########## docs/gravitino-server-config.md: ########## @@ -138,6 +138,36 @@ The plugin provides several operational modes for how to process event, supporti For more details, please refer to the definition of the plugin. +### Audit log configuration + +The audit log framework defines how audit logs are formatted and written to various storages. The formatter defines an interface that transforms different `Event` types into a unified `AuditLog`. The writer defines an interface to writing AuditLog to different storages. + +Gravitino provides a default implement to log basic audit information to a file, you could extend the audit system by implementation corresponding interfaces. + +| Property name | Description | Default value | Required | Since Version | +|---------------------------------------|----------------------------------------|---------------------------------------------|----------|---------------| +| `gravitino.audit.enabled` | The audit log enable flag. | false | NO | 0.7.0 | +| `gravitino.audit.writer.className` | The class name of audit log writer. | org.apache.gravitino.audit.FileAuditWriter | NO | 0.7.0 | +| `gravitino.audit.formatter.className` | The class name of audit log formatter. | org.apache.gravitino.audit.SimpleFormatter | NO | 0.7.0 | + +#### Audit log writer + +The `AuditLogWriter` defines an interface that enables the writing of metadata audit logs to different storage mediums such as files, databases, etc. + +Writer configuration begins with `gravitino.audit.writer.${name}`, where ${name} is replaced with the actual writer name defined in method `name()`. `FileAuditWriter` is a default implement to log audit information, whose name is `file`. + +| Property name | Description | Default value | Required | Since Version | +|-----------------------------------------------------|-----------------------------------------------------------------------------------------|---------------------|----------|---------------| +| `gravitino.audit.writer.file.fileName` | The audit log file name, the path is ${sys:gravitino.log.path}/${fileName} | gravitino_audit.log | NO | 0.7.0 | +| `gravitino.audit.writer.file.immediateFlush` | whether the writer will flush at the end of each write operation. | false | NO | 0.7.0 | Review Comment: whether -> Whether ########## docs/gravitino-server-config.md: ########## @@ -138,6 +138,36 @@ The plugin provides several operational modes for how to process event, supporti For more details, please refer to the definition of the plugin. +### Audit log configuration + +The audit log framework defines how audit logs are formatted and written to various storages. The formatter defines an interface that transforms different `Event` types into a unified `AuditLog`. The writer defines an interface to writing AuditLog to different storages. + +Gravitino provides a default implement to log basic audit information to a file, you could extend the audit system by implementation corresponding interfaces. + +| Property name | Description | Default value | Required | Since Version | +|---------------------------------------|----------------------------------------|---------------------------------------------|----------|---------------| +| `gravitino.audit.enabled` | The audit log enable flag. | false | NO | 0.7.0 | +| `gravitino.audit.writer.className` | The class name of audit log writer. | org.apache.gravitino.audit.FileAuditWriter | NO | 0.7.0 | +| `gravitino.audit.formatter.className` | The class name of audit log formatter. | org.apache.gravitino.audit.SimpleFormatter | NO | 0.7.0 | + +#### Audit log writer + +The `AuditLogWriter` defines an interface that enables the writing of metadata audit logs to different storage mediums such as files, databases, etc. + +Writer configuration begins with `gravitino.audit.writer.${name}`, where ${name} is replaced with the actual writer name defined in method `name()`. `FileAuditWriter` is a default implement to log audit information, whose name is `file`. + +| Property name | Description | Default value | Required | Since Version | +|-----------------------------------------------------|-----------------------------------------------------------------------------------------|---------------------|----------|---------------| +| `gravitino.audit.writer.file.fileName` | The audit log file name, the path is ${sys:gravitino.log.path}/${fileName} | gravitino_audit.log | NO | 0.7.0 | +| `gravitino.audit.writer.file.immediateFlush` | whether the writer will flush at the end of each write operation. | false | NO | 0.7.0 | +| `gravitino.audit.writer.file.append` | whether the log will be written to the end or the beginning of the file. | true | NO | 0.7.0 | +| `gravitino.audit.writer.file.buffered` | whether the writer use an internal `ByteBuffer` to store audit log before writing them. | true | NO | 0.7.0 | +| `gravitino.audit.writer.file.bufferSize` | The size of the `ByteBuffer`. | 8192 | NO | 0.7.0 | +| `gravitino.audit.writer.file.lineSeparator` | The audit log line separator. | \n | NO | 0.7.0 | + +#### Audit log formatter +The Formatter defines an interface that formats metadata audit logs into a unified format. `SimpleFormatter` is a default implement to format audit information, it not needs any configuration. Review Comment: it not needs any configuration. -> you don't need to do extra configurations. ########## core/src/main/java/org/apache/gravitino/audit/FileAuditWriter.java: ########## @@ -0,0 +1,126 @@ +/* + * 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.gravitino.audit; + +import com.google.common.annotations.VisibleForTesting; +import java.io.BufferedWriter; +import java.io.FileOutputStream; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.nio.charset.StandardCharsets; +import java.util.Map; +import org.apache.gravitino.exceptions.GravitinoRuntimeException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * DefaultFileAuditWriter is the default implementation of AuditLogWriter, which writes audit logs + * to a file. + */ +public class FileAuditWriter implements AuditLogWriter { + private static final Logger Log = LoggerFactory.getLogger(FileAuditWriter.class); + + public static final String LOG_FILE_NAME_CONFIG = "fileName"; + + public static final String APPEND = "append"; + + public static final String FILE_IMMEDIATE_FLUSH_CONFIG = "immediateFlush"; + + public static final String BUFFERED = "buffered"; + + public static final String BUFFER_SIZE = "bufferSize"; + + public static final String LINE_SEPARATOR = "lineSeparator"; + + private Formatter formatter; + private Writer outWriter; + @VisibleForTesting String fileName; + + boolean immediateFlush; + + String lineSeparator; + + boolean append; + + boolean buffered; + + int bufferSize; + + @Override + public Formatter getFormatter() { + return formatter; + } + + @Override + public void init(Formatter formatter, Map<String, String> properties) { + this.formatter = formatter; + this.fileName = + System.getProperty("gravitino.log.path") + + "/" + + properties.getOrDefault(LOG_FILE_NAME_CONFIG, "gravitino_audit.log"); + this.immediateFlush = + Boolean.parseBoolean(properties.getOrDefault(FILE_IMMEDIATE_FLUSH_CONFIG, "false")); + this.lineSeparator = properties.getOrDefault(LINE_SEPARATOR, "\n"); Review Comment: I prefer to remove lineSeparator, because user doesn't need to config it in most cases. and maybe we should use a more platform independent way to represent line seperator since it will be `\r\n` in windows. -- 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. To unsubscribe, e-mail: commits-unsubscr...@gravitino.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org