dasniko/testcontainers-keycloak

Ability to import realm from local file

hohwille opened this issue · 6 comments

Description

You have hardwired in your code that realm files must be on the classpath:

if (!importFiles.isEmpty()) {
for (String importFile : importFiles) {
// TODO: a strategy for files with the same name but in the different dirs
String importFileInContainer = DEFAULT_REALM_IMPORT_FILES_LOCATION + FilenameUtils.getName(importFile);
withCopyFileToContainer(MountableFile.forClasspathResource(importFile), importFileInContainer);
}
commandParts.add("--import-realm");
}

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:

throw new IllegalStateException("You are trying to set custom container commands, which is currently not supported by this Testcontainer.");

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.

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

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?

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.

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

@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.

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