-
-
Notifications
You must be signed in to change notification settings - Fork 99
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
BE: RBAC: Add integration test for Active Directory auth #726
Open
wernerdv
wants to merge
10
commits into
kafbat:main
Choose a base branch
from
wernerdv:ad_it_test
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1c358bd
BE: RBAC: Add integration test for Active Directory auth
wernerdv 892fa47
Merge branch 'main' into ad_it_test
wernerdv 21712c5
BE: RBAC: Add integration test for Active Directory auth
wernerdv 1fd6aff
BE: RBAC: Add integration test for Active Directory auth
wernerdv 40a5f0f
BE: RBAC: Add integration test for Active Directory auth
wernerdv 874c105
Merge branch 'main' into ad_it_test
wernerdv db7c2a3
Revert "BE: RBAC: Add integration test for Active Directory auth"
wernerdv d8beb34
Merge branch 'refs/heads/main' into ad_it_test
wernerdv 9b585e7
Merge branch 'main' into ad_it_test
wernerdv fff7443
Merge branch 'main' into ad_it_test
wernerdv 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
120 changes: 120 additions & 0 deletions
120
api/src/test/java/io/kafbat/ui/ActiveDirectoryIntegrationTest.java
This file contains 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,120 @@ | ||
package io.kafbat.ui; | ||
|
||
import static io.kafbat.ui.AbstractIntegrationTest.LOCAL; | ||
import static io.kafbat.ui.container.ActiveDirectoryContainer.DOMAIN; | ||
import static io.kafbat.ui.container.ActiveDirectoryContainer.EMPTY_PERMISSIONS_USER; | ||
import static io.kafbat.ui.container.ActiveDirectoryContainer.FIRST_USER_WITH_GROUP; | ||
import static io.kafbat.ui.container.ActiveDirectoryContainer.PASSWORD; | ||
import static io.kafbat.ui.container.ActiveDirectoryContainer.SECOND_USER_WITH_GROUP; | ||
import static io.kafbat.ui.container.ActiveDirectoryContainer.USER_WITHOUT_GROUP; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import io.kafbat.ui.container.ActiveDirectoryContainer; | ||
import io.kafbat.ui.model.AuthenticationInfoDTO; | ||
import io.kafbat.ui.model.ResourceTypeDTO; | ||
import io.kafbat.ui.model.UserPermissionDTO; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.context.ApplicationContextInitializer; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.web.reactive.server.WebTestClient; | ||
import org.springframework.web.reactive.function.BodyInserters; | ||
|
||
@SpringBootTest | ||
@ActiveProfiles("rbac-ad") | ||
@AutoConfigureWebTestClient(timeout = "60000") | ||
@ContextConfiguration(initializers = {ActiveDirectoryIntegrationTest.Initializer.class}) | ||
public class ActiveDirectoryIntegrationTest { | ||
private static final String SESSION = "SESSION"; | ||
|
||
private static final ActiveDirectoryContainer ACTIVE_DIRECTORY = new ActiveDirectoryContainer(); | ||
|
||
@Autowired | ||
private WebTestClient webTestClient; | ||
|
||
@BeforeAll | ||
public static void setup() { | ||
ACTIVE_DIRECTORY.start(); | ||
} | ||
|
||
@AfterAll | ||
public static void shutdown() { | ||
ACTIVE_DIRECTORY.stop(); | ||
} | ||
|
||
@Test | ||
public void testUserPermissions() { | ||
AuthenticationInfoDTO info = authenticationInfo(FIRST_USER_WITH_GROUP); | ||
|
||
assertNotNull(info); | ||
assertTrue(info.getRbacEnabled()); | ||
|
||
List<UserPermissionDTO> permissions = info.getUserInfo().getPermissions(); | ||
|
||
assertFalse(permissions.isEmpty()); | ||
assertTrue(permissions.stream().anyMatch(permission -> | ||
permission.getClusters().contains(LOCAL) && permission.getResource() == ResourceTypeDTO.TOPIC)); | ||
assertEquals(permissions, authenticationInfo(SECOND_USER_WITH_GROUP).getUserInfo().getPermissions()); | ||
assertEquals(permissions, authenticationInfo(USER_WITHOUT_GROUP).getUserInfo().getPermissions()); | ||
} | ||
|
||
@Test | ||
public void testEmptyPermissions() { | ||
assertTrue(Objects.requireNonNull(authenticationInfo(EMPTY_PERMISSIONS_USER)) | ||
.getUserInfo() | ||
.getPermissions() | ||
.isEmpty() | ||
); | ||
} | ||
|
||
private String session(String name) { | ||
return Objects.requireNonNull( | ||
webTestClient | ||
.post() | ||
.uri("/login") | ||
.contentType(MediaType.APPLICATION_FORM_URLENCODED) | ||
.body(BodyInserters.fromFormData("username", name).with("password", PASSWORD)) | ||
.exchange() | ||
.expectStatus() | ||
.isFound() | ||
.returnResult(String.class) | ||
.getResponseCookies() | ||
.getFirst(SESSION)) | ||
.getValue(); | ||
} | ||
|
||
private AuthenticationInfoDTO authenticationInfo(String name) { | ||
return webTestClient | ||
.get() | ||
.uri("/api/authorization") | ||
.cookie(SESSION, session(name)) | ||
.exchange() | ||
.expectStatus() | ||
.isOk() | ||
.returnResult(AuthenticationInfoDTO.class) | ||
.getResponseBody() | ||
.blockFirst(); | ||
} | ||
|
||
public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> { | ||
@Override | ||
public void initialize(@NotNull ConfigurableApplicationContext context) { | ||
System.setProperty("spring.ldap.urls", ACTIVE_DIRECTORY.getLdapUrl()); | ||
System.setProperty("oauth2.ldap.activeDirectory", "true"); | ||
System.setProperty("oauth2.ldap.activeDirectory.domain", DOMAIN); | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
api/src/test/java/io/kafbat/ui/container/ActiveDirectoryContainer.java
This file contains 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,79 @@ | ||
package io.kafbat.ui.container; | ||
|
||
import com.github.dockerjava.api.command.InspectContainerResponse; | ||
import java.io.IOException; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.testcontainers.containers.GenericContainer; | ||
import org.testcontainers.utility.DockerImageName; | ||
|
||
@Slf4j | ||
public class ActiveDirectoryContainer extends GenericContainer<ActiveDirectoryContainer> { | ||
public static final String DOMAIN = "corp.kafbat.io"; | ||
public static final String PASSWORD = "StrongPassword123"; | ||
public static final String FIRST_USER_WITH_GROUP = "JohnDoe"; | ||
public static final String SECOND_USER_WITH_GROUP = "JohnWick"; | ||
public static final String USER_WITHOUT_GROUP = "JackSmith"; | ||
public static final String EMPTY_PERMISSIONS_USER = "JohnJames"; | ||
|
||
private static final String DOMAIN_DC = "dc=corp,dc=kafbat,dc=io"; | ||
private static final String GROUP = "group"; | ||
private static final String FIRST_GROUP = "firstGroup"; | ||
private static final String SECOND_GROUP = "secondGroup"; | ||
private static final String DOMAIN_EMAIL = "kafbat.io"; | ||
private static final String SAMBA_TOOL = "samba-tool"; | ||
private static final int LDAP_PORT = 389; | ||
private static final DockerImageName IMAGE_NAME = DockerImageName.parse("nowsci/samba-domain:latest"); | ||
|
||
public ActiveDirectoryContainer() { | ||
super(IMAGE_NAME); | ||
|
||
withExposedPorts(LDAP_PORT); | ||
|
||
withEnv("DOMAIN", DOMAIN); | ||
withEnv("DOMAIN_DC", DOMAIN_DC); | ||
withEnv("DOMAIN_EMAIL", DOMAIN_EMAIL); | ||
withEnv("DOMAINPASS", PASSWORD); | ||
withEnv("NOCOMPLEXITY", "true"); | ||
withEnv("INSECURELDAP", "true"); | ||
|
||
withPrivilegedMode(true); | ||
} | ||
|
||
protected void containerIsStarted(InspectContainerResponse containerInfo) { | ||
createUser(EMPTY_PERMISSIONS_USER); | ||
createUser(USER_WITHOUT_GROUP); | ||
createUser(FIRST_USER_WITH_GROUP); | ||
createUser(SECOND_USER_WITH_GROUP); | ||
|
||
exec(SAMBA_TOOL, GROUP, "add", FIRST_GROUP); | ||
exec(SAMBA_TOOL, GROUP, "add", SECOND_GROUP); | ||
exec(SAMBA_TOOL, GROUP, "addmembers", FIRST_GROUP, FIRST_USER_WITH_GROUP); | ||
exec(SAMBA_TOOL, GROUP, "addmembers", SECOND_GROUP, SECOND_USER_WITH_GROUP); | ||
} | ||
|
||
public String getLdapUrl() { | ||
return String.format("ldap://%s:%s", getHost(), getMappedPort(LDAP_PORT)); | ||
} | ||
|
||
private void createUser(String name) { | ||
exec(SAMBA_TOOL, "user", "create", name, PASSWORD, "--mail-address", name + '@' + DOMAIN_EMAIL); | ||
exec(SAMBA_TOOL, "user", "setexpiry", name, "--noexpiry"); | ||
} | ||
|
||
private void exec(String... cmd) { | ||
ExecResult result; | ||
try { | ||
result = execInContainer(cmd); | ||
} catch (IOException | InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
if (result.getStdout() != null && !result.getStdout().isEmpty()) { | ||
log.info("Output: {}", result.getStdout()); | ||
} | ||
|
||
if (result.getExitCode() != 0) { | ||
throw new IllegalStateException(result.toString()); | ||
} | ||
} | ||
} |
This file contains 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,23 @@ | ||
auth: | ||
type: LDAP | ||
rbac: | ||
roles: | ||
- name: "roleName" | ||
clusters: | ||
- local | ||
subjects: | ||
- provider: ldap_ad | ||
type: group | ||
value: firstGroup | ||
- provider: ldap_ad | ||
type: group | ||
value: secondGroup | ||
- provider: ldap_ad | ||
type: user | ||
value: JackSmith | ||
permissions: | ||
- resource: applicationconfig | ||
actions: all | ||
- resource: topic | ||
value: ".*" | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, otherwise there will be an error when starting the container:
in the documentation,
--privileged
flag is also used in the exampleshttps://nowsci.com/samba-domain/#examples-with-docker-run