Include projectName in audit logs (#10892)

- Closes #9875
This commit is contained in:
Radosław Waśko 2024-08-27 15:13:28 +02:00 committed by GitHub
parent 1500849c32
commit ff5e4c4e0a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 33 additions and 13 deletions

View File

@ -1,19 +1,41 @@
package org.enso.base;
import org.enso.base.polyglot.EnsoMeta;
import org.graalvm.polyglot.Value;
/** A Java interface to the `Enso_Project` type. */
public final class CurrentEnsoProject {
private final String name;
private final String namespace;
private static CurrentEnsoProject cached = null;
private static boolean isCached = false;
private CurrentEnsoProject(String name, String namespace) {
this.name = name;
this.namespace = namespace;
}
public static CurrentEnsoProject get() {
// TODO this currently does not work, because of bug
// https://github.com/enso-org/enso/issues/9845
return null;
if (!isCached) {
Value ensoProject =
EnsoMeta.callStaticModuleMethod("Standard.Base.Meta.Enso_Project", "enso_project");
if (ensoProject.hasMember("name") && ensoProject.hasMember("namespace")) {
Value namespace = ensoProject.invokeMember("namespace");
Value name = ensoProject.invokeMember("name");
if (namespace == null || name == null) {
cached = null;
} else {
cached = new CurrentEnsoProject(name.asString(), namespace.asString());
}
} else {
cached = null;
}
isCached = true;
}
return cached;
}
public String getName() {

View File

@ -55,8 +55,7 @@ public class AuditLogMessage implements AuditLogAPI.LogMessage {
var copy = metadata.deepCopy();
copy.set(OPERATION, TextNode.valueOf(operation));
// TODO the null check should no longer be needed once
// https://github.com/enso-org/enso/issues/9845 is fixed
// The project name may be null if a script is run outside a project.
if (projectName != null) {
copy.set(PROJECT_NAME, TextNode.valueOf(projectName));
}

View File

@ -25,7 +25,7 @@ public final class CloudAuditedConnection extends AuditedConnection {
metadata.put("asset_id", relatedAssetId);
}
try {
metadata.put("connection_uri", underlying.getMetaData().getURL());
metadata.put("connectionUri", underlying.getMetaData().getURL());
} catch (SQLException e) {
// We ignore the exception, only logging it
logger.warning("Failed to get connection URI for " + underlying + ": " + e.getMessage());
@ -34,7 +34,7 @@ public final class CloudAuditedConnection extends AuditedConnection {
private void audit(String operationType, String message) {
var metadataCopy = metadata.deepCopy();
metadataCopy.put("sequence_number", sequenceNumber++);
metadataCopy.put("sequenceNumber", sequenceNumber++);
AuditLog.logAsync(operationType, message, metadataCopy);
}

View File

@ -30,9 +30,7 @@ add_specs suite_builder =
event
my_event.metadata.get "operation" . should_equal "TestEvent"
# TODO this test should be re-enabled after https://github.com/enso-org/enso/issues/9845 is fixed
#my_event.metadata.get "projectName" . should_equal "enso_dev.Base_Tests"
my_event.metadata.get "projectName" . should_equal "enso_dev.Base_Tests"
my_event.message . should_equal "Message"
my_event.user_email . should_equal Enso_User.current.email

View File

@ -41,15 +41,16 @@ add_specs suite_builder prefix ~datalink_to_connection database_pending =
create = relevant_events.find (e-> e.message.contains "CREATE")
create.should_succeed
create.user_email . should_equal Enso_User.current.email
create.metadata.get "connection_uri" . should_contain "jdbc:"
create.metadata.get "connectionUri" . should_contain "jdbc:"
create.metadata.get "projectName" . should_equal enso_project.namespace+"."+enso_project.name
insert = relevant_events.find (e-> e.message.contains "INSERT INTO")
insert.should_succeed
# The insert query should not contain column cell data - only column names / metadata.
insert.message.should_not_contain "my_payload"
create_sequence_number = create.metadata.get "sequence_number"
insert_sequence_number = insert.metadata.get "sequence_number"
create_sequence_number = create.metadata.get "sequenceNumber"
insert_sequence_number = insert.metadata.get "sequenceNumber"
create_sequence_number.should_be_a Integer
insert_sequence_number.should_be_a Integer
(create_sequence_number < insert_sequence_number) . should_be_true