You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ranger.apache.org by rm...@apache.org on 2016/12/17 02:53:20 UTC
incubator-ranger git commit: RANGER-1189:Enhance and provide APIs to
getAllpolicies with different filter options
Repository: incubator-ranger
Updated Branches:
refs/heads/ranger-0.6 043daf31b -> f2990fe78
RANGER-1189:Enhance and provide APIs to getAllpolicies with different filter options
Project: http://git-wip-us.apache.org/repos/asf/incubator-ranger/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ranger/commit/f2990fe7
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ranger/tree/f2990fe7
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ranger/diff/f2990fe7
Branch: refs/heads/ranger-0.6
Commit: f2990fe78260b08144a204697770ad374d1a3d6b
Parents: 043daf3
Author: rmani <rm...@hortonworks.com>
Authored: Fri Oct 21 10:13:31 2016 -0700
Committer: rmani <rm...@hortonworks.com>
Committed: Fri Dec 16 18:52:08 2016 -0800
----------------------------------------------------------------------
.../org/apache/ranger/rest/PublicAPIsv2.java | 21 ++++++
.../apache/ranger/rest/TestPublicAPIsv2.java | 69 +++++++++++++++++++-
2 files changed, 89 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/f2990fe7/security-admin/src/main/java/org/apache/ranger/rest/PublicAPIsv2.java
----------------------------------------------------------------------
diff --git a/security-admin/src/main/java/org/apache/ranger/rest/PublicAPIsv2.java b/security-admin/src/main/java/org/apache/ranger/rest/PublicAPIsv2.java
index 6ecb356..19ddc87 100644
--- a/security-admin/src/main/java/org/apache/ranger/rest/PublicAPIsv2.java
+++ b/security-admin/src/main/java/org/apache/ranger/rest/PublicAPIsv2.java
@@ -39,6 +39,7 @@ import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
+import java.util.ArrayList;
import java.util.List;
@Path("public/v2")
@@ -283,6 +284,26 @@ public class PublicAPIsv2 {
}
@GET
+ @Path("/api/policy/")
+ @Produces({ "application/json", "application/xml" })
+ public List<RangerPolicy> getPolicies(@Context HttpServletRequest request) {
+
+ List<RangerPolicy> ret = new ArrayList<RangerPolicy>();
+
+ if(logger.isDebugEnabled()) {
+ logger.debug("==> PublicAPIsv2.getPolicies()");
+ }
+
+ ret = serviceREST.getPolicies(request).getPolicies();
+
+ if(logger.isDebugEnabled()) {
+ logger.debug("<== PublicAPIsv2.getPolicies(Request: " + request.getQueryString() + " Result Size: " + ret.size() );
+ }
+
+ return ret;
+ }
+
+ @GET
@Path("/api/service/{servicename}/policy/{policyname}")
@Produces({ "application/json", "application/xml" })
public RangerPolicy getPolicyByName(@PathParam("servicename") String serviceName,
http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/f2990fe7/security-admin/src/test/java/org/apache/ranger/rest/TestPublicAPIsv2.java
----------------------------------------------------------------------
diff --git a/security-admin/src/test/java/org/apache/ranger/rest/TestPublicAPIsv2.java b/security-admin/src/test/java/org/apache/ranger/rest/TestPublicAPIsv2.java
index 85934f5..798e2a5 100644
--- a/security-admin/src/test/java/org/apache/ranger/rest/TestPublicAPIsv2.java
+++ b/security-admin/src/test/java/org/apache/ranger/rest/TestPublicAPIsv2.java
@@ -27,6 +27,7 @@ import javax.servlet.http.HttpServletRequest;
import org.apache.ranger.common.ContextUtil;
import org.apache.ranger.common.RESTErrorUtil;
+import org.apache.ranger.common.RangerSearchUtil;
import org.apache.ranger.common.UserSessionBase;
import org.apache.ranger.plugin.model.RangerPolicy;
import org.apache.ranger.plugin.model.RangerService;
@@ -44,6 +45,7 @@ import org.apache.ranger.plugin.model.RangerServiceDef.RangerServiceConfigDef;
import org.apache.ranger.plugin.util.SearchFilter;
import org.apache.ranger.security.context.RangerContextHolder;
import org.apache.ranger.security.context.RangerSecurityContext;
+import org.apache.ranger.service.RangerPolicyService;
import org.apache.ranger.view.RangerPolicyList;
import org.apache.ranger.view.RangerServiceDefList;
import org.apache.ranger.view.RangerServiceList;
@@ -66,13 +68,21 @@ public class TestPublicAPIsv2 {
private static Long Id = 8L;
+ private static Long Id2 =10L;
+
@InjectMocks
PublicAPIsv2 publicAPIsv2 = new PublicAPIsv2();
-
+
@Mock
ServiceREST serviceREST;
@Mock
+ RangerSearchUtil searchUtil;
+
+ @Mock
+ RangerPolicyService policyService;
+
+ @Mock
RESTErrorUtil restErrorUtil;
@Rule
@@ -185,6 +195,46 @@ public class TestPublicAPIsv2 {
return policy;
}
+ private RangerPolicy rangerPolicy1() {
+ List<RangerPolicyItemAccess> accesses = new ArrayList<RangerPolicyItemAccess>();
+ List<String> users = new ArrayList<String>();
+ List<String> groups = new ArrayList<String>();
+ List<RangerPolicyItemCondition> conditions = new ArrayList<RangerPolicyItemCondition>();
+ List<RangerPolicyItem> policyItems = new ArrayList<RangerPolicyItem>();
+ RangerPolicyItem rangerPolicyItem = new RangerPolicyItem();
+ rangerPolicyItem.setAccesses(accesses);
+ rangerPolicyItem.setConditions(conditions);
+ rangerPolicyItem.setGroups(groups);
+ rangerPolicyItem.setUsers(users);
+ rangerPolicyItem.setDelegateAdmin(false);
+
+ policyItems.add(rangerPolicyItem);
+
+ Map<String, RangerPolicyResource> policyResource = new HashMap<String, RangerPolicyResource>();
+ RangerPolicyResource rangerPolicyResource = new RangerPolicyResource();
+ rangerPolicyResource.setIsExcludes(true);
+ rangerPolicyResource.setIsRecursive(true);
+ rangerPolicyResource.setValue("2");
+ rangerPolicyResource.setValues(users);
+ policyResource.put("resource", rangerPolicyResource);
+ RangerPolicy policy = new RangerPolicy();
+ policy.setId(Id2);
+ policy.setCreateTime(new Date());
+ policy.setDescription("policy");
+ policy.setGuid("policyguid");
+ policy.setIsEnabled(true);
+ policy.setName("HDFS_1-1-20150316062454");
+ policy.setUpdatedBy("Admin");
+ policy.setUpdateTime(new Date());
+ policy.setService("HDFS_1-1-20150316062454");
+ policy.setIsAuditEnabled(true);
+ policy.setPolicyItems(policyItems);
+ policy.setResources(policyResource);
+ policy.setService("HDFS_2");
+
+ return policy;
+ }
+
@Test
public void test1getServiceDef() throws Exception {
RangerServiceDef rangerServiceDef = rangerServiceDef();
@@ -536,4 +586,21 @@ public class TestPublicAPIsv2 {
Mockito.verify(serviceREST).getPolicies((SearchFilter) Mockito.anyObject());
Mockito.verify(serviceREST).deletePolicy(Id);
}
+
+ @Test
+ public void test26getPolicies() throws Exception {
+ HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
+ RangerPolicyList policyList = Mockito.mock(RangerPolicyList.class);
+ List<RangerPolicy> rangerPolicies = new ArrayList<RangerPolicy>();
+ RangerPolicy rangerpolicy1 = rangerPolicy();
+ RangerPolicy rangerpolicy2 = rangerPolicy1();
+ rangerPolicies.add(rangerpolicy1);
+ rangerPolicies.add(rangerpolicy2);
+ Mockito.when(serviceREST.getPolicies(request)).thenReturn(policyList);
+ Mockito.when(policyList.getPolicies()).thenReturn(rangerPolicies);
+ List<RangerPolicy> dbRangerPolicies = publicAPIsv2.getPolicies(request);
+ Assert.assertNotNull(dbRangerPolicies);
+ Assert.assertEquals(dbRangerPolicies.size(), rangerPolicies.size());
+ Mockito.verify(serviceREST).getPolicies(request);
+ }
}