Skip to content

Commit cd93135

Browse files
authored
Fix regex tests (#1105)
1 parent 14d9ec0 commit cd93135

File tree

6 files changed

+134
-94
lines changed

6 files changed

+134
-94
lines changed

api/src/main/java/io/kafbat/ui/model/rbac/Role.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static com.google.common.base.Preconditions.checkArgument;
44

5+
import java.util.ArrayList;
56
import java.util.List;
67
import lombok.Data;
78

@@ -11,7 +12,7 @@ public class Role {
1112
String name;
1213
List<String> clusters;
1314
List<Subject> subjects;
14-
List<Permission> permissions;
15+
List<Permission> permissions = new ArrayList<>();
1516

1617
public void validate() {
1718
checkArgument(clusters != null && !clusters.isEmpty(), "Role clusters cannot be empty");

api/src/test/java/io/kafbat/ui/config/RegexBasedProviderAuthorityExtractorTest.java

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,17 @@
44
import static org.junit.jupiter.api.Assertions.assertFalse;
55
import static org.junit.jupiter.api.Assertions.assertNotNull;
66
import static org.junit.jupiter.api.Assertions.assertTrue;
7-
import static org.mockito.Mockito.when;
87
import static org.springframework.security.oauth2.client.registration.ClientRegistration.withRegistrationId;
98

10-
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
119
import io.kafbat.ui.config.auth.OAuthProperties;
12-
import io.kafbat.ui.model.rbac.Role;
10+
import io.kafbat.ui.config.auth.RoleBasedAccessControlProperties;
1311
import io.kafbat.ui.service.rbac.AccessControlService;
1412
import io.kafbat.ui.service.rbac.extractor.CognitoAuthorityExtractor;
1513
import io.kafbat.ui.service.rbac.extractor.GithubAuthorityExtractor;
1614
import io.kafbat.ui.service.rbac.extractor.GoogleAuthorityExtractor;
1715
import io.kafbat.ui.service.rbac.extractor.OauthAuthorityExtractor;
1816
import io.kafbat.ui.service.rbac.extractor.ProviderAuthorityExtractor;
1917
import io.kafbat.ui.util.AccessControlServiceMock;
20-
import java.io.IOException;
21-
import java.io.InputStream;
2218
import java.time.Instant;
2319
import java.time.temporal.ChronoUnit;
2420
import java.util.HashMap;
@@ -28,39 +24,40 @@
2824
import lombok.SneakyThrows;
2925
import org.junit.jupiter.api.BeforeEach;
3026
import org.junit.jupiter.api.Test;
27+
import org.junit.jupiter.api.extension.ExtendWith;
28+
import org.springframework.beans.factory.annotation.Autowired;
29+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3130
import org.springframework.security.core.authority.AuthorityUtils;
3231
import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest;
3332
import org.springframework.security.oauth2.core.AuthorizationGrantType;
3433
import org.springframework.security.oauth2.core.OAuth2AccessToken;
3534
import org.springframework.security.oauth2.core.user.DefaultOAuth2User;
3635
import org.springframework.security.oauth2.core.user.OAuth2User;
37-
36+
import org.springframework.test.context.TestPropertySource;
37+
import org.springframework.test.context.junit.jupiter.SpringExtension;
38+
39+
@ExtendWith(SpringExtension.class)
40+
@EnableConfigurationProperties(RoleBasedAccessControlProperties.class)
41+
@TestPropertySource(
42+
locations = "classpath:application-roles-definition.yml",
43+
factory = YamlPropertySourceFactory.class
44+
)
3845
public class RegexBasedProviderAuthorityExtractorTest {
3946

40-
41-
private final AccessControlService accessControlService = new AccessControlServiceMock().getMock();
42-
ProviderAuthorityExtractor extractor;
47+
@Autowired
48+
private RoleBasedAccessControlProperties properties;
49+
private AccessControlService accessControlService;
4350

4451
@BeforeEach
45-
void setUp() throws IOException {
46-
47-
YAMLMapper mapper = new YAMLMapper();
48-
49-
InputStream rolesFile = this.getClass()
50-
.getClassLoader()
51-
.getResourceAsStream("roles_definition.yaml");
52-
53-
Role[] roles = mapper.readValue(rolesFile, Role[].class);
54-
55-
when(accessControlService.getRoles()).thenReturn(List.of(roles));
56-
52+
public void configure() {
53+
this.accessControlService = new AccessControlServiceMock(properties.getRoles()).getMock();
5754
}
5855

5956
@SneakyThrows
6057
@Test
6158
void extractOauth2Authorities() {
6259

63-
extractor = new OauthAuthorityExtractor();
60+
ProviderAuthorityExtractor extractor = new OauthAuthorityExtractor();
6461

6562
OAuth2User oauth2User = new DefaultOAuth2User(
6663
AuthorityUtils.createAuthorityList("SCOPE_message:read"),
@@ -84,7 +81,7 @@ void extractOauth2Authorities() {
8481
@Test()
8582
void extractOauth2Authorities_blankEmail() {
8683

87-
extractor = new OauthAuthorityExtractor();
84+
ProviderAuthorityExtractor extractor = new OauthAuthorityExtractor();
8885

8986
OAuth2User oauth2User = new DefaultOAuth2User(
9087
AuthorityUtils.createAuthorityList("SCOPE_message:read"),
@@ -108,7 +105,7 @@ void extractOauth2Authorities_blankEmail() {
108105
@Test
109106
void extractCognitoAuthorities() {
110107

111-
extractor = new CognitoAuthorityExtractor();
108+
ProviderAuthorityExtractor extractor = new CognitoAuthorityExtractor();
112109

113110
OAuth2User oauth2User = new DefaultOAuth2User(
114111
AuthorityUtils.createAuthorityList("SCOPE_message:read"),
@@ -133,7 +130,7 @@ void extractCognitoAuthorities() {
133130
@Test
134131
void extractGithubAuthorities() {
135132

136-
extractor = new GithubAuthorityExtractor();
133+
ProviderAuthorityExtractor extractor = new GithubAuthorityExtractor();
137134

138135
OAuth2User oauth2User = new DefaultOAuth2User(
139136
AuthorityUtils.createAuthorityList("SCOPE_message:read"),
@@ -170,7 +167,7 @@ void extractGithubAuthorities() {
170167
@Test
171168
void extractGoogleAuthorities() {
172169

173-
extractor = new GoogleAuthorityExtractor();
170+
ProviderAuthorityExtractor extractor = new GoogleAuthorityExtractor();
174171

175172
OAuth2User oauth2User = new DefaultOAuth2User(
176173
AuthorityUtils.createAuthorityList("SCOPE_message:read"),
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.kafbat.ui.config;
2+
3+
import java.io.IOException;
4+
import java.util.List;
5+
import org.jetbrains.annotations.NotNull;
6+
import org.springframework.boot.env.YamlPropertySourceLoader;
7+
import org.springframework.core.env.PropertySource;
8+
import org.springframework.core.io.support.EncodedResource;
9+
import org.springframework.core.io.support.PropertySourceFactory;
10+
11+
public class YamlPropertySourceFactory implements PropertySourceFactory {
12+
private final YamlPropertySourceLoader loader = new YamlPropertySourceLoader();
13+
14+
@Override
15+
public @NotNull PropertySource<?> createPropertySource(String name, EncodedResource resource)
16+
throws IOException {
17+
List<PropertySource<?>> loaded = loader.load(name, resource.getResource());
18+
if (loaded.size() == 1) {
19+
return loaded.getFirst();
20+
} else {
21+
throw new IOException(resource.getResource().getFilename());
22+
}
23+
}
24+
}

api/src/test/java/io/kafbat/ui/util/AccessControlServiceMock.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,23 @@
44
import static org.mockito.ArgumentMatchers.anyString;
55
import static org.mockito.Mockito.when;
66

7+
import io.kafbat.ui.model.rbac.Role;
78
import io.kafbat.ui.service.rbac.AccessControlService;
9+
import java.util.List;
810
import org.mockito.Mockito;
911
import reactor.core.publisher.Mono;
1012

1113
public class AccessControlServiceMock {
14+
private final List<Role> roles;
15+
16+
public AccessControlServiceMock(List<Role> roles) {
17+
this.roles = roles;
18+
}
19+
20+
public AccessControlServiceMock() {
21+
this(List.of());
22+
}
23+
1224

1325
public AccessControlService getMock() {
1426
AccessControlService mock = Mockito.mock(AccessControlService.class);
@@ -17,6 +29,7 @@ public AccessControlService getMock() {
1729
when(mock.isSchemaAccessible(anyString(), anyString())).thenReturn(Mono.just(true));
1830

1931
when(mock.filterViewableTopics(any(), any())).then(invocation -> Mono.just(invocation.getArgument(0)));
32+
when(mock.getRoles()).thenReturn(roles);
2033

2134
return mock;
2235
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
rbac:
2+
roles:
3+
- name: 'admin'
4+
subjects:
5+
- provider: 'OAUTH'
6+
value: 'ROLE-[A-Z]+'
7+
type: 'role'
8+
regex: 'true'
9+
- provider: 'OAUTH_COGNITO'
10+
value: 'ROLE-ADMIN'
11+
type: 'group'
12+
- provider: 'OAUTH_GOOGLE'
13+
type: 'domain'
14+
value: 'memelord.lol'
15+
clusters:
16+
- local
17+
- remote
18+
permissions:
19+
- resource: APPLICATIONCONFIG
20+
actions: [ all ]
21+
- name: 'viewer'
22+
subjects:
23+
- provider: 'LDAP'
24+
value: 'CS-XXX'
25+
type: 'kafka-viewer'
26+
- provider: 'OAUTH'
27+
value: '.*@kafka.com'
28+
type: 'user'
29+
regex: 'true'
30+
- provider: 'OAUTH_COGNITO'
31+
value: '.*@kafka.com'
32+
type: 'user'
33+
regex: 'true'
34+
- provider: 'OAUTH_GITHUB'
35+
value: '.*@kafka.com'
36+
type: 'user'
37+
regex: 'true'
38+
- provider: 'OAUTH_GOOGLE'
39+
value: 'john@kafka.com'
40+
type: 'user'
41+
clusters:
42+
- remote
43+
permissions:
44+
- resource: APPLICATIONCONFIG
45+
actions: [ all ]
46+
- name: 'editor'
47+
subjects:
48+
- provider: 'OAUTH'
49+
value: 'ROLE_EDITOR'
50+
type: 'role'
51+
clusters:
52+
- local
53+
permissions:
54+
- resource: APPLICATIONCONFIG
55+
actions: [ all ]
56+
- name: "no one's role"
57+
clusters:
58+
- local
59+
- remote
60+
subjects:
61+
- provider: 'OAUTH'
62+
value: '.*XXX'
63+
type: 'role'
64+
- provider: 'OAUTH_GITHUB'
65+
value: '.*XXX'
66+
type: 'user'
67+
- provider: 'OAUTH_COGNITO'
68+
value: '.*XXX'
69+
type: 'user'
70+
- provider: 'OAUTH_GOOGLE'
71+
value: '.*XXX'
72+
type: 'domain'

api/src/test/resources/roles_definition.yaml

Lines changed: 0 additions & 67 deletions
This file was deleted.

0 commit comments

Comments
 (0)