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

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


The following commit(s) were added to refs/heads/main by this push:
     new e612481b6d Revert "Optimize SecurityContext#isInOneOf (#1874)" (#1875)
e612481b6d is described below

commit e612481b6dcc54e813cebb8d0c6b7e839f3dbccd
Author: Christopher L. Shannon <[email protected]>
AuthorDate: Fri Apr 3 20:42:44 2026 -0400

    Revert "Optimize SecurityContext#isInOneOf (#1874)" (#1875)
    
    This reverts commit b404e1e335011af93ff3c40318bcfc2fc2800660.
    
    This change needs more work because DefaultAuthorizationMap requires
    equals() to be used the way it handles a wildcard Principal. This needs
    to be fixed before the change can be made
---
 .../apache/activemq/security/SecurityContext.java  | 16 ++--
 .../activemq/security/SecurityContextTest.java     | 95 ----------------------
 2 files changed, 8 insertions(+), 103 deletions(-)

diff --git 
a/activemq-broker/src/main/java/org/apache/activemq/security/SecurityContext.java
 
b/activemq-broker/src/main/java/org/apache/activemq/security/SecurityContext.java
index f24b6ffc3f..fd677ce590 100644
--- 
a/activemq-broker/src/main/java/org/apache/activemq/security/SecurityContext.java
+++ 
b/activemq-broker/src/main/java/org/apache/activemq/security/SecurityContext.java
@@ -54,9 +54,14 @@ public abstract class SecurityContext {
     }
 
     public boolean isInOneOf(Set<?> allowedPrincipals) {
-        for (Object allowedPrincipal : allowedPrincipals) {
-            if (contains(allowedPrincipal)) {
-                return true;
+        Iterator<?> allowedIter = allowedPrincipals.iterator();
+        HashSet<?> userPrincipals = new HashSet<Object>(getPrincipals());
+        while (allowedIter.hasNext()) {
+            Iterator<?> userIter = userPrincipals.iterator();
+            Object allowedPrincipal = allowedIter.next();
+            while (userIter.hasNext()) {
+                if (allowedPrincipal.equals(userIter.next()))
+                    return true;
             }
         }
         return false;
@@ -64,11 +69,6 @@ public abstract class SecurityContext {
 
     public abstract Set<Principal> getPrincipals();
 
-    public boolean contains(Object principal) {
-        Set<Principal> principals = getPrincipals();
-        return principals != null && principals.contains(principal);
-    }
-
     public String getUserName() {
         return userName;
     }
diff --git 
a/activemq-broker/src/test/java/org/apache/activemq/security/SecurityContextTest.java
 
b/activemq-broker/src/test/java/org/apache/activemq/security/SecurityContextTest.java
deleted file mode 100644
index 51ffb95d81..0000000000
--- 
a/activemq-broker/src/test/java/org/apache/activemq/security/SecurityContextTest.java
+++ /dev/null
@@ -1,95 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.activemq.security;
-
-import static 
org.apache.activemq.security.SecurityContextTest.TestPrincipal.testPrincipal;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-
-import java.security.Principal;
-import java.util.Objects;
-import java.util.Set;
-import org.junit.Test;
-
-public class SecurityContextTest {
-
-    @Test
-    public void testIsOneOf() {
-        SecurityContext context = newContext(Set.of(testPrincipal("one"),
-                testPrincipal("two"), testPrincipal("three")));
-
-        // test valid combos
-        assertTrue(context.isInOneOf(Set.of(testPrincipal("one"))));
-        assertTrue(context.isInOneOf(Set.of(testPrincipal("two"))));
-        assertTrue(context.isInOneOf(Set.of(testPrincipal("three"))));
-        assertTrue(context.isInOneOf(Set.of(testPrincipal("three"),
-                testPrincipal("four"), testPrincipal("five"))));
-
-        // test no matching
-        assertFalse(context.isInOneOf(Set.of(testPrincipal("four"),
-                testPrincipal("five"))));
-        assertFalse(context.isInOneOf(Set.of()));
-        // different impl types, should not find
-        assertFalse(context.isInOneOf(Set.of((Principal) () -> "one")));
-
-        // empty set
-        context = newContext(Set.of());
-        assertFalse(context.isInOneOf(Set.of(testPrincipal("one"))));
-        assertFalse(context.isInOneOf(Set.of()));
-    }
-
-    private SecurityContext newContext(Set<Principal> principals) {
-        return new SecurityContext("user") {
-            @Override
-            public Set<Principal> getPrincipals() {
-                return principals;
-            }
-        };
-    }
-
-    static class TestPrincipal implements Principal {
-        private final String name;
-
-        private TestPrincipal(String name) {
-            this.name = name;
-        }
-
-        @Override
-        public String getName() {
-            return name;
-        }
-
-        @Override
-        public boolean equals(Object o) {
-            if (o == null || getClass() != o.getClass()) {
-                return false;
-            }
-            TestPrincipal that = (TestPrincipal) o;
-            return Objects.equals(name, that.name);
-        }
-
-        @Override
-        public int hashCode() {
-            return Objects.hashCode(name);
-        }
-
-        static TestPrincipal testPrincipal(String name) {
-            return new TestPrincipal(name);
-        }
-    }
-
-}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information, visit: https://activemq.apache.org/contact


Reply via email to