Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ public class AuditREST {
private static final String PROP_POLICY_REST_URL = RANGER_ADMIN_CONFIG_PREFIX + ".policy.rest.url";
private static final String PROP_POLICY_POLL_INTERVAL_MS = RANGER_ADMIN_CONFIG_PREFIX + ".policy.pollIntervalMs";
private static final long DEFAULT_POLL_INTERVAL_MS = 30 * 1000L;
private static final String WILDCARD_SERVICE_NAME = "*";

private static final Map<String, Set<String>> configuredAllowedUsers;

private static volatile Set<String> wildcardServiceUsers = Collections.emptySet();
private static volatile Map<String, Set<String>> allowedServiceUsers;
private static RangerAdminRESTClient rangerAdminClient;
private static Timer serviceAccountUsersRefreshTimer;
Expand Down Expand Up @@ -152,18 +154,16 @@ static Map<String, Set<String>> mergeAllowedUsers(Map<String, Set<String>> confi
Set<String> serviceAccountUsers = entry.getValue();

if (StringUtils.isNotBlank(serviceName) && serviceAccountUsers != null && !serviceAccountUsers.isEmpty()) {
Set<String> allowedUsers = ret.get(serviceName);

if (allowedUsers == null) {
allowedUsers = new HashSet<>();

ret.put(serviceName, allowedUsers);
}
Set<String> allowedUsers = ret.computeIfAbsent(serviceName, k -> new HashSet<>());

allowedUsers.addAll(serviceAccountUsers);
}
}

Set<String> wildcardServiceUsers = ret.get(WILDCARD_SERVICE_NAME);

AuditREST.wildcardServiceUsers = wildcardServiceUsers != null ? wildcardServiceUsers : Collections.emptySet();

return ret;
}

Expand Down Expand Up @@ -433,14 +433,16 @@ private static void initializeAuthToLocal() {
* @return true if user is allowed, false otherwise
*/
private boolean isAllowedServiceUser(String serviceName, String userName) {
boolean ret;
final boolean ret;

if (StringUtils.isNotBlank(serviceName) && StringUtils.isNotBlank(userName)) {
if (StringUtils.isBlank(serviceName) || StringUtils.isBlank(userName)) {
ret = false;
} else if (wildcardServiceUsers.contains(userName)) {
ret = true;
} else {
Set<String> allowedUsers = allowedServiceUsers.get(serviceName);

ret = allowedUsers != null && allowedUsers.contains(userName);
} else {
ret = false;
}

LOG.debug("isAllowedServiceUser(serviceName={}, userName={}): ret={}", serviceName, userName, ret);
Expand Down
47 changes: 14 additions & 33 deletions pdp/src/main/java/org/apache/ranger/pdp/rest/RangerPdpREST.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public class RangerPdpREST {

private final Map<String, Set<String>> configuredDelegationUsers = new HashMap<>();

private volatile Set<String> wildcardServiceUsers = Collections.emptySet();
private volatile Map<String, Set<String>> delegationUsersByService = Collections.emptyMap();
private RangerAdminRESTClient rangerAdminClient;
private Timer serviceAccountUsersRefreshTimer;
Expand Down Expand Up @@ -501,19 +502,14 @@ private void recordRequestMetrics(Response ret, long startNanos, HttpServletRequ
}

private boolean isDelegationUserForService(String serviceName, String userName) {
final boolean ret;
Map<String, Set<String>> currentDelegationUsersByService = delegationUsersByService;
final boolean ret;

if (currentDelegationUsersByService.isEmpty()) {
ret = false;
} else if (StringUtils.isBlank(serviceName) || StringUtils.isBlank(userName)) {
if (StringUtils.isBlank(serviceName) || StringUtils.isBlank(userName)) {
ret = false;
} else if (wildcardServiceUsers.contains(userName)) {
ret = true;
} else {
Set<String> delegationUsers = currentDelegationUsersByService.get(serviceName);

if (delegationUsers == null) {
delegationUsers = currentDelegationUsersByService.get(RangerPdpConstants.WILDCARD_SERVICE_NAME);
}
Set<String> delegationUsers = delegationUsersByService.get(serviceName);

ret = delegationUsers != null && delegationUsers.contains(userName);
}
Expand Down Expand Up @@ -568,43 +564,28 @@ void refreshDelegationUsers() {
}
}

static Map<String, Set<String>> mergeDelegationUsers(Map<String, Set<String>> configuredDelegationUsers, Map<String, Set<String>> serviceAccountUsersByServiceName) {
Map<String, Set<String>> ret = new HashMap<>();
Set<String> wildcardDelegationUsers = configuredDelegationUsers.get(WILDCARD_SERVICE_NAME);

if (wildcardDelegationUsers == null) {
wildcardDelegationUsers = Collections.emptySet();
}
Map<String, Set<String>> mergeDelegationUsers(Map<String, Set<String>> configuredDelegationUsers, Map<String, Set<String>> serviceAccountUsersByServiceName) {
Map<String, Set<String>> ret = new HashMap<>();

for (Map.Entry<String, Set<String>> entry : configuredDelegationUsers.entrySet()) {
Set<String> delegationUsers = new HashSet<>(entry.getValue());

if (!WILDCARD_SERVICE_NAME.equals(entry.getKey())) {
delegationUsers.addAll(wildcardDelegationUsers);
}

ret.put(entry.getKey(), delegationUsers);
ret.put(entry.getKey(), new HashSet<>(entry.getValue()));
}

for (Map.Entry<String, Set<String>> entry : serviceAccountUsersByServiceName.entrySet()) {
String serviceName = entry.getKey();
Set<String> serviceAccountUsers = entry.getValue();

if (StringUtils.isNotBlank(serviceName) && serviceAccountUsers != null && !serviceAccountUsers.isEmpty()) {
Set<String> delegationUsersForService = ret.get(serviceName);
Set<String> delegationUsers = new HashSet<>();

if (delegationUsersForService != null) {
delegationUsers.addAll(delegationUsersForService);
} else if (!WILDCARD_SERVICE_NAME.equals(serviceName)) {
delegationUsers.addAll(wildcardDelegationUsers);
}
Set<String> delegationUsers = ret.computeIfAbsent(serviceName, k -> new HashSet<>());

delegationUsers.addAll(serviceAccountUsers);
ret.put(serviceName, delegationUsers);
}
}

Set<String> wildcardServiceUsers = ret.get(WILDCARD_SERVICE_NAME);

this.wildcardServiceUsers = (wildcardServiceUsers != null) ? wildcardServiceUsers : Collections.emptySet();

return ret;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,12 @@ public void testMergeDelegationUsersUnionsServiceAccountUsersAndWildcard() {
serviceAccountUsersByServiceName.put("svc1", Collections.singleton("svc-user"));
serviceAccountUsersByServiceName.put("svc2", Collections.singleton("only-dynamic"));

Map<String, Set<String>> mergedDelegationUsers = RangerPdpREST.mergeDelegationUsers(configuredDelegationUsers, serviceAccountUsersByServiceName);
RangerPdpREST pdpREST = new RangerPdpREST();

assertEquals(new HashSet<>(Arrays.asList(CALLER_DELEGATION_ALLOWED, "wild", "svc-user")), mergedDelegationUsers.get("svc1"));
assertEquals(new HashSet<>(Arrays.asList("wild", "only-dynamic")), mergedDelegationUsers.get("svc2"));
Map<String, Set<String>> mergedDelegationUsers = pdpREST.mergeDelegationUsers(configuredDelegationUsers, serviceAccountUsersByServiceName);

assertEquals(new HashSet<>(Arrays.asList(CALLER_DELEGATION_ALLOWED, "svc-user")), mergedDelegationUsers.get("svc1"));
assertEquals(new HashSet<>(Arrays.asList("only-dynamic")), mergedDelegationUsers.get("svc2"));
assertEquals(Collections.singleton("wild"), mergedDelegationUsers.get("*"));
}

Expand Down