-
Notifications
You must be signed in to change notification settings - Fork 3.8k
feat: Role mapping support in pac4j extension #18778
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nozjkoitop
wants to merge
9
commits into
apache:master
Choose a base branch
from
deep-bi:feature-role-mapping-in-pac4j
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
178c632
oidc role mapping support added
nozjkoitop ddab1ab
add docs entru
nozjkoitop fcab78b
checkstyle
nozjkoitop c57638a
removed redundant NotBlank annotation to fix dep analyze problem
nozjkoitop 4eca0d2
role mapping improvement
nozjkoitop 0561cbd
fix todo
nozjkoitop 2a6b95d
trigger checks
nozjkoitop 16735f3
fix review comments
nozjkoitop 9625e56
master merged
nozjkoitop File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
...ecurity/src/main/java/org/apache/druid/security/basic/authorization/RoleProviderUtil.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.druid.security.basic.authorization; | ||
|
|
||
| import com.google.common.annotations.VisibleForTesting; | ||
| import org.apache.druid.java.util.common.logger.Logger; | ||
| import org.apache.druid.security.basic.authorization.db.cache.BasicAuthorizerCacheManager; | ||
| import org.apache.druid.security.basic.authorization.entity.BasicAuthorizerRole; | ||
| import org.apache.druid.security.basic.authorization.entity.BasicAuthorizerUser; | ||
| import org.apache.druid.server.security.AuthenticationResult; | ||
|
|
||
| import javax.annotation.Nullable; | ||
| import java.util.HashSet; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class RoleProviderUtil | ||
| { | ||
| private static final Logger LOG = new Logger(RoleProviderUtil.class); | ||
| public static final String ROLE_CLAIM_CONTEXT_KEY = "druidRoles"; | ||
|
|
||
| public static Set<String> getUserRoles( | ||
| Map<String, BasicAuthorizerUser> userMap, | ||
| String authorizerPrefix, | ||
| AuthenticationResult authenticationResult, | ||
| BasicAuthorizerCacheManager cacheManager | ||
| ) | ||
| { | ||
| Set<String> claims = RoleProviderUtil.claimValuesFromCtx(authenticationResult.getContext()); | ||
|
|
||
| if (claims != null) { | ||
| return getRolesByClaimValue( | ||
| authorizerPrefix, | ||
| claims, | ||
| cacheManager | ||
| ); | ||
| } else { | ||
| return getRolesByIdentity( | ||
| userMap, | ||
| authenticationResult.getIdentity() | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| public static Set<String> getRolesByIdentity( | ||
| Map<String, BasicAuthorizerUser> userMap, | ||
| String identity | ||
| ) | ||
| { | ||
| Set<String> roles = new HashSet<>(); | ||
|
|
||
| BasicAuthorizerUser user = userMap.get(identity); | ||
| if (user != null) { | ||
| roles.addAll(user.getRoles()); | ||
| } | ||
| return roles; | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| public static Set<String> getRolesByClaimValue( | ||
| String authorizerPrefix, | ||
| Set<String> claimValue, | ||
| BasicAuthorizerCacheManager cacheManager | ||
| ) | ||
| { | ||
| Map<String, BasicAuthorizerRole> roleMap = cacheManager.getRoleMap(authorizerPrefix); | ||
|
|
||
| if (roleMap == null) { | ||
| return Set.of(); | ||
| } | ||
|
|
||
| final Set<String> matched = roleMap.keySet() | ||
| .stream() | ||
| .filter(claimValue::contains) | ||
| .collect(Collectors.toUnmodifiableSet()); | ||
|
|
||
| if (matched.isEmpty()) { | ||
| LOG.warn("Role claim present but no values mapped to Druid roles"); | ||
| } | ||
|
|
||
| return matched; | ||
| } | ||
|
|
||
| @Nullable | ||
| protected static Set<String> claimValuesFromCtx(Map<String, Object> ctx) | ||
| { | ||
| Object value = (ctx == null) ? null : ctx.get(RoleProviderUtil.ROLE_CLAIM_CONTEXT_KEY); | ||
| if (!(value instanceof Set)) { | ||
| if (value != null) { | ||
| LOG.warn( | ||
| "Role claim context key [%s] is present but expected Set, got [%s]", | ||
| RoleProviderUtil.ROLE_CLAIM_CONTEXT_KEY, | ||
| value.getClass().getName() | ||
| ); | ||
| } | ||
| return null; | ||
| } | ||
| Set<?> rawClaimValues = (Set<?>) value; | ||
|
|
||
| Set<String> result = new HashSet<>(); | ||
| for (Object claimValue : rawClaimValues) { | ||
| if (!(claimValue instanceof String)) { | ||
| LOG.warn( | ||
| "Role claim context key [%s] contains a non-String value of type [%s]", | ||
| RoleProviderUtil.ROLE_CLAIM_CONTEXT_KEY, | ||
| claimValue == null ? "null" : claimValue.getClass().getName() | ||
| ); | ||
| return null; | ||
| } | ||
| String str = ((String) claimValue).trim(); | ||
| if (!str.isEmpty()) { | ||
| result.add(str); | ||
| } | ||
| } | ||
| return result.isEmpty() ? null : result; | ||
| } | ||
| } | ||
87 changes: 87 additions & 0 deletions
87
...t/java/org/apache/druid/security/authorization/MetadataStoreRoleProviderGetRolesTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.druid.security.authorization; | ||
|
|
||
| import org.apache.druid.security.basic.authorization.MetadataStoreRoleProvider; | ||
| import org.apache.druid.security.basic.authorization.RoleProviderUtil; | ||
| import org.apache.druid.security.basic.authorization.db.cache.BasicAuthorizerCacheManager; | ||
| import org.apache.druid.security.basic.authorization.entity.BasicAuthorizerRole; | ||
| import org.apache.druid.security.basic.authorization.entity.BasicAuthorizerUser; | ||
| import org.apache.druid.server.security.AuthenticationResult; | ||
| import org.junit.Test; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| public class MetadataStoreRoleProviderGetRolesTest | ||
| { | ||
|
|
||
| @Test | ||
| public void returnsRolesByClaimValuesWhenPresent() | ||
| { | ||
| Map<String, BasicAuthorizerRole> roles = new HashMap<>(); | ||
| roles.put("admin", null); | ||
| roles.put("viewer", null); | ||
|
|
||
| Set<String> viewerRole = Set.of("viewer"); | ||
|
|
||
| BasicAuthorizerUser user = new BasicAuthorizerUser("alice", viewerRole); | ||
|
|
||
| Map<String, BasicAuthorizerUser> users = Map.of("alice", user); | ||
|
|
||
| BasicAuthorizerCacheManager cache = new StubCacheManager(users, roles); | ||
| MetadataStoreRoleProvider provider = new MetadataStoreRoleProvider(cache); | ||
|
|
||
| Set<String> claims = Set.of("admin", "extraneous"); | ||
|
|
||
| Map<String, Object> ctx = Map.of(RoleProviderUtil.ROLE_CLAIM_CONTEXT_KEY, claims); | ||
|
|
||
| AuthenticationResult ar = new AuthenticationResult("alice", "basic", "pac4j", ctx); | ||
|
|
||
| Set<String> out = provider.getRoles("basic", ar); | ||
| Set<String> expected = Set.of("admin"); | ||
| assertEquals(expected, out); | ||
| } | ||
|
|
||
| @Test | ||
| public void fallsBackToIdentityWhenNoClaimContext() | ||
| { | ||
| Set<String> viewerRole = Set.of("viewer"); | ||
| BasicAuthorizerUser user = new BasicAuthorizerUser("alice", viewerRole); | ||
|
|
||
| Map<String, BasicAuthorizerUser> users = Map.of("alice", user); | ||
|
|
||
| Map<String, BasicAuthorizerRole> roles = new HashMap<>(); | ||
| roles.put("admin", null); | ||
|
|
||
| BasicAuthorizerCacheManager cache = new StubCacheManager(users, roles); | ||
| MetadataStoreRoleProvider provider = new MetadataStoreRoleProvider(cache); | ||
|
|
||
| AuthenticationResult ar = new AuthenticationResult("alice", "basic", "pac4j", Collections.emptyMap()); | ||
|
|
||
| Set<String> out = provider.getRoles("basic", ar); | ||
| Set<String> expected = Set.of("viewer"); | ||
| assertEquals(expected, out); | ||
| } | ||
| } |
79 changes: 79 additions & 0 deletions
79
...-security/src/test/java/org/apache/druid/security/authorization/RoleProviderUtilTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.druid.security.authorization; | ||
|
|
||
| import org.apache.druid.security.basic.authorization.RoleProviderUtil; | ||
| import org.apache.druid.security.basic.authorization.db.cache.BasicAuthorizerCacheManager; | ||
| import org.apache.druid.security.basic.authorization.entity.BasicAuthorizerRole; | ||
| import org.apache.druid.security.basic.authorization.entity.BasicAuthorizerUser; | ||
| import org.junit.Test; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
|
|
||
| public class RoleProviderUtilTest | ||
| { | ||
|
|
||
| @Test | ||
| public void getRolesByIdentityAddsRolesWhenUserFound() | ||
| { | ||
| Set<String> roles = Set.of("r1", "r2"); | ||
| BasicAuthorizerUser user = new BasicAuthorizerUser("id", roles); | ||
|
|
||
| Map<String, BasicAuthorizerUser> userMap = Map.of("id", user); | ||
|
|
||
| Set<String> out = RoleProviderUtil.getRolesByIdentity(userMap, "id"); | ||
| assertEquals(roles, out); | ||
| } | ||
|
|
||
| @Test | ||
| public void getRolesByIdentityNoopWhenUserMissing() | ||
| { | ||
| Map<String, BasicAuthorizerUser> userMap = Map.of(); | ||
| Set<String> out = RoleProviderUtil.getRolesByIdentity(userMap, "missing"); | ||
| assertTrue(out.isEmpty()); | ||
| } | ||
|
|
||
| @Test | ||
| public void getRolesByClaimValuesFiltersByRoleNames() | ||
| { | ||
| Map<String, BasicAuthorizerRole> roles = new HashMap<>(); | ||
| roles.put("r1", null); | ||
| roles.put("r2", null); | ||
|
|
||
| BasicAuthorizerCacheManager cache = new StubCacheManager(Map.of(), roles); | ||
|
|
||
| Set<String> claims = Set.of("r2", "nope"); | ||
| Set<String> out = RoleProviderUtil.getRolesByClaimValue("authz", claims, cache); | ||
| assertEquals(Set.of("r2"), out); | ||
| } | ||
|
|
||
| @Test | ||
| public void getRolesByClaimValuesThrowsWhenRoleMapNull() | ||
| { | ||
| BasicAuthorizerCacheManager cache = new StubCacheManager(Map.of(), null); | ||
| assertTrue(RoleProviderUtil.getRolesByClaimValue("authz", Set.of("r2"), cache).isEmpty()); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there is a role claim set i.e. values is not null but we don't find expected type here then instead of returning null and thus not enforcing any role should we throw error?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Id not throw here, cuz it's more of a misconfiguration than an exceptional case, however i can add warnings on such cases to pinpoint the issue