dasniko / testcontainers-keycloak

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

Ability to import realm from local file #131

Closed hohwille closed 2 months ago

hohwille commented 3 months ago

Description

You have hardwired in your code that realm files must be on the classpath: https://github.com/dasniko/testcontainers-keycloak/blob/3bf421ac8cf50eaeb1fc978444e2bc3506f38116/src/main/java/dasniko/testcontainers/keycloak/ExtendableKeycloakContainer.java#L190-L197 I would like to reuse the config file I already have in my project for docker compose without duplicating it into src/test/resources. I can manually inject it into the container like this:

      byte[] data = Files.readAllBytes(Path.of("../docker/keycloak/import/realm.json"));
      KEYCLOAK.withCopyToContainer(Transferable.of(data), "/opt/keycloak/data/import/realm.json");

However, I cannot set the --import-realm option then: https://github.com/dasniko/testcontainers-keycloak/blob/3bf421ac8cf50eaeb1fc978444e2bc3506f38116/src/main/java/dasniko/testcontainers/keycloak/ExtendableKeycloakContainer.java#L228

It would be awesome to have a way to specify a regular file to import as realm e.g. like this:

      KEYCLOAK.withRealmImportFile("file:../docker/keycloak/import/realm.json");

Or otherwise have a way to add custom entries to commandParts e.g. via withCommand.

Motivation

So that I am not forced to redundantly add the realm config I already have for docker compose into src/test/resources.

Details

Already provided in description.

exaucae commented 2 months ago

you can use .withFileSystemBind("../docker/realm-export.json", "/opt/keycloak/data/import/realm-export.json",

dasniko commented 2 months ago

Hi @hohwille I can think of setting the --import-realm flag as default, even if not import file is available. This wouldn't hurt if there's no file and one can simply set any file outside the classpath (with copy or bind) as import file. Would that be ok for you?

hohwille commented 2 months ago

Hi @hohwille I can think of setting the --import-realm flag as default, even if not import file is available. This wouldn't hurt if there's no file and one can simply set any file outside the classpath (with copy or bind) as import file. Would that be ok for you?

That would indeed be a great option. You could also think about adding a withRealmImportFile that takes a MoutableFile as argument and make the String arg variant of this method use MountableFile.forClasspathResource(importFile) and delegate to the MoutableFile one.

dasniko commented 2 months ago

@hohwille so, #138 will be ok for you?

hohwille commented 2 months ago

@hohwille so, #138 will be ok for you?

Sure, you can close this issue if you implemented #138 However, also have a look at #137 and consider that in the same context.

dasniko commented 2 months ago

Hopefully you've seen that for #137 there the PR #139 suggestion!?