Search in sources :

Example 76 with RangerPolicy

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

the class ServiceDBStore method getPaginatedServicePolicies.

public PList<RangerPolicy> getPaginatedServicePolicies(Long serviceId, SearchFilter filter) throws Exception {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> ServiceDBStore.getPaginatedServicePolicies(" + serviceId + ")");
    }
    XXService service = daoMgr.getXXService().getById(serviceId);
    if (service == null) {
        throw new Exception("service does not exist - id='" + serviceId);
    }
    PList<RangerPolicy> ret = getPaginatedServicePolicies(service.getName(), filter);
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== ServiceDBStore.getPaginatedServicePolicies(" + serviceId + ")");
    }
    return ret;
}
Also used : RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) XXService(org.apache.ranger.entity.XXService) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) JSONException(org.codehaus.jettison.json.JSONException)

Example 77 with RangerPolicy

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

the class ServiceDBStore method deleteService.

@Override
public void deleteService(Long id) throws Exception {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> ServiceDBStore.deleteService(" + id + ")");
    }
    RangerService service = getService(id);
    if (service == null) {
        throw new Exception("no service exists with ID=" + id);
    }
    List<XXPolicy> policies = daoMgr.getXXPolicy().findByServiceId(service.getId());
    RangerPolicy rangerPolicy = null;
    for (XXPolicy policy : policies) {
        LOG.info("Deleting Policy, policyName: " + policy.getName());
        rangerPolicy = getPolicy(policy.getId());
        deletePolicy(rangerPolicy);
    }
    XXServiceConfigMapDao configDao = daoMgr.getXXServiceConfigMap();
    List<XXServiceConfigMap> configs = configDao.findByServiceId(service.getId());
    for (XXServiceConfigMap configMap : configs) {
        configDao.remove(configMap);
    }
    Long version = service.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);
    }
    service.setVersion(version);
    svcService.delete(service);
    dataHistService.createObjectDataHistory(service, RangerDataHistService.ACTION_DELETE);
    List<XXTrxLog> trxLogList = svcService.getTransactionLog(service, RangerServiceService.OPERATION_DELETE_CONTEXT);
    bizUtil.createTrxLog(trxLogList);
}
Also used : XXServiceConfigMap(org.apache.ranger.entity.XXServiceConfigMap) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) RangerService(org.apache.ranger.plugin.model.RangerService) XXTrxLog(org.apache.ranger.entity.XXTrxLog) XXPolicy(org.apache.ranger.entity.XXPolicy) XXServiceConfigMapDao(org.apache.ranger.db.XXServiceConfigMapDao) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) JSONException(org.codehaus.jettison.json.JSONException)

Example 78 with RangerPolicy

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

the class ServiceDBStore method createDefaultPolicies.

void createDefaultPolicies(RangerService createdService) throws Exception {
    RangerBaseService svc = serviceMgr.getRangerServiceByService(createdService, this);
    if (svc != null) {
        List<String> serviceCheckUsers = getServiceCheckUsers(createdService);
        List<RangerPolicy> defaultPolicies = svc.getDefaultRangerPolicies();
        if (CollectionUtils.isNotEmpty(defaultPolicies)) {
            createDefaultPolicyUsersAndGroups(defaultPolicies);
            for (RangerPolicy defaultPolicy : defaultPolicies) {
                if (CollectionUtils.isNotEmpty(serviceCheckUsers) && StringUtils.equalsIgnoreCase(defaultPolicy.getService(), createdService.getName())) {
                    RangerPolicyItem defaultAllowPolicyItem = CollectionUtils.isNotEmpty(defaultPolicy.getPolicyItems()) ? defaultPolicy.getPolicyItems().get(0) : null;
                    if (defaultAllowPolicyItem == null) {
                        LOG.error("There is no allow-policy-item in the default-policy:[" + defaultPolicy + "]");
                    } else {
                        RangerPolicy.RangerPolicyItem policyItem = new RangerPolicy.RangerPolicyItem();
                        policyItem.setUsers(serviceCheckUsers);
                        policyItem.setAccesses(defaultAllowPolicyItem.getAccesses());
                        policyItem.setDelegateAdmin(true);
                        defaultPolicy.getPolicyItems().add(policyItem);
                    }
                }
                boolean isPolicyItemValid = validatePolicyItems(defaultPolicy.getPolicyItems()) && validatePolicyItems(defaultPolicy.getDenyPolicyItems()) && validatePolicyItems(defaultPolicy.getAllowExceptions()) && validatePolicyItems(defaultPolicy.getDenyExceptions()) && validatePolicyItems(defaultPolicy.getDataMaskPolicyItems()) && validatePolicyItems(defaultPolicy.getRowFilterPolicyItems());
                if (isPolicyItemValid) {
                    createPolicy(defaultPolicy);
                } else {
                    LOG.warn("Default policy won't be created,since policyItems not valid-either users/groups not present or access not present in policy.");
                }
            }
        }
    }
}
Also used : RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) RangerBaseService(org.apache.ranger.plugin.service.RangerBaseService) RangerPolicyItem(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItem) VXString(org.apache.ranger.view.VXString) RangerPolicyItem(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItem)

Example 79 with RangerPolicy

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

the class TestServiceDBStore method tess29deletePolicy.

@Test
public void tess29deletePolicy() throws Exception {
    setup();
    XXServiceDao xServiceDao = Mockito.mock(XXServiceDao.class);
    XXService xService = Mockito.mock(XXService.class);
    XXServiceVersionInfoDao xServiceVersionInfoDao = Mockito.mock(XXServiceVersionInfoDao.class);
    XXServiceVersionInfo xServiceVersionInfo = Mockito.mock(XXServiceVersionInfo.class);
    XXPolicyLabelMapDao xPolicyLabelMapDao = Mockito.mock(XXPolicyLabelMapDao.class);
    XXPolicyItemDao xPolicyItemDao = Mockito.mock(XXPolicyItemDao.class);
    XXPolicyItemDataMaskInfoDao xPolicyItemDataMaskInfoDao = Mockito.mock(XXPolicyItemDataMaskInfoDao.class);
    XXPolicyItemRowFilterInfoDao xPolicyItemRowFilterInfoDao = Mockito.mock(XXPolicyItemRowFilterInfoDao.class);
    XXPolicyItemConditionDao xPolicyItemConditionDao = Mockito.mock(XXPolicyItemConditionDao.class);
    XXPolicyItemGroupPermDao xPolicyItemGroupPermDao = Mockito.mock(XXPolicyItemGroupPermDao.class);
    XXPolicyItemUserPermDao xPolicyItemUserPermDao = Mockito.mock(XXPolicyItemUserPermDao.class);
    XXPolicyItemAccessDao xPolicyItemAccessDao = Mockito.mock(XXPolicyItemAccessDao.class);
    XXPolicyResourceDao xPolicyResourceDao = Mockito.mock(XXPolicyResourceDao.class);
    XXPolicyResourceMapDao xPolicyResourceMapDao = Mockito.mock(XXPolicyResourceMapDao.class);
    RangerService rangerService = rangerService();
    RangerPolicy rangerPolicy = rangerPolicy();
    String name = "HDFS_1-1-20150316062453";
    List<XXPolicyItem> policyItemList = new ArrayList<XXPolicyItem>();
    XXPolicyItem policyItem = new XXPolicyItem();
    policyItem.setAddedByUserId(Id);
    policyItem.setCreateTime(new Date());
    policyItem.setDelegateAdmin(false);
    policyItem.setId(Id);
    policyItem.setOrder(1);
    policyItem.setPolicyId(Id);
    policyItem.setUpdatedByUserId(Id);
    policyItem.setUpdateTime(new Date());
    policyItemList.add(policyItem);
    List<XXPolicyItemCondition> policyItemConditionList = new ArrayList<XXPolicyItemCondition>();
    XXPolicyItemCondition policyItemCondition = new XXPolicyItemCondition();
    policyItemCondition.setAddedByUserId(Id);
    policyItemCondition.setCreateTime(new Date());
    policyItemCondition.setType(1L);
    policyItemCondition.setId(Id);
    policyItemCondition.setOrder(1);
    policyItemCondition.setPolicyItemId(Id);
    policyItemCondition.setUpdatedByUserId(Id);
    policyItemCondition.setUpdateTime(new Date());
    policyItemConditionList.add(policyItemCondition);
    List<XXPolicyItemGroupPerm> policyItemGroupPermList = new ArrayList<XXPolicyItemGroupPerm>();
    XXPolicyItemGroupPerm policyItemGroupPerm = new XXPolicyItemGroupPerm();
    policyItemGroupPerm.setAddedByUserId(Id);
    policyItemGroupPerm.setCreateTime(new Date());
    policyItemGroupPerm.setGroupId(Id);
    List<XXServiceConfigMap> xConfMapList = new ArrayList<XXServiceConfigMap>();
    XXServiceConfigMap xConfMap = new XXServiceConfigMap();
    xConfMap.setAddedByUserId(null);
    xConfMap.setConfigkey(name);
    xConfMap.setConfigvalue(name);
    xConfMap.setCreateTime(new Date());
    xConfMap.setServiceId(null);
    xConfMap.setId(Id);
    xConfMap.setUpdatedByUserId(null);
    xConfMap.setUpdateTime(new Date());
    xConfMapList.add(xConfMap);
    policyItemGroupPerm.setId(Id);
    policyItemGroupPerm.setOrder(1);
    policyItemGroupPerm.setPolicyItemId(Id);
    policyItemGroupPerm.setUpdatedByUserId(Id);
    policyItemGroupPerm.setUpdateTime(new Date());
    policyItemGroupPermList.add(policyItemGroupPerm);
    List<XXPolicyItemUserPerm> policyItemUserPermList = new ArrayList<XXPolicyItemUserPerm>();
    XXPolicyItemUserPerm policyItemUserPerm = new XXPolicyItemUserPerm();
    policyItemUserPerm.setAddedByUserId(Id);
    policyItemUserPerm.setCreateTime(new Date());
    policyItemUserPerm.setPolicyItemId(Id);
    policyItemUserPerm.setId(Id);
    policyItemUserPerm.setOrder(1);
    policyItemUserPerm.setUpdatedByUserId(Id);
    policyItemUserPerm.setUpdateTime(new Date());
    policyItemUserPermList.add(policyItemUserPerm);
    List<XXPolicyItemAccess> policyItemAccessList = new ArrayList<XXPolicyItemAccess>();
    XXPolicyItemAccess policyItemAccess = new XXPolicyItemAccess();
    policyItemAccess.setAddedByUserId(Id);
    policyItemAccess.setCreateTime(new Date());
    policyItemAccess.setPolicyitemid(Id);
    policyItemAccess.setId(Id);
    policyItemAccess.setOrder(1);
    policyItemAccess.setUpdatedByUserId(Id);
    policyItemAccess.setUpdateTime(new Date());
    policyItemAccessList.add(policyItemAccess);
    List<XXPolicyResource> policyResourceList = new ArrayList<XXPolicyResource>();
    XXPolicyResource policyResource = new XXPolicyResource();
    policyResource.setId(Id);
    policyResource.setCreateTime(new Date());
    policyResource.setAddedByUserId(Id);
    policyResource.setIsExcludes(false);
    policyResource.setIsRecursive(false);
    policyResource.setPolicyId(Id);
    policyResource.setResDefId(Id);
    policyResource.setUpdatedByUserId(Id);
    policyResource.setUpdateTime(new Date());
    policyResourceList.add(policyResource);
    List<XXPolicyResourceMap> policyResourceMapList = new ArrayList<XXPolicyResourceMap>();
    XXPolicyResourceMap policyResourceMap = new XXPolicyResourceMap();
    policyResourceMap.setAddedByUserId(Id);
    policyResourceMap.setCreateTime(new Date());
    policyResourceMap.setId(Id);
    policyResourceMap.setOrder(1);
    policyResourceMap.setResourceId(Id);
    policyResourceMap.setUpdatedByUserId(Id);
    policyResourceMap.setUpdateTime(new Date());
    policyResourceMap.setValue("1L");
    policyResourceMapList.add(policyResourceMap);
    List<XXPolicyLabelMap> xxPolicyLabelMapList = new ArrayList<>();
    List<XXServiceConfigDef> xServiceConfigDefList = new ArrayList<XXServiceConfigDef>();
    XXServiceConfigDef serviceConfigDefObj = new XXServiceConfigDef();
    serviceConfigDefObj.setId(Id);
    xServiceConfigDefList.add(serviceConfigDefObj);
    Mockito.when(policyService.read(rangerPolicy.getId())).thenReturn(rangerPolicy);
    Mockito.when(daoManager.getXXService()).thenReturn(xServiceDao);
    Mockito.when(xServiceDao.findByName(name)).thenReturn(xService);
    Mockito.when(svcService.getPopulatedViewObject(xService)).thenReturn(rangerService);
    Mockito.when(daoManager.getXXPolicyItem()).thenReturn(xPolicyItemDao);
    Mockito.when(xPolicyItemDao.findByPolicyId(policyItem.getId())).thenReturn(policyItemList);
    Mockito.when(daoManager.getXXPolicyItemDataMaskInfo()).thenReturn(xPolicyItemDataMaskInfoDao);
    Mockito.when(daoManager.getXXPolicyItemRowFilterInfo()).thenReturn(xPolicyItemRowFilterInfoDao);
    Mockito.when(daoManager.getXXPolicyItemCondition()).thenReturn(xPolicyItemConditionDao);
    Mockito.when(xPolicyItemConditionDao.findByPolicyItemId(policyItemCondition.getId())).thenReturn(policyItemConditionList);
    Mockito.when(daoManager.getXXPolicyItemGroupPerm()).thenReturn(xPolicyItemGroupPermDao);
    Mockito.when(xPolicyItemGroupPermDao.findByPolicyItemId(policyItem.getId())).thenReturn(policyItemGroupPermList);
    Mockito.when(daoManager.getXXPolicyItemUserPerm()).thenReturn(xPolicyItemUserPermDao);
    Mockito.when(xPolicyItemUserPermDao.findByPolicyItemId(Id)).thenReturn(policyItemUserPermList);
    Mockito.when(daoManager.getXXPolicyItemAccess()).thenReturn(xPolicyItemAccessDao);
    Mockito.when(xPolicyItemAccessDao.findByPolicyItemId(policyItemAccess.getId())).thenReturn(policyItemAccessList);
    Mockito.when(daoManager.getXXPolicyResource()).thenReturn(xPolicyResourceDao);
    Mockito.when(xPolicyResourceDao.findByPolicyId(policyResource.getId())).thenReturn(policyResourceList);
    Mockito.when(daoManager.getXXPolicyResourceMap()).thenReturn(xPolicyResourceMapDao);
    Mockito.when(xPolicyResourceMapDao.findByPolicyResId(policyResourceMap.getId())).thenReturn(policyResourceMapList);
    Mockito.when(daoManager.getXXService()).thenReturn(xServiceDao);
    Mockito.when(xServiceDao.getById(Id)).thenReturn(xService);
    Mockito.when(daoManager.getXXServiceVersionInfo()).thenReturn(xServiceVersionInfoDao);
    Mockito.when(xServiceVersionInfoDao.findByServiceId(Id)).thenReturn(xServiceVersionInfo);
    Mockito.when(xServiceVersionInfoDao.update(xServiceVersionInfo)).thenReturn(xServiceVersionInfo);
    Mockito.when(daoManager.getXXService()).thenReturn(xServiceDao);
    Mockito.when(xServiceDao.getById(rangerService.getId())).thenReturn(xService);
    Mockito.when(!bizUtil.hasAccess(xService, null)).thenReturn(true);
    Mockito.when(daoManager.getXXPolicyLabelMap()).thenReturn(xPolicyLabelMapDao);
    Mockito.when(xPolicyLabelMapDao.findByPolicyId(rangerPolicy.getId())).thenReturn(xxPolicyLabelMapList);
    serviceDBStore.deletePolicy(Id);
}
Also used : ArrayList(java.util.ArrayList) VXString(org.apache.ranger.view.VXString) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) RangerService(org.apache.ranger.plugin.model.RangerService) Date(java.util.Date) Test(org.junit.Test)

Example 80 with RangerPolicy

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

the class TestServiceDBStore method test31getPaginatedPolicies.

@Test
public void test31getPaginatedPolicies() throws Exception {
    SearchFilter filter = new SearchFilter();
    filter.setParam(SearchFilter.POLICY_NAME, "policyName");
    filter.setParam(SearchFilter.SERVICE_NAME, "serviceName");
    RangerPolicyList policyListObj = new RangerPolicyList();
    policyListObj.setPageSize(0);
    policyListObj.setResultSize(1);
    policyListObj.setSortBy("asc");
    policyListObj.setSortType("1");
    policyListObj.setStartIndex(0);
    policyListObj.setTotalCount(10);
    PList<RangerPolicy> dbRangerPolicyList = serviceDBStore.getPaginatedPolicies(filter);
    Assert.assertNotNull(dbRangerPolicyList);
}
Also used : RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) SearchFilter(org.apache.ranger.plugin.util.SearchFilter) RangerPolicyList(org.apache.ranger.view.RangerPolicyList) Test(org.junit.Test)

Aggregations

RangerPolicy (org.apache.ranger.plugin.model.RangerPolicy)196 ArrayList (java.util.ArrayList)78 Test (org.junit.Test)73 RangerService (org.apache.ranger.plugin.model.RangerService)52 VXString (org.apache.ranger.view.VXString)48 HashMap (java.util.HashMap)38 RangerPolicyItem (org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItem)36 RangerPolicyResource (org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource)33 SearchFilter (org.apache.ranger.plugin.util.SearchFilter)30 WebApplicationException (javax.ws.rs.WebApplicationException)29 RangerServiceDef (org.apache.ranger.plugin.model.RangerServiceDef)27 RangerPolicyItemAccess (org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItemAccess)26 Path (javax.ws.rs.Path)23 Produces (javax.ws.rs.Produces)22 RangerPerfTracer (org.apache.ranger.plugin.util.RangerPerfTracer)20 Date (java.util.Date)19 IOException (java.io.IOException)18 XXService (org.apache.ranger.entity.XXService)18 ServicePolicies (org.apache.ranger.plugin.util.ServicePolicies)16 RangerPolicyList (org.apache.ranger.view.RangerPolicyList)15