Skip to content

Commit 94350d9

Browse files
committed
RANGER-5185: fix potential NPE in references to RangerBasePlugin.policyEngine
1 parent dbaad69 commit 94350d9

1 file changed

Lines changed: 11 additions & 15 deletions

File tree

agents-common/src/main/java/org/apache/ranger/plugin/service/RangerBasePlugin.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ public void setPolicies(ServicePolicies policies) {
611611
} else {
612612
LOG.warn("Leaving current policy engine as-is");
613613
LOG.warn("Policies are not saved to cache. policyVersion in the policy-cache may be different than in Ranger-admin, even though the policies are the same!");
614-
LOG.warn("Ranger-PolicyVersion:[{}], Cached-PolicyVersion:[{}]", policies != null ? policies.getPolicyVersion() : -1L, this.policyEngine != null ? this.policyEngine.getPolicyVersion() : -1L);
614+
LOG.warn("Ranger-PolicyVersion:[{}], Cached-PolicyVersion:[{}]", policies != null ? policies.getPolicyVersion() : -1L, getPoliciesVersion());
615615
}
616616
} catch (Exception e) {
617617
LOG.error("setPolicies: policy engine initialization failed! Leaving current policy engine as-is. Exception : ", e);
@@ -883,14 +883,11 @@ public RangerRoles getRangerRoles() {
883883
}
884884

885885
public Set<RangerRole> getRangerRoleForPrincipal(String principal, String type) {
886-
Set<RangerRole> ret = new HashSet<>();
887-
Set<RangerRole> rangerRoles = null;
886+
Set<RangerRole> ret = new HashSet<>();
887+
RangerPolicyEngine policyEngine = this.policyEngine;
888+
RangerRoles roles = policyEngine != null ? policyEngine.getRangerRoles() : null;
889+
Set<RangerRole> rangerRoles = roles != null ? roles.getRangerRoles() : null;
888890
Map<String, Set<String>> roleMapping = null;
889-
RangerRoles roles = getRangerRoles();
890-
891-
if (roles != null) {
892-
rangerRoles = roles.getRangerRoles();
893-
}
894891

895892
if (rangerRoles != null) {
896893
RangerPluginContext rangerPluginContext = policyEngine.getPluginContext();
@@ -930,6 +927,7 @@ public Set<RangerRole> getRangerRoleForPrincipal(String principal, String type)
930927
}
931928
}
932929
}
930+
933931
return ret;
934932
}
935933

@@ -1063,18 +1061,14 @@ public void refreshPoliciesAndTags() {
10631061
LOG.debug("==> refreshPoliciesAndTags()");
10641062

10651063
try {
1066-
RangerPolicyEngine policyEngine = this.policyEngine;
1064+
long oldPolicyVersion = getPoliciesVersion();
10671065

10681066
// Synch-up policies
1069-
long oldPolicyVersion = policyEngine.getPolicyVersion();
1070-
10711067
if (refresher != null) {
10721068
refresher.syncPoliciesWithAdmin(accessTrigger);
10731069
}
10741070

1075-
policyEngine = this.policyEngine; // might be updated in syncPoliciesWithAdmin()
1076-
1077-
long newPolicyVersion = policyEngine.getPolicyVersion();
1071+
long newPolicyVersion = getPoliciesVersion();
10781072

10791073
if (oldPolicyVersion == newPolicyVersion) {
10801074
// Synch-up tags
@@ -1196,7 +1190,9 @@ public Map<String, String> getServiceConfigs() {
11961190
}
11971191

11981192
public Long getPolicyVersion() {
1199-
return this.policyEngine == null ? -1L : this.policyEngine.getPolicyVersion();
1193+
RangerPolicyEngine policyEngine = this.policyEngine;
1194+
1195+
return policyEngine == null ? -1L : policyEngine.getPolicyVersion();
12001196
}
12011197

12021198
protected RangerPolicyEngine getPolicyEngine() {

0 commit comments

Comments
 (0)