dasniko / testcontainers-keycloak

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

Container doesn't start if I add a Realm file to upload #121

Closed AnakinPt closed 8 months ago

AnakinPt commented 8 months ago

Describe the bug

When I'm trying to import a realm exported from an existing keycloak server, the testcontainer fails with this message.

I was following the steps in https://www.baeldung.com/spring-boot-keycloak-integration-testing and have this code in my test:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
@Testcontainers
class OAuth2ControllerTest {

  @Container
  protected static KeycloakContainer keycloakContainer =
      new KeycloakContainer()
          .withReuse(true)
          .withStartupTimeout(Duration.of(1, ChronoUnit.MINUTES))
          .withAdminUsername("admin")
          .withAdminPassword("admin")
          .withRealmImportFile("keycloak/realm-export-basic.json");

@DynamicPropertySource
  static void registerPgProperties(DynamicPropertyRegistry registry) {
    registry.add(
        "spring.security.oauth2.resourceserver.jwt.issuer-uri",
        () -> keycloakContainer.getAuthServerUrl() + "/realms/cfi");
  }

...
}

realm-export-basic.json

Version

3.0.0

Expected behavior

Keycloak starts and I can run tests on it.

Actual behavior

Container doesn't start

How to Reproduce?

Run a keycloak locally with docker image bitnami/keycloak:22.0.4-debian-11-r3 (I use the bitnami helm chart to run), do a configuration of a basic realm in the admin console, partially export it with groups, roles and clients, and then try to start a container with that file.

Relevant log output

2023-10-17 14:14:17 2023-10-17 13:14:17,663 INFO  [org.keycloak.exportimport.singlefile.SingleFileImportProvider] (main) Full importing from file /opt/keycloak/bin/../data/import/realm-export-basic.json
2023-10-17 14:14:19 2023-10-17 13:14:19,855 ERROR [org.keycloak.quarkus.runtime.cli.ExecutionExceptionHandler] (main) ERROR: Failed to start server in (development) mode
2023-10-17 14:14:19 2023-10-17 13:14:19,855 ERROR [org.keycloak.quarkus.runtime.cli.ExecutionExceptionHandler] (main) ERROR: Script upload is disabled
2023-10-17 14:14:19 2023-10-17 13:14:19,856 ERROR [org.keycloak.quarkus.runtime.cli.ExecutionExceptionHandler] (main) For more details run the same command passing the '--verbose' option. Also you can use '--help' to see the details about the usage of the particular command.


### Anything else?

_No response_
dasniko commented 8 months ago

Have you tried to import the json file into another, regular Keycloak server? Most likely there's something in your json which Keycloak doesn't like.

AnakinPt commented 8 months ago

I think it was me who had a wrong file. I might forgotten to remove "authorizationSettings" from the file. Closing as today is working properly.