8000 fix: allows for the dev-file database to be accessible by shawkins · Pull Request #41431 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

fix: allows for the dev-file database to be accessible - #41431

Closed
shawkins wants to merge 1 commit into
keycloak:mainfrom
shawkins:iss33800
Closed

shawkins wants to merge 1 commit into
keycloak:mainfrom
shawkins:iss33800

Conversation

@shawkins
@shawkins shawkins commented Jul 25, 2025 •
Copy link
Copy Markdown
Contributor

Draft of automatically adding support for AUTO_SERVER.

This restricts the auto close changes for jdbc ping to only the case when infinispan is the cache (a check for non-custom or non-jdbc ping stack seems too involved).

There is a security implication here because h2 chooses to bind to a non-localhost ip if available. For example from my lock.db file: server=192.168.1.55:33123 - so we may not want to automatically enable this mode.

closes: #33800

cc @ryanemerson @thomasdarimont @Dissem @mabartos @vmuzikar

closes: keycloak#33800

Signed-off-by: Steve Hawkins <shawkins@redhat.com>
@thomasdarimont
Copy link
Copy Markdown
Contributor

I just gave this branch a try

Terminal 1 without infinispan

KC_BOOTSTRAP_ADMIN_USERNAME=admin KC_BOOTSTRAP_ADMIN_PASSWORD=admin bin/kc.sh --verbose start-dev

Export in separate terminal

bin/kc.sh export --realm master --file master-realm.json

works

Terminal 1 with infinispan:

KC_BOOTSTRAP_ADMIN_USERNAME=admin KC_BOOTSTRAP_ADMIN_PASSWORD=admin bin/kc.sh --verbose start-dev --cache=ispn

Export in separate terminal

bin/kc.sh export --realm master --file master-realm.json

fails (as expected) with:

Caused by: org.h2.mvstore.MVStoreException: The file is locked: /path/to/keycloak/data/h2/keycloakdb.mv.db [2.3.230/7]

I think this is fine, as one usually does not run Keycloak with embedded h2 in clustered mode.

@mabartos
Copy link
Copy Markdown
Member

There is a security implication here because h2 chooses to bind to a non-localhost ip if available. For example from my lock.db file: server=192.168.1.55:33123 - so we may not want to automatically enable this mode.

@shawkins I've just briefly checked it and it might be possible to bind it to address we want:

@shawkins
shawkins commented Jul 30, 2025 •
Copy link
Copy Markdown
Contributor Author

Thinking about it more there are two separate cases here:

  • non-containerized: binding to just localhost will secure things. It would be nice if there were a way to do this that wasn't a system property (I had only checked the url properties), but we can just check if it already set before overwriting it. A random still port seems ok.

  • containerized: we still have a problem. Unless we update the examples to use a local directory mount, the db files will only exist in the container which means you have to shell into it to run the export - that won't work unless we also address any remaining issues with running the export command on the same machine as a running server. The complexity of documenting using a local directory mount isn't much better than suggesting making a database copy: when it fails to export realm with docker container #33800 (comment)

@mabartos
Copy link
Copy Markdown
Member

The complexity of documenting using a local directory mount isn't much better than suggesting making a database copy:

@shawkins Isn't it? I think it might simplify things a little bit as users would not need to provide the DB properties, right? So we can probably agree that we need to document some "workaround" for the containerized approach and the question is what's gonna be better, right? Could you try to show the approach with the local directory mount?

Or what would you suggest?

@shawkins
shawkins commented Jul 30, 2025 •
Copy link
Copy Markdown
Contributor Author

Isn't it? I think it might simplify things a little bit as users would not need to provide the DB properties, right?

Only if you mount location is directly in a keycloak install. The user showing the workaround on #33800 (comment) copied the file to temp location instead.

So we can probably agree that we need to document some "workaround" for the containerized approach and the question is what's gonna be better, right? Could you try to show the approach with the local directory mount?

Let's assume we force the bi 8000 nding of the h2 server to localhost and a well-known port, we at least need to update the examples for the port binding and the mount:

docker run -p 127.0.0.1:8080:8080 -p 127.0.0.1:<db-port>:<db-port> quay.io/keycloak/keycloak:latest start-dev --mount type=bind,source=~/h2,target=/opt/keycloak/data/h2

Then

docker run quay.io/keycloak/keycloak:latest export ... --mount type=bind,source=~/h2,target=/opt/keycloak/data/h2

Using a volume would probably be better so we don't have to show / assume a local working directory.

@shawkins
Copy link
Copy Markdown
Contributor Author

So we can either:

  • refine the changes in this pr, then update the guide to show using a local dir / volume.

  • just show / refine the copy approach:

# with the start-dev container running
docker exec -it my_keycloak sh -c  "cp -rp /opt/keycloak/data/h2 /tmp
docker run quay.io/keycloak/keycloak:latest export ... --mount type=bind,source=/tmp/h2,target=/opt/keycloak/data/h2

Either way the user is on the hook for cleaning up the external location or volume. The only practical benefit to the first approach is that you don't have to be running the start-dev container.

@shawkins
shawkins commented Aug 7, 2025
Copy link
Copy Markdown
Contributor Author

Given the lack of additional feedback, I'm assuming at this point we can just expand on the copy approach instead.

@vmuzikar
Copy link
Copy Markdown
Contributor

Not sure if this was mentioned in this context, but we needed to disable AUTO_SERVER in the past, see #30560.

@shawkins
Copy link
Copy Markdown
Contributor Author

Not sure if this was mentioned in this context, but we needed to disable AUTO_SERVER in the past, see #30560.

It hadn't been yet. Based upon the discussion above if we were to continue with this path, we'd forcably set the h2 server bind address to localhost and use a well known / configurable port.

The only possible further workaround is to use AUTO_SERVER only for start / start-dev while the other commands would use fully remote connections. However this would be a breaking change for some and also require that the keycloak container is running to perfrom an export.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

when it fails to export realm with docker container

4 participants

0