8000 fix: provide a simple mechanism to get a full export in dev mode by shawkins · Pull Request #47112 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

fix: provide a simple mechanism to get a full export in dev mode - #47112

Draft
shawkins wants to merge 3 commits into
keycloak:mainfrom
shawkins:iss33800-b
Draft

shawkins wants to merge 3 commits into
keycloak:mainfrom
shawkins:iss33800-b

Conversation

@shawkins
@shawkins shawkins commented Mar 12, 2026 •
Copy link
Copy Markdown
Contributor

closes: #33800

Based upon the previous attempts to resolve this and the continued comments on the issue, I'd like to propose adding a special purpose endpoint for this. It's not ideal and not something that is applicable to production, but it has a minimal code footprint and will work without a lot of additional documentation. We can just show using kcadm.sh (via docker):

kcadm.sh get dev-export -r name > name-realm.json

Thoughts @keycloak/cloud-native ?

@vmuzikar
Copy link
Copy Markdown
Contributor

If it's for dev purposes, what is the issue with just shutting down the server and just doing kc.sh export?

@michalvavrik
michalvavrik commented Mar 13, 2026 •
Copy link
Copy Markdown
Member

If it's for dev purposes, what is the issue with just shutting down the server and just doing kc.sh export?

I cannot speak for @shawkins , but you can only do that if you have persistent storage that lives after you stopped the server.

I invested few hours trying to do it with h2 devfile for we would like to having ability to export realms configured by users in Keycloak Dev Service and I never managed it. That is a separate issue (I know about all other export related issues and I watch them), because we are not using DEV mode now in Quarkus, so this PR won't help us (which is not your motivation, so np).

@vmuzikar
Copy link
Copy Markdown
Contributor

but you can only do that if you have persistent storage that lives after you stopped the server

Yeah, dev-mem DB would be probably the only use case. However, isn't it the whole purpose of using dev-mem that it's ephemeral? As a user I need to count with its limitations.

@michalvavrik
Copy link
Copy Markdown
Member

but you can only do that if you have persistent storage that lives after you stopped the server

Yeah, dev-mem DB would be probably the only use case. However, isn't it the whole purpose of using dev-mem that it's ephemeral? As a user I need to count with its limitations.

it is absolutely fine if it is ephemeral, but I still would like to having ability to do full export when using dev-mem (not just in DEV mode), we had users asking for it and interestingly, when I re-read #33800 there was also user using Quarkus keycloak dev service who tried to do it manually.

Anyway, I don't want to steal the show, let's go back to @shawkins proposal :-) I just tried to provide one of contexts from my user experience.

@shawkins
Copy link
Copy Markdown
Contributor Author

If it's for dev purposes, what is the issue with just shutting down the server and just doing kc.sh export?

To add onto what @michalvavrik and users on the issue are saying that is either not possible with dev-mem (which I'm not sure that users are explicitly configuring via dev services) or not straight-forward with dev-file - see the workarounds to copy the h2 files out of the container.

@vmuzikar
Copy link
Copy Markdown
Contributor

Ok, thanks for expanding on the reasoning @shawkins @michalvavrik, makes sense.

@ahus1 @pedroigor Do you have any concerns with this?

@mabartos
mabartos commented Mar 13, 2026 •
Copy link
Copy Markdown
Member

@shawkins +1 I like this minimalistic approach. It'd be maybe good to mention why it's possible only in dev mode to prevent any additional questions. Like briefly mention why it should not be used in prod. WDYT?

Then, providing the docs should happen.

Thanks!

@shawkins
Copy link
Copy Markdown
Contributor Author

Like briefly mention why it should not be used in prod. WDYT?

Do you mean on the issue, in the operation doc, or in our other docs?

An explanation could include:

  • for prod this is the same as an export command, use that instead
  • this may have too large of a memory footprint for a running production server
  • this is the internal format of the export, which won't have secrets stripped, so we generally don't want to expose that over the wire

Anything else?

@vmuzikar
Copy link
Copy Markdown
Contributor

Anything else?

Wondering, whether we should even straight disable that endpoint in prod profile...

@stianst stianst left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not support full realm export over REST. I get this is in dev mode only, but this can lead to folks actually using dev in production to workaround this, and also if there is a bug it could lead to the full export being available in prod as well. It also leads to confusion as there is different things support in dev/testing vs what is supported in production.

@shawkins
shawkins commented Mar 16, 2026 •
Copy link
Copy Markdown
Contributor Author

Wondering, whether we should even straight disable that endpoint in prod profile...

You mean instead of a check in the method, use a mechanism so that the endpoint isn't available?

We should not support full realm export over REST. I get this is in dev mode only, but this can lead to folks actually using dev in production to workaround this, and also if there is a bug it could lead to the full export being available in prod as well. It also leads to confusion as there is different things support in dev/testing vs what is supported in production.

I fully agree that this is not ideal. However #33800 is probably the most voted upon issue we have and we've yet to offer a satisfactory solution in quite some time, so I think we should expand what we're willing to consider to give new users of Keycloak a better experience.

If restricting only to dev mode seems too weak, I would also consider checking that the db must be dev-mem or dev-file, or even add a restriction that the request can only come from localhost.

If the full export over rest is not acceptable under any circumstance, the next thought is that dev-export rather than returning the realm will save it to data/import or other well-known path.

@shawkins
shawkins commented Mar 19, 2026 •
Copy link
Copy Markdown
Contributor Author

Updated based upon the previous comment. The endpoint is now performing an export to the file system under our temp directory /realm-name

It seems fine if we also restrict to localhost (not yet added to the pr) - unless the user sets up a mount ahead of time, they will have to go into the container in some way to get the file.

It was also changed to check only for the db type, not dev mode. @michalvavrik indicated quarkus use cases where a dev database is intentionally used in prod mode.

Alternatives and/or other considerations:

  • could remove the dev-mem db entirely - this is introducing a hard requirement that the container must be running to get the export. There will never be a way to workaround that.
  • for dev-file we could go back to the idea of using AUTO_SERVER - this however also requires the container to be running. We would restrict h2 to localhost and a configurable port. By shelling into the running container you could then perform the export - as long as there were no conflicts by simultaneously running the export (which we have not in general guarenteed for import / export commands). Or if the container exposes the port, then the export could be run by a separate container / process.
  • finally as discussed on the issue there's a variety of strategies for using the h2 database files directly.

@shawkins
shawkins commented Apr 9, 2026
Copy link
Copy Markdown
Contributor Author

@stianst are you ok to move ahead with the endpoint that exports to a file as described in the previous comment?

closes: keycloak#33800

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

Refined to show this as fully working. What's left to do if this is acceptable:

  • documentation
  • testing negative cases non-realm admin, non-h2 database

@stianst
stianst commented May 6, 2026
Copy link
Copy Markdown
Contributor

I'm really not keen on having this in the Admin APIs at all. I get that it's only for h2 dev database, but some folks actually use that in production, and I could see this being unintentionally expanded on in the future. I'd like to keep a pretty hard stance on the no export through Admin APIs, regardless if it's dumping to a file or sending via REST; some alternative ideas:

  • Don't use dev-mem, and just do dev-file if you want to be able to export it. We could even add a short-hand to the kc.sh, something like kc.sh export --h2-db=my.h2.db ...
  • Maybe we could consider adding an option like --export-on-shutdown to kc.sh start-dev

@stianst
stianst commented May 6, 2026
Copy link
Copy Markdown
Contributor

One other option maybe:

  • start Keycloak with dev-file in one container
  • copy the db file; then start another container that does an export from that

That could be documented as an example

@stianst
stianst commented May 6, 2026
Copy link
Copy Markdown
Contributor

For dev-mem I'd just do a warning at startup stating you are using a transient database, and changes will be lost

@stianst
stianst commented May 6, 2026
Copy link
Copy Markdown
Contributor

Just trying to capture the actual use-case behind this; it seems the use-case is folks want a convinient way to use the admin console as an editor to create a realm export that can later be used for testing, or bootstrapping a real production realm.

If that is it, then I'd say it would be nice to focus on a good solution to that; it can even be a different start-up mode of the server; even a different distribution of KC, or something like that. Basically by focusing the use-case we can better focus the approach.

Just to illustrate let's say it's something like:

kc.sh editor --create-realm myrealm.json
kc.sh editor --edit myrealm.json

@shawkins
shawkins commented May 6, 2026
Copy link
Copy Markdown
Contributor Author

Don't use dev-mem, and just do dev-file if you want to be able to export it. We could even add a short-hand to the kc.sh, something like kc.sh export --h2-db=my.h2.db ...

This sounds like it is an export against the running container, which won't work as the db file will be locked (also we are not currently ensuring you can run import/export commands along side the server process).

Maybe we could consider adding an option like --export-on-shutdown to kc.sh start-dev

Unless that would be hidden and/or restricted to only h2 scenarios, that could cause even more confusion than the endpoint approach.

Do you want to flesh this out as an enhancement in another issue? Does it accept realm(s), a path, etc.?

This would probably need to be enabled in some way by default when using h2 as well - see the final discussion point.

start Keycloak with dev-file in one container
copy the db file; then start another container that does an export from that

This is discussed on the issue. It does not seem like something that should be documented. Also the container must not be running or you risk getting a corrupted copy (h2 warns against doing this).

For dev-mem I'd just do a warning at startup stating you are using a transient database, and changes will be lost

Can you create an issue for that? dev-mem was brought into the discussion here because the approaches initially discussed here would work for both h2 variants.

In particular if you want to persue previous approach of documenting shutting down the container and copying the database file, then we'll want as strong as warning as possible for dev-mem.

Just trying to capture the actual use-case behind this; it seems the use-case is folks want a convinient way to use the admin console as an editor to create a realm export that can later be used for testing, or bootstrapping a real production realm.

I'm not sure that's a good phrasing. Users are currently simply following the existing quickstarts, or using quarkus dev services, then find it hard to propagate any of the work to a real instance.

You could certainly start going down the path of introducing new commands or distributions, but without consistently pointing users to that and/or warning them not to do any work they'll want to later save with a non-production database, some will still hit this problem.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

when it fails to export realm with docker container

5 participants

0