jerryshao commented on code in PR #13478:
URL: https://github.com/apache/gravitino/pull/13478#discussion_r4255248534
##########
core/src/main/java/org/apache/gravitino/policy/PolicyManager.java:
##########
@@ -341,6 +343,7 @@ private PolicyEntity updatePolicyEntity(PolicyEntity
policyEntity, PolicyChange.
}
newContent.validate();
+ validateReadRestrictionContent(policyEntity.policyType(), newContent);
Review Comment:
[Important] The merge resolution hoisted read-restriction validation out of
the `UpdateContent` arm, so a rename-only or comment-only alter now re-parses
the stored expression.
At 8d046c5 and still at 94563da, this call sat inside `else if (change
instanceof PolicyChange.UpdateContent)`, immediately after `newContent =
updateContent.getContent()` (`git show
94563da:core/src/main/java/org/apache/gravitino/policy/PolicyManager.java` line
338), so it only ever saw an expression the caller had just supplied. The final
merge 061713c moved it out of the loop, next to upstream's new
`newContent.validate()`. `newContent` is initialised to
`policyEntity.content()` at :310, so for `alterPolicy(metalake, name,
PolicyChange.rename("new_name"))` the loop never reassigns it and the
already-stored expression is parsed again.
That reintroduces exactly what @yuqi1129 raised in the first round.
`ROW_FILTER` and `COLUMN_MASK` already exist on main and accepted any non-blank
string before this PR, so a policy stored by an older server - or any policy
whose expression stops parsing after a future grammar tightening - can no
longer be renamed or re-commented: `alterPolicy` fails with the parser's
`IllegalArgumentException`, which `PolicyExceptionHandler` turns into a 400.
Delete and enable/disable are unaffected (`deletePolicy` at :185 and
`changePolicyEnabledState` at :244-261 never go through `updatePolicyEntity`),
so the alter path is the only regression.
Putting the call back under the `UpdateContent` arm, or guarding it with
`newContent != policyEntity.content()`, restores the behaviour the earlier
round asked for. A rename test on a ROW_FILTER policy whose stored content was
written directly to the entity store would pin it.
Verified by: read `updatePolicyEntity` at HEAD (:307-353) and compared with
`git show 8d046c5:` and `git show 94563da:` of the same file, and with `git
show origin/main:` (upstream has only `newContent.validate()` at that point, no
read-restriction call); traced the rename path from `alterPolicy` (:151-171)
and confirmed the enable/disable and delete paths bypass it.
##########
core/src/test/java/org/apache/gravitino/policy/rego/TestRestrictedRegoLexer.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.gravitino.policy.rego;
+
+import java.util.List;
+import org.apache.gravitino.policy.rego.RestrictedRegoLexer.Token;
+import org.apache.gravitino.policy.rego.RestrictedRegoLexer.TokenType;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/** Tests for {@link RestrictedRegoLexer}. */
+public class TestRestrictedRegoLexer {
+
+ @Test
+ void testTokenizesSourceWithOneBasedLocations() {
+ RestrictedRegoLexer lexer = new RestrictedRegoLexer("filter :=\n
col(\"x\") >= -1");
+
+ assertToken(lexer.nextToken(), TokenType.FILTER, "filter", 1, 1);
+ assertToken(lexer.nextToken(), TokenType.ASSIGN, ":=", 1, 8);
+ assertToken(lexer.nextToken(), TokenType.COL, "col", 2, 2);
+ assertToken(lexer.nextToken(), TokenType.LPAREN, "(", 2, 5);
+ assertToken(lexer.nextToken(), TokenType.STRING, "\"x\"", 2, 6);
+ assertToken(lexer.nextToken(), TokenType.RPAREN, ")", 2, 9);
+ assertToken(lexer.nextToken(), TokenType.GTE, ">=", 2, 11);
+ assertToken(lexer.nextToken(), TokenType.NUMBER, "-1", 2, 14);
+ assertToken(lexer.nextToken(), TokenType.EOF, "", 2, 16);
+ }
+
+ @Test
+ void testRejectsInvalidUnicodeEscapeDigits() {
+ List<String> invalidEscapes =
+ List.of(
+ RestrictedRegoTestUtils.unicodeEscape(0x0660, 0x0660, 0x0664,
0x0661),
+ RestrictedRegoTestUtils.unicodeEscape(0xFF10, 0xFF10, 0xFF14,
0xFF11),
+ RestrictedRegoTestUtils.unicodeEscape(0x0966, 0x0966, 0x096A,
0x0967),
+ RestrictedRegoTestUtils.unicodeEscape('0', '0', 0x0664, 0x0661));
+
+ for (String invalidEscape : invalidEscapes) {
+ IllegalArgumentException error =
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () -> new RestrictedRegoLexer("\"" + invalidEscape +
"\"").nextToken());
Review Comment:
[Nit] The Unicode-escape regression test now drives the lexer only, so
nothing asserts the end-to-end rejection any more.
Before the split the same four cases went through
`parseFilterExpression("col(\"" + invalidEscape + "\") == \"x\"")`, i.e. the
full facade path. Here they call `new RestrictedRegoLexer(...).nextToken()`
directly. The behaviour is unchanged today - I re-ran all four (Arabic-Indic,
fullwidth, Devanagari and the mixed `\u00` + Arabic-Indic case) through
`RestrictedRegoExpressionParserFacade.validateRowFilter` on this head and each
still fails with `invalid Unicode escape in string` at line 1, column 15 - but
the guarantee the first-round fix established is now only pinned at one of its
two layers.
The second layer is `RestrictedRegoSourceParser.decodeString` (:329-335, the
`isAsciiHexDigit` check inside `case 'u'`). It is unreachable through the
facade, because `RestrictedRegoLexer.java:114-119` rejects any non-ASCII-hex
digit while scanning the string token, so no test can reach it and nothing
would fail if it were dropped. Keeping one facade-level assertion alongside the
lexer one costs a line and restores the end-to-end claim; the golden file would
do it just as well, since this rejection is not among its 19 cases.
Verified by: diffed this method against `git show
8d046c5:core/src/test/java/org/apache/gravitino/policy/rego/TestRestrictedRegoExpressionParserFacade.java`,
read both hex guards on this head, and re-ran the four escapes through the
facade in a standalone build of the package.
--
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]