You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by iu...@apache.org on 2021/08/26 15:36:26 UTC

[brooklyn-server] branch master updated: Checking also in the entitlement context and usser session

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

iuliana pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git


The following commit(s) were added to refs/heads/master by this push:
     new 9eaf95f  Checking also in the entitlement context and usser session
     new 1484d8f  Merge pull request #1235 from jcabrerizo/feature/loging-userName-from-session-v3
9eaf95f is described below

commit 9eaf95fe2e798eb5cade496524d8863d6749793b
Author: Juan Cabrerizo <ju...@cloudsoft.io>
AuthorDate: Thu Aug 26 15:35:21 2021 +0100

    Checking also in the entitlement context and usser session
---
 .../rest/filter/LoggingResourceFilter.java         | 29 +++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)

diff --git a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/filter/LoggingResourceFilter.java b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/filter/LoggingResourceFilter.java
index 397de0e..1d03b9e 100644
--- a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/filter/LoggingResourceFilter.java
+++ b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/filter/LoggingResourceFilter.java
@@ -37,6 +37,9 @@ import javax.ws.rs.core.SecurityContext;
 import javax.ws.rs.ext.Provider;
 
 import org.apache.brooklyn.core.BrooklynLogging;
+import org.apache.brooklyn.core.mgmt.entitlement.Entitlements;
+import org.apache.brooklyn.rest.util.MultiSessionAttributeAdapter;
+import org.apache.brooklyn.util.guava.Maybe;
 import org.apache.brooklyn.util.time.Duration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -127,21 +130,41 @@ public class LoggingResourceFilter implements ContainerRequestFilter, ContainerR
         
         SecurityContext securityContext = requestContext.getSecurityContext();
         Principal userPrincipal = (securityContext != null) ? requestContext.getSecurityContext().getUserPrincipal() : null;
-        String userName = (userPrincipal != null) ? userPrincipal.getName() : "<no-user>";
+        String userName = (userPrincipal != null) ? userPrincipal.getName() : tryFindUserName();
         String remoteAddr = servletRequest.getRemoteAddr();
         
         StringBuilder message = new StringBuilder("Request received: ")
                 .append(method)
                 .append(" ")
                 .append(path)
-                .append(" from ")
+                .append(" from user '")
                 .append(userName)
-                .append(" @ ")
+                .append("' @ ")
                 .append(remoteAddr);
 
         log(LOG, level, message.toString());
     }
 
+    private String tryFindUserName(){
+        // trying to find it on the Entitlement context
+        Maybe<String> entitlementContextUserMaybe = Entitlements.getEntitlementContextUserMaybe();
+        if(entitlementContextUserMaybe.isPresent()){
+            return entitlementContextUserMaybe.get();
+        }
+
+        // trying to find it on the session
+        if (servletRequest != null) {
+            MultiSessionAttributeAdapter s = MultiSessionAttributeAdapter.of(servletRequest, false);
+            if (s != null) {
+                String userName = (String) s.getAttribute(BrooklynSecurityProviderFilterHelper.AUTHENTICATED_USER_SESSION_ATTRIBUTE);
+                if(userName !=null){
+                    return userName;
+                }
+            }
+        }
+        return  "<no-user>";
+    }
+
     private void logResponse(ContainerRequestContext requestContext, ContainerResponseContext responseContext, Duration requestDuration, LogLevel level) {
         if (!isLogEnabled(LOG, level)) return;