use of org.apache.ranger.plugin.util.ServicePolicies in project ranger by apache.
the class RangerBasePlugin method getDefaultSvcPolicies.
private ServicePolicies getDefaultSvcPolicies() {
ServicePolicies ret = null;
RangerServiceDef serviceDef = getServiceDef();
if (serviceDef == null) {
serviceDef = getDefaultServiceDef();
}
if (serviceDef != null) {
ret = new ServicePolicies();
ret.setServiceDef(serviceDef);
ret.setServiceName(serviceName);
ret.setPolicies(new ArrayList<RangerPolicy>());
}
return ret;
}
use of org.apache.ranger.plugin.util.ServicePolicies in project ranger by apache.
the class RangerPolicyEngineCache method getPolicyEngine.
final synchronized RangerPolicyEngine getPolicyEngine(String serviceName, ServiceStore svcStore, RangerPolicyEngineOptions options) {
RangerPolicyEngine ret = null;
if (serviceName != null) {
ret = policyEngineCache.get(serviceName);
long policyVersion = ret != null ? ret.getPolicyVersion() : -1;
if (svcStore != null) {
try {
ServicePolicies policies = svcStore.getServicePoliciesIfUpdated(serviceName, policyVersion);
if (policies != null) {
if (ret == null) {
ret = addPolicyEngine(policies, options);
} else if (policies.getPolicyVersion() != null && !policies.getPolicyVersion().equals(policyVersion)) {
ret = addPolicyEngine(policies, options);
}
}
} catch (Exception excp) {
LOG.error("getPolicyEngine(" + serviceName + "): failed to get latest policies from service-store", excp);
}
}
}
return ret;
}
use of org.apache.ranger.plugin.util.ServicePolicies in project ranger by apache.
the class TestServiceDBStore method test33getServicePoliciesIfUpdated.
@Test
public void test33getServicePoliciesIfUpdated() throws Exception {
XXServiceDao xServiceDao = Mockito.mock(XXServiceDao.class);
XXServiceVersionInfoDao xServiceVersionInfoDao = Mockito.mock(XXServiceVersionInfoDao.class);
XXService xService = new XXService();
xService.setAddedByUserId(Id);
xService.setCreateTime(new Date());
xService.setDescription("Hdfs service");
xService.setGuid("serviceguid");
xService.setId(Id);
xService.setIsEnabled(true);
xService.setName("Hdfs");
xService.setPolicyUpdateTime(new Date());
xService.setPolicyVersion(1L);
xService.setType(1L);
xService.setUpdatedByUserId(Id);
xService.setUpdateTime(new Date());
XXServiceVersionInfo xServiceVersionInfo = new XXServiceVersionInfo();
xServiceVersionInfo.setServiceId(Id);
xServiceVersionInfo.setPolicyVersion(1L);
xServiceVersionInfo.setPolicyUpdateTime(new Date());
xServiceVersionInfo.setTagVersion(1L);
xServiceVersionInfo.setTagUpdateTime(new Date());
String serviceName = "HDFS_1";
Long lastKnownVersion = 1l;
Mockito.when(daoManager.getXXService()).thenReturn(xServiceDao);
Mockito.when(daoManager.getXXServiceVersionInfo()).thenReturn(xServiceVersionInfoDao);
Mockito.when(xServiceDao.findByName(serviceName)).thenReturn(xService);
Mockito.when(xServiceVersionInfoDao.findByServiceName(serviceName)).thenReturn(xServiceVersionInfo);
ServicePolicies dbServicePolicies = serviceDBStore.getServicePoliciesIfUpdated(serviceName, lastKnownVersion);
Assert.assertNull(dbServicePolicies);
}
use of org.apache.ranger.plugin.util.ServicePolicies in project ranger by apache.
the class TestAssetREST method testGetResourceJSON.
@Test
public void testGetResourceJSON() {
RangerService rangerService = rangerService(Id);
String file = "testGetResourceJSON";
VXAsset vXAsset = vXAsset(Id);
Date date = new Date();
String strdt = date.toString();
X509Certificate[] certchain = new X509Certificate[1];
certchain[0] = Mockito.mock(X509Certificate.class);
ServicePolicies servicePolicies = servicePolicies();
RangerPolicy rangerPolicy = rangerPolicy(Id);
List<RangerPolicy> policies = new ArrayList<RangerPolicy>();
policies.add(rangerPolicy);
Mockito.when(request.getParameter("epoch")).thenReturn(strdt);
Mockito.when(request.getAttribute("javax.servlet.request.X509Certificate")).thenReturn(certchain);
Mockito.when(request.getHeader("X-FORWARDED-FOR")).thenReturn("valid");
Mockito.when(request.isSecure()).thenReturn(true);
Mockito.when(request.getParameter("policyCount")).thenReturn("4");
Mockito.when(request.getParameter("agentId")).thenReturn("12");
// Mockito.when(PropertiesUtil.getBooleanProperty("ranger.service.http.enabled",true)).thenReturn(true);
try {
Mockito.when(serviceREST.getServicePoliciesIfUpdated(Mockito.anyString(), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyString(), Mockito.anyString(), (HttpServletRequest) Mockito.any())).thenReturn(servicePolicies);
} catch (Exception e) {
fail("test failed due to: " + e.getMessage());
}
Mockito.when(serviceUtil.getServiceByName("hdfs_dev")).thenReturn(rangerService);
Mockito.when(serviceUtil.toVXAsset(rangerService)).thenReturn(vXAsset);
Mockito.when(assetMgr.getLatestRepoPolicy((VXAsset) Mockito.any(), Mockito.<VXResource>anyList(), Mockito.anyLong(), (X509Certificate[]) Mockito.any(), Mockito.anyBoolean(), Mockito.anyString(), Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(), Mockito.anyString())).thenReturn(file);
String actualFile = assetREST.getResourceJSON(request, "hdfs_dev");
Assert.assertEquals(file, actualFile);
Mockito.verify(serviceUtil).getServiceByName("hdfs_dev");
Mockito.verify(serviceUtil).toVXAsset(rangerService);
Mockito.verify(request).getParameter("epoch");
Mockito.verify(request).getAttribute("javax.servlet.request.X509Certificate");
Mockito.verify(request).getHeader("X-FORWARDED-FOR");
Mockito.verify(request).isSecure();
Mockito.verify(request).getParameter("policyCount");
Mockito.verify(request).getParameter("agentId");
Mockito.verify(assetMgr).getLatestRepoPolicy((VXAsset) Mockito.any(), Mockito.<VXResource>anyList(), Mockito.anyLong(), (X509Certificate[]) Mockito.any(), Mockito.anyBoolean(), Mockito.anyString(), Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(), Mockito.anyString());
}
use of org.apache.ranger.plugin.util.ServicePolicies in project ranger by apache.
the class TestServiceREST method test16createPolicyFalse.
@Test
public void test16createPolicyFalse() throws Exception {
RangerPolicy rangerPolicy = rangerPolicy();
RangerServiceDef rangerServiceDef = rangerServiceDef();
List<RangerPolicy> policies = new ArrayList<RangerPolicy>();
RangerPolicy rangPolicy = new RangerPolicy();
policies.add(rangPolicy);
String userName = "admin";
Set<String> userGroupsList = new HashSet<String>();
userGroupsList.add("group1");
userGroupsList.add("group2");
ServicePolicies servicePolicies = new ServicePolicies();
servicePolicies.setServiceId(Id);
servicePolicies.setServiceName("Hdfs_1");
servicePolicies.setPolicyVersion(1L);
servicePolicies.setPolicyUpdateTime(new Date());
servicePolicies.setServiceDef(rangerServiceDef);
servicePolicies.setPolicies(policies);
List<RangerAccessTypeDef> rangerAccessTypeDefList = new ArrayList<RangerServiceDef.RangerAccessTypeDef>();
RangerAccessTypeDef rangerAccessTypeDefObj = new RangerAccessTypeDef();
rangerAccessTypeDefObj.setLabel("Read");
rangerAccessTypeDefObj.setName("read");
rangerAccessTypeDefObj.setRbKeyLabel(null);
rangerAccessTypeDefList.add(rangerAccessTypeDefObj);
XXServiceDef xServiceDef = serviceDef();
XXService xService = xService();
XXServiceDefDao xServiceDefDao = Mockito.mock(XXServiceDefDao.class);
XXServiceDao xServiceDao = Mockito.mock(XXServiceDao.class);
Mockito.when(validatorFactory.getPolicyValidator(svcStore)).thenReturn(policyValidator);
Mockito.when(bizUtil.isAdmin()).thenReturn(true);
Mockito.when(bizUtil.getCurrentUserLoginId()).thenReturn(userName);
Mockito.when(daoManager.getXXService()).thenReturn(xServiceDao);
Mockito.when(xServiceDao.findByName(Mockito.anyString())).thenReturn(xService);
Mockito.when(daoManager.getXXServiceDef()).thenReturn(xServiceDefDao);
Mockito.when(xServiceDefDao.getById(xService.getType())).thenReturn(xServiceDef);
Mockito.when(svcStore.createPolicy((RangerPolicy) Mockito.any())).thenReturn(rangPolicy);
RangerPolicy dbRangerPolicy = serviceREST.createPolicy(rangerPolicy, null);
Assert.assertNotNull(dbRangerPolicy);
Mockito.verify(bizUtil, Mockito.times(2)).isAdmin();
Mockito.verify(validatorFactory).getPolicyValidator(svcStore);
Mockito.verify(daoManager).getXXService();
Mockito.verify(daoManager).getXXServiceDef();
}
Aggregations