Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MESH-299 | Audit Search Logs #3928

Merged
merged 9 commits into from
Jan 14, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public static EntityAuditActionV2 fromString(String strValue) {
private Map<String, Object> detail;
private AtlasEntityHeader entityDetail;
private Map<String, String> headers;
private List<AtlasEntityHeader> linkedEntities;

public EntityAuditEventV2() { }

Expand Down Expand Up @@ -250,6 +251,19 @@ public void setHeaders(Map<String, String> headers) {
this.headers = headers;
}

public List<AtlasEntityHeader> getLinkedEntities() {
return linkedEntities;
}

public void setLinkedEntities(List<AtlasEntityHeader> linkedEntities) {
this.linkedEntities = linkedEntities;
}

@JsonIgnore
public boolean hasLinkedEntities() {
return linkedEntities != null && !linkedEntities.isEmpty();
}

@JsonIgnore
public String getEntityDefinitionString() {
if (entity != null) {
Expand Down Expand Up @@ -315,6 +329,11 @@ public String toString() {
sb.append(", detail=").append(detail);
sb.append(", created=").append(created);
sb.append(", headers=").append(headers);

if (hasLinkedEntities()) {
sb.append(", linkedEntities=").append(linkedEntities);
}
PRATHAM2002-DS marked this conversation as resolved.
Show resolved Hide resolved

ankitpatnaik-atlan marked this conversation as resolved.
Show resolved Hide resolved
sb.append('}');

return sb.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.model.audit.EntityAuditEventV2;
import org.apache.atlas.model.audit.EntityAuditSearchResult;
import org.apache.atlas.model.instance.AtlasEntityHeader;
import org.apache.atlas.type.AtlasType;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.configuration.Configuration;
Expand All @@ -47,6 +48,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.apache.atlas.repository.store.graph.v2.EntityGraphRetriever;

import javax.inject.Inject;
import javax.inject.Singleton;
Expand All @@ -60,6 +62,7 @@
import java.util.*;

import static java.nio.charset.Charset.defaultCharset;
import static org.apache.atlas.repository.Constants.DOMAIN_GUIDS;
import static org.springframework.util.StreamUtils.copyToString;

/**
Expand All @@ -85,6 +88,7 @@ public class ESBasedAuditRepository extends AbstractStorageBasedAuditRepository
private static final String DETAIL = "detail";
private static final String ENTITY = "entity";
private static final String bulkMetadata = String.format("{ \"index\" : { \"_index\" : \"%s\" } }%n", INDEX_NAME);
private static final Set<String> linkedAttributes = new HashSet<>(Arrays.asList(DOMAIN_GUIDS));
ankitpatnaik-atlan marked this conversation as resolved.
Show resolved Hide resolved

/*
* created → event creation time
Expand All @@ -94,13 +98,14 @@ public class ESBasedAuditRepository extends AbstractStorageBasedAuditRepository

private RestClient lowLevelClient;
private final Configuration configuration;
private EntityGraphRetriever entityGraphRetriever;

@Inject
public ESBasedAuditRepository(Configuration configuration) {
public ESBasedAuditRepository(Configuration configuration, EntityGraphRetriever entityGraphRetriever) {
this.configuration = configuration;
this.entityGraphRetriever = entityGraphRetriever;
}


@Override
public void putEventsV1(List<EntityAuditEvent> events) throws AtlasException {

Expand Down Expand Up @@ -243,6 +248,35 @@ private EntityAuditSearchResult getResultFromResponse(String responseString) thr
eventKey = event.getEntityId() + ":" + event.getTimestamp();
}

Map<String, Object> detail = event.getDetail();
if (detail != null && detail.containsKey("attributes")) {
Map<String, Object> attributes = (Map<String, Object>) detail.get("attributes");
List<AtlasEntityHeader> linkedEntityList = new ArrayList<>();

for (Map.Entry<String, Object> entry: attributes.entrySet()) {
if (linkedAttributes.contains(entry.getKey())) {
List<String> guids = (List<String>) entry.getValue();

if (guids != null && !guids.isEmpty()){
for (String guid: guids){
try {
AtlasEntityHeader entityHeader = fetchAtlasEntityHeader(guid);
if (entityHeader != null) {
linkedEntityList.add(entityHeader);
}
} catch (AtlasBaseException e) {
throw new AtlasBaseException(e);
}
}
}
}
}

if(!linkedEntityList.isEmpty()){
event.setLinkedEntities(linkedEntityList);
}
}

event.setHeaders((Map<String, String>) source.get("headers"));

event.setEventKey(eventKey);
Expand All @@ -258,6 +292,15 @@ private EntityAuditSearchResult getResultFromResponse(String responseString) thr
return searchResult;
}

private AtlasEntityHeader fetchAtlasEntityHeader(String domainGUID) throws AtlasBaseException {
try {
AtlasEntityHeader entityHeader = entityGraphRetriever.toAtlasEntityHeader(domainGUID);
return entityHeader;
} catch (AtlasBaseException e) {
throw new AtlasBaseException(e);
}
}

ankitpatnaik-atlan marked this conversation as resolved.
Show resolved Hide resolved
private String performSearchOnIndex(String queryString) throws IOException {
HttpEntity entity = new NStringEntity(queryString, ContentType.APPLICATION_JSON);
String endPoint = INDEX_NAME + "/_search";
Expand Down
Loading