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

reta pushed a commit to branch 3.5.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git

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

    [CXF-9105] add synchronized to the principal iteration (#2245)
    
    (cherry picked from commit 96aca2ebaece9348c9e3a7538e4d45b66537c22c)
    (cherry picked from commit 28612db362733d9bb23e4007b8ffcdddea831d7b)
    (cherry picked from commit 9da6c020f2a104e6eec6d611e7abf5f16fc586ee)
    
    # Conflicts:
    #       
rt/features/logging/src/main/java/org/apache/cxf/ext/logging/event/DefaultLogEventMapper.java
---
 .../ext/logging/event/DefaultLogEventMapper.java   | 35 ++++++++++++++--------
 .../cxf/ext/logging/DefaultLogEventMapperTest.java | 29 ++++++++++++++++++
 2 files changed, 52 insertions(+), 12 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 dd7897d45a..78f5d002e7 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,25 +126,35 @@ 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() {
         Subject subject = Subject.getSubject(AccessController.getContext());
         return subject != null && subject.getPrincipals() != null
-            ? subject.getPrincipals().iterator() : Collections.emptyIterator();
+                ? subject.getPrincipals() : Collections.emptySet();
     }
 
     private Map<String, String> getHeaders(Message message) {
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..724bf4f2e4 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, 
Collections.emptySet(), Collections.emptySet());
+
+        LogEvent event = Subject.doAs(subject, (PrivilegedAction<LogEvent>) () 
-> mapper.map(message));
+        String[] splitPrincipals = event.getPrincipal().split(",");
+        Set<String> expected = new HashSet<>(Arrays.asList("principal-0", 
"principal-1", "principal-2"));
+
+        assertEquals(expected, 
Arrays.stream(splitPrincipals).collect(Collectors.toSet()));
+    }
+
 }

Reply via email to