Search in sources :

Example 36 with XXPolicy

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

the class ServiceDBStore method updatePolicy.

@Override
public RangerPolicy updatePolicy(RangerPolicy policy) throws Exception {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> ServiceDBStore.updatePolicy(" + policy + ")");
    }
    XXPolicy xxExisting = daoMgr.getXXPolicy().getById(policy.getId());
    RangerPolicy existing = policyService.getPopulatedViewObject(xxExisting);
    if (existing == null) {
        throw new Exception("no policy exists with ID=" + policy.getId());
    }
    RangerService service = getServiceByName(policy.getService());
    if (service == null) {
        throw new Exception("service does not exist - name=" + policy.getService());
    }
    XXServiceDef xServiceDef = daoMgr.getXXServiceDef().findByName(service.getType());
    if (xServiceDef == null) {
        throw new Exception("service-def does not exist - name=" + service.getType());
    }
    if (!StringUtils.equalsIgnoreCase(existing.getService(), policy.getService())) {
        throw new Exception("policy id=" + policy.getId() + " already exists in service " + existing.getService() + ". It can not be moved to service " + policy.getService());
    }
    boolean renamed = !StringUtils.equalsIgnoreCase(policy.getName(), existing.getName());
    if (renamed) {
        XXPolicy newNamePolicy = daoMgr.getXXPolicy().findByNameAndServiceIdAndZoneId(policy.getName(), service.getId(), xxExisting.getZoneId());
        if (newNamePolicy != null) {
            throw new Exception("another policy already exists with name '" + policy.getName() + "'. ID=" + newNamePolicy.getId());
        }
    }
    List<String> policyLabels = policy.getPolicyLabels();
    Set<String> uniquePolicyLabels = new TreeSet<>(policyLabels);
    policy.setCreateTime(xxExisting.getCreateTime());
    if (StringUtils.isEmpty(policy.getGuid())) {
        policy.setGuid(xxExisting.getGuid());
    }
    policy.setVersion(xxExisting.getVersion());
    List<XXTrxLog> trxLogList = policyService.getTransactionLog(policy, xxExisting, existing, RangerPolicyService.OPERATION_UPDATE_CONTEXT);
    updatePolicySignature(policy);
    policy = policyService.update(policy);
    XXPolicy newUpdPolicy = daoMgr.getXXPolicy().getById(policy.getId());
    policyRefUpdater.cleanupRefTables(policy);
    deleteExistingPolicyLabel(policy);
    policyRefUpdater.createNewPolMappingForRefTable(policy, newUpdPolicy, xServiceDef);
    createOrMapLabels(newUpdPolicy, uniquePolicyLabels);
    RangerPolicy updPolicy = policyService.getPopulatedViewObject(newUpdPolicy);
    boolean updateServiceInfoRoleVersion = false;
    if (isSupportsRolesDownloadByService()) {
        updateServiceInfoRoleVersion = isRoleDownloadRequired(updPolicy, service);
    }
    handlePolicyUpdate(service, RangerPolicyDelta.CHANGE_TYPE_POLICY_UPDATE, updPolicy, updateServiceInfoRoleVersion);
    dataHistService.createObjectDataHistory(updPolicy, RangerDataHistService.ACTION_UPDATE);
    bizUtil.createTrxLog(trxLogList);
    return updPolicy;
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) TreeSet(java.util.TreeSet) RangerService(org.apache.ranger.plugin.model.RangerService) VXString(org.apache.ranger.view.VXString) XXTrxLog(org.apache.ranger.entity.XXTrxLog) XXPolicy(org.apache.ranger.entity.XXPolicy) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) JSONException(org.codehaus.jettison.json.JSONException)

Example 37 with XXPolicy

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

the class ServiceDBStore method createPolicy.

@Override
public RangerPolicy createPolicy(RangerPolicy policy) throws Exception {
    RangerService service = getServiceByName(policy.getService());
    if (service == null) {
        throw new Exception("service does not exist - name=" + policy.getService());
    }
    XXServiceDef xServiceDef = daoMgr.getXXServiceDef().findByName(service.getType());
    if (xServiceDef == null) {
        throw new Exception("service-def does not exist - name=" + service.getType());
    }
    Long zoneId = RangerSecurityZone.RANGER_UNZONED_SECURITY_ZONE_ID;
    String zoneName = policy.getZoneName();
    if (StringUtils.isNotEmpty(zoneName)) {
        RangerSecurityZone zone = getSecurityZone(zoneName);
        if (zone == null) {
            throw new Exception("zone does not exist - name=" + zoneName);
        } else {
            zoneId = zone.getId();
        }
    }
    XXPolicy existing = daoMgr.getXXPolicy().findByNameAndServiceIdAndZoneId(policy.getName(), service.getId(), zoneId);
    if (existing != null) {
        throw new Exception("policy already exists: ServiceName=" + policy.getService() + "; PolicyName=" + policy.getName() + ". ID=" + existing.getId());
    }
    List<String> policyLabels = policy.getPolicyLabels();
    Set<String> uniquePolicyLabels = new TreeSet<>(policyLabels);
    policy.setVersion(Long.valueOf(1));
    updatePolicySignature(policy);
    if (populateExistingBaseFields) {
        assignedIdPolicyService.setPopulateExistingBaseFields(true);
        daoMgr.getXXPolicy().setIdentityInsert(true);
        policy = assignedIdPolicyService.create(policy, true);
        daoMgr.getXXPolicy().setIdentityInsert(false);
        daoMgr.getXXPolicy().updateSequence();
        assignedIdPolicyService.setPopulateExistingBaseFields(false);
    } else {
        policy = policyService.create(policy, true);
    }
    XXPolicy xCreatedPolicy = daoMgr.getXXPolicy().getById(policy.getId());
    policyRefUpdater.createNewPolMappingForRefTable(policy, xCreatedPolicy, xServiceDef);
    createOrMapLabels(xCreatedPolicy, uniquePolicyLabels);
    RangerPolicy createdPolicy = policyService.getPopulatedViewObject(xCreatedPolicy);
    boolean updateServiceInfoRoleVersion = false;
    if (isSupportsRolesDownloadByService()) {
        updateServiceInfoRoleVersion = isRoleDownloadRequired(createdPolicy, service);
    }
    handlePolicyUpdate(service, RangerPolicyDelta.CHANGE_TYPE_POLICY_CREATE, createdPolicy, updateServiceInfoRoleVersion);
    dataHistService.createObjectDataHistory(createdPolicy, RangerDataHistService.ACTION_CREATE);
    List<XXTrxLog> trxLogList = getTransactionLogList(createdPolicy, RangerPolicyService.OPERATION_IMPORT_CREATE_CONTEXT, RangerPolicyService.OPERATION_CREATE_CONTEXT);
    bizUtil.createTrxLog(trxLogList);
    return createdPolicy;
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) RangerSecurityZone(org.apache.ranger.plugin.model.RangerSecurityZone) VXString(org.apache.ranger.view.VXString) XXTrxLog(org.apache.ranger.entity.XXTrxLog) XXPolicy(org.apache.ranger.entity.XXPolicy) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) JSONException(org.codehaus.jettison.json.JSONException) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) TreeSet(java.util.TreeSet) RangerService(org.apache.ranger.plugin.model.RangerService)

Example 38 with XXPolicy

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

the class ServiceDBStore method getPolicyId.

@Override
public Long getPolicyId(final Long serviceId, final String policyName, final Long zoneId) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> ServiceDBStore.getPolicyId()");
    }
    Long ret = null;
    XXPolicy xxPolicy = daoMgr.getXXPolicy().findByNameAndServiceIdAndZoneId(policyName, serviceId, zoneId);
    if (xxPolicy != null) {
        ret = xxPolicy.getId();
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== ServiceDBStore.getPolicyId()");
    }
    return ret;
}
Also used : XXPolicy(org.apache.ranger.entity.XXPolicy)

Example 39 with XXPolicy

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

the class TestRangerPolicyServiceBase method test2mapViewToEntityBeanNullValue.

@Test
public void test2mapViewToEntityBeanNullValue() {
    XXServiceDao xServiceDao = Mockito.mock(XXServiceDao.class);
    RangerPolicy rangerPolicy = rangerPolicy();
    XXPolicy policy = policy();
    int OPERATION_CONTEXT = 0;
    Mockito.when(restErrorUtil.createRESTException("No corresponding service found for policyName: " + rangerPolicy.getName() + "Service Not Found : " + rangerPolicy.getName(), MessageEnums.INVALID_INPUT_DATA)).thenThrow(new WebApplicationException());
    thrown.expect(WebApplicationException.class);
    Mockito.when(daoManager.getXXService()).thenReturn(xServiceDao);
    Mockito.when(xServiceDao.findByName(rangerPolicy.getService())).thenReturn(null);
    XXPolicy dbPolicy = policyService.mapViewToEntityBean(rangerPolicy, policy, OPERATION_CONTEXT);
    Assert.assertNotNull(dbPolicy);
    Assert.assertEquals(dbPolicy.getId(), policy.getId());
    Assert.assertEquals(dbPolicy.getGuid(), policy.getGuid());
    Assert.assertEquals(dbPolicy.getName(), policy.getName());
    Assert.assertEquals(dbPolicy.getAddedByUserId(), policy.getAddedByUserId());
    Assert.assertEquals(dbPolicy.getIsEnabled(), policy.getIsEnabled());
    Assert.assertEquals(dbPolicy.getVersion(), policy.getVersion());
    Assert.assertEquals(dbPolicy.getDescription(), policy.getDescription());
    Mockito.verify(daoManager).getXXService();
}
Also used : RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) WebApplicationException(javax.ws.rs.WebApplicationException) XXServiceDao(org.apache.ranger.db.XXServiceDao) XXPolicy(org.apache.ranger.entity.XXPolicy) Test(org.junit.Test)

Example 40 with XXPolicy

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

the class TestRangerPolicyServiceBase method test1mapViewToEntityBean.

@Test
public void test1mapViewToEntityBean() {
    XXServiceDao xServiceDao = Mockito.mock(XXServiceDao.class);
    XXService xService = Mockito.mock(XXService.class);
    XXServiceDefDao xServiceDefDao = Mockito.mock(XXServiceDefDao.class);
    XXServiceDef xServiceDef = Mockito.mock(XXServiceDef.class);
    RangerPolicy rangerPolicy = rangerPolicy();
    XXPolicy policy = policy();
    int OPERATION_CONTEXT = 0;
    Mockito.when(daoManager.getXXService()).thenReturn(xServiceDao);
    Mockito.when(xServiceDao.findByName(rangerPolicy.getService())).thenReturn(xService);
    Mockito.when(daoManager.getXXServiceDef()).thenReturn(xServiceDefDao);
    Mockito.when(xServiceDefDao.getById(xService.getType())).thenReturn(xServiceDef);
    XXPolicy dbPolicy = policyService.mapViewToEntityBean(rangerPolicy, policy, OPERATION_CONTEXT);
    Assert.assertNotNull(dbPolicy);
    Assert.assertEquals(dbPolicy.getId(), policy.getId());
    Assert.assertEquals(dbPolicy.getGuid(), policy.getGuid());
    Assert.assertEquals(dbPolicy.getName(), policy.getName());
    Assert.assertEquals(dbPolicy.getAddedByUserId(), policy.getAddedByUserId());
    Assert.assertEquals(dbPolicy.getIsEnabled(), policy.getIsEnabled());
    Assert.assertEquals(dbPolicy.getVersion(), policy.getVersion());
    Assert.assertEquals(dbPolicy.getDescription(), policy.getDescription());
    Mockito.verify(daoManager).getXXService();
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) XXServiceDefDao(org.apache.ranger.db.XXServiceDefDao) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) XXServiceDao(org.apache.ranger.db.XXServiceDao) XXService(org.apache.ranger.entity.XXService) XXPolicy(org.apache.ranger.entity.XXPolicy) Test(org.junit.Test)

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