dlmarion commented on code in PR #31: URL: https://github.com/apache/accumulo-access/pull/31#discussion_r1376064038
########## src/main/java/org/apache/accumulo/access/ParserEvaluator.java: ########## @@ -0,0 +1,140 @@ +/* + * 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.apache.accumulo.access.ByteUtils.isAndOrOperator; + +import java.util.function.Predicate; + +/** + * Code for parsing and evaluating an access expression at the same time. + */ +final class ParserEvaluator { + + public static final byte OPEN_PAREN = (byte) '('; + public static final byte CLOSE_PAREN = (byte) ')'; + private static final byte[] EMPTY = new byte[0]; + private static final ThreadLocal<BytesWrapper> lookupWrappers = + ThreadLocal.withInitial(() -> new BytesWrapper(EMPTY)); + private static final ThreadLocal<Tokenizer> tokenizers = + ThreadLocal.withInitial(() -> new Tokenizer(EMPTY)); + + // TODO would be cleaner to have AccessEvaluatorImpl pass these in rather than use thread locals. + private static final Predicate<BytesWrapper> TRUE_AUTH_PREDICATE = bw -> true; + private static final Predicate<BytesWrapper> FALSE_AUTH_PREDICATE = bw -> false; + + public static boolean parseAccessExpression(byte[] expression, + Predicate<BytesWrapper> authorizedPredicate) { + + Tokenizer tokenizer = tokenizers.get(); + tokenizer.reset(expression); + + if (!tokenizer.hasNext()) { + return true; + } + + BytesWrapper lookupWrapper = lookupWrappers.get(); + var node = parseExpression(tokenizer, authorizedPredicate, lookupWrapper); + + if (tokenizer.hasNext()) { + // not all input was read, so not a valid expression + tokenizer.error("Unexpected character '" + (char) tokenizer.peek() + "'"); + } + + return node; + } + + private static boolean parseExpression(Tokenizer tokenizer, + Predicate<BytesWrapper> authorizedPredicate, BytesWrapper lookupWrapper) { + + boolean result = + parseParenExpressionOrAuthorization(tokenizer, authorizedPredicate, lookupWrapper); + + if (tokenizer.hasNext()) { + var operator = tokenizer.peek(); Review Comment: `tokenizer.peek` returns a `byte`, not a `char`. When doing comparisons, use the public static members from `ByteUtils`, AND_OPERATOR and OR_OPERATOR in this case, to remove the byte to char translation. ########## src/main/java/org/apache/accumulo/access/BytesWrapper.java: ########## @@ -131,4 +131,14 @@ public int hashCode() { public String toString() { return new String(data, offset, length, UTF_8); } + + public void set(byte[] data, int offset, int length) { Review Comment: Seems like the constructor should call this to avoid duplication. ########## src/main/java/org/apache/accumulo/access/ParserEvaluator.java: ########## @@ -0,0 +1,140 @@ +/* + * 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.apache.accumulo.access.ByteUtils.isAndOrOperator; + +import java.util.function.Predicate; + +/** + * Code for parsing and evaluating an access expression at the same time. + */ +final class ParserEvaluator { + + public static final byte OPEN_PAREN = (byte) '('; + public static final byte CLOSE_PAREN = (byte) ')'; + private static final byte[] EMPTY = new byte[0]; + private static final ThreadLocal<BytesWrapper> lookupWrappers = + ThreadLocal.withInitial(() -> new BytesWrapper(EMPTY)); + private static final ThreadLocal<Tokenizer> tokenizers = + ThreadLocal.withInitial(() -> new Tokenizer(EMPTY)); + + // TODO would be cleaner to have AccessEvaluatorImpl pass these in rather than use thread locals. + private static final Predicate<BytesWrapper> TRUE_AUTH_PREDICATE = bw -> true; + private static final Predicate<BytesWrapper> FALSE_AUTH_PREDICATE = bw -> false; + + public static boolean parseAccessExpression(byte[] expression, + Predicate<BytesWrapper> authorizedPredicate) { + + Tokenizer tokenizer = tokenizers.get(); + tokenizer.reset(expression); + + if (!tokenizer.hasNext()) { + return true; + } + + BytesWrapper lookupWrapper = lookupWrappers.get(); + var node = parseExpression(tokenizer, authorizedPredicate, lookupWrapper); + + if (tokenizer.hasNext()) { + // not all input was read, so not a valid expression + tokenizer.error("Unexpected character '" + (char) tokenizer.peek() + "'"); + } + + return node; + } + + private static boolean parseExpression(Tokenizer tokenizer, + Predicate<BytesWrapper> authorizedPredicate, BytesWrapper lookupWrapper) { + + boolean result = + parseParenExpressionOrAuthorization(tokenizer, authorizedPredicate, lookupWrapper); + + if (tokenizer.hasNext()) { + var operator = tokenizer.peek(); + if (operator == '&') { + result = parseAndExpression(result, tokenizer, authorizedPredicate, lookupWrapper); + if (tokenizer.hasNext() && isAndOrOperator(tokenizer.peek())) { + // A case of mixed operators, lets give a clear error message + tokenizer.error("Cannot mix '|' and '&'"); + } + } else if (operator == '|') { + result = parseOrExpression(result, tokenizer, authorizedPredicate, lookupWrapper); + if (tokenizer.hasNext() && isAndOrOperator(tokenizer.peek())) { + // A case of mixed operators, lets give a clear error message + tokenizer.error("Cannot mix '|' and '&'"); + } + } + } + + return result; + } + + private static boolean parseAndExpression(boolean result, Tokenizer tokenizer, + Predicate<BytesWrapper> authorizedPredicate, BytesWrapper lookupWrapper) { + do { + if (!result) { + // Once the "and" expression is false, can avoid doing set lookups and only validate the + // rest of the expression. + authorizedPredicate = FALSE_AUTH_PREDICATE; + } + tokenizer.advance(); + var nextResult = + parseParenExpressionOrAuthorization(tokenizer, authorizedPredicate, lookupWrapper); + result &= nextResult; + } while (tokenizer.hasNext() && tokenizer.peek() == '&'); Review Comment: Same comment here about byte to char translation ########## src/main/java/org/apache/accumulo/access/ParserEvaluator.java: ########## @@ -0,0 +1,140 @@ +/* + * 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.apache.accumulo.access.ByteUtils.isAndOrOperator; + +import java.util.function.Predicate; + +/** + * Code for parsing and evaluating an access expression at the same time. + */ +final class ParserEvaluator { + + public static final byte OPEN_PAREN = (byte) '('; + public static final byte CLOSE_PAREN = (byte) ')'; + private static final byte[] EMPTY = new byte[0]; + private static final ThreadLocal<BytesWrapper> lookupWrappers = + ThreadLocal.withInitial(() -> new BytesWrapper(EMPTY)); + private static final ThreadLocal<Tokenizer> tokenizers = + ThreadLocal.withInitial(() -> new Tokenizer(EMPTY)); + + // TODO would be cleaner to have AccessEvaluatorImpl pass these in rather than use thread locals. + private static final Predicate<BytesWrapper> TRUE_AUTH_PREDICATE = bw -> true; + private static final Predicate<BytesWrapper> FALSE_AUTH_PREDICATE = bw -> false; + + public static boolean parseAccessExpression(byte[] expression, + Predicate<BytesWrapper> authorizedPredicate) { + + Tokenizer tokenizer = tokenizers.get(); + tokenizer.reset(expression); + + if (!tokenizer.hasNext()) { + return true; + } + + BytesWrapper lookupWrapper = lookupWrappers.get(); + var node = parseExpression(tokenizer, authorizedPredicate, lookupWrapper); + + if (tokenizer.hasNext()) { + // not all input was read, so not a valid expression + tokenizer.error("Unexpected character '" + (char) tokenizer.peek() + "'"); + } + + return node; + } + + private static boolean parseExpression(Tokenizer tokenizer, + Predicate<BytesWrapper> authorizedPredicate, BytesWrapper lookupWrapper) { + + boolean result = + parseParenExpressionOrAuthorization(tokenizer, authorizedPredicate, lookupWrapper); + + if (tokenizer.hasNext()) { + var operator = tokenizer.peek(); + if (operator == '&') { + result = parseAndExpression(result, tokenizer, authorizedPredicate, lookupWrapper); + if (tokenizer.hasNext() && isAndOrOperator(tokenizer.peek())) { + // A case of mixed operators, lets give a clear error message + tokenizer.error("Cannot mix '|' and '&'"); + } + } else if (operator == '|') { + result = parseOrExpression(result, tokenizer, authorizedPredicate, lookupWrapper); + if (tokenizer.hasNext() && isAndOrOperator(tokenizer.peek())) { + // A case of mixed operators, lets give a clear error message + tokenizer.error("Cannot mix '|' and '&'"); + } + } + } + + return result; + } + + private static boolean parseAndExpression(boolean result, Tokenizer tokenizer, + Predicate<BytesWrapper> authorizedPredicate, BytesWrapper lookupWrapper) { + do { + if (!result) { + // Once the "and" expression is false, can avoid doing set lookups and only validate the + // rest of the expression. + authorizedPredicate = FALSE_AUTH_PREDICATE; + } + tokenizer.advance(); + var nextResult = + parseParenExpressionOrAuthorization(tokenizer, authorizedPredicate, lookupWrapper); + result &= nextResult; + } while (tokenizer.hasNext() && tokenizer.peek() == '&'); + return result; + } + + private static boolean parseOrExpression(boolean result, Tokenizer tokenizer, + Predicate<BytesWrapper> authorizedPredicate, BytesWrapper lookupWrapper) { + do { + if (result) { + // Once the "or" expression is true, can avoid doing set lookups and only validate the rest + // of the expression. + authorizedPredicate = TRUE_AUTH_PREDICATE; + } + tokenizer.advance(); + var nextResult = + parseParenExpressionOrAuthorization(tokenizer, authorizedPredicate, lookupWrapper); + result |= nextResult; + } while (tokenizer.hasNext() && tokenizer.peek() == '|'); Review Comment: Same comment here about byte to char translation -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
