From bd499755a28d8c8f35601d783a7335e142d349d9 Mon Sep 17 00:00:00 2001 From: Steven Hawkins Date: Wed, 23 Oct 2024 07:11:42 -0400 Subject: [PATCH] fix: providing a separate session for each file (#34210) closes: #34095 Signed-off-by: Steve Hawkins --- .../it/cli/dist/ImportAtStartupDistTest.java | 22 ++++++++++++++++++- .../exportimport/ExportImportManager.java | 13 +++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/dist/ImportAtStartupDistTest.java b/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/dist/ImportAtStartupDistTest.java index 4f9a913325..031000147b 100644 --- a/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/dist/ImportAtStartupDistTest.java +++ b/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/dist/ImportAtStartupDistTest.java @@ -17,7 +17,9 @@ package org.keycloak.it.cli.dist; +import java.io.File; import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Path; import java.util.function.Consumer; import org.junit.jupiter.api.Test; @@ -30,6 +32,9 @@ import org.keycloak.it.junit5.extension.RawDistOnly; import org.keycloak.it.utils.KeycloakDistribution; import org.keycloak.it.utils.RawKeycloakDistribution; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; + import io.quarkus.deployment.util.FileUtil; import io.quarkus.test.junit.main.Launch; import io.quarkus.test.junit.main.LaunchResult; @@ -45,6 +50,21 @@ public class ImportAtStartupDistTest { CLIResult cliResult = (CLIResult) result; cliResult.assertMessage("Realm 'quickstart-realm' imported"); } + + @Test + @BeforeStartDistribution(CreateRealmConfigurationFile.class) + void testMultipleImport(KeycloakDistribution dist) throws IOException { + RawKeycloakDistribution rawDist = dist.unwrap(RawKeycloakDistribution.class); + Path dir = rawDist.getDistPath().resolve("data").resolve("import"); + + // add another realm + Files.write(dir.resolve("realm2.json"), Files.readAllLines(dir.resolve("realm.json")).stream() + .map(s -> s.replace("quickstart-realm", "other-realm")).toList()); + + CLIResult cliResult = dist.run("start-dev", "--import-realm"); + cliResult.assertMessage("Realm 'quickstart-realm' imported"); + cliResult.assertMessage("Realm 'other-realm' imported"); + } @Test @BeforeStartDistribution(CreateRealmConfigurationFileAndDir.class) @@ -115,7 +135,7 @@ public class ImportAtStartupDistTest { result.assertMessage("Realm 'quickstart-realm' imported"); result.assertNoMessage("Not importing realm master from file"); } - + public static class CreateRealmConfigurationFile implements Consumer { @Override diff --git a/services/src/main/java/org/keycloak/exportimport/ExportImportManager.java b/services/src/main/java/org/keycloak/exportimport/ExportImportManager.java index adab353850..02479e4eaa 100644 --- a/services/src/main/java/org/keycloak/exportimport/ExportImportManager.java +++ b/services/src/main/java/org/keycloak/exportimport/ExportImportManager.java @@ -170,8 +170,17 @@ public class ExportImportManager { } return filesToImport.stream().map(file -> () -> { - ExportImportConfig.setFile(file); - return session.getProvider(ImportProvider.class, providerId); + // we need a new session to pickup the static system property + // file setting - it is picked up by the provider only at create time + // this will eventually need to be consolidated with the master existance check + // to prevent double parsing + KeycloakSession newSession = session.getKeycloakSessionFactory().create(); + try { + ExportImportConfig.setFile(file); + return newSession.getProvider(ImportProvider.class, providerId); + } finally { + newSession.close(); + } }); } return Stream.empty();