Search in sources :

Example 1 with XXPolicy

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

the class TestXUserMgr method test27DeleteXGroup.

@Test
public void test27DeleteXGroup() {
    setup();
    boolean force = true;
    VXGroup vXGroup = new VXGroup();
    vXGroup.setId(userId);
    vXGroup.setDescription("group test");
    vXGroup.setName("grouptest");
    // XXGroup
    XXGroupDao xXGroupDao = Mockito.mock(XXGroupDao.class);
    XXGroup xXGroup = new XXGroup();
    Mockito.when(daoManager.getXXGroup()).thenReturn(xXGroupDao);
    Mockito.when(xXGroupDao.getById(vXGroup.getId())).thenReturn(xXGroup);
    Mockito.when(xGroupService.populateViewBean(xXGroup)).thenReturn(vXGroup);
    // VXGroupUser
    VXGroupUserList vxGroupUserList = new VXGroupUserList();
    XXGroupUserDao xGroupUserDao = Mockito.mock(XXGroupUserDao.class);
    VXGroupUser vxGroupUser = new VXGroupUser();
    vxGroupUser.setId(userId);
    vxGroupUser.setName("group user test");
    vxGroupUser.setOwner("Admin");
    vxGroupUser.setUserId(userId);
    vxGroupUser.setUpdatedBy("User");
    Mockito.when(xGroupUserService.searchXGroupUsers((SearchCriteria) Mockito.any())).thenReturn(vxGroupUserList);
    Mockito.when(daoManager.getXXGroupUser()).thenReturn(xGroupUserDao);
    // VXPermMap
    VXPermMapList vXPermMapList = new VXPermMapList();
    XXPermMapDao xXPermMapDao = Mockito.mock(XXPermMapDao.class);
    Mockito.when(xPermMapService.searchXPermMaps((SearchCriteria) Mockito.any())).thenReturn(vXPermMapList);
    Mockito.when(daoManager.getXXPermMap()).thenReturn(xXPermMapDao);
    // VXAuditMap
    VXAuditMapList vXAuditMapList = new VXAuditMapList();
    XXAuditMapDao xXAuditMapDao = Mockito.mock(XXAuditMapDao.class);
    Mockito.when(xAuditMapService.searchXAuditMaps((SearchCriteria) Mockito.any())).thenReturn(vXAuditMapList);
    Mockito.when(daoManager.getXXAuditMap()).thenReturn(xXAuditMapDao);
    // XXGroupGroup
    XXGroupGroupDao xXGroupGroupDao = Mockito.mock(XXGroupGroupDao.class);
    List<XXGroupGroup> xXGroupGroups = new ArrayList<XXGroupGroup>();
    Mockito.when(daoManager.getXXGroupGroup()).thenReturn(xXGroupGroupDao);
    Mockito.when(xXGroupGroupDao.findByGroupId(userId)).thenReturn(xXGroupGroups);
    // update XXGroupPermission
    XXGroupPermissionDao xXGroupPermissionDao = Mockito.mock(XXGroupPermissionDao.class);
    Mockito.when(daoManager.getXXGroupPermission()).thenReturn(xXGroupPermissionDao);
    List<XXGroupPermission> xXGroupPermissions = new ArrayList<XXGroupPermission>();
    Mockito.when(xXGroupPermissionDao.findByGroupId(vXGroup.getId())).thenReturn(xXGroupPermissions);
    // update XXPolicyItemUserPerm
    XXPolicyDao xXPolicyDao = Mockito.mock(XXPolicyDao.class);
    List<XXPolicy> xXPolicyList = new ArrayList<XXPolicy>();
    Mockito.when(daoManager.getXXPolicy()).thenReturn(xXPolicyDao);
    Mockito.when(xXPolicyDao.findByGroupId(userId)).thenReturn(xXPolicyList);
    xUserMgr.deleteXGroup(vXGroup.getId(), force);
    Mockito.verify(xGroupUserService).searchXGroupUsers((SearchCriteria) Mockito.any());
}
Also used : VXGroupUserList(org.apache.ranger.view.VXGroupUserList) XXGroupPermission(org.apache.ranger.entity.XXGroupPermission) XXAuditMapDao(org.apache.ranger.db.XXAuditMapDao) XXGroupPermissionDao(org.apache.ranger.db.XXGroupPermissionDao) ArrayList(java.util.ArrayList) VXGroupUser(org.apache.ranger.view.VXGroupUser) VXAuditMapList(org.apache.ranger.view.VXAuditMapList) XXPolicy(org.apache.ranger.entity.XXPolicy) XXGroupUserDao(org.apache.ranger.db.XXGroupUserDao) XXGroup(org.apache.ranger.entity.XXGroup) XXPermMapDao(org.apache.ranger.db.XXPermMapDao) XXPolicyDao(org.apache.ranger.db.XXPolicyDao) VXPermMapList(org.apache.ranger.view.VXPermMapList) VXGroup(org.apache.ranger.view.VXGroup) XXGroupGroupDao(org.apache.ranger.db.XXGroupGroupDao) XXGroupDao(org.apache.ranger.db.XXGroupDao) XXGroupGroup(org.apache.ranger.entity.XXGroupGroup) Test(org.junit.Test)

Example 2 with XXPolicy

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

the class PatchForMigratingOldRegimePolicyJson_J10046 method portPolicy.

private void portPolicy(String serviceType, RangerPolicy policy) throws Exception {
    logger.info("==> portPolicy(id=" + policy.getId() + ")");
    String policyText = JsonUtils.objectToJson(policy);
    if (StringUtils.isEmpty(policyText)) {
        throw new Exception("Failed to convert policy to json string. Policy: [id=" + policy.getId() + "; name=" + policy.getName() + "; serviceType=" + serviceType + "]");
    }
    XXPolicyDao policyDao = daoMgr.getXXPolicy();
    XXPolicy dbBean = policyDao.getById(policy.getId());
    dbBean.setPolicyText(policyText);
    policyDao.update(dbBean);
    try {
        Set<String> accesses = new HashSet<>();
        Set<String> users = new HashSet<>();
        Set<String> groups = new HashSet<>();
        Set<String> conditions = new HashSet<>();
        Set<String> dataMasks = new HashSet<>();
        buildLists(policy.getPolicyItems(), accesses, conditions, users, groups);
        buildLists(policy.getDenyPolicyItems(), accesses, conditions, users, groups);
        buildLists(policy.getAllowExceptions(), accesses, conditions, users, groups);
        buildLists(policy.getDenyExceptions(), accesses, conditions, users, groups);
        buildLists(policy.getDataMaskPolicyItems(), accesses, conditions, users, groups);
        buildLists(policy.getRowFilterPolicyItems(), accesses, conditions, users, groups);
        buildList(policy.getDataMaskPolicyItems(), dataMasks);
        addResourceDefRef(serviceType, policy);
        addUserNameRef(policy.getId(), users);
        addGroupNameRef(policy.getId(), groups);
        addAccessDefRef(serviceType, policy.getId(), accesses);
        addPolicyConditionDefRef(serviceType, policy.getId(), conditions);
        addDataMaskDefRef(serviceType, policy.getId(), dataMasks);
    } catch (Exception e) {
        logger.error("portPoliry(id=" + policy.getId() + ") failed!!");
        logger.error("Offending policy:" + policyText);
        throw e;
    }
    logger.info("<== portPolicy(id=" + policy.getId() + ")");
}
Also used : XXPolicyDao(org.apache.ranger.db.XXPolicyDao) XXPolicy(org.apache.ranger.entity.XXPolicy) HashSet(java.util.HashSet)

Example 3 with XXPolicy

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

the class PatchForMigratingOldRegimePolicyJson_J10046 method migrateRangerPolicyTableWithPolicyJson.

private void migrateRangerPolicyTableWithPolicyJson() throws Exception {
    logger.info("==> updateRangerPolicyTableWithPolicyJson() ");
    List<XXPolicy> xxPolicyList = daoMgr.getXXPolicy().getAllByPolicyItem();
    if (CollectionUtils.isNotEmpty(xxPolicyList)) {
        for (XXPolicy xxPolicy : xxPolicyList) {
            logger.info("XXPolicy : " + xxPolicy);
            RangerPolicy policy = svcStore.getPolicy(xxPolicy.getId());
            if (policy != null) {
                TransactionTemplate txTemplate = new TransactionTemplate(txManager);
                RangerService service = svcStore.getServiceByName(policy.getService());
                PolicyUpdaterThread updaterThread = new PolicyUpdaterThread(txTemplate, service, policy);
                updaterThread.setDaemon(true);
                updaterThread.start();
                updaterThread.join();
                String errorMsg = updaterThread.getErrorMsg();
                if (StringUtils.isNotEmpty(errorMsg)) {
                    throw new Exception(errorMsg);
                }
            }
        }
    } else {
        logger.info("no old XXPolicyItems found ");
    }
    logger.info("<== updateRangerPolicyTableWithPolicyJson() ");
}
Also used : RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) RangerService(org.apache.ranger.plugin.model.RangerService) XXPolicy(org.apache.ranger.entity.XXPolicy)

Example 4 with XXPolicy

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

the class PatchForAtlasResourceAndAccessTypeUpdate_J10016 method updatePolicyForRelationshipType.

private void updatePolicyForRelationshipType() {
    logger.info("===> updatePolicyForRelationshipType ");
    XXPortalUser xxPortalUser = daoMgr.getXXPortalUser().findByLoginId(LOGIN_ID_ADMIN);
    Long currentUserId = xxPortalUser.getId();
    XXServiceDef xXServiceDefObj = daoMgr.getXXServiceDef().findByName(EmbeddedServiceDefsUtil.EMBEDDED_SERVICEDEF_ATLAS_NAME);
    if (xXServiceDefObj == null) {
        logger.debug("xXServiceDefObj not found with name : " + EmbeddedServiceDefsUtil.EMBEDDED_SERVICEDEF_ATLAS_NAME);
        return;
    }
    Long xServiceDefId = xXServiceDefObj.getId();
    XXResourceDef xxResourceDef = daoMgr.getXXResourceDef().findByNameAndServiceDefId(RESOURCE_DEF_NAME, xServiceDefId);
    List<XXPolicyResource> policyResources = daoMgr.getXXPolicyResource().findByResDefId(xxResourceDef.getId());
    for (XXPolicyResource xxPolicyResource : policyResources) {
        XXPolicy xxPolicy = daoMgr.getXXPolicy().getById(xxPolicyResource.getPolicyid());
        List<XXPolicyItem> xxPolicyItems = daoMgr.getXXPolicyItem().findByPolicyId(xxPolicy.getId());
        for (XXPolicyItem xxPolicyItem : xxPolicyItems) {
            XXGroup xxGroup = daoMgr.getXXGroup().findByGroupName(GROUP_PUBLIC);
            if (xxGroup == null) {
                logger.error("Group name 'public' not found in database");
                return;
            }
            Long publicGroupId = xxGroup.getId();
            XXPolicyItemGroupPerm xxPolicyItemGroupPerm = new XXPolicyItemGroupPerm();
            xxPolicyItemGroupPerm.setPolicyItemId(xxPolicyItem.getId());
            xxPolicyItemGroupPerm.setGroupId(publicGroupId);
            xxPolicyItemGroupPerm.setOrder(0);
            xxPolicyItemGroupPerm.setAddedByUserId(currentUserId);
            xxPolicyItemGroupPerm.setUpdatedByUserId(currentUserId);
            daoMgr.getXXPolicyItemGroupPerm().create(xxPolicyItemGroupPerm);
        }
    }
    logger.info("<=== updatePolicyForRelationshipType ");
}
Also used : XXPortalUser(org.apache.ranger.entity.XXPortalUser) XXServiceDef(org.apache.ranger.entity.XXServiceDef) XXPolicyItemGroupPerm(org.apache.ranger.entity.XXPolicyItemGroupPerm) XXPolicyItem(org.apache.ranger.entity.XXPolicyItem) XXGroup(org.apache.ranger.entity.XXGroup) XXPolicyResource(org.apache.ranger.entity.XXPolicyResource) XXPolicy(org.apache.ranger.entity.XXPolicy) XXResourceDef(org.apache.ranger.entity.XXResourceDef)

Example 5 with XXPolicy

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

the class PatchForAtlasToAddTypeRead_J10040 method updateDefaultPolicyForType.

private void updateDefaultPolicyForType() throws Exception {
    logger.info("==> updateDefaultPolicyForType() ");
    XXServiceDef xXServiceDefObj = daoMgr.getXXServiceDef().findByName(EmbeddedServiceDefsUtil.EMBEDDED_SERVICEDEF_ATLAS_NAME);
    if (xXServiceDefObj == null) {
        logger.debug("ServiceDef not found with name :" + EmbeddedServiceDefsUtil.EMBEDDED_SERVICEDEF_ATLAS_NAME);
        return;
    }
    Long xServiceDefId = xXServiceDefObj.getId();
    List<XXService> xxServices = daoMgr.getXXService().findByServiceDefId(xServiceDefId);
    for (XXService xxService : xxServices) {
        List<XXPolicy> xxPolicies = daoMgr.getXXPolicy().findByServiceId(xxService.getId());
        for (XXPolicy xxPolicy : xxPolicies) {
            if (xxPolicy.getName().equalsIgnoreCase(ALL_TYPE_RESOURCE_DEF_NAME)) {
                RangerPolicy rPolicy = svcDBStore.getPolicy(xxPolicy.getId());
                List<RangerPolicyItem> policyItems = rPolicy.getPolicyItems();
                for (RangerPolicyItem item : policyItems) {
                    if (!checkIfTypeReadPermissionSet(item)) {
                        List<RangerPolicyItemAccess> itemAccesses = item.getAccesses();
                        itemAccesses.add(getTypeReadPolicyItemAccesses());
                        item.setAccesses(itemAccesses);
                    }
                }
                RangerPolicyItem rangerPolicyItemReadType = new RangerPolicyItem();
                rangerPolicyItemReadType.setDelegateAdmin(Boolean.FALSE);
                rangerPolicyItemReadType.setAccesses(Arrays.asList(getTypeReadPolicyItemAccesses()));
                rangerPolicyItemReadType.setGroups(Arrays.asList(GROUP_PUBLIC));
                policyItems.add(rangerPolicyItemReadType);
                svcDBStore.updatePolicy(rPolicy);
            }
        }
    }
    logger.info("<== updateDefaultPolicyForType() ");
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) RangerPolicyItemAccess(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItemAccess) XXService(org.apache.ranger.entity.XXService) XXPolicy(org.apache.ranger.entity.XXPolicy) RangerPolicyItem(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItem)

Aggregations

XXPolicy (org.apache.ranger.entity.XXPolicy)43 RangerPolicy (org.apache.ranger.plugin.model.RangerPolicy)25 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)12 XXPolicyDao (org.apache.ranger.db.XXPolicyDao)11 VXAuditMapList (org.apache.ranger.view.VXAuditMapList)10 VXPermMapList (org.apache.ranger.view.VXPermMapList)10 XXPortalUser (org.apache.ranger.entity.XXPortalUser)9 XXService (org.apache.ranger.entity.XXService)9 XXServiceDef (org.apache.ranger.entity.XXServiceDef)9 XXUser (org.apache.ranger.entity.XXUser)9 XXUserDao (org.apache.ranger.db.XXUserDao)8 RangerService (org.apache.ranger.plugin.model.RangerService)8 VXAuditMap (org.apache.ranger.view.VXAuditMap)8 VXPermMap (org.apache.ranger.view.VXPermMap)8 WebApplicationException (javax.ws.rs.WebApplicationException)7 XXAuditMapDao (org.apache.ranger.db.XXAuditMapDao)6 XXGroupUserDao (org.apache.ranger.db.XXGroupUserDao)6 XXPermMapDao (org.apache.ranger.db.XXPermMapDao)6 XXPortalUserDao (org.apache.ranger.db.XXPortalUserDao)6