You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@accumulo.apache.org by "keith-turner (via GitHub)" <gi...@apache.org> on 2023/09/21 16:58:21 UTC
[GitHub] [accumulo-access] keith-turner commented on a diff in pull request #4: Use ANTLR4 for specification validation
keith-turner commented on code in PR #4:
URL: https://github.com/apache/accumulo-access/pull/4#discussion_r1333318882
##########
src/test/java/org/apache/accumulo/access/grammar/antlr/AccessExpressionAntlrEvaluator.java:
##########
@@ -0,0 +1,119 @@
+package org.apache.accumulo.access.grammar.antlr;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.antlr.v4.runtime.tree.ParseTree;
+import org.antlr.v4.runtime.tree.TerminalNode;
+import org.apache.accumulo.access.AccessEvaluator;
+import org.apache.accumulo.access.AccessExpression;
+import org.apache.accumulo.access.Authorizations;
+import org.apache.accumulo.access.IllegalAccessExpressionException;
+import org.apache.accumulo.access.grammars.AccessExpressionParser.Access_expressionContext;
+import org.apache.accumulo.access.grammars.AccessExpressionParser.Access_tokenContext;
+import org.apache.accumulo.access.grammars.AccessExpressionParser.And_expressionContext;
+import org.apache.accumulo.access.grammars.AccessExpressionParser.And_operatorContext;
+import org.apache.accumulo.access.grammars.AccessExpressionParser.Or_expressionContext;
+import org.apache.accumulo.access.grammars.AccessExpressionParser.Or_operatorContext;
+
+public class AccessExpressionAntlrEvaluator implements AccessEvaluator {
+
+ private class Entity {
+
+ private Set<String> authorizations;
+
+ @Override
+ public String toString() {
+ return "Entity [authorizations=" + authorizations + "]";
+ }
+
+ }
+
+ private final List<Entity> entities;
+
+ public AccessExpressionAntlrEvaluator(List<Authorizations> authSets) {
+ entities = new ArrayList<>(authSets.size());
+
+ for (Authorizations a : authSets) {
+ Set<String> entityAuths = a.asSet();
+ Entity e = new Entity();
+ entities.add(e);
+ e.authorizations = new HashSet<>(entityAuths.size() * 2);
+ a.asSet().stream().forEach(auth -> {
+ e.authorizations.add(auth);
+ e.authorizations.add(new String(AccessEvaluator.escape(auth.getBytes(UTF_8), true), UTF_8));
Review Comment:
Can use this existing API method.
```suggestion
var quoted = AccessExpression.quote(auth);
if (!quoted.startsWith("\"")) {
quoted = '"' + quoted + '"';
}
e.authorizations.add(quoted);
```
##########
src/main/java/org/apache/accumulo/access/AccessEvaluator.java:
##########
@@ -56,6 +55,17 @@ public interface AccessEvaluator {
*/
boolean canAccess(AccessExpression accessExpression) throws IllegalAccessExpressionException;
+ /**
+ * Properly escapes an authorization string. The string can be quoted if desired.
+ *
+ * @param auth
+ * @param quote
+ * @return
+ */
+ static byte[] escape(byte[] auth, boolean quote) {
+ return AccessEvaluatorImpl.escape(auth, quote);
+ }
+
Review Comment:
I think this can be removed. I have another comment that changes the code using this to an existing API method.
```suggestion
```
##########
src/test/java/org/apache/accumulo/access/grammar/antlr/Antlr4Tests.java:
##########
@@ -0,0 +1,203 @@
+package org.apache.accumulo.access.grammar.antlr;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.List;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import org.antlr.v4.runtime.BailErrorStrategy;
+import org.antlr.v4.runtime.CharStreams;
+import org.antlr.v4.runtime.CodePointCharStream;
+import org.antlr.v4.runtime.CommonTokenStream;
+import org.antlr.v4.runtime.ConsoleErrorListener;
+import org.antlr.v4.runtime.LexerNoViableAltException;
+import org.antlr.v4.runtime.RecognitionException;
+import org.antlr.v4.runtime.Recognizer;
+import org.apache.accumulo.access.AccessEvaluator;
+import org.apache.accumulo.access.AccessEvaluatorTest;
+import org.apache.accumulo.access.AccessEvaluatorTest.ExpectedResult;
+import org.apache.accumulo.access.AccessEvaluatorTest.TestDataSet;
+import org.apache.accumulo.access.AccessEvaluatorTest.TestExpressions;
+import org.apache.accumulo.access.AccessExpression;
+import org.apache.accumulo.access.Authorizations;
+import org.apache.accumulo.access.IllegalAccessExpressionException;
+import org.apache.accumulo.access.grammars.AccessExpressionLexer;
+import org.apache.accumulo.access.grammars.AccessExpressionParser;
+import org.apache.accumulo.access.grammars.AccessExpressionParser.Access_expressionContext;
+import org.junit.jupiter.api.Test;
+
+public class Antlr4Tests {
+
+ private void testParse(String input) throws Exception {
+ CodePointCharStream expression = CharStreams.fromString(input);
+ final AtomicLong errors = new AtomicLong(0);
+ AccessExpressionLexer lexer = new AccessExpressionLexer(expression) {
+
+ @Override
+ public void recover(LexerNoViableAltException e) {
+ System.out.println("Error in lexer. Expression: " + expression + ", msg: " + e);
+ super.recover(e);
+ errors.incrementAndGet();
+ }
+
+ @Override
+ public void recover(RecognitionException re) {
+ System.out.println("Error in lexer. Expression: " + expression + ", msg: " + re);
+ super.recover(re);
+ errors.incrementAndGet();
+ }
+
+ };
+ AccessExpressionParser parser = new AccessExpressionParser(new CommonTokenStream(lexer));
+ parser.setErrorHandler(new BailErrorStrategy());
+ parser.removeErrorListeners();
+ parser.addErrorListener(new ConsoleErrorListener() {
+
+ @Override
+ public void syntaxError(Recognizer<?,?> recognizer, Object offendingSymbol, int line,
+ int charPositionInLine, String msg, RecognitionException e) {
+ super.syntaxError(recognizer, offendingSymbol, line, charPositionInLine, msg, e);
+ errors.incrementAndGet();
+ }
+
+ });
+ try {
+ Access_expressionContext ctx = parser.access_expression();
+ assertNull(ctx.exception);
+ } catch (RuntimeException e1) {
+ throw new AssertionError(e1);
+ }
+ assertEquals(0, errors.get());
+ assertEquals(0, parser.getNumberOfSyntaxErrors());
+ }
+
+ @Test
+ public void testCompareWithAccessExpressionImplParsing() throws Exception {
+ // This test checks that the parsing of the AccessExpressions in testdata.json
+ // using ANTLR have the same outcome as AccessExpression.of()
+ List<TestDataSet> testData = AccessEvaluatorTest.readTestData();
+ for (TestDataSet testSet : testData) {
+ for (TestExpressions test : testSet.tests) {
+ ExpectedResult result = test.expectedResult;
+ for (String cv : test.expressions) {
+ // System.out.println("Testing: " + cv);
+ if (result == ExpectedResult.ERROR) {
+ assertThrows(IllegalAccessExpressionException.class, () -> AccessExpression.of(cv));
+ assertThrows(AssertionError.class, () -> testParse(cv));
+ } else {
+ AccessExpression.of(cv);
+ testParse(cv);
+ }
+ }
+ }
+ }
+ }
+
+ @Test
+ public void testSimpleEvaluation() throws Exception {
+ String accessExpression = "(one&two)|(foo&bar)";
+ Authorizations auths = Authorizations.of("four", "three", "one", "two");
+ AccessExpressionAntlrEvaluator eval = new AccessExpressionAntlrEvaluator(List.of(auths));
+ assertTrue(eval.canAccess(accessExpression));
+ }
+
+ @Test
+ public void testSimpleEvaluationFailure() throws Exception {
+ String accessExpression = "(A&B&C)";
+ Authorizations auths = Authorizations.of("A", "C");
+ AccessExpressionAntlrEvaluator eval = new AccessExpressionAntlrEvaluator(List.of(auths));
+ assertFalse(eval.canAccess(accessExpression));
+ }
+
+ @Test
+ public void testCompareAntlrEvaluationAgainstAccessEvaluatorImpl() throws Exception {
+ // This test checks that the evaluation of the AccessExpressions in testdata.json
+ // using ANTLR have the same outcome as AccessEvaluatorImpl
+ List<TestDataSet> testData = AccessEvaluatorTest.readTestData();
+ for (TestDataSet testSet : testData) {
+ for (TestExpressions test : testSet.tests) {
+ for (String expression : test.expressions) {
+ List<Authorizations> authSets =
+ Stream.of(testSet.auths).map(Authorizations::of).collect(Collectors.toList());
+ AccessEvaluator evaluator = AccessEvaluator.builder().authorizations(authSets).build();
+
+ AccessExpressionAntlrEvaluator antlr = new AccessExpressionAntlrEvaluator(authSets);
Review Comment:
These can be moved outside of the loop.
```suggestion
List<Authorizations> authSets =
Stream.of(testSet.auths).map(Authorizations::of).collect(Collectors.toList());
AccessEvaluator evaluator = AccessEvaluator.builder().authorizations(authSets).build();
AccessExpressionAntlrEvaluator antlr = new AccessExpressionAntlrEvaluator(authSets);
for (String expression : test.expressions) {
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@accumulo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org