Search in sources :

Example 1 with XXPolicyConditionDef

use of org.apache.ranger.entity.XXPolicyConditionDef in project ranger by apache.

the class ServiceDBStore method deleteServiceDef.

public void deleteServiceDef(Long serviceDefId, Boolean forceDelete) throws Exception {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> ServiceDBStore.deleteServiceDef(" + serviceDefId + ", " + forceDelete + ")");
    }
    bizUtil.blockAuditorRoleUser();
    UserSessionBase session = ContextUtil.getCurrentUserSession();
    if (session == null) {
        throw restErrorUtil.createRESTException("UserSession cannot be null, only Admin can update service-def", MessageEnums.OPER_NO_PERMISSION);
    }
    if (!session.isKeyAdmin() && !session.isUserAdmin()) {
        throw restErrorUtil.createRESTException("User is not allowed to update service-def, only Admin can update service-def", MessageEnums.OPER_NO_PERMISSION);
    }
    RangerServiceDef serviceDef = getServiceDef(serviceDefId);
    if (serviceDef == null) {
        throw restErrorUtil.createRESTException("No Service Definiton found for Id: " + serviceDefId, MessageEnums.DATA_NOT_FOUND);
    }
    List<XXService> serviceList = daoMgr.getXXService().findByServiceDefId(serviceDefId);
    if (!forceDelete) {
        if (CollectionUtils.isNotEmpty(serviceList)) {
            throw restErrorUtil.createRESTException("Services exists under given service definition, can't delete Service-Def: " + serviceDef.getName(), MessageEnums.OPER_NOT_ALLOWED_FOR_ENTITY);
        }
    }
    if (CollectionUtils.isNotEmpty(serviceList)) {
        for (XXService service : serviceList) {
            deleteService(service.getId());
        }
    }
    XXDataMaskTypeDefDao dataMaskDao = daoMgr.getXXDataMaskTypeDef();
    List<XXDataMaskTypeDef> dataMaskDefs = dataMaskDao.findByServiceDefId(serviceDefId);
    for (XXDataMaskTypeDef dataMaskDef : dataMaskDefs) {
        dataMaskDao.remove(dataMaskDef);
    }
    List<XXAccessTypeDef> accTypeDefs = daoMgr.getXXAccessTypeDef().findByServiceDefId(serviceDefId);
    for (XXAccessTypeDef accessType : accTypeDefs) {
        deleteXXAccessTypeDef(accessType);
    }
    XXContextEnricherDefDao xContextEnricherDao = daoMgr.getXXContextEnricherDef();
    List<XXContextEnricherDef> contextEnrichers = xContextEnricherDao.findByServiceDefId(serviceDefId);
    for (XXContextEnricherDef context : contextEnrichers) {
        xContextEnricherDao.remove(context);
    }
    XXEnumDefDao enumDefDao = daoMgr.getXXEnumDef();
    List<XXEnumDef> enumDefList = enumDefDao.findByServiceDefId(serviceDefId);
    for (XXEnumDef enumDef : enumDefList) {
        List<XXEnumElementDef> enumEleDefList = daoMgr.getXXEnumElementDef().findByEnumDefId(enumDef.getId());
        for (XXEnumElementDef eleDef : enumEleDefList) {
            daoMgr.getXXEnumElementDef().remove(eleDef);
        }
        enumDefDao.remove(enumDef);
    }
    XXPolicyConditionDefDao policyCondDao = daoMgr.getXXPolicyConditionDef();
    List<XXPolicyConditionDef> policyCondList = policyCondDao.findByServiceDefId(serviceDefId);
    for (XXPolicyConditionDef policyCond : policyCondList) {
        List<XXPolicyRefCondition> xxPolicyRefConditions = daoMgr.getXXPolicyRefCondition().findByConditionDefId(policyCond.getId());
        for (XXPolicyRefCondition XXPolicyRefCondition : xxPolicyRefConditions) {
            daoMgr.getXXPolicyRefCondition().remove(XXPolicyRefCondition);
        }
        policyCondDao.remove(policyCond);
    }
    List<XXResourceDef> resDefList = daoMgr.getXXResourceDef().findByServiceDefId(serviceDefId);
    for (XXResourceDef resDef : resDefList) {
        deleteXXResourceDef(resDef);
    }
    XXServiceConfigDefDao configDefDao = daoMgr.getXXServiceConfigDef();
    List<XXServiceConfigDef> configDefList = configDefDao.findByServiceDefId(serviceDefId);
    for (XXServiceConfigDef configDef : configDefList) {
        configDefDao.remove(configDef);
    }
    Long version = serviceDef.getVersion();
    if (version == null) {
        version = Long.valueOf(1);
        LOG.info("Found Version Value: `null`, so setting value of version to 1, While updating object, version should not be null.");
    } else {
        version = Long.valueOf(version.longValue() + 1);
    }
    serviceDef.setVersion(version);
    serviceDefService.delete(serviceDef);
    LOG.info("ServiceDefinition has been deleted successfully. Service-Def Name: " + serviceDef.getName());
    dataHistService.createObjectDataHistory(serviceDef, RangerDataHistService.ACTION_DELETE);
    postDelete(serviceDef);
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== ServiceDBStore.deleteServiceDef(" + serviceDefId + ", " + forceDelete + ")");
    }
}
Also used : XXDataMaskTypeDefDao(org.apache.ranger.db.XXDataMaskTypeDefDao) XXPolicyConditionDefDao(org.apache.ranger.db.XXPolicyConditionDefDao) UserSessionBase(org.apache.ranger.common.UserSessionBase) XXDataMaskTypeDef(org.apache.ranger.entity.XXDataMaskTypeDef) XXEnumDefDao(org.apache.ranger.db.XXEnumDefDao) XXPolicyConditionDef(org.apache.ranger.entity.XXPolicyConditionDef) XXPolicyRefCondition(org.apache.ranger.entity.XXPolicyRefCondition) XXAccessTypeDef(org.apache.ranger.entity.XXAccessTypeDef) XXServiceConfigDef(org.apache.ranger.entity.XXServiceConfigDef) XXResourceDef(org.apache.ranger.entity.XXResourceDef) XXEnumElementDef(org.apache.ranger.entity.XXEnumElementDef) RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef) XXEnumDef(org.apache.ranger.entity.XXEnumDef) XXService(org.apache.ranger.entity.XXService) XXServiceConfigDefDao(org.apache.ranger.db.XXServiceConfigDefDao) XXContextEnricherDef(org.apache.ranger.entity.XXContextEnricherDef) XXContextEnricherDefDao(org.apache.ranger.db.XXContextEnricherDefDao)

Example 2 with XXPolicyConditionDef

use of org.apache.ranger.entity.XXPolicyConditionDef in project ranger by apache.

the class RangerBaseModelService method getConditionName.

protected String getConditionName(Long conditionDefId) {
    String ret = null;
    XXPolicyConditionDef conditionDef = conditionDefId == null ? null : daoMgr.getXXPolicyConditionDef().getById(conditionDefId);
    if (conditionDef != null) {
        ret = conditionDef.getName();
    }
    return ret;
}
Also used : XXPolicyConditionDef(org.apache.ranger.entity.XXPolicyConditionDef)

Example 3 with XXPolicyConditionDef

use of org.apache.ranger.entity.XXPolicyConditionDef in project ranger by apache.

the class TestRangerServiceDefServiceBase method test11populateRangerPolicyConditionDefToXXnullValue.

@Test
public void test11populateRangerPolicyConditionDefToXXnullValue() {
    RangerPolicyConditionDef rangerConditionDefvObj = null;
    XXPolicyConditionDef policyConditionDefObj = null;
    XXServiceDef serviceDefObj = null;
    Mockito.when(restErrorUtil.createRESTException("RangerServiceDef cannot be null.", MessageEnums.DATA_NOT_FOUND)).thenThrow(new WebApplicationException());
    thrown.expect(WebApplicationException.class);
    XXPolicyConditionDef dbPolicyConditionDef = rangerServiceDefService.populateRangerPolicyConditionDefToXX(rangerConditionDefvObj, policyConditionDefObj, serviceDefObj, 1);
    Assert.assertNull(dbPolicyConditionDef);
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) XXPolicyConditionDef(org.apache.ranger.entity.XXPolicyConditionDef) WebApplicationException(javax.ws.rs.WebApplicationException) RangerPolicyConditionDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerPolicyConditionDef) Test(org.junit.Test)

Example 4 with XXPolicyConditionDef

use of org.apache.ranger.entity.XXPolicyConditionDef in project ranger by apache.

the class TestRangerServiceDefServiceBase method test12populateXXToRangerPolicyConditionDef.

@Test
public void test12populateXXToRangerPolicyConditionDef() {
    XXPolicyConditionDef policyConditionDefObj = new XXPolicyConditionDef();
    policyConditionDefObj.setAddedByUserId(Id);
    policyConditionDefObj.setCreateTime(new Date());
    policyConditionDefObj.setDefid(Id);
    policyConditionDefObj.setDescription("policy");
    policyConditionDefObj.setId(Id);
    policyConditionDefObj.setName("country");
    policyConditionDefObj.setOrder(0);
    policyConditionDefObj.setUpdatedByUserId(Id);
    policyConditionDefObj.setUpdateTime(new Date());
    RangerPolicyConditionDef dbRangerPolicyConditionDef = rangerServiceDefService.populateXXToRangerPolicyConditionDef(policyConditionDefObj);
    Assert.assertNotNull(dbRangerPolicyConditionDef);
    Assert.assertEquals(dbRangerPolicyConditionDef.getName(), policyConditionDefObj.getName());
    Assert.assertEquals(dbRangerPolicyConditionDef.getDescription(), policyConditionDefObj.getDescription());
    Assert.assertEquals(dbRangerPolicyConditionDef.getEvaluator(), policyConditionDefObj.getEvaluator());
    Assert.assertEquals(dbRangerPolicyConditionDef.getLabel(), policyConditionDefObj.getLabel());
}
Also used : XXPolicyConditionDef(org.apache.ranger.entity.XXPolicyConditionDef) RangerPolicyConditionDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerPolicyConditionDef) Date(java.util.Date) Test(org.junit.Test)

Example 5 with XXPolicyConditionDef

use of org.apache.ranger.entity.XXPolicyConditionDef in project ranger by apache.

the class ServiceDBStore method createNewPolicyItemForPolicy.

private XXPolicyItem createNewPolicyItemForPolicy(RangerPolicy policy, XXPolicy xPolicy, RangerPolicyItem policyItem, XXServiceDef xServiceDef, int itemOrder, int policyItemType) throws Exception {
    XXPolicyItem xPolicyItem = new XXPolicyItem();
    xPolicyItem = rangerAuditFields.populateAuditFields(xPolicyItem, xPolicy);
    xPolicyItem.setDelegateAdmin(policyItem.getDelegateAdmin());
    xPolicyItem.setItemType(policyItemType);
    xPolicyItem.setIsEnabled(Boolean.TRUE);
    xPolicyItem.setComments(null);
    xPolicyItem.setPolicyId(policy.getId());
    xPolicyItem.setOrder(itemOrder);
    xPolicyItem = daoMgr.getXXPolicyItem().create(xPolicyItem);
    List<RangerPolicyItemAccess> accesses = policyItem.getAccesses();
    for (int i = 0; i < accesses.size(); i++) {
        RangerPolicyItemAccess access = accesses.get(i);
        XXAccessTypeDef xAccTypeDef = daoMgr.getXXAccessTypeDef().findByNameAndServiceId(access.getType(), xPolicy.getService());
        if (xAccTypeDef == null) {
            throw new Exception(access.getType() + ": is not a valid access-type. policy='" + policy.getName() + "' service='" + policy.getService() + "'");
        }
        XXPolicyItemAccess xPolItemAcc = new XXPolicyItemAccess();
        xPolItemAcc = (XXPolicyItemAccess) rangerAuditFields.populateAuditFields(xPolItemAcc, xPolicyItem);
        xPolItemAcc.setIsAllowed(access.getIsAllowed());
        xPolItemAcc.setType(xAccTypeDef.getId());
        xPolItemAcc.setPolicyitemid(xPolicyItem.getId());
        xPolItemAcc.setOrder(i);
        daoMgr.getXXPolicyItemAccess().create(xPolItemAcc);
    }
    List<String> users = policyItem.getUsers();
    for (int i = 0; i < users.size(); i++) {
        String user = users.get(i);
        if (StringUtils.isBlank(user)) {
            continue;
        }
        XXUser xUser = daoMgr.getXXUser().findByUserName(user);
        if (xUser == null) {
            throw new Exception(user + ": user does not exist. policy='" + policy.getName() + "' service='" + policy.getService() + "' user='" + user + "'");
        }
        XXPolicyItemUserPerm xUserPerm = new XXPolicyItemUserPerm();
        xUserPerm = rangerAuditFields.populateAuditFields(xUserPerm, xPolicyItem);
        xUserPerm.setUserId(xUser.getId());
        xUserPerm.setPolicyItemId(xPolicyItem.getId());
        xUserPerm.setOrder(i);
        xUserPerm = daoMgr.getXXPolicyItemUserPerm().create(xUserPerm);
    }
    List<String> groups = policyItem.getGroups();
    for (int i = 0; i < groups.size(); i++) {
        String group = groups.get(i);
        if (StringUtils.isBlank(group)) {
            continue;
        }
        XXGroup xGrp = daoMgr.getXXGroup().findByGroupName(group);
        if (xGrp == null) {
            throw new Exception(group + ": group does not exist. policy='" + policy.getName() + "' service='" + policy.getService() + "' group='" + group + "'");
        }
        XXPolicyItemGroupPerm xGrpPerm = new XXPolicyItemGroupPerm();
        xGrpPerm = rangerAuditFields.populateAuditFields(xGrpPerm, xPolicyItem);
        xGrpPerm.setGroupId(xGrp.getId());
        xGrpPerm.setPolicyItemId(xPolicyItem.getId());
        xGrpPerm.setOrder(i);
        xGrpPerm = daoMgr.getXXPolicyItemGroupPerm().create(xGrpPerm);
    }
    List<RangerPolicyItemCondition> conditions = policyItem.getConditions();
    for (RangerPolicyItemCondition condition : conditions) {
        XXPolicyConditionDef xPolCond = daoMgr.getXXPolicyConditionDef().findByServiceDefIdAndName(xServiceDef.getId(), condition.getType());
        if (xPolCond == null) {
            throw new Exception(condition.getType() + ": is not a valid condition-type. policy='" + xPolicy.getName() + "' service='" + xPolicy.getService() + "'");
        }
        for (int i = 0; i < condition.getValues().size(); i++) {
            String value = condition.getValues().get(i);
            XXPolicyItemCondition xPolItemCond = new XXPolicyItemCondition();
            xPolItemCond = rangerAuditFields.populateAuditFields(xPolItemCond, xPolicyItem);
            xPolItemCond.setPolicyItemId(xPolicyItem.getId());
            xPolItemCond.setType(xPolCond.getId());
            xPolItemCond.setValue(value);
            xPolItemCond.setOrder(i);
            daoMgr.getXXPolicyItemCondition().create(xPolItemCond);
        }
    }
    return xPolicyItem;
}
Also used : XXUser(org.apache.ranger.entity.XXUser) XXPolicyItemGroupPerm(org.apache.ranger.entity.XXPolicyItemGroupPerm) XXPolicyItemUserPerm(org.apache.ranger.entity.XXPolicyItemUserPerm) VXString(org.apache.ranger.view.VXString) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) JSONException(org.codehaus.jettison.json.JSONException) XXPolicyItemCondition(org.apache.ranger.entity.XXPolicyItemCondition) XXPolicyItem(org.apache.ranger.entity.XXPolicyItem) XXPolicyItemAccess(org.apache.ranger.entity.XXPolicyItemAccess) XXGroup(org.apache.ranger.entity.XXGroup) XXPolicyConditionDef(org.apache.ranger.entity.XXPolicyConditionDef) XXAccessTypeDef(org.apache.ranger.entity.XXAccessTypeDef) RangerPolicyItemAccess(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItemAccess) RangerPolicyItemCondition(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItemCondition)

Aggregations

XXPolicyConditionDef (org.apache.ranger.entity.XXPolicyConditionDef)12 XXAccessTypeDef (org.apache.ranger.entity.XXAccessTypeDef)5 XXPolicyRefCondition (org.apache.ranger.entity.XXPolicyRefCondition)5 XXServiceDef (org.apache.ranger.entity.XXServiceDef)5 RangerPolicyConditionDef (org.apache.ranger.plugin.model.RangerServiceDef.RangerPolicyConditionDef)5 XXDataMaskTypeDef (org.apache.ranger.entity.XXDataMaskTypeDef)4 XXResourceDef (org.apache.ranger.entity.XXResourceDef)4 ArrayList (java.util.ArrayList)3 XXContextEnricherDefDao (org.apache.ranger.db.XXContextEnricherDefDao)3 XXDataMaskTypeDefDao (org.apache.ranger.db.XXDataMaskTypeDefDao)3 XXEnumDefDao (org.apache.ranger.db.XXEnumDefDao)3 XXPolicyConditionDefDao (org.apache.ranger.db.XXPolicyConditionDefDao)3 XXServiceConfigDefDao (org.apache.ranger.db.XXServiceConfigDefDao)3 XXContextEnricherDef (org.apache.ranger.entity.XXContextEnricherDef)3 XXEnumDef (org.apache.ranger.entity.XXEnumDef)3 XXEnumElementDef (org.apache.ranger.entity.XXEnumElementDef)3 XXServiceConfigDef (org.apache.ranger.entity.XXServiceConfigDef)3 RangerPolicyItemAccess (org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItemAccess)3 VXString (org.apache.ranger.view.VXString)3 Date (java.util.Date)2