Skip to content

Commit

Permalink
feat(auth): allows List or String in topics-filter-regex (#659)
Browse files Browse the repository at this point in the history
close #524
close #656
  • Loading branch information
twobeeb authored Apr 8, 2021
1 parent 771d0aa commit d5c6272
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/akhq/configs/Group.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
public class Group {
String name;
List<String> roles;
Map<String, Object> attributes;
Map<String, List<String>> attributes;
}
2 changes: 1 addition & 1 deletion src/main/java/org/akhq/utils/UserGroupUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Map<String, Object> getUserAttributes(List<String> groups) {
.flatMap(group -> (group.getAttributes() != null) ? group.getAttributes().entrySet().stream() : null)
.collect(Collectors.toMap(
Map.Entry::getKey,
item -> new ArrayList<>(Collections.singletonList(item.getValue())),
item -> new ArrayList<>(item.getValue()),
(e1, e2) -> {
((List) e1).addAll((List) e2); return e1;
}
Expand Down
73 changes: 73 additions & 0 deletions src/test/java/org/akhq/configs/UserGroupUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.akhq.configs;

import io.micronaut.context.ApplicationContext;
import org.akhq.utils.UserGroupUtils;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertIterableEquals;

public class UserGroupUtilsTest {
@Test
void testTopicRegexpAsString() {
ApplicationContext ctx = ApplicationContext.run(ApplicationContext.class,"filterregex");

UserGroupUtils userGroupUtils = ctx.getBean(UserGroupUtils.class);

List<String> actual = (List<String>)userGroupUtils.getUserAttributes(List.of("as-string")).get("topicsFilterRegexp");

assertEquals(
1,
actual.size()
);
assertIterableEquals(
List.of("test.*"),
actual
);

ctx.close();
}

@Test
void testTopicRegexpAsList() {

ApplicationContext ctx = ApplicationContext.run(ApplicationContext.class,"filterregex");

UserGroupUtils userGroupUtils = ctx.getBean(UserGroupUtils.class);

List<String> actual = (List<String>)userGroupUtils.getUserAttributes(List.of("as-list")).get("topicsFilterRegexp");

assertEquals(
2,
actual.size()
);
assertIterableEquals(
List.of("item1","item2"),
actual
);

ctx.close();
}
@Test
void testTopicRegexpAsMixed() {

ApplicationContext ctx = ApplicationContext.run(ApplicationContext.class,"filterregex");

UserGroupUtils userGroupUtils = ctx.getBean(UserGroupUtils.class);

List<String> actual = (List<String>)userGroupUtils.getUserAttributes(List.of("as-string","as-list")).get("topicsFilterRegexp");

assertEquals(
3,
actual.size()
);
assertIterableEquals(
List.of("test.*","item1","item2"),
actual
);

ctx.close();
}
}
15 changes: 15 additions & 0 deletions src/test/resources/application-filterregex.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
akhq:
security:
groups:
as-string:
roles:
- topic/read
attributes:
topics-filter-regexp: "test.*"
as-list:
roles:
- topic/read
attributes:
topics-filter-regexp:
- "item1"
- "item2"

0 comments on commit d5c6272

Please sign in to comment.