Skip to content

Commit

Permalink
Add test for building JWT with multiple groups with same verbs+resour…
Browse files Browse the repository at this point in the history
…ceTypes
  • Loading branch information
ThomasCAI-mlv committed Jan 7, 2025
1 parent bf53d85 commit f2dcaaa
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.michelin.ns4kafka.security;

import static com.michelin.ns4kafka.security.auth.JwtCustomClaimNames.ROLE_BINDINGS;
import static io.micronaut.core.util.StringUtils.EMPTY_STRING;

import com.michelin.ns4kafka.model.RoleBinding;
import com.michelin.ns4kafka.property.SecurityProperties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
@Singleton
public class AuthenticationService {


@Inject
ResourceBasedSecurityRule resourceBasedSecurityRule;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,19 @@ void shouldReturnAuthenticationSuccessWhenAdminWithGroups() {
assertEquals("admin", response.getAuthentication().get().getName());
assertTrue(response.getAuthentication().get().getRoles().contains(ResourceBasedSecurityRule.IS_ADMIN));
assertTrue(response.getAuthentication().get().getAttributes()
.containsKey("roleBindings"));
.containsKey(ROLE_BINDINGS));
assertEquals(List.of("ns1"),
((List<AuthenticationRoleBinding>) response.getAuthentication().get().getAttributes()
.get("roleBindings")).getFirst()
.get(ROLE_BINDINGS)).getFirst()
.getNamespaces());
assertTrue(
((List<AuthenticationRoleBinding>) response.getAuthentication().get().getAttributes()
.get("roleBindings")).getFirst()
.get(ROLE_BINDINGS)).getFirst()
.getVerbs()
.containsAll(List.of(RoleBinding.Verb.POST, RoleBinding.Verb.GET)));
assertTrue(
((List<AuthenticationRoleBinding>) response.getAuthentication().get().getAttributes()
.get("roleBindings")).getFirst()
.get(ROLE_BINDINGS)).getFirst()
.getResourceTypes()
.containsAll(List.of("topics", "acls")));
}
Expand Down Expand Up @@ -155,20 +155,105 @@ void shouldReturnAuthenticationSuccessWhenUserWithGroups() {
assertEquals("user", response.getAuthentication().get().getName());
assertTrue(response.getAuthentication().get().getRoles().isEmpty());
assertTrue(response.getAuthentication().get().getAttributes()
.containsKey("roleBindings"));
.containsKey(ROLE_BINDINGS));
assertEquals(List.of("ns1"),
((List<AuthenticationRoleBinding>) response.getAuthentication().get().getAttributes()
.get("roleBindings")).getFirst()
.get(ROLE_BINDINGS)).getFirst()
.getNamespaces());
assertTrue(
((List<AuthenticationRoleBinding>) response.getAuthentication().get().getAttributes()
.get("roleBindings")).getFirst()
.get(ROLE_BINDINGS)).getFirst()
.getVerbs()
.containsAll(List.of(RoleBinding.Verb.POST, RoleBinding.Verb.GET)));
assertTrue(
((List<AuthenticationRoleBinding>) response.getAuthentication().get().getAttributes()
.get("roleBindings")).getFirst()
.get(ROLE_BINDINGS)).getFirst()
.getResourceTypes()
.containsAll(List.of("topics", "acls")));
}

@Test
@SuppressWarnings("unchecked")
void shouldReturnAuthenticationSuccessWhenMultipleGroupsWithSameVerbsAndResourceTypes() {
RoleBinding roleBinding1 = RoleBinding.builder()
.metadata(Metadata.builder()
.name("ns1-rb")
.namespace("ns1")
.build())
.spec(RoleBinding.RoleBindingSpec.builder()
.role(RoleBinding.Role.builder()
.resourceTypes(List.of("topics", "acls"))
.verbs(List.of(RoleBinding.Verb.POST, RoleBinding.Verb.GET))
.build())
.subject(RoleBinding.Subject.builder()
.subjectName("group1")
.subjectType(RoleBinding.SubjectType.GROUP)
.build())
.build())
.build();

RoleBinding roleBinding2 = RoleBinding.builder()
.metadata(Metadata.builder()
.name("ns2-rb")
.namespace("ns2")
.build())
.spec(RoleBinding.RoleBindingSpec.builder()
.role(RoleBinding.Role.builder()
.resourceTypes(List.of("topics"))
.verbs(List.of(RoleBinding.Verb.GET))
.build())
.subject(RoleBinding.Subject.builder()
.subjectName("group2")
.subjectType(RoleBinding.SubjectType.GROUP)
.build())
.build())
.build();

RoleBinding roleBinding3 = RoleBinding.builder()
.metadata(Metadata.builder()
.name("ns3-rb")
.namespace("ns3")
.build())
.spec(RoleBinding.RoleBindingSpec.builder()
.role(RoleBinding.Role.builder()
.resourceTypes(List.of("topics", "acls"))
.verbs(List.of(RoleBinding.Verb.POST, RoleBinding.Verb.GET))
.build())
.subject(RoleBinding.Subject.builder()
.subjectName("group3")
.subjectType(RoleBinding.SubjectType.GROUP)
.build())
.build())
.build();

when(roleBindingService.findAllByGroups(any()))
.thenReturn(List.of(roleBinding1, roleBinding2, roleBinding3));

when(resourceBasedSecurityRule.computeRolesFromGroups(any()))
.thenReturn(List.of());

AuthenticationResponse response = authenticationService.buildAuthJwtGroups("user", List.of("group1"));

assertTrue(response.getAuthentication().isPresent());
assertEquals("user", response.getAuthentication().get().getName());
assertTrue(response.getAuthentication().get().getRoles().isEmpty());
assertTrue(response.getAuthentication().get().getAttributes().containsKey(ROLE_BINDINGS));
assertTrue(
((List<AuthenticationRoleBinding>) response.getAuthentication().get().getAttributes().get(ROLE_BINDINGS))
.containsAll(
List.of(
AuthenticationRoleBinding.builder()
.namespaces(List.of("ns1", "ns3"))
.verbs(List.of(RoleBinding.Verb.POST, RoleBinding.Verb.GET))
.resourceTypes(List.of("topics", "acls"))
.build(),
AuthenticationRoleBinding.builder()
.namespaces(List.of("ns2"))
.verbs(List.of(RoleBinding.Verb.GET))
.resourceTypes(List.of("topics"))
.build()
)
)
);
}
}

0 comments on commit f2dcaaa

Please sign in to comment.