From 08630b6f3740dafb9138e8de57636e59884b804d Mon Sep 17 00:00:00 2001 From: ektavarma10 Date: Mon, 4 Dec 2023 17:26:18 +0530 Subject: [PATCH] Transfer serialisation logic for audit events from scrubSearchResults to where audit logs are created for Elasticsearch --- .../audit/RangerDefaultAuditHandler.java | 11 +++++---- .../atlas/audit/model/AuthzAuditEvent.java | 24 ++++++++++++++++--- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/auth-agents-common/src/main/java/org/apache/atlas/plugin/audit/RangerDefaultAuditHandler.java b/auth-agents-common/src/main/java/org/apache/atlas/plugin/audit/RangerDefaultAuditHandler.java index 4f760ee28e..4a5b680d8e 100644 --- a/auth-agents-common/src/main/java/org/apache/atlas/plugin/audit/RangerDefaultAuditHandler.java +++ b/auth-agents-common/src/main/java/org/apache/atlas/plugin/audit/RangerDefaultAuditHandler.java @@ -43,6 +43,7 @@ import java.util.List; import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Collectors; public class RangerDefaultAuditHandler implements RangerAccessResultProcessor { @@ -135,9 +136,9 @@ public AuthzAuditEvent getAuthzEvents(RangerAccessResult result) { ret.setClientType(request.getClientType()); ret.setSessionId(request.getSessionId()); ret.setAclEnforcer(moduleName); - Set tags = getTags(request); + Set tags = getTags(request); if (tags != null) { - ret.setTags(tags); + ret.setTags(tags.stream().map(tag -> (Object) tag).collect(Collectors.toSet())); } ret.setAdditionalInfo(getAdditionalInfo(request)); ret.setClusterName(request.getClusterName()); @@ -251,15 +252,15 @@ public AuthzAuditEvent createAuthzAuditEvent() { return new AuthzAuditEvent(); } - protected final Set getTags(RangerAccessRequest request) { - Set ret = null; + protected final Set getTags(RangerAccessRequest request) { + Set ret = null; Set tags = RangerAccessRequestUtil.getRequestTagsFromContext(request.getContext()); if (CollectionUtils.isNotEmpty(tags)) { ret = new HashSet<>(); for (RangerTagForEval tag : tags) { - ret.add(writeObjectAsString(tag)); + ret.add(tag); } } diff --git a/auth-audits/src/main/java/org/apache/atlas/audit/model/AuthzAuditEvent.java b/auth-audits/src/main/java/org/apache/atlas/audit/model/AuthzAuditEvent.java index f0bae5480c..f333420541 100644 --- a/auth-audits/src/main/java/org/apache/atlas/audit/model/AuthzAuditEvent.java +++ b/auth-audits/src/main/java/org/apache/atlas/audit/model/AuthzAuditEvent.java @@ -21,17 +21,25 @@ import com.google.gson.annotations.SerializedName; import org.apache.commons.lang.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.htrace.shaded.fasterxml.jackson.databind.ObjectMapper; +import java.io.Serializable; import java.util.Date; import java.util.HashSet; import java.util.Set; +import java.util.stream.Collectors; public class AuthzAuditEvent extends AuditEventBase { protected static String FIELD_SEPARATOR = ";"; + private static ObjectMapper objectMapper = new ObjectMapper(); protected static final int MAX_ACTION_FIELD_SIZE = 1800; protected static final int MAX_REQUEST_DATA_FIELD_SIZE = 1800; + private static final Log LOG = LogFactory.getLog(AuthzAuditEvent.class); + @SerializedName("repoType") protected int repositoryType = 0; @@ -110,7 +118,7 @@ public class AuthzAuditEvent extends AuditEventBase { protected long eventDurationMS = 0; @SerializedName("tags") - protected Set tags = new HashSet<>(); + protected Set tags = new HashSet<>(); @SerializedName("additional_info") protected String additionalInfo; @@ -483,14 +491,14 @@ public long getEventDurationMS() { } public Set getTags() { - return tags; + return tags.stream().map(tag -> writeObjectAsString((Serializable) tag)).collect(Collectors.toSet()); } public void setEventDurationMS(long frequencyDurationMS) { this.eventDurationMS = frequencyDurationMS; } - public void setTags(Set tags) { + public void setTags(Set tags) { this.tags = tags; } @@ -582,4 +590,14 @@ protected StringBuilder toString(StringBuilder sb) { return sb; } + + private String writeObjectAsString(Serializable obj) { + String jsonStr = StringUtils.EMPTY; + try { + jsonStr = objectMapper.writeValueAsString(obj); + } catch (Exception e) { + LOG.error("Cannot create JSON string for object:[" + obj + "]", e); + } + return jsonStr; + } }