Skip to content

Commit 18d1dfc

Browse files
committed
RANGER-5061: checkstyle compliance updates - security-admin module - org.apache.ranger.security package
1 parent 2f9662e commit 18d1dfc

23 files changed

+658
-250
lines changed

security-admin/src/main/java/org/apache/ranger/security/context/RangerAPIMapping.java

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ public Set<String> getAvailableUITabs() {
6060
* RangerAPIList and should be mapped properly with UI tabs in the current class.
6161
*/
6262
public Set<String> getAssociatedTabsWithAPI(String apiName) {
63-
Set<String> associatedTabs = mapApiToTabs.get(apiName);
64-
return associatedTabs;
63+
return mapApiToTabs.get(apiName);
6564
}
6665

6766
private void init() {
@@ -146,10 +145,9 @@ private void mapReportsWithAPIs() {
146145
rangerAPIMappingWithUI.put(TAB_REPORTS, apiAssociatedWithReports);
147146

148147
for (String api : apiAssociatedWithReports) {
149-
if (mapApiToTabs.get(api) == null) {
150-
mapApiToTabs.put(api, new HashSet<>());
151-
}
152-
mapApiToTabs.get(api).add(TAB_REPORTS);
148+
Set<String> tabs = mapApiToTabs.computeIfAbsent(api, k -> new HashSet<>());
149+
150+
tabs.add(TAB_REPORTS);
153151
}
154152
}
155153

@@ -227,10 +225,9 @@ private void mapTagBasedPoliciesWithAPIs() {
227225
rangerAPIMappingWithUI.put(TAB_TAG_BASED_POLICIES, apiAssociatedWithTagBasedPolicy);
228226

229227
for (String api : apiAssociatedWithTagBasedPolicy) {
230-
if (mapApiToTabs.get(api) == null) {
231-
mapApiToTabs.put(api, new HashSet<>());
232-
}
233-
mapApiToTabs.get(api).add(TAB_TAG_BASED_POLICIES);
228+
Set<String> tabs = mapApiToTabs.computeIfAbsent(api, k -> new HashSet<>());
229+
230+
tabs.add(TAB_TAG_BASED_POLICIES);
234231
}
235232
}
236233

@@ -274,10 +271,9 @@ private void mapKeyManagerWithAPIs() {
274271
rangerAPIMappingWithUI.put(TAB_KEY_MANAGER, apiAssociatedWithKeyManager);
275272

276273
for (String api : apiAssociatedWithKeyManager) {
277-
if (mapApiToTabs.get(api) == null) {
278-
mapApiToTabs.put(api, new HashSet<>());
279-
}
280-
mapApiToTabs.get(api).add(TAB_KEY_MANAGER);
274+
Set<String> tabs = mapApiToTabs.computeIfAbsent(api, k -> new HashSet<>());
275+
276+
tabs.add(TAB_KEY_MANAGER);
281277
}
282278
}
283279

@@ -306,10 +302,9 @@ private void mapPermissionsWithAPIs() {
306302
rangerAPIMappingWithUI.put(TAB_PERMISSIONS, apiAssociatedWithPermissions);
307303

308304
for (String api : apiAssociatedWithPermissions) {
309-
if (mapApiToTabs.get(api) == null) {
310-
mapApiToTabs.put(api, new HashSet<>());
311-
}
312-
mapApiToTabs.get(api).add(TAB_PERMISSIONS);
305+
Set<String> tabs = mapApiToTabs.computeIfAbsent(api, k -> new HashSet<>());
306+
307+
tabs.add(TAB_PERMISSIONS);
313308
}
314309
}
315310

@@ -363,10 +358,9 @@ private void mapUGWithAPIs() {
363358
rangerAPIMappingWithUI.put(TAB_USERS_GROUPS, apiAssociatedWithUserAndGroups);
364359

365360
for (String api : apiAssociatedWithUserAndGroups) {
366-
if (mapApiToTabs.get(api) == null) {
367-
mapApiToTabs.put(api, new HashSet<>());
368-
}
369-
mapApiToTabs.get(api).add(TAB_USERS_GROUPS);
361+
Set<String> tabs = mapApiToTabs.computeIfAbsent(api, k -> new HashSet<>());
362+
363+
tabs.add(TAB_USERS_GROUPS);
370364
}
371365
}
372366

@@ -434,10 +428,9 @@ private void mapAuditWithAPIs() {
434428
rangerAPIMappingWithUI.put(TAB_AUDIT, apiAssociatedWithAudit);
435429

436430
for (String api : apiAssociatedWithAudit) {
437-
if (mapApiToTabs.get(api) == null) {
438-
mapApiToTabs.put(api, new HashSet<>());
439-
}
440-
mapApiToTabs.get(api).add(TAB_AUDIT);
431+
Set<String> tabs = mapApiToTabs.computeIfAbsent(api, k -> new HashSet<>());
432+
433+
tabs.add(TAB_AUDIT);
441434
}
442435
}
443436

@@ -517,10 +510,9 @@ private void mapResourceBasedPoliciesWithAPIs() {
517510
rangerAPIMappingWithUI.put(TAB_RESOURCE_BASED_POLICIES, apiAssociatedWithRBPolicies);
518511

519512
for (String api : apiAssociatedWithRBPolicies) {
520-
if (mapApiToTabs.get(api) == null) {
521-
mapApiToTabs.put(api, new HashSet<>());
522-
}
523-
mapApiToTabs.get(api).add(TAB_RESOURCE_BASED_POLICIES);
513+
Set<String> tabs = mapApiToTabs.computeIfAbsent(api, k -> new HashSet<>());
514+
515+
tabs.add(TAB_RESOURCE_BASED_POLICIES);
524516
}
525517
}
526518

@@ -579,10 +571,9 @@ private void mapGDSWithAPIs() {
579571
rangerAPIMappingWithUI.put(TAB_GDS, apiAssociatedWithGDS);
580572

581573
for (String api : apiAssociatedWithGDS) {
582-
if (mapApiToTabs.get(api) == null) {
583-
mapApiToTabs.put(api, new HashSet<>());
584-
}
585-
mapApiToTabs.get(api).add(TAB_GDS);
574+
Set<String> tabs = mapApiToTabs.computeIfAbsent(api, k -> new HashSet<>());
575+
576+
tabs.add(TAB_GDS);
586577
}
587578
}
588579
}

security-admin/src/main/java/org/apache/ranger/security/context/RangerAdminOpContext.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
import java.io.Serializable;
2323

2424
public class RangerAdminOpContext implements Serializable {
25-
private static final long serialVersionUID = 1L;
26-
private boolean bulkModeContext;
27-
private Boolean createPrincipalsIfAbsent;
25+
private static final long serialVersionUID = 1L;
26+
27+
private boolean bulkModeContext;
28+
private Boolean createPrincipalsIfAbsent;
2829

2930
public boolean isBulkModeContext() {
3031
return bulkModeContext;

security-admin/src/main/java/org/apache/ranger/security/context/RangerPreAuthSecurityHandler.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,81 +52,105 @@ public class RangerPreAuthSecurityHandler {
5252
@Autowired
5353
SessionMgr sessionMgr;
5454

55-
public boolean isAPIAccessible(String methodName) throws Exception {
55+
public boolean isAPIAccessible(String methodName) {
5656
if (methodName == null) {
5757
return false;
5858
}
5959

6060
UserSessionBase userSession = ContextUtil.getCurrentUserSession();
61+
6162
if (userSession == null) {
6263
logger.warn("WARNING: UserSession found null. Some non-authorized user might be trying to access the API.");
64+
6365
return false;
6466
}
6567

6668
if (userSession.isUserAdmin()) {
6769
logger.debug("WARNING: Logged in user is System Admin, System Admin is allowed to access all the tabs except Key Manager. Reason for returning true is, In few cases system admin needs to have access on Key Manager tabs as well.");
70+
6871
return true;
6972
}
7073

7174
Set<String> associatedTabs = rangerAPIMapping.getAssociatedTabsWithAPI(methodName);
75+
7276
if (CollectionUtils.isEmpty(associatedTabs)) {
7377
return true;
7478
}
79+
7580
if (associatedTabs.contains(RangerAPIMapping.TAB_PERMISSIONS) && userSession.isAuditUserAdmin()) {
7681
return true;
7782
}
83+
7884
return isAPIAccessible(associatedTabs);
7985
}
8086

81-
public boolean isAPIAccessible(Set<String> associatedTabs) throws Exception {
87+
public boolean isAPIAccessible(Set<String> associatedTabs) {
8288
UserSessionBase userSession = ContextUtil.getCurrentUserSession();
89+
8390
if (userSession != null) {
8491
sessionMgr.refreshPermissionsIfNeeded(userSession);
92+
8593
if (userSession.getRangerUserPermission() != null) {
8694
CopyOnWriteArraySet<String> accessibleModules = userSession.getRangerUserPermission().getUserPermissions();
95+
8796
if (CollectionUtils.containsAny(accessibleModules, associatedTabs)) {
8897
return true;
8998
}
9099
}
91100
}
101+
92102
VXResponse gjResponse = new VXResponse();
103+
93104
gjResponse.setStatusCode(HttpServletResponse.SC_FORBIDDEN);
94105
gjResponse.setMsgDesc("User is not allowed to access the API");
106+
95107
throw restErrorUtil.generateRESTException(gjResponse);
96108
}
97109

98110
public boolean isAPISpnegoAccessible() {
99111
UserSessionBase userSession = ContextUtil.getCurrentUserSession();
112+
100113
if (userSession != null && (userSession.isSpnegoEnabled() || userSession.isUserAdmin() || userSession.isAuditUserAdmin())) {
101114
return true;
102115
} else if (userSession != null && (userSession.isUserAdmin() || userSession.isKeyAdmin() || userSession.isAuditKeyAdmin())) {
103116
return true;
104117
}
118+
105119
VXResponse gjResponse = new VXResponse();
120+
106121
gjResponse.setStatusCode(HttpServletResponse.SC_FORBIDDEN);
107122
gjResponse.setMsgDesc("User is not allowed to access the API");
123+
108124
throw restErrorUtil.generateRESTException(gjResponse);
109125
}
110126

111127
public boolean isAdminOrKeyAdminRole() {
112128
UserSessionBase userSession = ContextUtil.getCurrentUserSession();
129+
113130
if (userSession != null && (userSession.isKeyAdmin() || userSession.isUserAdmin())) {
114131
return true;
115132
}
133+
116134
VXResponse gjResponse = new VXResponse();
135+
117136
gjResponse.setStatusCode(HttpServletResponse.SC_FORBIDDEN); // assert user is authenticated.
118137
gjResponse.setMsgDesc("User is not allowed to access the API");
138+
119139
throw restErrorUtil.generateRESTException(gjResponse);
120140
}
121141

122142
public boolean isAdminRole() {
123143
UserSessionBase userSession = ContextUtil.getCurrentUserSession();
144+
124145
if (userSession != null && userSession.isUserAdmin()) {
125146
return true;
126147
}
148+
127149
VXResponse gjResponse = new VXResponse();
150+
128151
gjResponse.setStatusCode(HttpServletResponse.SC_FORBIDDEN); // assert user is authenticated.
129152
gjResponse.setMsgDesc("User is not allowed to access the API");
153+
130154
throw restErrorUtil.generateRESTException(gjResponse);
131155
}
132156
}

security-admin/src/main/java/org/apache/ranger/security/context/RangerSecurityContext.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@
2828
import java.io.Serializable;
2929

3030
public class RangerSecurityContext implements Serializable {
31-
private static final long serialVersionUID = 1L;
32-
private UserSessionBase userSession;
33-
private RequestContext requestContext;
31+
private static final long serialVersionUID = 1L;
32+
33+
private UserSessionBase userSession;
34+
private RequestContext requestContext;
3435

3536
public UserSessionBase getUserSession() {
3637
return userSession;

0 commit comments

Comments
 (0)