105105@ Scope ("request" )
106106@ Transactional (propagation = Propagation .REQUIRES_NEW )
107107public class GdsREST {
108- public static final String GDS_POLICY_EXPR_CONDITION = "expression" ;
109108 private static final Logger LOG = LoggerFactory .getLogger (GdsREST .class );
110109 private static final Logger PERF_LOG = RangerPerfTracer .getPerfLogger ("rest.GdsREST" );
110+
111+ public static final String GDS_POLICY_EXPR_CONDITION = "expression" ;
112+
111113 private static final String PRINCIPAL_TYPE_USER = RangerPrincipal .PrincipalType .USER .name ().toLowerCase ();
112114 private static final String PRINCIPAL_TYPE_GROUP = RangerPrincipal .PrincipalType .GROUP .name ().toLowerCase ();
113115 private static final String PRINCIPAL_TYPE_ROLE = RangerPrincipal .PrincipalType .ROLE .name ().toLowerCase ();
114116 private static final String DEFAULT_PRINCIPAL_TYPE = PRINCIPAL_TYPE_USER ;
115117 private static final RangerAdminConfig config = RangerAdminConfig .getInstance ();
116118 private static final int SHARED_RESOURCES_MAX_BATCH_SIZE = config .getInt ("ranger.admin.rest.gds.shared.resources.max.batch.size" , 100 );
119+
117120 @ Autowired
118121 GdsDBStore gdsStore ;
119122
@@ -197,19 +200,27 @@ public List<RangerSharedResource> addDatasetResources(@PathParam("id") Long data
197200 RangerPerfTracer perf = null ;
198201
199202 try {
203+ if (RangerPerfTracer .isPerfTraceEnabled (PERF_LOG )) {
204+ perf = RangerPerfTracer .getPerfTracer (PERF_LOG , "GdsREST.addDatasetResources(datasetId=" + datasetId + ")" );
205+ }
206+
200207 Long serviceId = validateAndGetServiceId (serviceName );
201208 Long zoneId = validateAndGetZoneId (zoneName );
202209 Long dataShareId = getOrCreateDataShare (datasetId , serviceId , zoneId , serviceName );
210+
203211 // Add resources to DataShare
204212 for (RangerSharedResource resource : resources ) {
205213 resource .setDataShareId (dataShareId );
214+
206215 RangerSharedResource rangerSharedResource = addSharedResource (resource );
216+
207217 ret .add (rangerSharedResource );
208218 }
209219 } catch (WebApplicationException excp ) {
210220 throw excp ;
211221 } catch (Throwable excp ) {
212222 LOG .error ("GdsREST.addDatasetResources(datasetId={} serviceName={} zoneName={} resources={}) failed!" , datasetId , serviceName , zoneName , resources , excp );
223+
213224 throw restErrorUtil .createRESTException (excp .getMessage ());
214225 } finally {
215226 RangerPerfTracer .log (perf );
@@ -501,6 +512,7 @@ public RangerPolicy updateDatasetPolicy(@PathParam("id") Long datasetId, @PathPa
501512
502513 try {
503514 policy .setId (policyId );
515+
504516 ret = gdsStore .updateDatasetPolicy (datasetId , policy );
505517 } catch (WebApplicationException excp ) {
506518 throw excp ;
@@ -826,6 +838,7 @@ public RangerPolicy updateProjectPolicy(@PathParam("id") Long projectId, @PathPa
826838
827839 try {
828840 policy .setId (policyId );
841+
829842 ret = gdsStore .updateProjectPolicy (projectId , policy );
830843 } catch (WebApplicationException excp ) {
831844 throw excp ;
@@ -1840,6 +1853,7 @@ public RangerPolicyHeader updateDataSetGrants(@PathParam("id") Long id, List<Ran
18401853
18411854 if (policyWithModifiedGrants != null ) {
18421855 RangerPolicy updatedPolicy = gdsStore .updateDatasetPolicy (id , policyWithModifiedGrants );
1856+
18431857 ret = rangerPolicyHeaderOf (updatedPolicy );
18441858 } else {
18451859 throw restErrorUtil .createRESTException (HttpServletResponse .SC_NOT_MODIFIED , "No action performed: The grant may already exist or may not be found for deletion." , false );
@@ -1872,8 +1886,7 @@ List<RangerPolicyItem> filterPolicyItemsByRequest(RangerPolicy rangerPolicy, Htt
18721886
18731887 Predicate <RangerPolicyItem > byPrincipalPredicate = filterByPrincipalsPredicate (filteringPrincipals );
18741888 Predicate <RangerPolicyItem > byAccessTypePredicate = filterByAccessTypesPredicate (filteringAccessTypes );
1875-
1876- List <RangerPolicyItem > filteredPolicyItems = policyItems .stream ().filter (byPrincipalPredicate .and (byAccessTypePredicate )).collect (Collectors .toList ());
1889+ List <RangerPolicyItem > filteredPolicyItems = policyItems .stream ().filter (byPrincipalPredicate .and (byAccessTypePredicate )).collect (Collectors .toList ());
18771890
18781891 LOG .debug ("<== GdsREST.filterPolicyItemsByRequest(rangerPolicy: {}): filteredPolicyItems= {}" , rangerPolicy , filteredPolicyItems );
18791892
@@ -1897,19 +1910,19 @@ List<RangerGrant> transformPolicyItemsToGrants(List<RangerPolicyItem> policyItem
18971910 List <RangerPolicyItemAccess > policyItemAccesses = policyItem .getAccesses ();
18981911 List <RangerPolicyItemCondition > policyItemConditions = policyItem .getConditions ();
18991912
1900- List <String > policyItemAccessTypes = policyItemAccesses .stream ().map (x -> x . getType () ).collect (Collectors .toList ());
1913+ List <String > policyItemAccessTypes = policyItemAccesses .stream ().map (RangerPolicyItemAccess :: getType ).collect (Collectors .toList ());
19011914 List <String > policyItemConditionValues = policyItemConditions .stream ().flatMap (x -> x .getValues ().stream ()).collect (Collectors .toList ());
19021915
19031916 if (CollectionUtils .isNotEmpty (policyItemUsers )) {
1904- policyItemUsers .stream (). forEach (x -> ret .add (new RangerGrant (new RangerPrincipal (RangerPrincipal .PrincipalType .USER , x ), policyItemAccessTypes , policyItemConditionValues )));
1917+ policyItemUsers .forEach (x -> ret .add (new RangerGrant (new RangerPrincipal (RangerPrincipal .PrincipalType .USER , x ), policyItemAccessTypes , policyItemConditionValues )));
19051918 }
19061919
19071920 if (CollectionUtils .isNotEmpty (policyItemGroups )) {
1908- policyItemGroups .stream (). forEach (x -> ret .add (new RangerGrant (new RangerPrincipal (RangerPrincipal .PrincipalType .GROUP , x ), policyItemAccessTypes , policyItemConditionValues )));
1921+ policyItemGroups .forEach (x -> ret .add (new RangerGrant (new RangerPrincipal (RangerPrincipal .PrincipalType .GROUP , x ), policyItemAccessTypes , policyItemConditionValues )));
19091922 }
19101923
19111924 if (CollectionUtils .isNotEmpty (policyItemRoles )) {
1912- policyItemRoles .stream (). forEach (x -> ret .add (new RangerGrant (new RangerPrincipal (RangerPrincipal .PrincipalType .ROLE , x ), policyItemAccessTypes , policyItemConditionValues )));
1925+ policyItemRoles .forEach (x -> ret .add (new RangerGrant (new RangerPrincipal (RangerPrincipal .PrincipalType .ROLE , x ), policyItemAccessTypes , policyItemConditionValues )));
19131926 }
19141927 }
19151928
@@ -1924,17 +1937,19 @@ RangerPolicy updatePolicyWithModifiedGrants(RangerPolicy policy, List<RangerGran
19241937 try {
19251938 List <RangerPolicyItem > policyItems = policy .getPolicyItems ();
19261939 List <RangerPolicyItem > policyItemsToUpdate = policyItems .stream ().map (this ::copyOf ).collect (Collectors .toList ());
1927-
1928- Set <RangerPrincipal > principalsToUpdate = rangerGrants .stream ().map (RangerGrant ::getPrincipal ).collect (Collectors .toSet ());
1940+ Set <RangerPrincipal > principalsToUpdate = rangerGrants .stream ().map (RangerGrant ::getPrincipal ).collect (Collectors .toSet ());
19291941
19301942 for (RangerPrincipal principal : principalsToUpdate ) {
19311943 List <RangerPolicyItem > policyItemsToRemove = new ArrayList <>();
1944+
19321945 policyItemsToUpdate .stream ().filter (matchesPrincipalPredicate (principal )).forEach (policyItem -> {
19331946 removeMatchingPrincipalFromPolicyItem (policyItem , principal );
1947+
19341948 if (isPolicyItemEmpty (policyItem )) {
19351949 policyItemsToRemove .add (policyItem );
19361950 }
19371951 });
1952+
19381953 policyItemsToUpdate .removeAll (policyItemsToRemove );
19391954 }
19401955
@@ -1953,7 +1968,9 @@ RangerPolicy updatePolicyWithModifiedGrants(RangerPolicy policy, List<RangerGran
19531968 } catch (Exception e ) {
19541969 throw restErrorUtil .createRESTException (HttpServletResponse .SC_BAD_REQUEST , e .getMessage (), true );
19551970 }
1971+
19561972 LOG .debug ("<== GdsREST.updatePolicyWithModifiedGrants(updatedPolicy: {})" , policy );
1973+
19571974 return policy ;
19581975 }
19591976
@@ -1966,33 +1983,40 @@ private Long getOrCreateDataShare(Long datasetId, Long serviceId, Long zoneId, S
19661983 String dataShareName = "__dataset_" + datasetId + "__service_" + serviceId + "__zone_" + zoneId ;
19671984
19681985 SearchFilter filter = new SearchFilter ();
1986+
19691987 filter .setParam (SearchFilter .DATA_SHARE_NAME , dataShareName );
1988+
19701989 PList <RangerDataShare > dataSharePList = gdsStore .searchDataShares (filter );
19711990 List <RangerDataShare > dataShareList = dataSharePList .getList ();
19721991
19731992 if (CollectionUtils .isNotEmpty (dataShareList )) {
19741993 List <RangerDataShare > rangerDataShares = dataSharePList .getList ();
1994+
19751995 rangerDataShare = rangerDataShares .get (0 );
19761996 ret = rangerDataShare .getId ();
19771997 } else {
19781998 //Create a DataShare
19791999 RangerDataShare dataShare = new RangerDataShare ();
2000+
19802001 dataShare .setName (dataShareName );
19812002 dataShare .setDescription (dataShareName );
19822003 dataShare .setTermsOfUse (rangerDataset .getTermsOfUse ());
19832004 dataShare .setService (serviceName );
1984- Set < String > accessTypes = new HashSet <>(CollectionUtils . EMPTY_COLLECTION );
1985- dataShare . setDefaultAccessTypes ( accessTypes );
2005+ dataShare . setDefaultAccessTypes ( new HashSet <>() );
2006+
19862007 rangerDataShare = gdsStore .createDataShare (dataShare );
19872008
19882009 //Add DataShare to DataSet
19892010 List <RangerDataShareInDataset > rangerDataShareInDatasets = new ArrayList <>();
19902011 RangerDataShareInDataset rangerDataShareInDataset = new RangerDataShareInDataset ();
2012+
19912013 rangerDataShareInDataset .setDataShareId (rangerDataShare .getId ());
19922014 rangerDataShareInDataset .setDatasetId (rangerDataset .getId ());
19932015 rangerDataShareInDataset .setStatus (RangerGds .GdsShareStatus .REQUESTED );
19942016 rangerDataShareInDatasets .add (rangerDataShareInDataset );
2017+
19952018 addDataSharesInDataset (rangerDataset .getId (), rangerDataShareInDatasets );
2019+
19962020 ret = rangerDataShare .getId ();
19972021 }
19982022
@@ -2003,8 +2027,10 @@ private Long getOrCreateDataShare(Long datasetId, Long serviceId, Long zoneId, S
20032027
20042028 private Long validateAndGetServiceId (String serviceName ) {
20052029 Long ret ;
2030+
20062031 if (serviceName == null || serviceName .isEmpty ()) {
20072032 LOG .error ("ServiceName not provided" );
2033+
20082034 throw restErrorUtil .createRESTException ("ServiceName not provided." , MessageEnums .INVALID_INPUT_DATA );
20092035 }
20102036
@@ -2015,16 +2041,19 @@ private Long validateAndGetServiceId(String serviceName) {
20152041 ret = service .getId ();
20162042 } catch (Exception e ) {
20172043 LOG .error ("Requested Service not found. serviceName={}" , serviceName );
2044+
20182045 throw restErrorUtil .createRESTException ("Service:" + serviceName + " not found" , MessageEnums .DATA_NOT_FOUND );
20192046 }
20202047
20212048 if (service == null ) {
20222049 LOG .error ("Requested Service not found. serviceName={}" , serviceName );
2050+
20232051 throw restErrorUtil .createRESTException (HttpServletResponse .SC_NOT_FOUND , RangerServiceNotFoundException .buildExceptionMsg (serviceName ), false );
20242052 }
20252053
20262054 if (!service .getIsEnabled ()) {
20272055 LOG .error ("Requested Service is disabled. serviceName={}" , serviceName );
2056+
20282057 throw restErrorUtil .createRESTException ("Unauthorized access." , MessageEnums .OPER_NOT_ALLOWED_FOR_STATE );
20292058 }
20302059
@@ -2038,18 +2067,20 @@ private Long validateAndGetZoneId(String zoneName) {
20382067 return ret ;
20392068 }
20402069
2041- RangerSecurityZone rangerSecurityZone = null ;
2070+ RangerSecurityZone rangerSecurityZone ;
20422071
20432072 try {
20442073 rangerSecurityZone = serviceDBStore .getSecurityZone (zoneName );
20452074 ret = rangerSecurityZone .getId ();
20462075 } catch (Exception e ) {
20472076 LOG .error ("Requested Zone not found. ZoneName={}" , zoneName );
2077+
20482078 throw restErrorUtil .createRESTException ("Zone:" + zoneName + " not found" , MessageEnums .DATA_NOT_FOUND );
20492079 }
20502080
20512081 if (rangerSecurityZone == null ) {
20522082 LOG .error ("Requested Zone not found. ZoneName={}" , zoneName );
2083+
20532084 throw restErrorUtil .createRESTException (HttpServletResponse .SC_NOT_FOUND , RangerServiceNotFoundException .buildExceptionMsg (zoneName ), false );
20542085 }
20552086
@@ -2060,11 +2091,13 @@ private RangerPolicyHeader rangerPolicyHeaderOf(RangerPolicy rangerPolicy) {
20602091 LOG .debug ("==> GdsREST.rangerPolicyHeaderOf(rangerPolicy: {})" , rangerPolicy );
20612092
20622093 RangerPolicyHeader ret = null ;
2094+
20632095 if (rangerPolicy != null ) {
20642096 ret = new RangerPolicyHeader (rangerPolicy );
20652097 }
20662098
20672099 LOG .debug ("<== GdsREST.rangerPolicyHeaderOf(rangerPolicy: {}): ret= {}" , rangerPolicy , ret );
2100+
20682101 return ret ;
20692102 }
20702103
@@ -2092,14 +2125,14 @@ private RangerPolicyItem transformGrantToPolicyItem(RangerGrant grant) {
20922125 return null ;
20932126 }
20942127
2095- RangerPolicyItem policyItem = new RangerPolicyItem ();
2128+ RangerPolicyItem policyItem = new RangerPolicyItem ();
2129+ List <String > permissions = grant .getAccessTypes ();
2130+ List <String > conditions = grant .getConditions ();
20962131
2097- List <String > permissions = grant .getAccessTypes ();
20982132 if (CollectionUtils .isNotEmpty (permissions )) {
20992133 policyItem .setAccesses (permissions .stream ().map (accessType -> new RangerPolicyItemAccess (accessType , true )).collect (Collectors .toList ()));
21002134 }
21012135
2102- List <String > conditions = grant .getConditions ();
21032136 if (CollectionUtils .isNotEmpty (conditions )) {
21042137 policyItem .setConditions (conditions .stream ().map (condition -> new RangerPolicyItemCondition (GDS_POLICY_EXPR_CONDITION , Collections .singletonList (condition ))).collect (Collectors .toList ()));
21052138 }
@@ -2148,6 +2181,7 @@ private Predicate<RangerPolicyItem> filterByPrincipalsPredicate(String[] filteri
21482181 }
21492182
21502183 Map <String , Set <String >> principalCriteriaMap = new HashMap <>();
2184+
21512185 for (String principal : filteringPrincipals ) {
21522186 String [] parts = principal .split (":" );
21532187 String principalType = parts .length > 1 ? parts [0 ] : DEFAULT_PRINCIPAL_TYPE ;
@@ -2171,11 +2205,13 @@ private Predicate<RangerPolicyItem> filterByAccessTypesPredicate(String[] filter
21712205 }
21722206
21732207 Set <String > accessTypeSet = new HashSet <>(Arrays .asList (filteringAccessTypes ));
2208+
21742209 return policyItem -> policyItem .getAccesses ().stream ().anyMatch (access -> accessTypeSet .contains (access .getType ()));
21752210 }
21762211
21772212 private RangerPolicyItem copyOf (RangerPolicyItem policyItem ) {
21782213 RangerPolicyItem copy = new RangerPolicyItem ();
2214+
21792215 copy .setAccesses (new ArrayList <>(policyItem .getAccesses ()));
21802216 copy .setUsers (new ArrayList <>(policyItem .getUsers ()));
21812217 copy .setGroups (new ArrayList <>(policyItem .getGroups ()));
0 commit comments