- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 196
BE: RBAC: Implement instance-wide default role #1056
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
      
      
    
  
     Merged
                    Changes from 13 commits
      Commits
    
    
            Show all changes
          
          
            20 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      8c4d0ff
              
                BE: RBAC: Impl default role
              
              
                seono 55250e7
              
                Merge remote-tracking branch 'real-origin/main' into issues/344
              
              
                seono 7ddaeb8
              
                BE: RBAC: Impl default role
              
              
                seono dad4a37
              
                BE: RBAC: Impl default role
              
              
                seono 5a21c83
              
                Merge remote-tracking branch 'seono-github/issues/344' into issues/344
              
              
                seono 1bf5904
              
                [issues/344] Merge real-origin/main into issues/344
              
              
                seono 69045d5
              
                BE: fix Default Role test
              
              
                seono 273da6a
              
                BE: remove unnecessary test
              
              
                seono e0cba88
              
                BE: fix test error
              
              
                seono 7cbf653
              
                BE: address PR review comments
              
              
                seono daac844
              
                BE: RBAC:remove name from default role
              
              
                seono e5bb2fe
              
                BE: RBAC: remove default role clusters
              
              
                seono 0c7a8a1
              
                Merge branch 'main' into issues/344
              
              
                seono db75677
              
                BE: RBAC: Add default role
              
              
                 e320708
              
                Merge branch 'main' into issues/344
              
              
                germanosin c8a15ef
              
                Merge branch 'main' into issues/344
              
              
                germanosin e5bb106
              
                Merge branch 'main' into issues/344
              
              
                germanosin 596cae9
              
                BE: fix(rbac): address PR review comments
              
              
                 a6ae8e3
              
                BE: fix(rbac): revert remove defaultRole setter
              
              
                 1fc90cb
              
                Merge branch 'main' into issues/344
              
              
                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
    
  
  
    
              
        
          
  
    
      
          
            18 changes: 18 additions & 0 deletions
          
          18 
        
  api/src/main/java/io/kafbat/ui/model/rbac/DefaultRole.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,18 @@ | ||
| package io.kafbat.ui.model.rbac; | ||
|  | ||
| import static com.google.common.base.Preconditions.checkArgument; | ||
|  | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import lombok.Data; | ||
|  | ||
| @Data | ||
| public class DefaultRole { | ||
|  | ||
| private List<Permission> permissions = new ArrayList<>(); | ||
|  | ||
| public void validate() { | ||
| permissions.forEach(Permission::validate); | ||
| permissions.forEach(Permission::transform); | ||
| } | ||
| } | 
  
    
      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 | 
|---|---|---|
|  | @@ -21,5 +21,4 @@ public void validate() { | |
| permissions.forEach(Permission::transform); | ||
| subjects.forEach(Subject::validate); | ||
| } | ||
|  | ||
| } | ||
  
    
      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
    
  
  
    
              
        
          
  
    
      
          
            102 changes: 102 additions & 0 deletions
          
          102 
        
  ...c/test/java/io/kafbat/ui/service/rbac/AccessControlServiceDefaultRoleRbacEnabledTest.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,102 @@ | ||
| package io.kafbat.ui.service.rbac; | ||
|  | ||
| import static io.kafbat.ui.service.rbac.MockedRbacUtils.DEFAULT_ROLE; | ||
| import static io.kafbat.ui.service.rbac.MockedRbacUtils.PROD_CLUSTER; | ||
| import static io.kafbat.ui.service.rbac.MockedRbacUtils.getAccessContext; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|  | ||
| import io.kafbat.ui.AbstractIntegrationTest; | ||
| import io.kafbat.ui.config.auth.RbacUser; | ||
| import io.kafbat.ui.config.auth.RoleBasedAccessControlProperties; | ||
| import io.kafbat.ui.model.ClusterDTO; | ||
| import io.kafbat.ui.model.rbac.AccessContext; | ||
| import io.kafbat.ui.model.rbac.DefaultRole; | ||
| import java.util.List; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.Mock; | ||
| import org.mockito.MockedStatic; | ||
| import org.mockito.Mockito; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.security.core.Authentication; | ||
| import org.springframework.security.core.context.ReactiveSecurityContextHolder; | ||
| import org.springframework.security.core.context.SecurityContext; | ||
| import org.springframework.test.annotation.DirtiesContext; | ||
| import org.springframework.test.util.ReflectionTestUtils; | ||
| import reactor.core.publisher.Mono; | ||
| import reactor.test.StepVerifier; | ||
|  | ||
|  | ||
| /** | ||
| * Test class for AccessControlService with default role and RBAC enabled. | ||
| */ | ||
| @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) | ||
| public class AccessControlServiceDefaultRoleRbacEnabledTest extends AbstractIntegrationTest { | ||
|  | ||
| @Autowired | ||
| AccessControlService accessControlService; | ||
|  | ||
| @Mock | ||
| SecurityContext securityContext; | ||
|  | ||
| @Mock | ||
| Authentication authentication; | ||
|  | ||
| @Mock | ||
| RbacUser user; | ||
|  | ||
| @Mock | ||
| DefaultRole defaultRole; | ||
|  | ||
| @BeforeEach | ||
| void setUp() { | ||
|  | ||
| RoleBasedAccessControlProperties properties = mock(); | ||
| defaultRole = MockedRbacUtils.getDefaultRole(); | ||
| when(properties.getDefaultRole()).thenReturn(defaultRole); | ||
| when(properties.getRoles()).thenReturn(List.of()); // Return empty list for roles | ||
|  | ||
|  | ||
| ReflectionTestUtils.setField(accessControlService, "properties", properties); | ||
| ReflectionTestUtils.setField(accessControlService, "rbacEnabled", true); | ||
|  | ||
| // Mock security context | ||
| when(securityContext.getAuthentication()).thenReturn(authentication); | ||
| when(authentication.getPrincipal()).thenReturn(user); | ||
| } | ||
|  | ||
| public void withSecurityContext(Runnable runnable) { | ||
| try (MockedStatic<ReactiveSecurityContextHolder> ctxHolder = Mockito.mockStatic( | ||
| ReactiveSecurityContextHolder.class)) { | ||
| // Mock static method to get security context | ||
| ctxHolder.when(ReactiveSecurityContextHolder::getContext).thenReturn(Mono.just(securityContext)); | ||
| runnable.run(); | ||
| } | ||
| } | ||
|  | ||
| @Test | ||
| void validateAccess() { | ||
| withSecurityContext(() -> { | ||
| when(user.groups()).thenReturn(List.of(DEFAULT_ROLE)); | ||
|         
                  Haarolean marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| AccessContext context = getAccessContext(PROD_CLUSTER, true); | ||
| Mono<Void> validateAccessMono = accessControlService.validateAccess(context); | ||
| StepVerifier.create(validateAccessMono) | ||
| .expectComplete() | ||
| .verify(); | ||
| }); | ||
| } | ||
|  | ||
| @Test | ||
| void isClusterAccessible() { | ||
| withSecurityContext(() -> { | ||
| ClusterDTO clusterDto = new ClusterDTO(); | ||
| clusterDto.setName(PROD_CLUSTER); | ||
| Mono<Boolean> clusterAccessibleMono = accessControlService.isClusterAccessible(clusterDto); | ||
| StepVerifier.create(clusterAccessibleMono) | ||
| .expectNext(true) | ||
| .expectComplete() | ||
| .verify(); | ||
| }); | ||
| } | ||
| } | ||
  
    
      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
    
  
  
    
              
      
      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.