Skip to content

Commit

Permalink
Include projectName in audit logs (#10892)
Browse files Browse the repository at this point in the history
- Closes #9875
  • Loading branch information
radeusgd authored Aug 27, 2024
1 parent 1500849 commit ff5e4c4
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 13 deletions.
28 changes: 25 additions & 3 deletions std-bits/base/src/main/java/org/enso/base/CurrentEnsoProject.java
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ private ObjectNode computedMetadata() {
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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public CloudAuditedConnection(Connection underlying, String relatedAssetId) {
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());
Expand All @@ -34,7 +34,7 @@ public CloudAuditedConnection(Connection underlying, String relatedAssetId) {

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);
}

Expand Down
4 changes: 1 addition & 3 deletions test/Base_Tests/src/Network/Enso_Cloud/Audit_Log_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions test/Table_Tests/src/Database/Common/Audit_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ff5e4c4

Please sign in to comment.