Tutoriel : Mettre en place un fournisseur OIDC pour l’authentification

OKS prend en charge l’authentification OpenID Connect (OIDC) des utilisateurs via kubectl. Vous pouvez configurer votre cluster OKS pour activer le protocole d’authentification OIDC.

Cette stratégie d’authentification s’ajoute à l’authentification par certificat utilisée par défaut pour les clusters OKS. L’accès par certificat est utilisé lors des premières étapes pour ajouter des autorisations de contrôle d’accès basé sur les rôles (RBAC) aux utilisateurs connectés via OIDC.

Cette fonctionnalité est disponible à partir de la version 1.20 d’oks-cli.

Préparer votre fournisseur OIDC

Avant de commencer :

Dans cet exemple, nous allons utiliser GitLab en tant que fournisseur d’identité OIDC.

  1. Sur GitLab, accédez à vos applications : https://gitlab.<DOMAIN.NAME>/-/user_settings/applications.

  2. Cliquez sur Ajouter une nouvelle application pour ajouter un fournisseur d’authentification.
    Une section de configuration s’affiche.

  3. Dans le champ Name, saisissez un nom pour votre application.

  4. Dans le champ Redirect URI, saisissez http://localhost:8000, et désactivez l’option Confidentiel.

  5. Dans la liste Scopes, sélectionnez openid, profile et email, puis cliquez sur Enregistrer l’application.

Votre nouvelle application est créée, et GitLab affiche son identifiant et son secret.

Configurer OIDC pour votre cluster

Pour activer la prise en charge d’OIDC et afficher les noms d’utilisateur sous forme d’adresses e-mail plutôt que d’identifiants, vous devez configurer les propriétés de votre cluster à l’aide du paramètre set.

Créer un cluster

Pour créer un nouveau cluster, utilisez la commande cluster create en suivant cette syntaxe :

Exemple de requête
$ oks-cli cluster create \
  --cluster-name <NAME_OF_CLUSTER> \
  --set auth.oidc.client-id=<APPLICATION_ID> \
  --set auth.oidc.issuer-url=https://gitlab.<DOMAIN.NAME> \
  --set auth.oidc.username-claim=email

Cette commande contient les options suivantes que vous devez spécifier :

  • cluster-name : Le nom du cluster, d’une longueur maximale de 40 caractères alphanumériques et tirets (-). Ce nom doit commencer par une lettre minuscule et ne doit pas finir par un tiret. Il doit être unique dans le projet.

  • set : Permet de définir des propriétés.

    • auth.oidc.client-id : L’identifiant client fourni par le fournisseur OIDC. Il sera utilisé par le cluster pour se connecter au serveur.

    • auth.oidc.issuer-url : L’URL du serveur du fournisseur OIDC, utilisant le protocole HTTPS (https://).

    • auth.oidc.username-claim : Un JSON Web Token (JWT) fourni par le fournisseur OIDC. Par défaut, email. Il sera utilisé pour récupérer l’adresse e-mail de connexion côté Kubernetes.

Le cluster prend désormais en charge la connexion via OIDC à l’adresse https://gitlab.<DOMAIN.NAME>, en plus des connexions par certificat.

Mettre à jour un cluster

Pour mettre à jour un cluster existant, utilisez la commande cluster update en suivant cette syntaxe :

Exemple de requête
$ oks-cli cluster create \
  --cluster-name <NAME_OF_CLUSTER> \
  --set auth.oidc.client-id=<APPLICATION_ID> \
  --set auth.oidc.issuer-url=https://gitlab.<DOMAIN.NAME> \
  --set auth.oidc.username-claim=email

Cette commande contient les options suivantes que vous devez spécifier :

  • cluster-name : Le nom du cluster, d’une longueur maximale de 40 caractères alphanumériques et tirets (-). Ce nom doit commencer par une lettre minuscule et ne doit pas finir par un tiret. Il doit être unique dans le projet.

  • set : Permet de définir des propriétés.

    • auth.oidc.client-id : L’identifiant client fourni par le fournisseur OIDC. Il sera utilisé par le cluster pour se connecter au serveur.

    • auth.oidc.issuer-url : L’URL du serveur du fournisseur OIDC, utilisant le protocole HTTPS (https://).

    • auth.oidc.username-claim : Un JSON Web Token (JWT) fourni par le fournisseur OIDC. Par défaut, email. Il sera utilisé pour récupérer l’adresse e-mail de connexion côté Kubernetes.

Le cluster prend désormais en charge la connexion via OIDC à l’adresse https://gitlab.<DOMAIN.NAME>, en plus des connexions par certificat.

Configuring User-Based Authorizations

À ce jour, cette section est disponible en anglais uniquement.

Setting a kubeconfig Environment Variable

For the next commands, set a environment variable to print the kubeconfig file.

To set the KUBECONFIG variable, run the following command:

Request sample
$ export KUBECONFIG=`oks-cli cluster kubeconfig \
  --cluster-name <NAME_OF_CLUSTER> \
  --print-path \
  --refresh`

The KUBECONFIG environment variable is set to be kept up-to-date.

Defining Access Roles

To grant rights to users using the OIDC provider, you must define role-based authorizations.

You can check your current user on the OKS side using the kubectl whoami command:

Request sample
$ kubectl --kubeconfig $KUBECONFIG auth whoami
Response sample
ATTRIBUTE                                           VALUE
Username                                            cluster-admin
Groups                                              [system:authenticated]
Extra: authentication.kubernetes.io/credential-id   [X509SHA256=6c6ba4b6bd76414bd84ccb02c394478aeb68c465b5ac58febb78d8941df5991f]

To give the cluster-admin role to an email address used to log in to GitLab, use the kubectl create clusterrolebinding command:

Rolebinding sample
$ kubectl --kubeconfig $KUBECONFIG create clusterrolebinding oidc-cluster-admin \
  --clusterrole=cluster-admin \
  --user='<EMAIL-ADDRESS>'

This command contains the following options that you may need to specify:

  • kubeconfig: The path to the kubeconfig file to use for CLI requests.

  • clusterrole: The ClusterRole that this ClusterRoleBinding should reference.

  • user: The usernames to bind to the ClusterRole. This option can be repeated to add multiple users.

    You can use an username prefix such as oidc: in front of the email address. This can be modified through the cluster or through the kubeconfig file.

Enabling OIDC Authentication for kubectl

To enable OIDC Authentication for kubectl, use kubectl config set-credentials with the following syntax:

Request sample
$ kubectl config set-credentials oidc \
  --exec-api-version=client.authentication.k8s.io/v1 \
  --exec-interactive-mode=Never \
  --exec-command=kubectl \
  --exec-arg=oidc-login \
  --exec-arg=get-token \
  --exec-arg="--oidc-issuer-url=https://gitlab.<DOMAIN.NAME> " \
  --exec-arg="--oidc-client-id=<APPLICATION_ID>" \
  --exec-arg="--oidc-extra-scope=email" --kubeconfig=$KUBECONFIG

The following elements are added to the users section of the kubeconfig file.

kubeconfig file sample
...
users:
- name: cluster-admin
  user:
    client-certificate-data: CERT_ITSELF=
    client-key-data: CERT_KEY=
- name: oidc
  user:
    exec:
      apiVersion: client.authentication.k8s.io/v1
      args:
      - oidc-login
      - get-token
      - --oidc-issuer-url=https://gitlab.<DOMAIN.NAME>
      - --oidc-client-id=<APPLICATION_ID>
      - --oidc-extra-scope=email
      command: kubectl
      env: null
      interactiveMode: Never
      provideClusterInfo: false
...

Enforcing OIDC Identification When Logging In to the Cluster

Set the context user to oidc or to cluster-admin to enforce OIDC identification when you log in to the cluster by using the kubectl config set-context command:

Request sample
$ kubectl --kubeconfig $KUBECONFIG config set-context \
  --current \
  --user=oidc

This command contains the following options that you may need to specify:

  • kubeconfig: The path to the kubeconfig file to use for CLI requests.

  • current: Allows you to modify the current context.

  • user: The user for the context entry in kubeconfig.

You can test the updated configuration by using the kubectl get secrets command:

Request sample
$ kubectl get secrets -A

Your browser will open on the login page of your OIDC provider. Once logged in, you will be able to access the list of your cluster’s secrets.

Sharing Access to Your Cluster

To share access to your cluster, you can share the current kubeconfig file after removing the users from it.

To delete users, use kubectl config delete-user with the following syntax:

Request sample
$ kubectl --kubeconfig $KUBECONFIG config delete-user cluster-admin
Result sample
deleted user cluster-admin from $KUBECONFIG

With shared access, every user must authenticate through the OIDC provider and will have access rights defined by RBAC.

Request Sample: Printing the Shareable kubeconfig File
$ cat $KUBECONFIG
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: CLUSTER_CERT==
    server: https://test-133.123123123.project.eu-west-2.oks.outscale.com:6443
  name: test-133
contexts:
- context:
    cluster: test-133
    user: oidc
  name: oidc@test-133
current-context: oidc@test-133
kind: Config
preferences: {}
users:
- name: oidc
  user:
    exec:
      apiVersion: client.authentication.k8s.io/v1
      args:
      - oidc-login
      - get-token
      - --oidc-issuer-url=https://gitlab.mydomain.net
      - --oidc-client-id=123123123123123123123123123123123123123123123123
      - --oidc-extra-scope=email
      command: kubectl
      env: null
      interactiveMode: Never
      provideClusterInfo: false

Disabling OIDC Authentication

To disable OIDC authentication for your cluster, use cluster update with the following syntax:

Request sample
$ oks-cli cluster update \
  --cluster-name <NAME_OF_CLUSTER> \
  --set auth={}

Pages connexes

Méthodes API correspondantes