Search in sources :

Example 1 with RangerServiceResource

use of org.apache.ranger.plugin.model.RangerServiceResource in project ranger by apache.

the class AtlasHdfsResourceMapper method buildResource.

@Override
public RangerServiceResource buildResource(final RangerAtlasEntity entity) throws Exception {
    String path = (String) entity.getAttributes().get(ENTITY_ATTRIBUTE_PATH);
    String clusterName = (String) entity.getAttributes().get(ENTITY_ATTRIBUTE_CLUSTER_NAME);
    String qualifiedName = (String) entity.getAttributes().get(AtlasResourceMapper.ENTITY_ATTRIBUTE_QUALIFIED_NAME);
    String nameServiceId = (String) entity.getAttributes().get(ENTITY_ATTRIBUTE_NAME_SERVICE_ID);
    if (StringUtils.isEmpty(path)) {
        path = getResourceNameFromQualifiedName(qualifiedName);
        if (StringUtils.isEmpty(path)) {
            throwExceptionWithMessage("path not found in attribute '" + ENTITY_ATTRIBUTE_PATH + "' or '" + ENTITY_ATTRIBUTE_QUALIFIED_NAME + "'");
        }
    }
    if (StringUtils.isEmpty(clusterName)) {
        clusterName = getClusterNameFromQualifiedName(qualifiedName);
        if (StringUtils.isEmpty(clusterName)) {
            clusterName = defaultClusterName;
        }
        if (StringUtils.isEmpty(clusterName)) {
            throwExceptionWithMessage("attributes " + ENTITY_ATTRIBUTE_CLUSTER_NAME + ", " + ENTITY_ATTRIBUTE_QUALIFIED_NAME + "' not found in entity");
        }
    }
    String entityGuid = entity.getGuid();
    String serviceName = StringUtils.isNotBlank(nameServiceId) ? getCustomRangerServiceNameForClusterAndNameService(clusterName, nameServiceId) : getRangerServiceName(clusterName);
    Boolean isExcludes = Boolean.FALSE;
    Boolean isRecursive = Boolean.TRUE;
    Path pathObj = new Path(path);
    Map<String, RangerPolicyResource> elements = new HashMap<String, RangerPolicy.RangerPolicyResource>();
    elements.put(RANGER_TYPE_HDFS_PATH, new RangerPolicyResource(pathObj.toUri().getPath(), isExcludes, isRecursive));
    RangerServiceResource ret = new RangerServiceResource(entityGuid, serviceName, elements);
    return ret;
}
Also used : Path(org.apache.hadoop.fs.Path) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) HashMap(java.util.HashMap) RangerServiceResource(org.apache.ranger.plugin.model.RangerServiceResource) RangerPolicyResource(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource)

Example 2 with RangerServiceResource

use of org.apache.ranger.plugin.model.RangerServiceResource in project ranger by apache.

the class AtlasHiveResourceMapper method buildResource.

@Override
public RangerServiceResource buildResource(final 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 resourceStr = getResourceNameFromQualifiedName(qualifiedName);
    if (StringUtils.isEmpty(resourceStr)) {
        throwExceptionWithMessage("resource not found in attribute '" + ENTITY_ATTRIBUTE_QUALIFIED_NAME + "': " + qualifiedName);
    }
    String clusterName = getClusterNameFromQualifiedName(qualifiedName);
    if (StringUtils.isEmpty(clusterName)) {
        throwExceptionWithMessage("cluster-name not found in attribute '" + ENTITY_ATTRIBUTE_QUALIFIED_NAME + "': " + qualifiedName);
    }
    String entityType = entity.getTypeName();
    String entityGuid = entity.getGuid();
    String serviceName = getRangerServiceName(clusterName);
    String[] resources = resourceStr.split(QUALIFIED_NAME_DELIMITER);
    String dbName = resources.length > 0 ? resources[0] : null;
    String tblName = resources.length > 1 ? resources[1] : null;
    String colName = resources.length > 2 ? resources[2] : null;
    Map<String, RangerPolicyResource> elements = new HashMap<String, RangerPolicyResource>();
    if (StringUtils.equals(entityType, ENTITY_TYPE_HIVE_DB)) {
        if (StringUtils.isNotEmpty(dbName)) {
            elements.put(RANGER_TYPE_HIVE_DB, new RangerPolicyResource(dbName));
        }
    } else if (StringUtils.equals(entityType, ENTITY_TYPE_HIVE_TABLE)) {
        if (StringUtils.isNotEmpty(dbName) && StringUtils.isNotEmpty(tblName)) {
            elements.put(RANGER_TYPE_HIVE_DB, new RangerPolicyResource(dbName));
            elements.put(RANGER_TYPE_HIVE_TABLE, new RangerPolicyResource(tblName));
        }
    } else if (StringUtils.equals(entityType, ENTITY_TYPE_HIVE_COLUMN)) {
        if (StringUtils.isNotEmpty(dbName) && StringUtils.isNotEmpty(tblName) && StringUtils.isNotEmpty(colName)) {
            elements.put(RANGER_TYPE_HIVE_DB, new RangerPolicyResource(dbName));
            elements.put(RANGER_TYPE_HIVE_TABLE, new RangerPolicyResource(tblName));
            elements.put(RANGER_TYPE_HIVE_COLUMN, new RangerPolicyResource(colName));
        }
    } else {
        throwExceptionWithMessage("unrecognized entity-type: " + entityType);
    }
    if (elements.isEmpty()) {
        throwExceptionWithMessage("invalid qualifiedName for entity-type '" + entityType + "': " + qualifiedName);
    }
    RangerServiceResource ret = new RangerServiceResource(entityGuid, serviceName, elements);
    return ret;
}
Also used : HashMap(java.util.HashMap) RangerServiceResource(org.apache.ranger.plugin.model.RangerServiceResource) RangerPolicyResource(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource)

Example 3 with RangerServiceResource

use of org.apache.ranger.plugin.model.RangerServiceResource in project ranger by apache.

the class AtlasKafkaResourceMapper method buildResource.

@Override
public RangerServiceResource buildResource(final RangerAtlasEntity entity) throws Exception {
    String qualifiedName = (String) entity.getAttributes().get(AtlasResourceMapper.ENTITY_ATTRIBUTE_QUALIFIED_NAME);
    String topic = getResourceNameFromQualifiedName(qualifiedName);
    if (StringUtils.isEmpty(topic)) {
        throwExceptionWithMessage("topic not found in attribute '" + ENTITY_ATTRIBUTE_QUALIFIED_NAME + "'");
    }
    String clusterName = getClusterNameFromQualifiedName(qualifiedName);
    if (StringUtils.isEmpty(clusterName)) {
        clusterName = defaultClusterName;
    }
    if (StringUtils.isEmpty(clusterName)) {
        throwExceptionWithMessage("attribute '" + ENTITY_ATTRIBUTE_QUALIFIED_NAME + "' not found in entity");
    }
    Map<String, RangerPolicyResource> elements = new HashMap<String, RangerPolicy.RangerPolicyResource>();
    Boolean isExcludes = Boolean.FALSE;
    Boolean isRecursive = Boolean.TRUE;
    elements.put(RANGER_TYPE_KAFKA_TOPIC, new RangerPolicyResource(topic, isExcludes, isRecursive));
    String entityGuid = entity.getGuid();
    String serviceName = getRangerServiceName(clusterName);
    return new RangerServiceResource(entityGuid, serviceName, elements);
}
Also used : RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) HashMap(java.util.HashMap) RangerServiceResource(org.apache.ranger.plugin.model.RangerServiceResource) RangerPolicyResource(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource)

Example 4 with RangerServiceResource

use of org.apache.ranger.plugin.model.RangerServiceResource in project ranger by apache.

the class AtlasNotificationMapper method buildServiceTags.

private static ServiceTags buildServiceTags(RangerAtlasEntityWithTags entityWithTags, Map<String, ServiceTags> serviceTagsMap) {
    ServiceTags ret = null;
    RangerAtlasEntity entity = entityWithTags.getEntity();
    RangerServiceResource serviceResource = AtlasResourceMapperUtil.getRangerServiceResource(entity);
    if (serviceResource != null) {
        List<RangerTag> tags = getTags(entityWithTags);
        List<RangerTagDef> tagDefs = getTagDefs(entityWithTags);
        String serviceName = serviceResource.getServiceName();
        ret = createOrGetServiceTags(serviceTagsMap, serviceName);
        if (serviceTagsMap == null || CollectionUtils.isNotEmpty(tags)) {
            serviceResource.setId((long) ret.getServiceResources().size());
            ret.getServiceResources().add(serviceResource);
            List<Long> tagIds = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(tags)) {
                for (RangerTag tag : tags) {
                    tag.setId((long) ret.getTags().size());
                    ret.getTags().put(tag.getId(), tag);
                    tagIds.add(tag.getId());
                }
            }
            ret.getResourceToTagIds().put(serviceResource.getId(), tagIds);
            if (CollectionUtils.isNotEmpty(tagDefs)) {
                for (RangerTagDef tagDef : tagDefs) {
                    tagDef.setId((long) ret.getTagDefinitions().size());
                    ret.getTagDefinitions().put(tagDef.getId(), tagDef);
                }
            }
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Entity " + entityWithTags + " does not have any tags associated with it when full-sync is being done.");
                LOG.debug("Will not add this entity to serviceTags, so that this entity, if exists,  will be removed from ranger");
            }
        }
    } else {
        LOG.error("Failed to build serviceResource for entity:" + entity.getGuid());
    }
    return ret;
}
Also used : RangerTagDef(org.apache.ranger.plugin.model.RangerTagDef) RangerAtlasEntity(org.apache.ranger.tagsync.source.atlasrest.RangerAtlasEntity) RangerServiceResource(org.apache.ranger.plugin.model.RangerServiceResource) ServiceTags(org.apache.ranger.plugin.util.ServiceTags) RangerTag(org.apache.ranger.plugin.model.RangerTag) ArrayList(java.util.ArrayList)

Example 5 with RangerServiceResource

use of org.apache.ranger.plugin.model.RangerServiceResource in project ranger by apache.

the class TestHbaseResourceMapper method testHbaseColumnFamily.

@Test
public void testHbaseColumnFamily() throws Exception {
    Map<String, Object> entAttribs = new HashMap<String, Object>();
    entAttribs.put(ENTITY_ATTRIBUTE_QUALIFIED_NAME, COLUMN_FAMILY_QUALIFIED_NAME);
    RangerAtlasEntity entity = getHbaseColumnFamilyEntity(entAttribs);
    RangerServiceResource resource = resourceMapper.buildResource(entity);
    assertColumnFamilyResource(resource, false);
}
Also used : RangerAtlasEntity(org.apache.ranger.tagsync.source.atlasrest.RangerAtlasEntity) HashMap(java.util.HashMap) RangerServiceResource(org.apache.ranger.plugin.model.RangerServiceResource) Test(org.junit.Test)

Aggregations

RangerServiceResource (org.apache.ranger.plugin.model.RangerServiceResource)65 Test (org.junit.Test)42 HashMap (java.util.HashMap)37 RangerAtlasEntity (org.apache.ranger.tagsync.source.atlasrest.RangerAtlasEntity)30 WebApplicationException (javax.ws.rs.WebApplicationException)17 ExpectedException (org.junit.rules.ExpectedException)14 ArrayList (java.util.ArrayList)10 RangerTagResourceMap (org.apache.ranger.plugin.model.RangerTagResourceMap)7 RangerPolicyResource (org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource)5 Predicate (org.apache.commons.collections.Predicate)4 XXServiceResource (org.apache.ranger.entity.XXServiceResource)4 RangerTag (org.apache.ranger.plugin.model.RangerTag)4 Map (java.util.Map)3 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3 RangerTagDef (org.apache.ranger.plugin.model.RangerTagDef)3 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)3 PUT (javax.ws.rs.PUT)2 RangerPolicy (org.apache.ranger.plugin.model.RangerPolicy)2 RangerServiceResourceSignature (org.apache.ranger.plugin.store.RangerServiceResourceSignature)2