Skip to content

Commit 2ac2449

Browse files
RANGER-3439: Add rest api to get or delete ranger policy based on guid - #2
1 parent d2931d3 commit 2ac2449

File tree

5 files changed

+47
-23
lines changed

5 files changed

+47
-23
lines changed

security-admin/src/main/java/org/apache/ranger/biz/ServiceDBStore.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2298,10 +2298,10 @@ public RangerPolicy getPolicy(Long id) throws Exception {
22982298
return policyService.read(id);
22992299
}
23002300

2301-
public RangerPolicy getPolicy(String guid, String serviceName) throws Exception {
2301+
public RangerPolicy getPolicy(String guid, String serviceName, String zoneName) throws Exception {
23022302
RangerPolicy ret = null;
23032303
if (StringUtils.isNotBlank(guid) && StringUtils.isNotBlank(serviceName)) {
2304-
XXPolicy xPolicy = daoMgr.getXXPolicy().findByPolicyGUIDAndServiceName(guid, serviceName);
2304+
XXPolicy xPolicy = daoMgr.getXXPolicy().findPolicyByGUIDAndServiceNameAndZoneName(guid, serviceName, zoneName);
23052305
if (xPolicy != null) {
23062306
ret = policyService.getPopulatedViewObject(xPolicy);
23072307
}

security-admin/src/main/java/org/apache/ranger/db/XXPolicyDao.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,13 +285,25 @@ public List<XXPolicy> getAllByPolicyItem() {
285285
return ret;
286286
}
287287

288-
public XXPolicy findByPolicyGUIDAndServiceName(String guid, String serviceName) {
288+
public XXPolicy findPolicyByGUIDAndServiceNameAndZoneName(String guid, String serviceName, String zoneName) {
289289
if (guid == null || serviceName == null) {
290290
return null;
291291
}
292292
try {
293-
XXPolicy xPol = getEntityManager().createNamedQuery("XXPolicy.findByGUIDAndServiceName", tClass).setParameter("guid", guid).setParameter("serviceName", serviceName).getSingleResult();
294-
return xPol;
293+
if (zoneName == null || zoneName.trim().isEmpty()) {
294+
return getEntityManager().createNamedQuery("XXPolicy.findPolicyByPolicyGUIDAndServiceName", tClass)
295+
.setParameter("guid", guid)
296+
.setParameter("serviceName", serviceName)
297+
.setParameter("zoneId", RangerSecurityZone.RANGER_UNZONED_SECURITY_ZONE_ID)
298+
.getSingleResult();
299+
} else {
300+
return getEntityManager()
301+
.createNamedQuery("XXPolicy.findPolicyByPolicyGUIDAndServiceNameAndZoneName", tClass)
302+
.setParameter("guid", guid)
303+
.setParameter("serviceName", serviceName)
304+
.setParameter("zoneName", zoneName)
305+
.getSingleResult();
306+
}
295307
} catch (NoResultException e) {
296308
return null;
297309
}

security-admin/src/main/java/org/apache/ranger/rest/PublicAPIsv2.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,10 @@ public List<RangerPolicy> getPoliciesForResource(@PathParam("serviceDefName") St
414414
@GET
415415
@Path("/api/policy/guid/{guid}")
416416
@Produces({ "application/json", "application/xml" })
417-
public RangerPolicy getPolicyByGUIDAndServiceName(@PathParam("guid") String guid, @DefaultValue("") @QueryParam("serviceName") String serviceName) {
418-
return serviceREST.getPolicyByGUIDAndServiceName(guid, serviceName);
417+
public RangerPolicy getPolicyByGUIDAndServiceNameAndZoneName(@PathParam("guid") String guid,
418+
@DefaultValue("") @QueryParam("serviceName") String serviceName,
419+
@DefaultValue("") @QueryParam("ZoneName") String zoneName) {
420+
return serviceREST.getPolicyByGUIDAndServiceNameAndZoneName(guid, serviceName, zoneName);
419421
}
420422

421423
@POST
@@ -517,8 +519,10 @@ public void deletePolicyByName(@QueryParam("servicename") String serviceName,
517519
@DELETE
518520
@Path("/api/policy/guid/{guid}")
519521
@Produces({ "application/json", "application/xml" })
520-
public void deletePolicyByGUIDAndServiceName(@PathParam("guid") String guid, @DefaultValue("") @QueryParam("serviceName") String serviceName) {
521-
serviceREST.deletePolicyByGUIDAndServiceName(guid, serviceName);
522+
public void deletePolicyByGUIDAndServiceNameAndZoneName(@PathParam("guid") String guid,
523+
@DefaultValue("") @QueryParam("serviceName") String serviceName,
524+
@DefaultValue("") @QueryParam("zoneName") String zoneName) {
525+
serviceREST.deletePolicyByGUIDAndServiceNameAndZoneName(guid, serviceName, zoneName);
522526
}
523527

524528
@GET

security-admin/src/main/java/org/apache/ranger/rest/ServiceREST.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3761,24 +3761,26 @@ public ResponseEntity<List<ServiceDeleteResponse>> deleteClusterServices(@PathPa
37613761
@GET
37623762
@Path("/policies/guid/{guid}")
37633763
@Produces({ "application/json", "application/xml" })
3764-
public RangerPolicy getPolicyByGUIDAndServiceName(@PathParam("guid") String guid, @DefaultValue("") @QueryParam("serviceName") String serviceName) {
3764+
public RangerPolicy getPolicyByGUIDAndServiceNameAndZoneName(@PathParam("guid") String guid,
3765+
@DefaultValue("") @QueryParam("serviceName") String serviceName,
3766+
@DefaultValue("") @QueryParam("zoneName") String zoneName) {
37653767
if (LOG.isDebugEnabled()) {
3766-
LOG.debug("==> ServiceREST.getPolicyByGUIDAndServiceName(" + guid + ", " + serviceName + ")");
3768+
LOG.debug("==> ServiceREST.getPolicyByGUIDAndServiceNameAndZoneName(" + guid + ", " + serviceName + ", " + zoneName + ")");
37673769
}
37683770
RangerPolicy ret = null;
37693771
RangerPerfTracer perf = null;
37703772
try {
37713773
if (RangerPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
3772-
perf = RangerPerfTracer.getPerfTracer(PERF_LOG, "ServiceREST.getPolicyByGUIDAndServiceName(policyGUID=" + guid + ", serviceName="+ serviceName + ")");
3774+
perf = RangerPerfTracer.getPerfTracer(PERF_LOG, "ServiceREST.getPolicyByGUIDAndServiceNameAndZoneName(policyGUID=" + guid + ", serviceName="+ serviceName + ", zoneName="+ zoneName + ")");
37733775
}
3774-
ret = svcStore.getPolicy(guid, serviceName);
3776+
ret = svcStore.getPolicy(guid, serviceName, zoneName);
37753777
if (ret != null) {
37763778
ensureAdminAndAuditAccess(ret);
37773779
}
37783780
} catch (WebApplicationException excp) {
37793781
throw excp;
37803782
} catch (Throwable excp) {
3781-
LOG.error("getPolicyByGUIDAndServiceName(" + guid + "," + serviceName + ") failed", excp);
3783+
LOG.error("getPolicyByGUIDAndServiceNameAndZoneName(" + guid + "," + serviceName + ", " + zoneName + ") failed", excp);
37823784
throw restErrorUtil.createRESTException(excp.getMessage());
37833785
} finally {
37843786
RangerPerfTracer.log(perf);
@@ -3787,38 +3789,40 @@ public RangerPolicy getPolicyByGUIDAndServiceName(@PathParam("guid") String guid
37873789
throw restErrorUtil.createRESTException(HttpServletResponse.SC_NOT_FOUND, "Not found", true);
37883790
}
37893791
if (LOG.isDebugEnabled()) {
3790-
LOG.debug("<== ServiceREST.getPolicyByGUIDAndServiceName(" + guid + ", " + serviceName + "): " + ret);
3792+
LOG.debug("<== ServiceREST.getPolicyByGUIDAndServiceNameAndZoneName(" + guid + ", " + serviceName + ", " + zoneName +"): " + ret);
37913793
}
37923794
return ret;
37933795
}
37943796

37953797
@DELETE
37963798
@Path("/policies/guid/{guid}")
37973799
@Produces({ "application/json", "application/xml" })
3798-
public void deletePolicyByGUIDAndServiceName(@PathParam("guid") String guid, @DefaultValue("") @QueryParam("serviceName") String serviceName) {
3800+
public void deletePolicyByGUIDAndServiceNameAndZoneName(@PathParam("guid") String guid,
3801+
@DefaultValue("") @QueryParam("serviceName") String serviceName,
3802+
@DefaultValue("") @QueryParam("zoneName") String zoneName) {
37993803
if (LOG.isDebugEnabled()) {
3800-
LOG.debug("==> ServiceREST.deletePolicyByGUIDAndServiceName(" + guid + ", " + serviceName + ")");
3804+
LOG.debug("==> ServiceREST.deletePolicyByGUIDAndServiceNameAndZoneName(" + guid + ", " + serviceName + ", " + zoneName +")");
38013805
}
38023806
RangerPolicy ret = null;
38033807
RangerPerfTracer perf = null;
38043808
try {
38053809
if (RangerPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
3806-
perf = RangerPerfTracer.getPerfTracer(PERF_LOG, "ServiceREST.deletePolicyByGUIDAndServiceName(policyGUID=" + guid + ", serviceName="+ serviceName + ")");
3810+
perf = RangerPerfTracer.getPerfTracer(PERF_LOG, "ServiceREST.deletePolicyByGUIDAndServiceNameAndZoneName(policyGUID=" + guid + ", serviceName="+ serviceName + ", zoneName="+ zoneName +")");
38073811
}
3808-
ret = getPolicyByGUIDAndServiceName(guid, serviceName);
3812+
ret = getPolicyByGUIDAndServiceNameAndZoneName(guid, serviceName, zoneName);
38093813
if (ret != null) {
38103814
deletePolicy(ret.getId());
38113815
}
38123816
} catch (WebApplicationException excp) {
38133817
throw excp;
38143818
} catch (Throwable excp) {
3815-
LOG.error("deletePolicyByGUIDAndServiceName(" + guid + "," + serviceName + ") failed", excp);
3819+
LOG.error("deletePolicyByGUIDAndServiceNameAndZoneName(" + guid + "," + serviceName + ", " + zoneName + ") failed", excp);
38163820
throw restErrorUtil.createRESTException(excp.getMessage());
38173821
} finally {
38183822
RangerPerfTracer.log(perf);
38193823
}
38203824
if (LOG.isDebugEnabled()) {
3821-
LOG.debug("<== ServiceREST.deletePolicyByGUIDAndServiceName(" + guid + ", " + serviceName + ")");
3825+
LOG.debug("<== ServiceREST.deletePolicyByGUIDAndServiceNameAndZoneName(" + guid + ", " + serviceName + ", " + zoneName +")");
38223826
}
38233827
}
38243828

security-admin/src/main/resources/META-INF/jpa_named_queries.xml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,12 @@
393393
<query>select obj from XXPolicy obj where obj.id in (select item.policyId from XXPolicyItem item) </query>
394394
</named-query>
395395

396-
<named-query name="XXPolicy.findByGUIDAndServiceName">
397-
<query>select obj from XXPolicy obj, XXService svc where obj.guid = :guid and obj.service = svc.id and svc.name = :serviceName</query>
396+
<named-query name="XXPolicy.findPolicyByPolicyGUIDAndServiceName">
397+
<query>select obj from XXPolicy obj, XXService svc where obj.guid = :guid and obj.service = svc.id and svc.name = :serviceName and obj.zoneId = :zoneId</query>
398+
</named-query>
399+
400+
<named-query name="XXPolicy.findPolicyByPolicyGUIDAndServiceNameAndZoneName">
401+
<query>select obj from XXPolicy obj, XXService svc, XXSecurityZone zone where obj.guid = :guid and obj.service = svc.id and svc.name = :serviceName and obj.zoneId = zone.id and zone.name = :zoneName</query>
398402
</named-query>
399403

400404
<!-- XXServiceDef -->

0 commit comments

Comments
 (0)