This is an automated email from the ASF dual-hosted git repository. dlmarion pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/accumulo-access.git
The following commit(s) were added to refs/heads/main by this push: new 33be659 Modified Authorizations.of to only accept a Set (#68) 33be659 is described below commit 33be6595160d0f62041731a720cc2b80f898c243 Author: Dave Marion <dlmar...@apache.org> AuthorDate: Tue Mar 12 13:47:23 2024 -0400 Modified Authorizations.of to only accept a Set (#68) Closes #66 --------- Co-authored-by: Keith Turner <ktur...@apache.org> --- .../antlr/AccessExpressionAntlrBenchmark.java | 3 +- .../accumulo/access/grammar/antlr/Antlr4Tests.java | 7 ++-- .../apache/accumulo/access/AccessEvaluator.java | 7 ---- .../accumulo/access/AccessEvaluatorImpl.java | 8 ---- .../accumulo/access/AccessExpressionImpl.java | 2 +- .../org/apache/accumulo/access/Authorizations.java | 21 ++++++---- src/test/java/example/AccessExample.java | 4 +- .../accumulo/access/AccessEvaluatorTest.java | 18 +++++---- .../accumulo/access/AccessExpressionBenchmark.java | 7 ++-- .../apache/accumulo/access/AuthorizationTest.java | 45 ++++++++++++++++++++++ 10 files changed, 84 insertions(+), 38 deletions(-) diff --git a/src/it/antlr4-example/src/test/java/org/apache/accumulo/access/grammar/antlr/AccessExpressionAntlrBenchmark.java b/src/it/antlr4-example/src/test/java/org/apache/accumulo/access/grammar/antlr/AccessExpressionAntlrBenchmark.java index 0c3422f..75ec0a7 100644 --- a/src/it/antlr4-example/src/test/java/org/apache/accumulo/access/grammar/antlr/AccessExpressionAntlrBenchmark.java +++ b/src/it/antlr4-example/src/test/java/org/apache/accumulo/access/grammar/antlr/AccessExpressionAntlrBenchmark.java @@ -25,6 +25,7 @@ import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; +import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -86,7 +87,7 @@ public class AccessExpressionAntlrBenchmark { et.expressions = new ArrayList<>(); et.evaluator = new AccessExpressionAntlrEvaluator( - Stream.of(testDataSet.auths).map(Authorizations::of).collect(Collectors.toList())); + Stream.of(testDataSet.auths).map(a -> Authorizations.of(Set.of(a))).collect(Collectors.toList())); for (var tests : testDataSet.tests) { if (tests.expectedResult != TestDataLoader.ExpectedResult.ERROR) { diff --git a/src/it/antlr4-example/src/test/java/org/apache/accumulo/access/grammar/antlr/Antlr4Tests.java b/src/it/antlr4-example/src/test/java/org/apache/accumulo/access/grammar/antlr/Antlr4Tests.java index be40f3b..09870ae 100644 --- a/src/it/antlr4-example/src/test/java/org/apache/accumulo/access/grammar/antlr/Antlr4Tests.java +++ b/src/it/antlr4-example/src/test/java/org/apache/accumulo/access/grammar/antlr/Antlr4Tests.java @@ -27,6 +27,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.List; import java.util.concurrent.atomic.AtomicLong; +import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -120,7 +121,7 @@ public class Antlr4Tests { @Test public void testSimpleEvaluation() throws Exception { String accessExpression = "(one&two)|(foo&bar)"; - Authorizations auths = Authorizations.of("four", "three", "one", "two"); + Authorizations auths = Authorizations.of(Set.of("four", "three", "one", "two")); AccessExpressionAntlrEvaluator eval = new AccessExpressionAntlrEvaluator(List.of(auths)); assertTrue(eval.canAccess(accessExpression)); } @@ -128,7 +129,7 @@ public class Antlr4Tests { @Test public void testSimpleEvaluationFailure() throws Exception { String accessExpression = "(A&B&C)"; - Authorizations auths = Authorizations.of("A", "C"); + Authorizations auths = Authorizations.of(Set.of("A", "C")); AccessExpressionAntlrEvaluator eval = new AccessExpressionAntlrEvaluator(List.of(auths)); assertFalse(eval.canAccess(accessExpression)); } @@ -141,7 +142,7 @@ public class Antlr4Tests { for (TestDataSet testSet : testData) { List<Authorizations> authSets = - Stream.of(testSet.auths).map(Authorizations::of).collect(Collectors.toList()); + Stream.of(testSet.auths).map(a -> Authorizations.of(Set.of(a))).collect(Collectors.toList()); AccessEvaluator evaluator = AccessEvaluator.of(authSets); AccessExpressionAntlrEvaluator antlr = new AccessExpressionAntlrEvaluator(authSets); diff --git a/src/main/java/org/apache/accumulo/access/AccessEvaluator.java b/src/main/java/org/apache/accumulo/access/AccessEvaluator.java index 1ea4d23..3eff0d1 100644 --- a/src/main/java/org/apache/accumulo/access/AccessEvaluator.java +++ b/src/main/java/org/apache/accumulo/access/AccessEvaluator.java @@ -152,13 +152,6 @@ public interface AccessEvaluator { return new AccessEvaluatorImpl(AccessEvaluatorImpl.convert(authorizationSets)); } - /** - * Allows specifying a single set of authorizations. - */ - static AccessEvaluator of(String... authorizations) { - return new AccessEvaluatorImpl(AccessEvaluatorImpl.convert(authorizations)); - } - /** * An interface that is used to check if an authorization seen in an access expression is * authorized. diff --git a/src/main/java/org/apache/accumulo/access/AccessEvaluatorImpl.java b/src/main/java/org/apache/accumulo/access/AccessEvaluatorImpl.java index a73f627..7fe6077 100644 --- a/src/main/java/org/apache/accumulo/access/AccessEvaluatorImpl.java +++ b/src/main/java/org/apache/accumulo/access/AccessEvaluatorImpl.java @@ -56,14 +56,6 @@ final class AccessEvaluatorImpl implements AccessEvaluator { return authorizationLists; } - static Collection<List<byte[]>> convert(String... authorizations) { - final List<byte[]> authList = new ArrayList<>(authorizations.length); - for (final String auth : authorizations) { - authList.add(auth.getBytes(UTF_8)); - } - return Collections.singletonList(authList); - } - static Collection<List<byte[]>> convert(Authorizations authorizations) { final Set<String> authSet = authorizations.asSet(); final List<byte[]> authList = new ArrayList<>(authSet.size()); diff --git a/src/main/java/org/apache/accumulo/access/AccessExpressionImpl.java b/src/main/java/org/apache/accumulo/access/AccessExpressionImpl.java index 9585cb9..5c549b8 100644 --- a/src/main/java/org/apache/accumulo/access/AccessExpressionImpl.java +++ b/src/main/java/org/apache/accumulo/access/AccessExpressionImpl.java @@ -75,7 +75,7 @@ final class AccessExpressionImpl implements AccessExpression { @Override public Authorizations getAuthorizations() { - return AccessExpressionImpl.getAuthorizations(expression); + return getAuthorizations(expression); } static Authorizations getAuthorizations(byte[] expression) { diff --git a/src/main/java/org/apache/accumulo/access/Authorizations.java b/src/main/java/org/apache/accumulo/access/Authorizations.java index f1b1640..d3bd568 100644 --- a/src/main/java/org/apache/accumulo/access/Authorizations.java +++ b/src/main/java/org/apache/accumulo/access/Authorizations.java @@ -18,7 +18,6 @@ */ package org.apache.accumulo.access; -import java.util.Collection; import java.util.Set; /** @@ -33,12 +32,18 @@ import java.util.Set; * @since 1.0.0 */ public final class Authorizations { + private final Set<String> authorizations; private Authorizations(Set<String> authorizations) { this.authorizations = Set.copyOf(authorizations); } + /** + * Returns the set of authorization strings in this Authorization object + * + * @return immutable set of authorization strings + */ public Set<String> asSet() { return authorizations; } @@ -63,12 +68,14 @@ public final class Authorizations { return authorizations.toString(); } - public static Authorizations of(String... authorizations) { - return new Authorizations(Set.of(authorizations)); - } - - public static Authorizations of(Collection<String> authorizations) { - return new Authorizations(Set.copyOf(authorizations)); + /** + * Creates an Authorizations object from the set of input authorization strings. + * + * @param authorizations set of authorization strings + * @return Authorizations object + */ + public static Authorizations of(Set<String> authorizations) { + return new Authorizations(authorizations); } } diff --git a/src/test/java/example/AccessExample.java b/src/test/java/example/AccessExample.java index d62f309..5b4b2cc 100644 --- a/src/test/java/example/AccessExample.java +++ b/src/test/java/example/AccessExample.java @@ -22,9 +22,11 @@ package example; import java.io.PrintStream; import java.util.Arrays; import java.util.Map; +import java.util.Set; import java.util.TreeMap; import org.apache.accumulo.access.AccessEvaluator; +import org.apache.accumulo.access.Authorizations; public class AccessExample { @@ -56,7 +58,7 @@ public class AccessExample { Arrays.toString(authorizations)); // Create an access evaluator using the provided authorizations - AccessEvaluator evaluator = AccessEvaluator.of(authorizations); + AccessEvaluator evaluator = AccessEvaluator.of(Authorizations.of(Set.of(authorizations))); // Print each record whose access expression permits viewing using the provided authorizations getData().forEach((record, accessExpression) -> { diff --git a/src/test/java/org/apache/accumulo/access/AccessEvaluatorTest.java b/src/test/java/org/apache/accumulo/access/AccessEvaluatorTest.java index 8d1c522..bcba1fc 100644 --- a/src/test/java/org/apache/accumulo/access/AccessEvaluatorTest.java +++ b/src/test/java/org/apache/accumulo/access/AccessEvaluatorTest.java @@ -84,15 +84,15 @@ public class AccessEvaluatorTest { AccessEvaluator evaluator; assertTrue(testSet.auths.length >= 1); if (testSet.auths.length == 1) { - evaluator = AccessEvaluator.of(testSet.auths[0]); + evaluator = AccessEvaluator.of(Authorizations.of(Set.of(testSet.auths[0]))); runTestCases(testSet, evaluator); Set<String> auths = Stream.of(testSet.auths[0]).collect(Collectors.toSet()); evaluator = AccessEvaluator.of(auths::contains); runTestCases(testSet, evaluator); } else { - var authSets = - Stream.of(testSet.auths).map(Authorizations::of).collect(Collectors.toList()); + var authSets = Stream.of(testSet.auths).map(a -> Authorizations.of(Set.of(a))) + .collect(Collectors.toList()); evaluator = AccessEvaluator.of(authSets); runTestCases(testSet, evaluator); } @@ -184,10 +184,14 @@ public class AccessEvaluatorTest { @Test public void testEmptyAuthorizations() { - assertThrows(IllegalArgumentException.class, () -> AccessEvaluator.of("")); - assertThrows(IllegalArgumentException.class, () -> AccessEvaluator.of("", "A")); - assertThrows(IllegalArgumentException.class, () -> AccessEvaluator.of("A", "")); - assertThrows(IllegalArgumentException.class, () -> AccessEvaluator.of(Authorizations.of(""))); + assertThrows(IllegalArgumentException.class, + () -> AccessEvaluator.of(Authorizations.of(Set.of("")))); + assertThrows(IllegalArgumentException.class, + () -> AccessEvaluator.of(Authorizations.of(Set.of("", "A")))); + assertThrows(IllegalArgumentException.class, + () -> AccessEvaluator.of(Authorizations.of(Set.of("A", "")))); + assertThrows(IllegalArgumentException.class, + () -> AccessEvaluator.of(Authorizations.of(Set.of("")))); } @Test diff --git a/src/test/java/org/apache/accumulo/access/AccessExpressionBenchmark.java b/src/test/java/org/apache/accumulo/access/AccessExpressionBenchmark.java index 3d7ffa6..1c3f4a4 100644 --- a/src/test/java/org/apache/accumulo/access/AccessExpressionBenchmark.java +++ b/src/test/java/org/apache/accumulo/access/AccessExpressionBenchmark.java @@ -23,6 +23,7 @@ import static java.nio.charset.StandardCharsets.UTF_8; import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.Set; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -85,10 +86,10 @@ public class AccessExpressionBenchmark { et.expressions = new ArrayList<>(); if (testDataSet.auths.length == 1) { - et.evaluator = AccessEvaluator.of(testDataSet.auths[0]); + et.evaluator = AccessEvaluator.of(Authorizations.of(Set.of(testDataSet.auths[0]))); } else { - var authSets = - Stream.of(testDataSet.auths).map(Authorizations::of).collect(Collectors.toList()); + var authSets = Stream.of(testDataSet.auths).map(a -> Authorizations.of(Set.of(a))) + .collect(Collectors.toList()); et.evaluator = AccessEvaluator.of(authSets); } diff --git a/src/test/java/org/apache/accumulo/access/AuthorizationTest.java b/src/test/java/org/apache/accumulo/access/AuthorizationTest.java new file mode 100644 index 0000000..22636ae --- /dev/null +++ b/src/test/java/org/apache/accumulo/access/AuthorizationTest.java @@ -0,0 +1,45 @@ +/* + * 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 + * + * https://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.accumulo.access; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +import java.util.Set; + +import org.junit.jupiter.api.Test; + +public class AuthorizationTest { + + @Test + public void testEquality() { + Authorizations auths1 = Authorizations.of(Set.of("A", "B", "C")); + Authorizations auths2 = Authorizations.of(Set.of("A", "B", "C")); + + assertEquals(auths1, auths2); + assertEquals(auths1.hashCode(), auths2.hashCode()); + + Authorizations auths3 = Authorizations.of(Set.of("D", "E", "F")); + + assertNotEquals(auths1, auths3); + assertNotEquals(auths1.hashCode(), auths3.hashCode()); + + } + +}