wiki/HowtoKeycloak.md

260 lines
7.8 KiB
Markdown
Raw Normal View History

2023-06-21 17:49:14 +02:00
2023-06-23 15:54:45 +02:00
---
categories: security
title: HowtoKeycloak
...
2023-06-21 17:49:14 +02:00
2023-12-22 12:13:37 +01:00
[Keycloak](https://www.keycloak.org/) est un serveur d'authentification et d'autorisation écrit en Java, c'est un "Identity Access Manager" *IAM* open-source permettant de faire du Single-Sign-On et Single-Sign-Out *SS0*, c'est à dire d'utiliser un seul compte pour se connecter à différents services.
2023-06-21 17:49:14 +02:00
2023-06-23 15:54:45 +02:00
## Installation
2023-06-21 17:49:14 +02:00
2023-06-23 15:54:45 +02:00
liens : [documentation officielle](https://www.keycloak.org/getting-started/getting-started-zip), [git de keycloak](https://github.com/keycloak/keycloak/releases)
2023-06-21 17:49:14 +02:00
2023-12-22 12:13:37 +01:00
Installer Keycloak sur son hote avec OpenJDK (openjdk-17 pour Keycloak 23)
2023-06-21 17:49:14 +02:00
2023-06-23 15:54:45 +02:00
```{.bash}
2023-12-22 12:13:37 +01:00
# apt install openjdk-17-jre
2023-06-22 16:06:45 +02:00
```
2023-06-21 17:49:14 +02:00
Lister les releases pour en choisir une
2023-06-21 17:49:14 +02:00
2023-06-23 15:54:45 +02:00
```{.bash}
$ git ls-remote --tags https://github.com/keycloak/keycloak
2023-12-22 12:13:37 +01:00
...
99774b3f7a776476e7f04e472e13e3915971a6fb refs/tags/23.x.x
2023-06-22 16:06:45 +02:00
```
2023-06-21 17:49:14 +02:00
2023-12-22 12:13:37 +01:00
Récupérer et extraire le zip de la release souhaitée
2023-06-21 17:49:14 +02:00
2023-06-23 15:54:45 +02:00
```{.bash}
2023-12-22 12:13:37 +01:00
$ keycloak_release=23.0.3
2023-06-23 15:54:45 +02:00
$ keycloak_zip=keycloak-${keycloak_release}.zip
$ wget https://github.com/keycloak/keycloak/releases/download/${keycloak_release}/keycloak-${keycloak_release}.zip
$ unzip $keycloak_zip
$ keycloak_folder=${keycloak_zip%".zip"}
2023-06-22 16:06:45 +02:00
```
2023-06-21 17:49:14 +02:00
On peut alors lancer Keycloak en mode *dev* pour tester ou développer des thèmes keycloak en configurant son admin:
2023-06-21 17:49:14 +02:00
2023-12-22 12:13:37 +01:00
* *Au lancement du serveur*
2023-06-22 15:54:26 +02:00
2023-06-23 15:54:45 +02:00
```{.bash}
export KEYCLOAK_ADMIN=<username>
export KEYCLOAK_ADMIN_PASSWORD=<password>
bin/kc.sh start-dev
```
2023-06-21 17:49:14 +02:00
2023-12-22 12:13:37 +01:00
* *Ou en mode "click"*
2023-06-21 17:49:14 +02:00
2023-06-23 15:54:45 +02:00
Se connecter à http://localhost:8080/ et créer un username et password admin
2023-06-21 17:49:14 +02:00
2023-06-23 15:54:45 +02:00
Se connecter à http://localhost:8080/admin avec les identifiant admin
2023-06-22 15:54:26 +02:00
Pour Lancer en production, il va falloir configurer d'autres elements, voirs [configuration](#configuration)
### Unitée Systemd
On peut wrapper keycloak dans une unitée systemd, adaptez `ExecStart`
```{.ini}
# vim /etc/systemd/system/keycloak.service
[Unit]
Description=Keycloak server
After=network-online.target
Wants=network-online.target systemd-networkd-wait-online.service
[Service]
ExecStart=/root/keycloak-23.0.3/bin/kc.sh start
# Disable timeout logic and wait until process is stopped
TimeoutStopSec=0
# SIGTERM signal is used to stop the Java process
KillSignal=SIGTERM
# Send the signal only to the JVM rather than its control group
KillMode=process
# Java process is never killed
SendSIGKILL=no
# When a JVM receives a SIGTERM signal it exits with code 143
SuccessExitStatus=143
[Install]
WantedBy=multi-user.target
```
2023-06-22 15:54:26 +02:00
2023-12-22 12:13:37 +01:00
## Configuration pour de la production
2023-06-22 15:54:26 +02:00
Configuration d'une instance/server de prodution...
On peut spécifier de la configuration via des envs, arguments, fichier de configuration et KeyStore java. Celle-ci sera prise en comte dans cette ordre precedence (des env pourrons surcharger la conf en dur par exemple)
On peut voir la configuration qui sera chargé en jouant `$keycloak_folder/bin/kc.sh show-config`
Ici pour simplifier, on configurera via l'instance via son fichier de configuration dans `$keycloak_folder/conf/keycloak.conf`
>*Remarque* : la configuration par envs et arguments est persisté par Quarkus
* La documentation complète de la configuration d'une instance en production : <https://www.keycloak.org/server/configuration>
* Liste exhaustive des options de configuration : <https://www.keycloak.org/server/all-config>
2023-06-21 17:49:14 +02:00
2023-12-22 12:13:37 +01:00
### Base de données
2023-06-21 17:49:14 +02:00
Keycloak en production a besoin d'un accès à une base de donnée
2023-06-21 17:49:14 +02:00
2023-12-22 12:13:37 +01:00
lien : [documentation keycloak configuration bdd](https://www.keycloak.org/server/db)
2023-06-21 17:49:14 +02:00
Il faut créer une base de donnée `keycloak` et utilisateur associé pour Keycloak
> Si on utilise un autre nom de base, il faut alors specifier `db-schema` dans la configuration
2023-06-21 17:49:14 +02:00
2023-12-22 12:13:37 +01:00
* [HowtoPostgreSQL](/HowtoPostgreSQL.md#créer-un-utilisateur-et-une-base-de-données)
2023-06-21 17:49:14 +02:00
2023-12-22 12:13:37 +01:00
* [HowtoMySQL](/HowtoMySQL.md#créer-une-base-de-données-et-un-utilisateur-associé)
2023-06-21 17:49:14 +02:00
* Configuration via fichier configuration `keycloak.conf`
```{.ini}
# The database vendor.
db=postgres
# The username of the database user.
db-username=keycloak
# The password of the database user.
db-password=CHANGEZMOI
# The hostname of the default JDBC URL of the chosen vendor.
db-url-host=127.0.0.1
```
2023-06-21 17:49:14 +02:00
On peut spécifier en plus si besoin `db-url-port` pour le port ou même `db-url` pour spécifier entièrement l'URL [jdbc](/Glossaire.md#odbcjdbc)
2023-12-22 12:13:37 +01:00
* Configuration via arguments au démarrage
2023-06-21 17:49:14 +02:00
2023-06-23 15:54:45 +02:00
```{.bash}
bin/kc.sh start --db mariadb --db-url-host mymariadb --db-username evocloak --db-password change_me
2023-06-22 16:06:45 +02:00
```
2023-06-21 17:49:14 +02:00
### Hostname
<https://www.keycloak.org/server/hostname>
En production, un utilisateur doit se connecter en utilisant le hostname à préciser dans la configuration :
```{.ini}
# Hostname for the Keycloak server.
hostname=test.example.org
```
### SSL/TLS
<https://www.keycloak.org/server/enabletls>
On ne doit jamais exposer une instance de production en HTTP. Nous allons configurer celle-ci en HTTPS/TLS
Pour cela, nous avons besoin d'un certificat et de sa clef privé pour le hostname choisi... voir [HowtoSSL](/HowtoSSL.md#générer-un-certificat-auto-signé) ou [HowtoLetsEncrypt](/HowtoLetsEncrypt.md#génération-du-certificat) par exemple...
```{.ini}
# The file path to a server certificate or certificate chain in PEM format.
https-certificate-file=/root/keycloak_certificat.crt
# The file path to a private key in PEM format.
https-certificate-key-file=/root/keycloak_private.key
```
### Exemple de configuration
Exemple de configuration avec
WIP
## Configuration avancée
### Log et monitoring
* Par défaut les logs ne sont pas persisté dans un fichier, on peut l'activer et indiquer un fichier (par défaut dans data/log/keycloak.log dans le dossier de keycloak)
```
log=console,file
log-level=debug
log-file=/var/log/keycloak.log
```
2023-06-23 15:54:45 +02:00
### Haute disponibilité
2023-06-21 17:49:14 +02:00
WIP
keycloak est conçu pour fonctionner en haute disponibilité avec [infinispan](https://infinispan.org/) (In-Memory Distributed Data Store)
2023-06-21 17:49:14 +02:00
2023-06-23 15:54:45 +02:00
TODO https://www.keycloak.org/server/configuration-production -> https://www.keycloak.org/server/caching
2023-06-21 17:49:14 +02:00
2023-06-23 15:54:45 +02:00
## Administration
2023-06-21 17:49:14 +02:00
2023-06-23 15:54:45 +02:00
Lancer Keycloak en mode *dev*, pour tester Keycloak en http avec des valeurs par defaut ou en mode *prod*
2023-06-21 17:49:14 +02:00
2023-06-23 15:54:45 +02:00
```{.bash}
$ cd $keycloak_folder
$ bin/kc.sh start-dev
$ bin/kc.sh start
2023-06-22 16:06:45 +02:00
```
2023-06-21 17:49:14 +02:00
2023-06-23 15:54:45 +02:00
Voir les option de démarrage / aide
2023-06-21 17:49:14 +02:00
2023-06-23 15:54:45 +02:00
```{.bash}
bin/kc.sh start-dev --help
bin/kc.sh start --help
```
2023-06-21 17:49:14 +02:00
2023-06-23 15:54:45 +02:00
**Lancer Keycloak** en *dev* sur le port 8080
2023-06-21 17:49:14 +02:00
2023-06-23 15:54:45 +02:00
```{.bash}
keycloak_http_port=8080
bin/kc.sh start-dev --http-port ${keycloak_http_port}
```
2023-06-21 17:49:14 +02:00
2023-06-23 15:54:45 +02:00
### Gestion des royaumes "realm"
2023-06-21 17:49:14 +02:00
2023-06-22 16:06:45 +02:00
Un royaume est un ensemble isolé d'utilisateurs et d'applications géré par un administrateur
2023-06-21 17:49:14 +02:00
2023-06-22 16:06:45 +02:00
Le royaume **master** est seulement utilisé pour gérer Keycloak et ne doit pas être utiliser pour gérer d'autres applications
2023-06-21 17:49:14 +02:00
2023-06-22 16:06:45 +02:00
**Tester le fonctionnement des royaume** voir [getting-started-zip](https://www.keycloak.org/getting-started/getting-started-zip)
2023-06-21 17:49:14 +02:00
2023-06-22 16:06:45 +02:00
suivre bettement les étapes juqu'a arriver au message "hello, myuser" sur [https://www.keycloak.org/app/](https://www.keycloak.org/app/)
2023-06-21 17:49:14 +02:00
2023-06-22 16:06:45 +02:00
**Créer un royaume**
2023-06-21 17:49:14 +02:00
2023-06-22 16:06:45 +02:00
Dans le menu déroulant tout en haut à gauche choisir "Create Realm"
2023-06-21 17:49:14 +02:00
2023-06-22 16:06:45 +02:00
Choisir un nom de royaume "Realm name" puis le créer avec "Create"
2023-06-21 17:49:14 +02:00
2023-06-22 16:06:45 +02:00
**Créer un utilisateur**
2023-06-21 17:49:14 +02:00
2023-06-22 16:06:45 +02:00
ATTENTION : nous allons créer un utilisateur dans le royaume sélectionné dans le menu déroulant tout en haut à gauche, sauf cas particulier, il faudra éviter le royaume **master**
2023-06-21 17:49:14 +02:00
2023-06-22 16:06:45 +02:00
**Créer client**
2023-06-22 15:54:26 +02:00
2023-06-22 16:06:45 +02:00
TODO ajouter nextcloud https://stackoverflow.com/questions/48400812/sso-with-saml-keycloak-and-nextcloud
2023-06-21 17:49:14 +02:00
2023-06-22 16:06:45 +02:00
**User Federation**
2023-06-23 15:54:45 +02:00
permet d'avoir acces à LDAP et Active Directory
2023-06-22 16:06:45 +02:00
TODO Connection au à OpenLDAP https://rob-ferguson.me/keycloak-flowable-and-openldap/
2023-06-22 15:54:26 +02:00
2023-06-22 16:06:45 +02:00
TODO **Identity providers**
2023-06-21 17:49:14 +02:00
## TODO Gestion via la cli
2023-06-22 16:06:45 +02:00
:logbook:
2023-06-23 15:54:45 +02:00
CLOCK: [2023-06-21 Wed 14:43:19]--[2023-06-21 Wed 15:05:04] => 00:21:45
2023-06-22 16:06:45 +02:00
:END:
2023-06-21 17:49:14 +02:00
2023-06-23 15:54:45 +02:00
[documentation keycloak](https://www.keycloak.org/docs/latest/server_admin/#admin-cli)
2023-06-21 17:49:14 +02:00
# Ressources
2023-06-22 16:06:45 +02:00
https://github.com/thomasdarimont/awesome-keycloak
2023-06-22 15:54:26 +02:00
2023-06-22 16:06:45 +02:00
https://rob-ferguson.me/
2023-06-21 17:49:14 +02:00