-
-
Notifications
You must be signed in to change notification settings - Fork 151
RBAC: Make it possible to use regex for values #663
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
Merged
Haarolean
merged 23 commits into
kafbat:main
from
francoisvandenplas:issues/300-regex-for-rbac
Mar 20, 2025
Merged
Changes from 8 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
bd2c6fd
Issues/300: rbac now supports regex for values
33b4a54
Issues/300: fix checkstyle
f15fc92
Issues/300: Other extractors handle regex as values.
1c47f7c
Issues/300: Log message now reflects the new matching strategy
d42b88a
Merge branch 'main' into issues/300-regex-for-rbac
francoisvandenplas 0765d4a
Merge branch 'main' into issues/300-regex-for-rbac
francoisvandenplas 8ff1e16
Merge branch 'main' into issues/300-regex-for-rbac
Haarolean 4eb1f13
Merge branch 'main' into issues/300-regex-for-rbac
francoisvandenplas b689446
Issues/300: remove useless null check & add test scenarios
708b463
Issues/300: Remove useless null check
736cc4f
Merge branch 'main' into issues/300-regex-for-rbac
francoisvandenplas cf819d9
Merge branch 'main' into issues/300-regex-for-rbac
francoisvandenplas 57d1937
feat: introduce regex option
callaertanthony 997a7c4
Issues/300: Replace Lombok annotation & add log
9fe4604
Issues/300: Use Jackson to deserialize Roles[]
b06e9a4
Merge branch 'main' into issues/300-regex-for-rbac
francoisvandenplas 2a9199b
Issues/300: @Data provides required constructor & setters
aa989b1
Merge branch 'main' into issues/300-regex-for-rbac
Haarolean d7ed00f
Merge branch 'main' into issues/300-regex-for-rbac
francoisvandenplas 4eb3940
Merge branch 'main' into issues/300-regex-for-rbac
francoisvandenplas 1f40b68
Merge branch 'main' into issues/300-regex-for-rbac
francoisvandenplas 89a75e0
Merge branch 'main' into issues/300-regex-for-rbac
francoisvandenplas 8f16981
Merge branch 'main' into issues/300-regex-for-rbac
Haarolean 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
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
159 changes: 159 additions & 0 deletions
159
api/src/test/java/io/kafbat/ui/config/RegexBasedProviderAuthorityExtractorTest.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,159 @@ | ||
package io.kafbat.ui.config; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.when; | ||
import static org.springframework.security.oauth2.client.registration.ClientRegistration.withRegistrationId; | ||
|
||
import io.kafbat.ui.config.auth.OAuthProperties; | ||
import io.kafbat.ui.model.rbac.Role; | ||
import io.kafbat.ui.service.rbac.AccessControlService; | ||
import io.kafbat.ui.service.rbac.extractor.CognitoAuthorityExtractor; | ||
import io.kafbat.ui.service.rbac.extractor.GithubAuthorityExtractor; | ||
import io.kafbat.ui.service.rbac.extractor.GoogleAuthorityExtractor; | ||
import io.kafbat.ui.service.rbac.extractor.OauthAuthorityExtractor; | ||
import io.kafbat.ui.service.rbac.extractor.ProviderAuthorityExtractor; | ||
import io.kafbat.ui.util.AccessControlServiceMock; | ||
import java.io.InputStream; | ||
import java.time.Instant; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import lombok.SneakyThrows; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.security.core.authority.AuthorityUtils; | ||
import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest; | ||
import org.springframework.security.oauth2.core.AuthorizationGrantType; | ||
import org.springframework.security.oauth2.core.OAuth2AccessToken; | ||
import org.springframework.security.oauth2.core.user.DefaultOAuth2User; | ||
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
import org.yaml.snakeyaml.Yaml; | ||
import org.yaml.snakeyaml.introspector.BeanAccess; | ||
|
||
public class RegexBasedProviderAuthorityExtractorTest { | ||
|
||
|
||
private final AccessControlService accessControlService = new AccessControlServiceMock().getMock(); | ||
Yaml yaml; | ||
ProviderAuthorityExtractor extractor; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
yaml = new Yaml(); | ||
yaml.setBeanAccess(BeanAccess.FIELD); | ||
|
||
InputStream rolesFile = this.getClass() | ||
.getClassLoader() | ||
.getResourceAsStream("roles_definition.yaml"); | ||
|
||
Role[] roleArray = yaml.loadAs(rolesFile, Role[].class); | ||
when(accessControlService.getRoles()).thenReturn(List.of(roleArray)); | ||
|
||
} | ||
|
||
@SneakyThrows | ||
@Test | ||
void extractOauth2Authorities() { | ||
|
||
extractor = new OauthAuthorityExtractor(); | ||
|
||
OAuth2User oauth2User = new DefaultOAuth2User( | ||
AuthorityUtils.createAuthorityList("SCOPE_message:read"), | ||
Map.of("role_definition", Set.of("ROLE-ADMIN", "ANOTHER-ROLE"), "user_name", "john@kafka.com"), | ||
"user_name"); | ||
|
||
HashMap<String, Object> additionalParams = new HashMap<>(); | ||
OAuthProperties.OAuth2Provider provider = new OAuthProperties.OAuth2Provider(); | ||
provider.setCustomParams(Map.of("roles-field", "role_definition")); | ||
additionalParams.put("provider", provider); | ||
|
||
Set<String> roles = extractor.extract(accessControlService, oauth2User, additionalParams).block(); | ||
|
||
assertEquals(Set.of("viewer", "admin"), roles); | ||
|
||
} | ||
|
||
@SneakyThrows | ||
@Test | ||
void extractCognitoAuthorities() { | ||
|
||
extractor = new CognitoAuthorityExtractor(); | ||
|
||
OAuth2User oauth2User = new DefaultOAuth2User( | ||
AuthorityUtils.createAuthorityList("SCOPE_message:read"), | ||
Map.of("cognito:groups", List.of("ROLE-ADMIN", "ANOTHER-ROLE"), "user_name", "john@kafka.com"), | ||
"user_name"); | ||
|
||
HashMap<String, Object> additionalParams = new HashMap<>(); | ||
|
||
OAuthProperties.OAuth2Provider provider = new OAuthProperties.OAuth2Provider(); | ||
provider.setCustomParams(Map.of("roles-field", "role_definition")); | ||
additionalParams.put("provider", provider); | ||
|
||
Set<String> roles = extractor.extract(accessControlService, oauth2User, additionalParams).block(); | ||
|
||
assertEquals(Set.of("viewer", "admin"), roles); | ||
|
||
} | ||
|
||
@SneakyThrows | ||
@Test | ||
void extractGithubAuthorities() { | ||
|
||
extractor = new GithubAuthorityExtractor(); | ||
|
||
OAuth2User oauth2User = new DefaultOAuth2User( | ||
AuthorityUtils.createAuthorityList("SCOPE_message:read"), | ||
Map.of("login", "john@kafka.com"), | ||
"login"); | ||
|
||
HashMap<String, Object> additionalParams = new HashMap<>(); | ||
|
||
OAuthProperties.OAuth2Provider provider = new OAuthProperties.OAuth2Provider(); | ||
additionalParams.put("provider", provider); | ||
|
||
additionalParams.put("request", new OAuth2UserRequest( | ||
withRegistrationId("registration-1") | ||
.clientId("client-1") | ||
.clientSecret("secret") | ||
.redirectUri("https://client.com") | ||
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE) | ||
.authorizationUri("https://provider.com/oauth2/authorization") | ||
.tokenUri("https://provider.com/oauth2/token") | ||
.clientName("Client 1") | ||
.build(), | ||
new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, "XXXX", Instant.now(), | ||
Instant.now().plus(10, ChronoUnit.HOURS)))); | ||
|
||
Set<String> roles = extractor.extract(accessControlService, oauth2User, additionalParams).block(); | ||
|
||
assertEquals(Set.of("viewer"), roles); | ||
|
||
} | ||
|
||
@SneakyThrows | ||
@Test | ||
void extractGoogleAuthorities() { | ||
|
||
extractor = new GoogleAuthorityExtractor(); | ||
|
||
OAuth2User oauth2User = new DefaultOAuth2User( | ||
AuthorityUtils.createAuthorityList("SCOPE_message:read"), | ||
Map.of("hd", "test.domain.com", "email", "john@kafka.com"), | ||
"email"); | ||
|
||
HashMap<String, Object> additionalParams = new HashMap<>(); | ||
|
||
OAuthProperties.OAuth2Provider provider = new OAuthProperties.OAuth2Provider(); | ||
provider.setCustomParams(Map.of("roles-field", "role_definition")); | ||
additionalParams.put("provider", provider); | ||
|
||
Set<String> roles = extractor.extract(accessControlService, oauth2User, additionalParams).block(); | ||
|
||
assertEquals(Set.of("viewer", "admin"), roles); | ||
|
||
} | ||
|
||
} | ||
francoisvandenplas marked this conversation as resolved.
Show resolved
Hide resolved
|
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,49 @@ | ||
- name: 'admin' | ||
subjects: | ||
- provider: 'OAUTH' | ||
value: 'ROLE-[A-Z]+' | ||
type: 'role' | ||
- provider: 'OAUTH_COGNITO' | ||
value: 'ROLE-[A-Z]+' | ||
type: 'group' | ||
- provider: 'OAUTH_GOOGLE' | ||
value: '.*.domain.com' | ||
type: 'domain' | ||
clusters: | ||
- local | ||
- remote | ||
permissions: | ||
- resource: APPLICATIONCONFIG | ||
actions: [ all ] | ||
- name: 'viewer' | ||
subjects: | ||
- provider: 'LDAP' | ||
value: 'CS-XXX' | ||
type: 'kafka-viewer' | ||
- provider: 'OAUTH' | ||
value: '.*@kafka.com' | ||
type: 'user' | ||
- provider: 'OAUTH_COGNITO' | ||
value: '.*@kafka.com' | ||
type: 'user' | ||
- provider: 'OAUTH_GITHUB' | ||
value: '.*@kafka.com' | ||
type: 'user' | ||
- provider: 'OAUTH_GOOGLE' | ||
value: '.*@kafka.com' | ||
type: 'user' | ||
clusters: | ||
- remote | ||
permissions: | ||
- resource: APPLICATIONCONFIG | ||
actions: [ all ] | ||
- name: 'editor' | ||
subjects: | ||
- provider: 'OAUTH' | ||
value: 'ROLE_EDITOR' | ||
type: 'role' | ||
clusters: | ||
- local | ||
permissions: | ||
- resource: APPLICATIONCONFIG | ||
actions: [ all ] |
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.
Uh oh!
There was an error while loading. Please reload this page.