This is an automated email from the ASF dual-hosted git repository.

sarath pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/atlas.git


The following commit(s) were added to refs/heads/master by this push:
     new 05d8dec  ATLAS-4036, ATLAS-4037: Fix for Users and Client Id field in 
Admin Audits page
05d8dec is described below

commit 05d8dec6fea6ba7566a900e3a2caca1470c975fc
Author: Deep Singh <[email protected]>
AuthorDate: Thu Nov 19 22:37:53 2020 -0600

    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: Sarath Subramanian <[email protected]>
---
 .../atlas/repository/audit/AtlasAuditService.java  | 24 ++++++++++++++++++++++
 .../repository/audit/TypeDefAuditListener.java     |  7 +------
 .../store/graph/v2/AtlasTypeDefGraphStoreV2.java   | 12 +----------
 .../main/java/org/apache/atlas/RequestContext.java | 17 ++++++++++++++-
 .../apache/atlas/web/resources/AdminResource.java  | 16 +++------------
 .../apache/atlas/web/service/EmbeddedServer.java   | 19 ++---------------
 .../org/apache/atlas/web/service/ServiceState.java | 18 ++--------------
 7 files changed, 49 insertions(+), 64 deletions(-)

diff --git 
a/repository/src/main/java/org/apache/atlas/repository/audit/AtlasAuditService.java
 
b/repository/src/main/java/org/apache/atlas/repository/audit/AtlasAuditService.java
index d843204..a85e018 100644
--- 
a/repository/src/main/java/org/apache/atlas/repository/audit/AtlasAuditService.java
+++ 
b/repository/src/main/java/org/apache/atlas/repository/audit/AtlasAuditService.java
@@ -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()) {
diff --git 
a/repository/src/main/java/org/apache/atlas/repository/audit/TypeDefAuditListener.java
 
b/repository/src/main/java/org/apache/atlas/repository/audit/TypeDefAuditListener.java
index bfc300e..4fe9f59 100644
--- 
a/repository/src/main/java/org/apache/atlas/repository/audit/TypeDefAuditListener.java
+++ 
b/repository/src/main/java/org/apache/atlas/repository/audit/TypeDefAuditListener.java
@@ -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());
     }
 }
diff --git 
a/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/AtlasTypeDefGraphStoreV2.java
 
b/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/AtlasTypeDefGraphStoreV2.java
index ed17b92..96fb3b4 100644
--- 
a/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/AtlasTypeDefGraphStoreV2.java
+++ 
b/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/AtlasTypeDefGraphStoreV2.java
@@ -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();
     }
 }
diff --git a/server-api/src/main/java/org/apache/atlas/RequestContext.java 
b/server-api/src/main/java/org/apache/atlas/RequestContext.java
index befd726..32ffddd 100644
--- a/server-api/src/main/java/org/apache/atlas/RequestContext.java
+++ b/server-api/src/main/java/org/apache/atlas/RequestContext.java
@@ -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() {
diff --git 
a/webapp/src/main/java/org/apache/atlas/web/resources/AdminResource.java 
b/webapp/src/main/java/org/apache/atlas/web/resources/AdminResource.java
index 4db477e..6f8c9e4 100755
--- a/webapp/src/main/java/org/apache/atlas/web/resources/AdminResource.java
+++ b/webapp/src/main/java/org/apache/atlas/web/resources/AdminResource.java
@@ -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);
     }
 }
diff --git 
a/webapp/src/main/java/org/apache/atlas/web/service/EmbeddedServer.java 
b/webapp/src/main/java/org/apache/atlas/web/service/EmbeddedServer.java
index 1d1e08e..d6658fe 100755
--- a/webapp/src/main/java/org/apache/atlas/web/service/EmbeddedServer.java
+++ b/webapp/src/main/java/org/apache/atlas/web/service/EmbeddedServer.java
@@ -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);
             }
diff --git 
a/webapp/src/main/java/org/apache/atlas/web/service/ServiceState.java 
b/webapp/src/main/java/org/apache/atlas/web/service/ServiceState.java
index 93e6513..ea74d21 100644
--- a/webapp/src/main/java/org/apache/atlas/web/service/ServiceState.java
+++ b/webapp/src/main/java/org/apache/atlas/web/service/ServiceState.java
@@ -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);
             }

Reply via email to