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

reta pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/main by this push:
     new 96aca2ebae [CXF-9105] add synchronized to the principal iteration 
(#2245)
96aca2ebae is described below

commit 96aca2ebaece9348c9e3a7538e4d45b66537c22c
Author: sstremler <[email protected]>
AuthorDate: Sat Feb 1 15:33:35 2025 +0100

    [CXF-9105] add synchronized to the principal iteration (#2245)
---
 .../ext/logging/event/DefaultLogEventMapper.java   | 37 ++++++++++++++--------
 .../cxf/ext/logging/DefaultLogEventMapperTest.java | 29 +++++++++++++++++
 2 files changed, 53 insertions(+), 13 deletions(-)

diff --git 
a/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/event/DefaultLogEventMapper.java
 
b/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/event/DefaultLogEventMapper.java
index 2e77c8df64..6140201fee 100644
--- 
a/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/event/DefaultLogEventMapper.java
+++ 
b/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/event/DefaultLogEventMapper.java
@@ -19,6 +19,7 @@
 package org.apache.cxf.ext.logging.event;
 
 import java.security.AccessController;
+import java.security.Principal;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashMap;
@@ -109,7 +110,7 @@ public class DefaultLogEventMapper {
     }
 
     private String getPrincipal(Message message) {
-        String principal = getJAASPrincipal();
+        String principal = getConcatenatedJAASPrincipals();
         if (principal != null) {
             return principal;
         }
@@ -125,31 +126,41 @@ public class DefaultLogEventMapper {
         return null;
     }
 
-    private String getJAASPrincipal() {
-        StringBuilder principals = new StringBuilder();
-        Iterator<? extends Object> principalIt = getJAASPrincipals();
-        while (principalIt.hasNext()) {
-            principals.append(principalIt.next());
-            if (principalIt.hasNext()) {
-                principals.append(',');
+    private String getConcatenatedJAASPrincipals() {
+        StringBuilder principalsStringBuilder = new StringBuilder();
+        Set<Principal> principals = getJAASPrincipals();
+
+        if (principals.isEmpty()) {
+            return null;
+        }
+
+        synchronized (principals) {
+            Iterator<Principal> principalIt = principals.iterator();
+            while (principalIt.hasNext()) {
+                principalsStringBuilder.append(principalIt.next());
+                if (principalIt.hasNext()) {
+                    principalsStringBuilder.append(',');
+                }
             }
         }
-        if (principals.length() == 0) {
+
+        if (principalsStringBuilder.length() == 0) {
             return null;
         }
-        return principals.toString();
+
+        return principalsStringBuilder.toString();
     }
 
-    private Iterator<? extends Object> getJAASPrincipals() {
+    private Set<Principal> getJAASPrincipals() {
         try {
             Subject subject = 
Subject.getSubject(AccessController.getContext());
             return subject != null && subject.getPrincipals() != null
-                    ? subject.getPrincipals().iterator() : 
Collections.emptyIterator();
+                    ? subject.getPrincipals() : Collections.emptySet();
         } catch (UnsupportedOperationException e) {
             // JDK 23: The terminally deprecated method 
Subject.getSubject(AccessControlContext) has been re-specified
             // to throw UnsupportedOperationException if invoked when a 
Security Manager is not allowed.
             // see https://jdk.java.net/23/release-notes#JDK-8296244
-            return Collections.emptyIterator();
+            return Collections.emptySet();
         }
     }
 
diff --git 
a/rt/features/logging/src/test/java/org/apache/cxf/ext/logging/DefaultLogEventMapperTest.java
 
b/rt/features/logging/src/test/java/org/apache/cxf/ext/logging/DefaultLogEventMapperTest.java
index 9df839e6b6..f7c2a765c1 100644
--- 
a/rt/features/logging/src/test/java/org/apache/cxf/ext/logging/DefaultLogEventMapperTest.java
+++ 
b/rt/features/logging/src/test/java/org/apache/cxf/ext/logging/DefaultLogEventMapperTest.java
@@ -18,13 +18,20 @@
  */
 package org.apache.cxf.ext.logging;
 
+import java.security.Principal;
+import java.security.PrivilegedAction;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
 
+import javax.security.auth.Subject;
+
+import org.apache.cxf.common.security.SimplePrincipal;
 import org.apache.cxf.ext.logging.event.DefaultLogEventMapper;
 import org.apache.cxf.ext.logging.event.EventType;
 import org.apache.cxf.ext.logging.event.LogEvent;
@@ -147,4 +154,26 @@ public class DefaultLogEventMapperTest {
         assertEquals("PUT[test]", event.getOperationName());
     }
 
+    @Test
+    public void testMultiplePrincipalsReturnedByAccessControllerContext() {
+        DefaultLogEventMapper mapper = new DefaultLogEventMapper();
+        Message message = new MessageImpl();
+        message.put(Message.HTTP_REQUEST_METHOD, "GET");
+        message.put(Message.REQUEST_URI, "test");
+        Exchange exchange = new ExchangeImpl();
+        message.setExchange(exchange);
+
+        Set<Principal> principals = IntStream.range(0, 3)
+                .mapToObj(i -> new SimplePrincipal("principal-" + i))
+                .collect(Collectors.toSet());
+
+        Subject subject = new Subject(false, principals, Set.of(), Set.of());
+
+        LogEvent event = Subject.doAs(subject, (PrivilegedAction<LogEvent>) () 
-> mapper.map(message));
+        String[] splitPrincipals = event.getPrincipal().split(",");
+        Set<String> expected = Set.of("principal-0", "principal-1", 
"principal-2");
+
+        assertEquals(expected, 
Arrays.stream(splitPrincipals).collect(Collectors.toSet()));
+    }
+
 }

Reply via email to