Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.nio.file.AccessDeniedException: /opt/keycloak/bin/../data/import/realm.json #137

Closed
hohwille opened this issue Apr 25, 2024 · 2 comments · Fixed by #139
Closed

java.nio.file.AccessDeniedException: /opt/keycloak/bin/../data/import/realm.json #137

hohwille opened this issue Apr 25, 2024 · 2 comments · Fixed by #139
Labels
bug Something isn't working
Milestone

Comments

@hohwille
Copy link

Describe the bug

When using withRealmImportFile the realm file is copied into the container and then on startup imported by keycloak.
However, the copying uses default file attributes.
As it turns out this works for local development defaults but fails in our CI with this error:

INFO  [org.keycloak.exportimport.singlefile.SingleFileImportProvider] (main) Full importing from file /opt/keycloak/bin/../data/import/realm.json
ERROR [org.keycloak.quarkus.runtime.cli.ExecutionExceptionHandler] (main) ERROR: Failed to start server in (development) mode
ERROR [org.keycloak.quarkus.runtime.cli.ExecutionExceptionHandler] (main) ERROR: java.nio.file.AccessDeniedException: /opt/keycloak/bin/../data/import/realm.json
ERROR [org.keycloak.quarkus.runtime.cli.ExecutionExceptionHandler] (main) ERROR: /opt/keycloak/bin/../data/import/realm.json

It took me a long time to be able to debug into the container inside the CI server what is really tricky but I figured that the file permissions are wrong:

bash-5.1$ cd /opt/keycloak/data/import/
bash-5.1$ ls -la
total 84
drwxr-xr-x 2 root     root  4096 Apr 25 07:31 .
drwxrwxr-x 1 keycloak root  4096 Apr 25 07:31 ..
-rw---x--T 1 root     root 76050 Apr 22 17:00 realm.json

IMHO there are several problems:

  • keycloak container itself is following some anti-pattern running as a dedicated keycloak user but the default user for copying files in root. A lot of suck problems could be prevented, if keycloak would run as root.
  • IMHO the umask of the OS is applied here and on CI this is more restrictive so there are no read permissions for the keycloak user.

Solution suggestion:
So in your code where you create the MountableFile

withCopyFileToContainer(MountableFile.forClasspathResource(importFile), importFileInContainer);

you should provide the file permissions as 0777 to the MountableFile static factory method (be aware of the leading zero that in Java is used for octal notation causing a lot of confusion as 0777 != 777).

Version

3.3.0

Expected behavior

Keycloak will startup and not raise AccessDeniedException

Actual behavior

Keycloak causes this error and then immediately exists without starting up:

INFO  [org.keycloak.exportimport.singlefile.SingleFileImportProvider] (main) Full importing from file /opt/keycloak/bin/../data/import/realm.json
ERROR [org.keycloak.quarkus.runtime.cli.ExecutionExceptionHandler] (main) ERROR: Failed to start server in (development) mode
ERROR [org.keycloak.quarkus.runtime.cli.ExecutionExceptionHandler] (main) ERROR: java.nio.file.AccessDeniedException: /opt/keycloak/bin/../data/import/realm.json
ERROR [org.keycloak.quarkus.runtime.cli.ExecutionExceptionHandler] (main) ERROR: /opt/keycloak/bin/../data/import/realm.json

How to Reproduce?

Good question. Probably test on a linux OS and change your system default umask to something like 077 (umask 077).

Relevant log output

INFO  [org.keycloak.exportimport.singlefile.SingleFileImportProvider] (main) Full importing from file /opt/keycloak/bin/../data/import/realm.json
ERROR [org.keycloak.quarkus.runtime.cli.ExecutionExceptionHandler] (main) ERROR: Failed to start server in (development) mode
ERROR [org.keycloak.quarkus.runtime.cli.ExecutionExceptionHandler] (main) ERROR: java.nio.file.AccessDeniedException: /opt/keycloak/bin/../data/import/realm.json
ERROR [org.keycloak.quarkus.runtime.cli.ExecutionExceptionHandler] (main) ERROR: /opt/keycloak/bin/../data/import/realm.json

Anything else?

No response

@hohwille hohwille added the bug Something isn't working label Apr 25, 2024
dasniko added a commit that referenced this issue Apr 25, 2024
@dasniko
Copy link
Owner

dasniko commented Apr 25, 2024

@hohwille see #139
Does this solve your issue?

You are always welcome to also submit a PR with your suggested changes!

@dasniko
Copy link
Owner

dasniko commented Apr 25, 2024

But a 644 should also be sufficient. WDYT?

@dasniko dasniko added this to the 3.3.1 milestone Apr 25, 2024
dasniko added a commit that referenced this issue Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants