2019-12-03 10:22:58 +00:00
2020-05-09 01:16:38 +00:00
[[_user-cr]]
=== Creating a user custom resource
2019-12-03 10:22:58 +00:00
2020-05-09 01:16:38 +00:00
You can use the Operator to create users in {project_name} as defined by a custom resource. You define the properties of the user custom resource in a YAML file.
2019-12-03 10:22:58 +00:00
2020-05-09 01:16:38 +00:00
[NOTE]
====
You can update properties, except for the password, in the YAML file and changes appear in the {project_name} admin console, however changes to the admin console do not update the custom resource.
====
.Example YAML file for a user custom resource
2019-12-03 10:22:58 +00:00
```yaml
apiVersion: keycloak.org/v1alpha1
kind: KeycloakUser
metadata:
2020-05-09 01:16:38 +00:00
name: example-user
2019-12-03 10:22:58 +00:00
spec:
user:
username: "realm_user"
firstName: "John"
lastName: "Doe"
email: "user@example.com"
enabled: True
emailVerified: False
realmRoles:
- "offline_access"
clientRoles:
account:
- "manage-account"
realm-management:
- "manage-users"
realmSelector:
matchLabels:
2020-06-08 21:53:30 +00:00
ifeval::[{project_community}==true]
app: example-keycloak
endif::[]
ifeval::[{project_product}==true]
2019-12-03 10:22:58 +00:00
app: sso
2020-06-08 21:53:30 +00:00
endif::[]
2019-12-03 10:22:58 +00:00
```
2020-05-09 01:16:38 +00:00
.Prerequisites
* You have a YAML file for this custom resource.
* The `realmSelector` matches the labels of an existing realm custom resource.
* You have cluster-admin permission or an equivalent level of permissions granted by an administrator.
.Procedure
. Use this command on the YAML file that you created: `{create_cmd} -f <user_cr>.yaml`. For example:
+
[source,bash,subs=+attributes]
----
$ {create_cmd} -f initial_user.yaml
keycloak.keycloak.org/example-user created
----
. Log into the admin console for the related instance of {project_name}.
. Click Users.
2019-12-03 10:22:58 +00:00
2020-05-09 01:16:38 +00:00
. Search for the user that you defined in the YAML file.
+
2021-07-26 21:37:18 +00:00
You may need to switch to a different realm to find the user.
2020-05-09 01:16:38 +00:00
+
2021-07-26 21:37:18 +00:00
image:images/realm_user.png[]
2019-12-03 10:22:58 +00:00
2020-05-09 01:16:38 +00:00
.Results
2019-12-03 10:22:58 +00:00
2020-05-09 01:16:38 +00:00
After a user is created, the Operator creates a Secret containing the both username and password using the
following naming pattern: `credential-<realm name>-<username>-<namespace>`. Here's an example:
.`KeycloakUser` Secret
```yaml
kind: Secret
apiVersion: v1
data:
password: <base64 encoded password>
username: <base64 encoded username>
type: Opaque
```
Once the Operator processes the custom resource, view the status with this command:
[source,bash,subs=+attributes]
----
2020-06-08 21:53:30 +00:00
$ {create_cmd_brief} describe keycloak <CR-name>
2020-05-09 01:16:38 +00:00
----
.User custom resource Status
2019-12-03 10:22:58 +00:00
```yaml
Name: example-realm-user
Namespace: keycloak
2020-06-08 21:53:30 +00:00
ifeval::[{project_community}==true]
Labels: app=example-keycloak
endif::[]
ifeval::[{project_product}==true]
2019-12-03 10:22:58 +00:00
Labels: app=sso
2020-06-08 21:53:30 +00:00
endif::[]
2019-12-03 10:22:58 +00:00
API Version: keycloak.org/v1alpha1
Kind: KeycloakUser
Spec:
Realm Selector:
Match Labels:
2020-06-08 21:53:30 +00:00
ifeval::[{project_community}==true]
App: example-keycloak
endif::[]
ifeval::[{project_product}==true]
App: sso
endif::[]
2019-12-03 10:22:58 +00:00
User:
Email: realm_user@redhat.com
Credentials:
Type: password
Value: <user password>
Email Verified: false
Enabled: true
First Name: John
Last Name: Doe
Username: realm_user
Status:
Message:
Phase: reconciled
Events: <none>
```
2020-05-09 01:16:38 +00:00
.Additional resources
2019-12-03 10:22:58 +00:00
2020-05-09 01:16:38 +00:00
* If you have an external database, you can modify the Keycloak custom resource to support it. See xref:_external_database[Connecting to an external database].
* To back up your database using custom resources, see xref:_backup-cr[schedule database backups].