You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by GitBox <gi...@apache.org> on 2022/06/17 09:49:47 UTC

[GitHub] [doris] morrySnow commented on a diff in pull request #10213: [fix](auth) Authentication exception when the name of database or table contains an underscore in grant statement.

morrySnow commented on code in PR #10213:
URL: https://github.com/apache/doris/pull/10213#discussion_r899900703


##########
fe/fe-core/src/main/java/org/apache/doris/common/PatternMatcher.java:
##########
@@ -20,27 +20,58 @@
 import com.google.common.base.Strings;
 import com.google.common.collect.Sets;
 
+import java.util.Locale;
 import java.util.Set;
 import java.util.regex.Pattern;
 
 // Wrap for Java pattern and matcher
 public class PatternMatcher {
+    public static PatternMatcher MATCH_ANY = new PatternMatcher(Pattern.compile(".*"));

Review Comment:
   could be final



##########
fe/fe-core/src/main/java/org/apache/doris/common/PatternMatcher.java:
##########
@@ -20,27 +20,58 @@
 import com.google.common.base.Strings;
 import com.google.common.collect.Sets;
 
+import java.util.Locale;
 import java.util.Set;
 import java.util.regex.Pattern;
 
 // Wrap for Java pattern and matcher
 public class PatternMatcher {
+    public static PatternMatcher MATCH_ANY = new PatternMatcher(Pattern.compile(".*"));
     private Pattern pattern;
+    private String casePattern;
+    private boolean caseSensitive;
 
     private static final Set<Character> FORBIDDEN_CHARS = Sets.newHashSet('<', '(', '[', '{', '^', '=',
                                                                           '$', '!', '|', ']', '}', ')',
                                                                           '?', '*', '+', '>', '@');
 
+    public PatternMatcher(Pattern pattern) {
+        this.pattern = pattern;
+    }
+
+    public PatternMatcher(String casePattern, boolean caseSensitive) {
+        this.casePattern = caseSensitive ? casePattern : casePattern.toLowerCase(Locale.ROOT);
+        this.caseSensitive = caseSensitive;
+    }
+
     public boolean match(String candidate) {
-        if (pattern == null || candidate == null) {
-            // No pattern, how can I explain this? Return false now.
-            // No candidate, return false.
+        if (candidate == null) {
             return false;
         }
-        if (pattern.matcher(candidate).matches()) {
-            return true;
+        if (pattern != null) {
+            return pattern.matcher(candidate).matches();
+        }
+        if (caseSensitive) {
+            return candidate.equals(casePattern);
+        } else {
+            return candidate.toLowerCase(Locale.ROOT).equals(casePattern);

Review Comment:
   casePattern always lowerCase?



##########
fe/fe-core/src/main/java/org/apache/doris/common/PatternMatcher.java:
##########
@@ -20,27 +20,58 @@
 import com.google.common.base.Strings;
 import com.google.common.collect.Sets;
 
+import java.util.Locale;
 import java.util.Set;
 import java.util.regex.Pattern;
 
 // Wrap for Java pattern and matcher
 public class PatternMatcher {
+    public static PatternMatcher MATCH_ANY = new PatternMatcher(Pattern.compile(".*"));
     private Pattern pattern;
+    private String casePattern;

Review Comment:
   why use a `String` instead of a `Pattern`?



##########
fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/PrivEntryTest.java:
##########
@@ -0,0 +1,51 @@
+// 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.doris.mysql.privilege;
+
+import org.apache.doris.analysis.UserIdentity;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class PrivEntryTest {
+    @Test
+    public void testPatternMatch() throws Exception {
+        TablePrivEntry tablePrivEntry = TablePrivEntry.create(
+                "127.%", "db_db1", "user1", "tbl_tbl1", false,
+                PrivBitSet.of(PaloPrivilege.SELECT_PRIV, PaloPrivilege.DROP_PRIV));
+        // pattern match
+        boolean dp = tablePrivEntry.getDbPattern().match("db-db1");
+        boolean tp = tablePrivEntry.getTblPattern().match("tbl-tbl1");
+        // create TablePrivTable
+        TablePrivTable tablePrivTable = new TablePrivTable();
+        tablePrivTable.addEntry(tablePrivEntry, false, false);
+        UserIdentity userIdentity = new UserIdentity("user1", "127.%", false);
+        userIdentity.setIsAnalyzed();
+
+        PrivBitSet privs1 = PrivBitSet.of();
+        tablePrivTable.getPrivs(userIdentity, "db#db1", "tbl#tbl1", privs1);
+        boolean canDrop1 = PaloPrivilege.satisfy(privs1, PrivPredicate.DROP);
+
+        PrivBitSet privs2 = PrivBitSet.of();
+        tablePrivTable.getPrivs(userIdentity, "db_db1", "tbl_tbl1", privs2);
+        boolean canDrop2 = PaloPrivilege.satisfy(privs2, PrivPredicate.DROP);
+
+        Assert.assertTrue(!dp && !tp && !canDrop1);

Review Comment:
   it is better to assert one by one



##########
fe/fe-core/src/main/java/org/apache/doris/common/PatternMatcher.java:
##########
@@ -20,27 +20,58 @@
 import com.google.common.base.Strings;
 import com.google.common.collect.Sets;
 
+import java.util.Locale;
 import java.util.Set;
 import java.util.regex.Pattern;
 
 // Wrap for Java pattern and matcher
 public class PatternMatcher {
+    public static PatternMatcher MATCH_ANY = new PatternMatcher(Pattern.compile(".*"));
     private Pattern pattern;
+    private String casePattern;
+    private boolean caseSensitive;
 
     private static final Set<Character> FORBIDDEN_CHARS = Sets.newHashSet('<', '(', '[', '{', '^', '=',
                                                                           '$', '!', '|', ']', '}', ')',
                                                                           '?', '*', '+', '>', '@');
 
+    public PatternMatcher(Pattern pattern) {
+        this.pattern = pattern;
+    }
+
+    public PatternMatcher(String casePattern, boolean caseSensitive) {
+        this.casePattern = caseSensitive ? casePattern : casePattern.toLowerCase(Locale.ROOT);
+        this.caseSensitive = caseSensitive;
+    }
+
     public boolean match(String candidate) {
-        if (pattern == null || candidate == null) {
-            // No pattern, how can I explain this? Return false now.
-            // No candidate, return false.
+        if (candidate == null) {
             return false;
         }
-        if (pattern.matcher(candidate).matches()) {
-            return true;
+        if (pattern != null) {
+            return pattern.matcher(candidate).matches();
+        }
+        if (caseSensitive) {
+            return candidate.equals(casePattern);
+        } else {
+            return candidate.toLowerCase(Locale.ROOT).equals(casePattern);
+        }
+    }
+
+    public static PatternMatcher createCasePattern(String casePattern, boolean caseSensitive) {
+        return createCasePattern(casePattern, caseSensitive, false);
+    }
+
+    /**
+     * In grant operation, database and table support matching a single wildcard,
+     * the other cases are string case-sensitive equivalent matching.
+     */
+    public static PatternMatcher createCasePattern(

Review Comment:
   what's mean about 'case' in function name?



##########
fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/PrivEntryTest.java:
##########
@@ -0,0 +1,51 @@
+// 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.doris.mysql.privilege;
+
+import org.apache.doris.analysis.UserIdentity;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class PrivEntryTest {
+    @Test
+    public void testPatternMatch() throws Exception {

Review Comment:
   add a java doc comment or change the function name to to explicitly state that the test is for names that contain underscores



-- 
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: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org