You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ranger.apache.org by ab...@apache.org on 2026/03/28 01:23:05 UTC

(ranger) branch ranger-2.8 updated: RANGER-5522: update tagsync to process Atlas notifications for Trino entities (#895)

This is an automated email from the ASF dual-hosted git repository.

abhi pushed a commit to branch ranger-2.8
in repository https://gitbox.apache.org/repos/asf/ranger.git


The following commit(s) were added to refs/heads/ranger-2.8 by this push:
     new eea4acd3e RANGER-5522: update tagsync to process Atlas notifications for Trino entities (#895)
eea4acd3e is described below

commit eea4acd3e81466fcaaf47d5c8e29e71e60c62141
Author: Abhishek Kumar <ab...@apache.org>
AuthorDate: Fri Mar 27 18:00:14 2026 -0700

    RANGER-5522: update tagsync to process Atlas notifications for Trino entities (#895)
    
    (cherry picked from commit f180bee5bd45852b54a4357666e0b3b75517392a)
    (cherry picked from commit 9fb79297d06fc8bb8d3f381c27e8ca0cd4ffc773)
---
 .../source/atlas/AtlasResourceMapperUtil.java      |   1 +
 .../source/atlas/AtlasTrinoResourceMapper.java     | 122 ++++++++++++++
 .../tagsync/process/TestTrinoResourceMapper.java   | 179 +++++++++++++++++++++
 3 files changed, 302 insertions(+)

diff --git a/tagsync/src/main/java/org/apache/ranger/tagsync/source/atlas/AtlasResourceMapperUtil.java b/tagsync/src/main/java/org/apache/ranger/tagsync/source/atlas/AtlasResourceMapperUtil.java
index 177938c6a..867c786b6 100644
--- a/tagsync/src/main/java/org/apache/ranger/tagsync/source/atlas/AtlasResourceMapperUtil.java
+++ b/tagsync/src/main/java/org/apache/ranger/tagsync/source/atlas/AtlasResourceMapperUtil.java
@@ -90,6 +90,7 @@ static public boolean initializeAtlasResourceMappers(Properties properties) {
 		mapperNames.add("org.apache.ranger.tagsync.source.atlas.AtlasHbaseResourceMapper");
 		mapperNames.add("org.apache.ranger.tagsync.source.atlas.AtlasKafkaResourceMapper");
 		mapperNames.add("org.apache.ranger.tagsync.source.atlas.AtlasOzoneResourceMapper");
+		mapperNames.add("org.apache.ranger.tagsync.source.atlas.AtlasTrinoResourceMapper");
 
 		mapperNames.add(AtlasAdlsResourceMapper.class.getName());
 
diff --git a/tagsync/src/main/java/org/apache/ranger/tagsync/source/atlas/AtlasTrinoResourceMapper.java b/tagsync/src/main/java/org/apache/ranger/tagsync/source/atlas/AtlasTrinoResourceMapper.java
new file mode 100644
index 000000000..db9d25212
--- /dev/null
+++ b/tagsync/src/main/java/org/apache/ranger/tagsync/source/atlas/AtlasTrinoResourceMapper.java
@@ -0,0 +1,122 @@
+/*
+ * 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.tagsync.source.atlas;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource;
+import org.apache.ranger.plugin.model.RangerServiceResource;
+import org.apache.ranger.tagsync.source.atlasrest.RangerAtlasEntity;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class AtlasTrinoResourceMapper extends AtlasResourceMapper {
+    public static final String ENTITY_TYPE_TRINO_CATALOG  = "trino_catalog";
+    public static final String ENTITY_TYPE_TRINO_SCHEMA   = "trino_schema";
+    public static final String ENTITY_TYPE_TRINO_TABLE    = "trino_table";
+    public static final String ENTITY_TYPE_TRINO_COLUMN   = "trino_column";
+    public static final String RANGER_TYPE_TRINO_CATALOG  = "catalog";
+    public static final String RANGER_TYPE_TRINO_SCHEMA   = "schema";
+    public static final String RANGER_TYPE_TRINO_TABLE    = "table";
+    public static final String RANGER_TYPE_TRINO_COLUMN   = "column";
+
+    public static final String[] SUPPORTED_ENTITY_TYPES = {
+            ENTITY_TYPE_TRINO_CATALOG,
+            ENTITY_TYPE_TRINO_SCHEMA,
+            ENTITY_TYPE_TRINO_TABLE,
+            ENTITY_TYPE_TRINO_COLUMN
+    };
+
+    public AtlasTrinoResourceMapper() {
+        super("trino", SUPPORTED_ENTITY_TYPES);
+    }
+
+    /*
+     * qualifiedName can be of format, depending upon the entity-type:
+     * trino_catalog:  <catalog>@<instanceName>
+     * trino_schema:   <catalog>.<schema>@<instanceName>
+     * trino_table:    <catalog>.<schema>.<table>@<instanceName>
+     * trino_column:   <catalog>.<schema>.<table>.<column>@<instanceName>
+     */
+    @Override
+    public RangerServiceResource buildResource(RangerAtlasEntity entity) throws Exception {
+        String qualifiedName = (String) entity.getAttributes().get(AtlasResourceMapper.ENTITY_ATTRIBUTE_QUALIFIED_NAME);
+
+        if (StringUtils.isEmpty(qualifiedName)) {
+            throw new Exception("attribute '" + ENTITY_ATTRIBUTE_QUALIFIED_NAME + "' not found in entity");
+        }
+
+        String entityType  = entity.getTypeName();
+        String entityGuid  = entity.getGuid();
+        String resourceStr = getResourceNameFromQualifiedName(qualifiedName);
+
+        if (StringUtils.isEmpty(resourceStr)) {
+            throwExceptionWithMessage("resource not found in attribute '" + ENTITY_ATTRIBUTE_QUALIFIED_NAME + "': " + qualifiedName);
+        }
+
+        String trinoInstance = getClusterNameFromQualifiedName(qualifiedName);
+        if (StringUtils.equals(resourceStr, qualifiedName)) {
+            trinoInstance = resourceStr;
+        }
+
+        if (StringUtils.isEmpty(trinoInstance)) {
+            throwExceptionWithMessage("trino-instance not found in attribute '" + ENTITY_ATTRIBUTE_QUALIFIED_NAME + "': " + qualifiedName);
+        }
+        String serviceName = getRangerServiceName(trinoInstance);
+
+        String[] parts = resourceStr.split(QUALIFIED_NAME_DELIMITER);
+        if (parts.length < 1 || parts.length > 4) {
+            throwExceptionWithMessage("invalid resource format in attribute '" + ENTITY_ATTRIBUTE_QUALIFIED_NAME + "': " + qualifiedName);
+        }
+
+        Map<String, RangerPolicyResource> elements = new HashMap<>();
+        if (StringUtils.equals(entityType, ENTITY_TYPE_TRINO_CATALOG)) {
+            if (parts.length == 1 && StringUtils.isNotEmpty(parts[0])) {
+                elements.put(RANGER_TYPE_TRINO_CATALOG, new RangerPolicyResource(parts[0]));
+            }
+        } else if (StringUtils.equals(entityType, ENTITY_TYPE_TRINO_SCHEMA)) {
+            if (parts.length == 2 && StringUtils.isNotEmpty(parts[0]) && StringUtils.isNotEmpty(parts[1])) {
+                elements.put(RANGER_TYPE_TRINO_CATALOG, new RangerPolicyResource(parts[0]));
+                elements.put(RANGER_TYPE_TRINO_SCHEMA, new RangerPolicyResource(parts[1]));
+            }
+        } else if (StringUtils.equals(entityType, ENTITY_TYPE_TRINO_TABLE)) {
+            if (parts.length == 3 && StringUtils.isNotEmpty(parts[0]) && StringUtils.isNotEmpty(parts[1]) && StringUtils.isNotEmpty(parts[2])) {
+                elements.put(RANGER_TYPE_TRINO_CATALOG, new RangerPolicyResource(parts[0]));
+                elements.put(RANGER_TYPE_TRINO_SCHEMA, new RangerPolicyResource(parts[1]));
+                elements.put(RANGER_TYPE_TRINO_TABLE, new RangerPolicyResource(parts[2]));
+            }
+        } else if (StringUtils.equals(entityType, ENTITY_TYPE_TRINO_COLUMN)) {
+            if (parts.length == 4 && StringUtils.isNotEmpty(parts[0]) && StringUtils.isNotEmpty(parts[1]) && StringUtils.isNotEmpty(parts[2]) && StringUtils.isNotEmpty(parts[3])) {
+                elements.put(RANGER_TYPE_TRINO_CATALOG, new RangerPolicyResource(parts[0]));
+                elements.put(RANGER_TYPE_TRINO_SCHEMA, new RangerPolicyResource(parts[1]));
+                elements.put(RANGER_TYPE_TRINO_TABLE, new RangerPolicyResource(parts[2]));
+                elements.put(RANGER_TYPE_TRINO_COLUMN, new RangerPolicyResource(parts[3]));
+            }
+        } else {
+            throwExceptionWithMessage("unrecognized entity-type: " + entityType);
+        }
+
+        if (elements.isEmpty()) {
+            throwExceptionWithMessage("invalid qualifiedName for entity-type '" + entityType + "': " + qualifiedName);
+        }
+
+        return new RangerServiceResource(entityGuid, serviceName, elements);
+    }
+}
diff --git a/tagsync/src/test/java/org/apache/ranger/tagsync/process/TestTrinoResourceMapper.java b/tagsync/src/test/java/org/apache/ranger/tagsync/process/TestTrinoResourceMapper.java
new file mode 100644
index 000000000..111e3b560
--- /dev/null
+++ b/tagsync/src/test/java/org/apache/ranger/tagsync/process/TestTrinoResourceMapper.java
@@ -0,0 +1,179 @@
+/*
+ * 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.tagsync.process;
+
+import org.apache.ranger.plugin.model.RangerServiceResource;
+import org.apache.ranger.tagsync.source.atlas.AtlasTrinoResourceMapper;
+import org.apache.ranger.tagsync.source.atlasrest.RangerAtlasEntity;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+
+import static org.apache.ranger.tagsync.source.atlas.AtlasResourceMapper.ENTITY_ATTRIBUTE_QUALIFIED_NAME;
+import static org.apache.ranger.tagsync.source.atlas.AtlasTrinoResourceMapper.ENTITY_TYPE_TRINO_CATALOG;
+import static org.apache.ranger.tagsync.source.atlas.AtlasTrinoResourceMapper.ENTITY_TYPE_TRINO_COLUMN;
+import static org.apache.ranger.tagsync.source.atlas.AtlasTrinoResourceMapper.ENTITY_TYPE_TRINO_SCHEMA;
+import static org.apache.ranger.tagsync.source.atlas.AtlasTrinoResourceMapper.ENTITY_TYPE_TRINO_TABLE;
+import static org.apache.ranger.tagsync.source.atlas.AtlasTrinoResourceMapper.RANGER_TYPE_TRINO_CATALOG;
+import static org.apache.ranger.tagsync.source.atlas.AtlasTrinoResourceMapper.RANGER_TYPE_TRINO_COLUMN;
+import static org.apache.ranger.tagsync.source.atlas.AtlasTrinoResourceMapper.RANGER_TYPE_TRINO_SCHEMA;
+import static org.apache.ranger.tagsync.source.atlas.AtlasTrinoResourceMapper.RANGER_TYPE_TRINO_TABLE;
+
+public class TestTrinoResourceMapper {
+    private static final String CATALOG_QUALIFIED_NAME          = "sales@dev";
+    private static final String SCHEMA_QUALIFIED_NAME           = "sales.reporting@dev";
+    private static final String TABLE_QUALIFIED_NAME            = "sales.reporting.orders@dev";
+    private static final String COLUMN_QUALIFIED_NAME           = "sales.reporting.orders.customer_id@dev";
+    private static final String INVALID_RESOURCE_QUALIFIED_NAME = "sales.reporting.orders.customer_id.extra@dev";
+
+    private static final String SERVICE_NAME    = "dev_trino";
+    private static final String RANGER_CATALOG  = "sales";
+    private static final String RANGER_SCHEMA   = "reporting";
+    private static final String RANGER_TABLE    = "orders";
+    private static final String RANGER_COLUMN   = "customer_id";
+
+    AtlasTrinoResourceMapper resourceMapper = new AtlasTrinoResourceMapper();
+
+    @Test
+    public void testTrinoCatalog() throws Exception {
+        RangerAtlasEntity     entity   = getEntity(ENTITY_TYPE_TRINO_CATALOG, CATALOG_QUALIFIED_NAME);
+        RangerServiceResource resource = resourceMapper.buildResource(entity);
+
+        Assertions.assertEquals(SERVICE_NAME, resource.getServiceName());
+        assertCatalogResource(resource);
+    }
+
+    @Test
+    public void testTrinoSchema() throws Exception {
+        RangerAtlasEntity     entity   = getEntity(ENTITY_TYPE_TRINO_SCHEMA, SCHEMA_QUALIFIED_NAME);
+        RangerServiceResource resource = resourceMapper.buildResource(entity);
+
+        Assertions.assertEquals(SERVICE_NAME, resource.getServiceName());
+        assertSchemaResource(resource);
+    }
+
+    @Test
+    public void testTrinoTable() throws Exception {
+        RangerAtlasEntity     entity   = getEntity(ENTITY_TYPE_TRINO_TABLE, TABLE_QUALIFIED_NAME);
+        RangerServiceResource resource = resourceMapper.buildResource(entity);
+
+        Assertions.assertEquals(SERVICE_NAME, resource.getServiceName());
+        assertTableResource(resource);
+    }
+
+    @Test
+    public void testTrinoColumn() throws Exception {
+        RangerAtlasEntity     entity   = getEntity(ENTITY_TYPE_TRINO_COLUMN, COLUMN_QUALIFIED_NAME);
+        RangerServiceResource resource = resourceMapper.buildResource(entity);
+
+        Assertions.assertEquals(SERVICE_NAME, resource.getServiceName());
+        assertColumnResource(resource);
+    }
+
+    @Test
+    public void testInvalidCatalogEntity() {
+        assertException(getEntity(ENTITY_TYPE_TRINO_CATALOG, null), "attribute 'qualifiedName' not found");
+        assertException(getEntity(ENTITY_TYPE_TRINO_CATALOG, ""), "attribute 'qualifiedName' not found");
+    }
+
+    @Test
+    public void testInvalidSchemaEntity() {
+        assertException(getEntity(ENTITY_TYPE_TRINO_SCHEMA, null), "attribute 'qualifiedName' not found");
+        assertException(getEntity(ENTITY_TYPE_TRINO_SCHEMA, ""), "attribute 'qualifiedName' not found");
+        assertException(getEntity(ENTITY_TYPE_TRINO_SCHEMA, CATALOG_QUALIFIED_NAME), "invalid qualifiedName");
+    }
+
+    @Test
+    public void testInvalidTableEntity() throws Exception {
+        assertException(getEntity(ENTITY_TYPE_TRINO_TABLE, null), "attribute 'qualifiedName' not found");
+        assertException(getEntity(ENTITY_TYPE_TRINO_TABLE, ""), "attribute 'qualifiedName' not found");
+        assertException(getEntity(ENTITY_TYPE_TRINO_TABLE, SCHEMA_QUALIFIED_NAME), "invalid qualifiedName");
+
+        RangerServiceResource resource = resourceMapper.buildResource(getEntity(ENTITY_TYPE_TRINO_TABLE, "sales.reporting.orders"));
+        Assertions.assertNotEquals(1, resource.getResourceElements().size());
+    }
+
+    @Test
+    public void testInvalidColumnEntity() throws Exception {
+        assertException(getEntity(ENTITY_TYPE_TRINO_COLUMN, null), "attribute 'qualifiedName' not found");
+        assertException(getEntity(ENTITY_TYPE_TRINO_COLUMN, ""), "attribute 'qualifiedName' not found");
+        assertException(getEntity(ENTITY_TYPE_TRINO_COLUMN, TABLE_QUALIFIED_NAME), "invalid qualifiedName");
+
+        RangerServiceResource resource = resourceMapper.buildResource(getEntity(ENTITY_TYPE_TRINO_COLUMN, "sales.reporting.orders.customer_id"));
+        Assertions.assertNotEquals(1, resource.getResourceElements().size());
+
+        assertException(getEntity(ENTITY_TYPE_TRINO_COLUMN, INVALID_RESOURCE_QUALIFIED_NAME), "invalid resource format");
+    }
+
+    private RangerAtlasEntity getEntity(String entityType, String qualifiedName) {
+        return new RangerAtlasEntity(entityType, "guid-" + entityType, Collections.singletonMap(ENTITY_ATTRIBUTE_QUALIFIED_NAME, qualifiedName));
+    }
+
+    private void assertResourceElementCount(RangerServiceResource resource, int count) {
+        Assertions.assertNotNull(resource);
+        Assertions.assertEquals(SERVICE_NAME, resource.getServiceName());
+        Assertions.assertNotNull(resource.getResourceElements());
+        Assertions.assertEquals(count, resource.getResourceElements().size());
+    }
+
+    private void assertCatalogResource(RangerServiceResource resource) {
+        assertResourceElementCount(resource, 1);
+        assertResourceElementValue(resource, RANGER_TYPE_TRINO_CATALOG, RANGER_CATALOG);
+    }
+
+    private void assertSchemaResource(RangerServiceResource resource) {
+        assertResourceElementCount(resource, 2);
+        assertResourceElementValue(resource, RANGER_TYPE_TRINO_CATALOG, RANGER_CATALOG);
+        assertResourceElementValue(resource, RANGER_TYPE_TRINO_SCHEMA, RANGER_SCHEMA);
+    }
+
+    private void assertTableResource(RangerServiceResource resource) {
+        assertResourceElementCount(resource, 3);
+        assertResourceElementValue(resource, RANGER_TYPE_TRINO_CATALOG, RANGER_CATALOG);
+        assertResourceElementValue(resource, RANGER_TYPE_TRINO_SCHEMA, RANGER_SCHEMA);
+        assertResourceElementValue(resource, RANGER_TYPE_TRINO_TABLE, RANGER_TABLE);
+    }
+
+    private void assertColumnResource(RangerServiceResource resource) {
+        assertResourceElementCount(resource, 4);
+        assertResourceElementValue(resource, RANGER_TYPE_TRINO_CATALOG, RANGER_CATALOG);
+        assertResourceElementValue(resource, RANGER_TYPE_TRINO_SCHEMA, RANGER_SCHEMA);
+        assertResourceElementValue(resource, RANGER_TYPE_TRINO_TABLE, RANGER_TABLE);
+        assertResourceElementValue(resource, RANGER_TYPE_TRINO_COLUMN, RANGER_COLUMN);
+    }
+
+    private void assertResourceElementValue(RangerServiceResource resource, String resourceName, String value) {
+        Assertions.assertTrue(resource.getResourceElements().containsKey(resourceName));
+        Assertions.assertNotNull(resource.getResourceElements().get(resourceName).getValues());
+        Assertions.assertEquals(1, resource.getResourceElements().get(resourceName).getValues().size());
+        Assertions.assertEquals(value, resource.getResourceElements().get(resourceName).getValues().get(0));
+    }
+
+    private void assertException(RangerAtlasEntity entity, String exceptionMessage) {
+        try {
+            RangerServiceResource resource = resourceMapper.buildResource(entity);
+
+            Assertions.fail("Expected buildResource() to fail. But it returned " + resource);
+        } catch (Exception excp) {
+            Assertions.assertTrue(excp.getMessage().startsWith(exceptionMessage), "Unexpected exception message: expected=" + exceptionMessage + "; found " + excp.getMessage());
+        }
+    }
+}