Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CXF-9105] add synchronized to the principal iteration #2245

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -109,7 +110,7 @@ public LogEvent map(final Message message, final Set<String> sensitiveProtocolHe
}

private String getPrincipal(Message message) {
String principal = getJAASPrincipal();
String principal = getConcatenatedJAASPrincipals();
if (principal != null) {
return principal;
}
Expand All @@ -125,31 +126,41 @@ private String getPrincipal(Message message) {
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();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -147,4 +154,26 @@ public void testMap() {
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()));
}

}
Loading