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;
}
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;
}
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;
}
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();
}
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();
}
Aggregations