8000 Enhanced Bootstrapping and Recovering Admin Access · Issue #9829 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

Enhanced Bootstrapping and Recovering Admin Access #9829

Closed

Description

@stianst

Bootstrapping a temporary admin account

Bootstrapping supports both an admin user account and admin service account. This proposal is describing bootstrapping that is meant as a replacement for the current KEYCLOAK_ADMIN and KEYCLOAK_ADMIN_PASSWORD env vars. It does not replace the admin creation using Welcome Page.

Temporary admin user and admin service account specifications

  • Meant to be only a temporary account that is used for creating permanent and more secure admin access.
  • By default expires (is deleted) after 2 hours.
    • This is configurable by the user.
  • In case of the user account, clear warning is visible in the Admin Console when logged in as this user.
    • A bar at the top of all Admin Console pages.
  • The user/client is cleary marked as temporary in the lists in Admin Console.
  • A warning is emitted in the log any time such a user logs in.
  • The warnings include a note when the account will be deleted.

In automated scenarios, an admin service account is a more suitable alternative to an admin user.

Bootstrapping an admin user account

Bootstrapping an admin user using a dedicated command

Add a new command: kc.sh|bat bootstrap-admin user [--username=value] [--password=value] [--no-prompt] [--expiration=value].

  • The command works regardless if Keycloak was ever started (and master realm exists), or not.
  • If --username is not specified, the env variable KEYCLOAK_BOOTSTRAP_ADMIN_USER will be checked, if it is not defined, the user will be prompted to enter a username with a default preset to temp-admin.
  • If --password is not specified, the environment variable KEYCLOAK_BOOTSTRAP_ADMIN_PASSWORD will be checked, if it is not defined, the user will be prompted for a password.
  • If --no-prompt is specified, then prompting will not be used. If a value would have required prompting, an error will be shown.
  • If the account with the specified username exists an error will be shown.
  • --expiration specifies the number of minutes after which the account expires. If --expiration is not specified, the environment variable KEYCLOAK_BOOTSTRAP_ADMIN_EXPIRATION will be checked. Otherwise, the default 120 (2 hours) will be used.

Bootstrapping an admin user at server startup

  • Usable only if Keycloak was not started before, i.e. the master realm doesn't not exist.
  • When the server is starting (kc.sh|bat start|start-dev) and the condition specified above is met, it will check the KEYCLOAK_BOOTSTRAP_ADMIN_USER, KEYCLOAK_BOOTSTRAP_ADMIN_PASSWORD and KEYCLOAK_BOOTSTRAP_ADMIN_EXPIRATION env vars. Their meaning and functionality is identical to as when used as the dedicated command.
  • --no-prompt is implicit, i.e. if KEYCLOAK_BOOTSTRAP_ADMIN_USER is set but KEYCLOAK_BOOTSTRAP_ADMIN_PASSWORD is not, an error will be shown.

Bootstrapping an admin service account

  • Identical to bootstraping the admin user as described above with the following exceptions:
    • Instead of "user", "client-id" is used in param and env var names.
    • Instead of "password", "secret" is used in param and env var names.
  • Dedicated command: kc.sh|bat bootstrap-admin service [--client-id=value] [--secret=value] [--no-prompt] [--expiration].
  • Bootstrapping at server startup is supported too using the KEYCLOAK_BOOTSTRAP_ADMIN_CLIENT_ID and KEYCLOAK_BOOTSTRAP_ADMIN_CLIENT_SECRET env vars.

Based on the provided arguments, a Client is created in the master Realm with the following specificationss:

  • It has all flows disabled.
  • The Client requires authentication.
  • Service account is enabled.
  • The service account has the admin Realm Role assigned.

Bootstrapping using the Operator

There will be a dedicated field in the Keycloak CR for bootstrapping the admin user account and admin service account. The implementation will leverage the startup env vars, so it will be effective only for the initial bootstrapping of Keycloak.

kind: Keycloak
spec:
  bootstrap-admin:
    expiration: 260
    user:
      nameSecret: 
        name: keycloak-admin-secret
        key: username
      passwordSecret:
        name: keycloak-admin-secret
        key: password
    service:
      clientIdSecret: 
        name: keycloak-admin-secret
        key: client-id
      clientSecret:
        name: keycloak-admin-secret
        key: secret

  • The content of Secrets referenced by nameSecret and passwordSecret can be provided by the user, or the Operator will populate them with defaults.
  • If the Secret or the Key referenced by nameSecret don’t exist, the default as described above is used and is written back to the Secret.
  • If the Secret or the Key referenced by passwordSecret don’t exist, a password is generated and is written back to the Secret.
  • The content of Secrets referenced by clientIdSecret and clientSecret must be provided by the user.

Recovering the admin access

It is not possible to recover an admin account without already having admin access. It is possible though to create a new admin account using the dedicated command. In case of Operator/container deployments, we will document ssh-ing to the container is necessary.

Realms with increased security (OTP requirement, passwordles, ...) will have to use the admin service account to recover access to the master realm.


Originally discussed in #8549

### Tasks
- [ ] https://github.com/keycloak/keycloak/issues/30003
- [ ] https://github.com/keycloak/keycloak/issues/29324
- [ ] https://github.com/keycloak/keycloak/issues/30002
- [ ] https://github.com/keycloak/keycloak/issues/30004
- [ ] https://github.com/keycloak/keycloak/issues/30009
- [ ] https://github.com/keycloak/keycloak/issues/30011
- [ ] https://github.com/keycloak/keycloak/issues/29325
- [ ] https://github.com/keycloak/keycloak/issues/29355
- [ ] https://github.com/keycloak/keycloak/issues/30540
- [ ] https://github.com/keycloak/keycloak/issues/30658
- [ ] https://github.com/keycloak/keycloak/issues/31160
- [ ] https://github.com/keycloak/keycloak/issues/31491
### Follow-up tasks
- [ ] https://github.com/keycloak/keycloak/issues/30008
- [ ] https://github.com/keycloak/keycloak/issues/30010

Activity

  1. added this to the milestone on Feb 4, 2022
  2. modified the milestones: , 17.1.0 on Mar 8, 2022
  3. modified the milestones: 18.0.0, 19.0.0 on Mar 30, 2022
  4. modified the milestones: 19.0.0, 20.0.0 on Jul 26, 2022
  5. removed this from the 20.0.0 milestone on Oct 13, 2022
  6. 21 remaining items

  7. vmuzikar commented on Aug 21, 2024

    @vmuzikar
    Contributor

    Resolving this as all tasks are done now and only follow-ups remain.

  8. Barathwaja commented on Apr 25, 2025

    @Barathwaja
    Contributor

    Hi @vmuzikar , While testing this scenario, I noticed that the KC_BOOTSTRAP_ADMIN_USERNAME is being created in the default master realm instead of my custom realm. I don't see any KEYCLOAK_BOOTSTRAP_ADMIN_REALM or KC_BOOTSTRAP_ADMIN_REALM options currently implemented—please correct me if I'm mistaken.

  9. shawkins commented on Apr 27, 2025

    @shawkins
    Contributor

    @Barathwaja the bootstrap admin will be created in whatever is designated as the master realm, you may then use that account to create other realm level admins.

  10. Barathwaja commented on Apr 27, 2025

    @Barathwaja
    Contributor

    @shawkins - Correct — but in my scenario, I already have a custom realm with non-admin users and only one main admin user. If I accidentally delete that main admin, I should still be able to create a temporary user and recover/recreate admin user with privileges to it under my custom realm.

  11. atul-agg commented on Apr 29, 2025

    @atul-agg

    @pedroigor @vmuzikar @stianst is **KEYCLOAK_BOOTSTRAP_ADMIN_EXPIRATION ** still available? I don't see it here https://www.keycloak.org/server/all-config

    How to delete temporary admin automatically after bootstrapping.
    I am creating a permanent user at bootstrap. I don't want temporary user at all. Is there any way to avoid adding it or delete it later if adding it is compulsory.

  12. vmuzikar commented on Apr 29, 2025

    @vmuzikar
    Contributor

    is **KEYCLOAK_BOOTSTRAP_ADMIN_EXPIRATION ** still available?

    No, it was never really implemented.

    How to delete temporary admin automatically after bootstrapping.

    This is not possible at the moment. There's a tracker for it though it's unsure when or whether at all will get this implemented at this point.

  13. atul-agg commented on May 2, 2025

    @atul-agg

    @vmuzikar is it possible to bootstrap without temporary admin? I am including a permanent admin in master realm during startup using config.json file in /keycloak/data/import/ folder.

    Keycloak fails to start if I do not include KC_BOOTSTRAP_ADMIN_USERNAME and KC_BOOTSTRAP_ADMIN_PASSWORD in my docker-compose.yml file. Is there any solution for it?

  14. shawkins commented on May 2, 2025

    @shawkins
    Contributor

    @atul-agg the bootstrap properties are not required. Feel free to create a new issue or disscussion if you are having a problem.

  15. Barathwaja commented on May 2, 2025

    @Barathwaja
    Contributor

    @shawkins - Correct — but in my scenario, I already have a custom realm with non-admin users and only one main admin user. If I accidentally delete that main admin, I should still be able to create a temporary user and recover/recreate admin user with privileges to it under my custom realm.

    @shawkins - Just following up—I'd appreciate it if you could review and let me know if there's a way to create temporary users under my custom realm, which is already set as the default.

  16. shawkins commented on May 2, 2025

    @shawkins
    Contributor

    I'd appreciate it if you could review and let me know if there's a way to create temporary users under my custom realm, which is already set as the default.

    No, the original response reflects how the bootstrap admin feature functions. If there are additional questions, please start a new discussion.

  17. locked as resolved and limited conversation to collaborators on May 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    0