mneethiraj commented on code in PR #659:
URL: https://github.com/apache/ranger/pull/659#discussion_r2380515356


##########
authz-api/src/main/java/org/apache/ranger/authz/util/RangerResourceTemplate.java:
##########
@@ -0,0 +1,184 @@
+/*
+ * 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.ranger.authz.util;
+
+import org.apache.ranger.authz.api.RangerAuthzException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static 
org.apache.ranger.authz.api.RangerAuthzApiErrorCode.INVALID_RESOURCE_TEMPLATE_UNEXPECTED_MARKER_AT;
+
+public class RangerResourceTemplate {
+    private static final Logger LOG = 
LoggerFactory.getLogger(RangerResourceTemplate.class);
+
+    private static final char RESOURCE_START_CHAR = '{';
+    private static final char RESOURCE_END_CHAR   = '}';
+    private static final char ESCAPE_CHAR         = '\\';
+
+    private final String   template;   // examples: 
{database}.{table}.{column}, ofs://{bucket}/{volume}/{path}
+    private final String[] resources;  // examples: [database, table, column], 
  [bucket, volume, path]
+    private final String   prefix;     // examples: "",                        
  "ofs://"
+    private final String[] separators; // examples: [".", "."],                
  ["/", "/"]
+    private final String   suffix;
+
+    public RangerResourceTemplate(String template) throws RangerAuthzException 
{
+        List<String>  resources    = new ArrayList<>();
+        List<String>  separators   = new ArrayList<>();
+        String        prefix       = null;
+        boolean       isInEscape   = false;
+        boolean       isInResource = false;
+        StringBuilder currentToken = new StringBuilder();
+
+        for (int i = 0; i < template.length(); i++) {
+            char c = template.charAt(i);
+
+            if (isInEscape) {
+                currentToken.append(c);
+
+                isInEscape = false;
+            } else if (c == ESCAPE_CHAR) {
+                isInEscape = true;
+            } else if (c == RESOURCE_START_CHAR) {
+                if (isInResource) {
+                    throw new 
RangerAuthzException(INVALID_RESOURCE_TEMPLATE_UNEXPECTED_MARKER_AT, template, 
RESOURCE_START_CHAR, i);
+                }
+
+                if (prefix == null) {
+                    prefix = currentToken.toString();
+                } else {
+                    separators.add(currentToken.toString());
+                }
+
+                isInResource = true;
+
+                currentToken.setLength(0);
+            } else if (c == RESOURCE_END_CHAR) {
+                if (!isInResource) {
+                    throw new 
RangerAuthzException(INVALID_RESOURCE_TEMPLATE_UNEXPECTED_MARKER_AT, template, 
RESOURCE_END_CHAR, i);
+                }
+
+                resources.add(currentToken.toString());
+
+                isInResource = false;
+
+                currentToken.setLength(0);
+            } else {
+                currentToken.append(c);
+            }
+        }
+
+        this.template   = template;
+        this.resources  = resources.toArray(new String[0]);
+        this.separators = separators.toArray(new String[0]);
+        this.prefix     = prefix;
+        this.suffix     = currentToken.toString();
+    }
+
+    public String getTemplate() {
+        return template;
+    }
+
+    public Map<String, String> parse(String resource) {
+        Map<String, String> ret = null;
+
+        if (resource == null || resource.isEmpty()) {
+            LOG.debug("parse(resource='{}', template='{}'): empty or null 
resource", resource, template);
+        } else if (!resource.startsWith(prefix)) {
+            LOG.debug("parse(resource='{}', template='{}'): resource does not 
start with prefix {}", resource, template, prefix);
+        } else if (!resource.endsWith(suffix)) {
+            LOG.debug("parse(resource='{}', template='{}'): resource does not 
end with suffix {}", resource, template, suffix);
+        } else {
+            ret = new HashMap<>();
+
+            int tokenStartPos = prefix.length();
+
+            for (int i = 0; i < this.separators.length; i++) {
+                String sep    = this.separators[i];
+                int    idxSep = resource.indexOf(sep, tokenStartPos);

Review Comment:
   > Wouldn't it be better to limit the separator to be a single string in a 
template instead of a list of strings as we have in our current implementation
   
   Resources like, ADLS-Gen2 paths have format 
container-name@account-name/path/to/file.txt; hence the template should be 
flexible enough to handle such resources. 
   
   > Consider a template: {a}@{b}.{c}
   > Lets consider a resource {abc.def}@{pqr.stu}.{xyz}
   
   This is not the valid resource string. Given "." is the separator between 
resources `b` and `c`, resource `b` can not have the separator in its value. 



-- 
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]

Reply via email to