Commit 05d8dec6 by Deep Singh Committed by Sarath Subramanian

ATLAS-4036, ATLAS-4037: Fix for Users and Client Id field in Admin Audits page

Includes fixes for the following issues: ATLAS-4036: Empty "Users" Field in Admin audits ATLAS-4037: Empty Client ID Field in Admin audits Signed-off-by: 's avatarSarath Subramanian <sarath@apache.org>
parent f4b1c429
......@@ -19,6 +19,7 @@
package org.apache.atlas.repository.audit;
import org.apache.atlas.AtlasErrorCode;
import org.apache.atlas.RequestContext;
import org.apache.atlas.annotation.AtlasService;
import org.apache.atlas.annotation.GraphTransaction;
import org.apache.atlas.discovery.AtlasDiscoveryService;
......@@ -38,6 +39,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.inject.Inject;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
......@@ -62,6 +65,27 @@ public class AtlasAuditService {
dataAccess.saveNoLoad(entry);
}
public void add(AuditOperation operation, String params, String result, long resultCount) throws AtlasBaseException {
final Date startTime = new Date(RequestContext.get().getRequestTime());
final Date endTime = new Date();
add(operation, startTime, endTime, params, result, resultCount);
}
public void add(AuditOperation operation, Date startTime,
Date endTime, String params, String result, long resultCount) throws AtlasBaseException {
String userName = RequestContext.get().getCurrentUser();
String clientId = RequestContext.get().getClientIPAddress();
if (StringUtils.isEmpty(clientId)) {
try {
clientId = InetAddress.getLocalHost().getHostName() + ":" +InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
LOG.error("Exception occurred during InetAddress retrieval", e);
clientId = "unknown";
}
}
add(userName, operation, clientId, startTime, endTime, params, result, resultCount);
}
public void add(String userName, AuditOperation operation, String clientId, Date startTime,
Date endTime, String params, String result, long resultCount) throws AtlasBaseException {
if (LOG.isDebugEnabled()) {
......
......@@ -91,9 +91,6 @@ public class TypeDefAuditListener implements TypeDefChangeListener {
if (CollectionUtils.isEmpty(baseTypeDefList)) {
return;
}
final String clientIp = RequestContext.get().getClientIPAddress();
final Date startTime = new Date(RequestContext.get().getRequestTime());
final Date endTime = new Date();
Map<TypeCategory, List<AtlasBaseTypeDef>> groupByCategoryMap =
baseTypeDefList.stream().collect(Collectors.groupingBy(AtlasBaseTypeDef::getCategory));
......@@ -105,8 +102,6 @@ public class TypeDefAuditListener implements TypeDefChangeListener {
String typeDefJson = AtlasJson.toJson(groupByCategoryMap);
auditService.add(RequestContext.get().getUser() == null ? "" : RequestContext.get().getUser(), auditOperation,
clientIp != null ? clientIp : "", startTime, endTime, String.join(",", categories),
typeDefJson, baseTypeDefList.size());
auditService.add(auditOperation, String.join(",", categories), typeDefJson, baseTypeDefList.size());
}
}
......@@ -544,16 +544,6 @@ public class AtlasTypeDefGraphStoreV2 extends AtlasTypeDefGraphStore {
}
public static String getCurrentUser() {
String ret = RequestContext.getCurrentUser();
if (StringUtils.isBlank(ret)) {
ret = System.getProperty("user.name");
if (StringUtils.isBlank(ret)) {
ret = "atlas";
}
}
return ret;
return RequestContext.getCurrentUser();
}
}
......@@ -26,6 +26,7 @@ import org.apache.atlas.model.instance.AtlasObjectId;
import org.apache.atlas.utils.AtlasPerfMetrics;
import org.apache.atlas.utils.AtlasPerfMetrics.MetricRecorder;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.security.UserGroupInformation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -128,7 +129,21 @@ public class RequestContext {
public static String getCurrentUser() {
RequestContext context = CURRENT_CONTEXT.get();
return context != null ? context.getUser() : null;
String ret = context != null ? context.getUser() : null;
if (StringUtils.isBlank(ret)) {
try {
ret = UserGroupInformation.getLoginUser().getShortUserName();
} catch (Exception e) {
ret = null;
}
if (StringUtils.isBlank(ret)){
ret = System.getProperty("user.name");
if (StringUtils.isBlank(ret)) {
ret = "atlas";
}
}
}
return ret;
}
public String getUser() {
......
......@@ -506,13 +506,8 @@ public class AdminResource {
final List<AtlasEntityHeader> purgedEntities = resp.getPurgedEntities();
if(purgedEntities != null && purgedEntities.size() > 0) {
final String clientId = RequestContext.get().getClientIPAddress();
final Date startTime = new Date(RequestContext.get().getRequestTime());
final Date endTime = new Date();
auditService.add(AtlasAuthorizationUtils.getCurrentUserName(), AuditOperation.PURGE,
clientId != null ? clientId : "", startTime, endTime, guids.toString(),
resp.getPurgedEntitiesIds(), resp.getPurgedEntities().size());
auditService.add(AuditOperation.PURGE, guids.toString(), resp.getPurgedEntitiesIds(),
resp.getPurgedEntities().size());
}
return resp;
......@@ -765,15 +760,10 @@ public class AdminResource {
}
private void auditImportExportOperations(List<AtlasObjectId> objectIds, AuditOperation auditOperation, String params) throws AtlasBaseException {
final String clientIp = RequestContext.get().getClientIPAddress();
final Date startTime = new Date(RequestContext.get().getRequestTime());
final Date endTime = new Date();
Map<String, Long> entityCountByType = objectIds.stream().collect(Collectors.groupingBy(AtlasObjectId::getTypeName, Collectors.counting()));
int resultCount = objectIds.size();
auditService.add(RequestContext.get().getUser() == null ? "" : RequestContext.get().getUser(), auditOperation,
clientIp != null ? clientIp : "", startTime, endTime, params,
AtlasJson.toJson(entityCountByType), resultCount);
auditService.add(auditOperation, params, AtlasJson.toJson(entityCountByType), resultCount);
}
}
......@@ -133,27 +133,12 @@ public class EmbeddedServer {
serviceState = BeanUtil.getBean(ServiceState.class);
ServiceState.ServiceStateValue serviceStateValue = serviceState.getState();
String userName = RequestContext.getCurrentUser();
if (userName == null) {
userName = StringUtils.EMPTY;
}
if (serviceStateValue == ServiceState.ServiceStateValue.ACTIVE) {
String hostName = StringUtils.EMPTY;
String hostAddress = StringUtils.EMPTY;
Date date = new Date();
try {
hostName = InetAddress.getLocalHost().getHostName();
hostAddress = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
LOG.error("Exception occurred during InetAddress retrieval", e);
}
try {
auditService.add(userName, AtlasAuditEntry.AuditOperation.SERVER_START, hostName + ":" + hostAddress, SERVER_START_TIME, date, null, null, 0);
auditService.add(userName, AtlasAuditEntry.AuditOperation.SERVER_STATE_ACTIVE, hostName + ":" + hostAddress, date, date, null, null, 0);
auditService.add(AtlasAuditEntry.AuditOperation.SERVER_START, SERVER_START_TIME, date, null, null, 0);
auditService.add(AtlasAuditEntry.AuditOperation.SERVER_STATE_ACTIVE, date, date, null, null, 0);
} catch (AtlasBaseException e) {
LOG.error("Exception occurred during audit", e);
}
......
......@@ -98,26 +98,12 @@ public class ServiceState {
}
private void auditServerStatus() {
String userName = RequestContext.getCurrentUser();
if (userName == null) {
userName = StringUtils.EMPTY;
}
if (state == ServiceState.ServiceStateValue.ACTIVE) {
String hostName = StringUtils.EMPTY;
String hostAddress = StringUtils.EMPTY;
Date date = new Date();
try {
hostName = InetAddress.getLocalHost().getHostName();
hostAddress = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
LOG.error("Exception occurred during InetAddress retrieval", e);
}
try {
auditService.add(userName, AtlasAuditEntry.AuditOperation.SERVER_START, hostName + ":" + hostAddress, EmbeddedServer.SERVER_START_TIME, date, null, null, 0);
auditService.add(userName, AtlasAuditEntry.AuditOperation.SERVER_STATE_ACTIVE, hostName + ":" + hostAddress, date, date, null, null, 0);
auditService.add(AtlasAuditEntry.AuditOperation.SERVER_START, EmbeddedServer.SERVER_START_TIME, date, null, null, 0);
auditService.add(AtlasAuditEntry.AuditOperation.SERVER_STATE_ACTIVE, date, date, null, null, 0);
} catch (AtlasBaseException e) {
LOG.error("Exception occurred during audit", e);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment