Skip to content

Commit

Permalink
Remove obsolete keycloak.json manipulation before running AssumeRoleW…
Browse files Browse the repository at this point in the history
…ithWebIdentity testcontainer tests.

Signed-off-by: chenkins <chenkins44@gmail.com>
  • Loading branch information
chenkins authored and dkocher committed Aug 28, 2023
1 parent 8150cf5 commit bdfdcf0
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 74 deletions.
Expand Up @@ -24,89 +24,23 @@
import org.testcontainers.containers.wait.strategy.Wait;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

@Category(TestcontainerTest.class)
public abstract class AbstractAssumeRoleWithWebIdentityTest {
protected static final Logger log = LogManager.getLogger(AbstractAssumeRoleWithWebIdentityTest.class);

protected static final int OAUTH_TTL_MILLIS = 5000;

public static DockerComposeContainer prepareDockerComposeContainer(final String keyCloakRealmTempFile) {
public static DockerComposeContainer prepareDockerComposeContainer() {
log.info("Preparing docker compose container...");
return new DockerComposeContainer<>(
new File(AbstractAssumeRoleWithWebIdentityTest.class.getResource("/testcontainer/docker-compose.yml").getFile()))
.withEnv("KEYCLOAK_REALM_JSON", keyCloakRealmTempFile)
.withPull(false)
.withLocalCompose(true)
.withOptions("--compatibility")
.withExposedService("keycloak_1", 8080, Wait.forListeningPort())
.withExposedService("minio_1", 9000, Wait.forListeningPort());
}

public static String getKeyCloakFile(Map<String, String> replacements) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonElement je = new Gson().fromJson(new InputStreamReader(AbstractAssumeRoleWithWebIdentityTest.class.getResourceAsStream("/testcontainer/keycloak/keycloak-realm.json")), JsonElement.class);
JsonObject jo = je.getAsJsonObject();

for(Map.Entry<String, String> replacement : replacements.entrySet()) {
updateJsonValues(jo, replacement.getKey(), replacement.getValue());
}

String content = gson.toJson(jo);
try {
final Path tempFile = Files.createTempFile(null, null);
Files.write(tempFile, content.getBytes(StandardCharsets.UTF_8));
return tempFile.toAbsolutePath().toString();

}
catch(IOException e) {
throw new RuntimeException(e);
}
}

private static void updateJsonValues(JsonObject jsonObj, String key, String newVal) {
for(Map.Entry<String, JsonElement> entry : jsonObj.entrySet()) {
JsonElement element = entry.getValue();
if(element.isJsonArray()) {
updateJsonValues(element.getAsJsonArray(), key, newVal);
}
else if(element.isJsonObject()) {
updateJsonValues(element.getAsJsonObject(), key, newVal);
}
else if(entry.getKey().equals(key)) {
jsonObj.remove(key);
jsonObj.addProperty(key, newVal);
break;
}
}
}

private static void updateJsonValues(JsonArray asJsonArray, String key, String newVal) {
for(int index = 0; index < asJsonArray.size(); index++) {
JsonElement element = asJsonArray.get(index);
if(element.isJsonArray()) {
updateJsonValues(element.getAsJsonArray(), key, newVal);
}
else if(element.isJsonObject()) {
updateJsonValues(element.getAsJsonObject(), key, newVal);
}
}
}

protected static String getKeyCloakFile() {
return getKeyCloakFile(Collections.emptyMap());
}
}
Expand Up @@ -56,7 +56,7 @@
public class AssumeRoleWithWebIdentityAuthenticationTest extends AbstractAssumeRoleWithWebIdentityTest {

@ClassRule
public static DockerComposeContainer<?> compose = prepareDockerComposeContainer(getKeyCloakFile());
public static DockerComposeContainer<?> compose = prepareDockerComposeContainer();

@Test
public void testSuccessfulLogin() throws BackgroundException {
Expand Down
Expand Up @@ -41,7 +41,6 @@
import ch.cyberduck.core.transfer.TransferStatus;
import ch.cyberduck.test.TestcontainerTest;

import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
Expand All @@ -57,11 +56,11 @@
public class AssumeRoleWithWebIdentityAuthorizationTest extends AbstractAssumeRoleWithWebIdentityTest {

@ClassRule
public static DockerComposeContainer<?> compose = prepareDockerComposeContainer(getKeyCloakFile());
public static DockerComposeContainer<?> compose = prepareDockerComposeContainer();

@Test
public void testAuthorizationFindBucket() throws BackgroundException {
final Protocol profile = new ProfilePlistReader(new ProtocolFactory(new HashSet<>(Collections.singleton(new S3Protocol())))).read(
final Protocol profile = new ProfilePlistReader(new ProtocolFactory(new HashSet<>(Collections.singleton(new S3Protocol())))).read(
AbstractAssumeRoleWithWebIdentityTest.class.getResourceAsStream("/S3 (OIDC).cyberduckprofile"));
final Host host = new Host(profile, profile.getDefaultHostname(), new Credentials("rawuser", "rawuser"));
final S3Session session = new S3Session(host);
Expand All @@ -74,7 +73,7 @@ public void testAuthorizationFindBucket() throws BackgroundException {

@Test
public void testAuthorizationUserReadAccessOnBucket() throws BackgroundException {
final Protocol profile = new ProfilePlistReader(new ProtocolFactory(new HashSet<>(Collections.singleton(new S3Protocol())))).read(
final Protocol profile = new ProfilePlistReader(new ProtocolFactory(new HashSet<>(Collections.singleton(new S3Protocol())))).read(
AbstractAssumeRoleWithWebIdentityTest.class.getResourceAsStream("/S3 (OIDC).cyberduckprofile"));
final Host host = new Host(profile, profile.getDefaultHostname(), new Credentials("rouser", "rouser"));
final S3Session session = new S3Session(host);
Expand All @@ -88,7 +87,7 @@ public void testAuthorizationUserReadAccessOnBucket() throws BackgroundException

@Test
public void testAuthorizationWritePermissionOnBucket() throws BackgroundException {
final Protocol profile = new ProfilePlistReader(new ProtocolFactory(new HashSet<>(Collections.singleton(new S3Protocol())))).read(
final Protocol profile = new ProfilePlistReader(new ProtocolFactory(new HashSet<>(Collections.singleton(new S3Protocol())))).read(
AbstractAssumeRoleWithWebIdentityTest.class.getResourceAsStream("/S3 (OIDC).cyberduckprofile"));
final Host host = new Host(profile, profile.getDefaultHostname(), new Credentials("rawuser", "rawuser"));
final S3Session session = new S3Session(host);
Expand All @@ -105,7 +104,7 @@ public void testAuthorizationWritePermissionOnBucket() throws BackgroundExceptio

@Test
public void testAuthorizationNoWritePermissionOnBucket() throws BackgroundException {
final Protocol profile = new ProfilePlistReader(new ProtocolFactory(new HashSet<>(Collections.singleton(new S3Protocol())))).read(
final Protocol profile = new ProfilePlistReader(new ProtocolFactory(new HashSet<>(Collections.singleton(new S3Protocol())))).read(
AbstractAssumeRoleWithWebIdentityTest.class.getResourceAsStream("/S3 (OIDC).cyberduckprofile"));
final Host host = new Host(profile, profile.getDefaultHostname(), new Credentials("rouser", "rouser"));
final S3Session session = new S3Session(host);
Expand Down

0 comments on commit bdfdcf0

Please sign in to comment.