Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public static 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());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* 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_INSTANCE = "trino_instance";
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_instance: <instanceName>
Comment thread
kumaab marked this conversation as resolved.
Outdated
* 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)) {
Comment thread
kumaab marked this conversation as resolved.
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_INSTANCE) && StringUtils.equals(resourceStr, qualifiedName)) {
Comment thread
kumaab marked this conversation as resolved.
Outdated
elements.put(ENTITY_TYPE_TRINO_INSTANCE, new RangerPolicyResource(resourceStr)); // there is no mapping for trino_instance in Ranger
Comment thread
kumaab marked this conversation as resolved.
Outdated
} else 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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
/*
* 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_INSTANCE;
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 INSTANCE_QUALIFIED_NAME = "dev";
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
Comment thread
kumaab marked this conversation as resolved.
public void testTrinoInstance() throws Exception {
Comment thread
kumaab marked this conversation as resolved.
Outdated
RangerAtlasEntity entity = getEntity(ENTITY_TYPE_TRINO_INSTANCE, INSTANCE_QUALIFIED_NAME);
RangerServiceResource resource = resourceMapper.buildResource(entity);

Assertions.assertEquals(SERVICE_NAME, resource.getServiceName());
assertInstanceResource(resource);
}

@Test
public void testTrinoInstanceUsesQualifiedNameAsServiceNameSource() throws Exception {
RangerAtlasEntity entity = getEntity(ENTITY_TYPE_TRINO_INSTANCE, INSTANCE_QUALIFIED_NAME);
RangerServiceResource resource = resourceMapper.buildResource(entity);

Assertions.assertEquals("dev_trino", resource.getServiceName());
assertResourceElementCount(resource, 1);
assertResourceElementValue(resource, ENTITY_TYPE_TRINO_INSTANCE, INSTANCE_QUALIFIED_NAME);
}

@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");
}

@Test
public void testInvalidInstanceEntity() {
assertException(getEntity(ENTITY_TYPE_TRINO_INSTANCE, null), "attribute 'qualifiedName' not found");
assertException(getEntity(ENTITY_TYPE_TRINO_INSTANCE, ""), "attribute 'qualifiedName' not found");
assertException(getEntity(ENTITY_TYPE_TRINO_INSTANCE, CATALOG_QUALIFIED_NAME), "unrecognized entity-type");
}

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 assertInstanceResource(RangerServiceResource resource) {
assertResourceElementCount(resource, 1);
assertResourceElementValue(resource, ENTITY_TYPE_TRINO_INSTANCE, INSTANCE_QUALIFIED_NAME);
}

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());
}
}
}
Loading