dasniko / testcontainers-keycloak

A Testcontainer implementation for Keycloak IAM & SSO.
Apache License 2.0
328 stars 51 forks source link

Failed to read realm JSON file with the new version 2.1.1 (and thus KC 17) #65

Closed semangard closed 2 years ago

semangard commented 2 years ago

Hello,

I was using KC test container for a while. It was working correctly with the version 1.9.0. Then I deciced today to test it with version 2.1.1 (and thus KC 17) image but KC does not want to start anymore, I have the exception :

`22-03-13 17:26:58.031 / INFO --- [eam--1508215392] 🐳 [quay.io/keycloak/keycloak:17.0.0] : STDOUT: 2022-03-13 16:21:53,586 INFO [io.quarkus] (main) Installed features: [agroal, cdi, hibernate-orm, infinispan-client, jdbc-h2, jdbc-mariadb, jdbc-mssql, jdbc-mysql, jdbc-oracle, jdbc-postgresql, keycloak, narayana-jta, reactive-routes, resteasy, resteasy-jackson, smallrye-context-propagation, smallrye-health, smallrye-metrics, vault, vertx] 2022-03-13 17:26:58.041 / INFO --- [ main] o.t.c.wait.strategy.HttpWaitStrategy : /gracious_lederberg: Waiting for 120 seconds for URL: http://localhost:56189/ (where port 56189 maps to container port 8080) 2022-03-13 17:26:58.082 / INFO --- [ main] 🐳 [quay.io/keycloak/keycloak:17.0.0] : Container quay.io/keycloak/keycloak:17.0.0 started in PT1.3515094S 2022-03-13 17:26:58.082 / INFO --- [ main] 🐳 [quay.io/keycloak/keycloak:17.0.0] : Connect to Keycloak container to import given realm files. 2022-03-13 17:26:58.685 / INFO --- [ main] 🐳 [quay.io/keycloak/keycloak:17.0.0] : Importing realm from file keycloak-test-realm.json 2022-03-13 17:26:58.821 / ERROR --- [ main] 🐳 [quay.io/keycloak/keycloak:17.0.0] : Could not start container

java.lang.IllegalArgumentException: argument "src" is null at com.fasterxml.jackson.databind.ObjectMapper._assertNotNull(ObjectMapper.java:4757) at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3478) at dasniko.testcontainers.keycloak.KeycloakContainer.containerIsStarted(KeycloakContainer.java:185) at org.testcontainers.containers.GenericContainer.containerIsStarted(GenericContainer.java:688) at org.testcontainers.containers.GenericContainer.tryStart(GenericContainer.java:504) at org.testcontainers.containers.GenericContainer.lambda$doStart$0(GenericContainer.java:331) at org.rnorth.ducttape.unreliables.Unreliables.retryUntilSuccess(Unreliables.java:81) at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:329) at org.testcontainers.containers.GenericContainer.start(GenericContainer.java:317) at ch.hcuge.keycloak.test.extension.KeyCloakTestContainerExtension.beforeAll(KeyCloakTestContainerExtension.java:63) at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeBeforeAllCallbacks$10(ClassBasedTestDescriptor.java:381) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeBeforeAllCallbacks(ClassBasedTestDescriptor.java:381) at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.before(ClassBasedTestDescriptor.java:205) at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.before(ClassBasedTestDescriptor.java:80) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:148) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141) at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95) at java.base/java.util.ArrayList.forEach(ArrayList.java:1511) at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141) at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95) at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35) at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57) at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54) at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:107) at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88) at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54) at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67) at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52) at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114) at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:95) at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:91) at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:60) at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:98) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:529) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:756) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:452) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210) `

It seems it failed while importing my realm JSON (cf. attached file) keycloak-test-realm.json.txt

Test extension : image

semangard commented 2 years ago

FYI : it was still working also with: KC test container 1.10.0 + KC 17.0.0-legacy

dasniko commented 2 years ago

This is most probably because you specified the path of the realm file not properly. Where is your json file located? If it's, e.g. src/test/resources/keycloak-test-realm.json, you have to set .withRealmImportFile"/keycloak-test-realm.json"), not ...("keycloak-test-realm.json").

semangard commented 2 years ago

It was working properly with previous version... and I changed only version'numbers

test file is here: image

dasniko commented 2 years ago

The major version changed, so it's possible that parts of your previous environment doesn't work anymore with the most recent version. Also, it's very likely, that the legacy behavior only worked by accident. Set the / in front of your realm file and you should be good to go.

semangard commented 2 years ago

Seems better with the /

image

But now I am facing another exception:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository]: Factory method 'clientRegistrationRepository' threw exception; nested exception is java.lang.IllegalStateException: The Issuer "http://localhost:60190/realms/test" provided in the configuration metadata did not match the requested issuer "http://localhost:60190//realms/test" at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ... 174 more Caused by: java.lang.IllegalStateException: The Issuer "http://localhost:60190/realms/test" provided in the configuration metadata did not match the requested issuer "http://localhost:60190//realms/test"

with unchanged test config: image

and: image

==> might have to add a small thing : remove the traling slash of getAuthServerUrl()

semangard commented 2 years ago

OK it works after manually removing the trailing slash: image

BUT I have now a 3thd issue: if the test container is not removed after a test (which was working properly with previous versions) then I have the following exception while executing next test:

image

I looks like it tries to reimport the realm which fails because it was already imported into the kept up and running KC test container

dasniko commented 2 years ago

Please:

Thanks.