Nota

Abilitare un workflow delle azioni GitHub per accedere a un cluster Kubernetes utilizzando l'autenticazione OKE OpenID Connect

Introduzione

In questa esercitazione, autorizziamo un workflow GitHub Actions per eseguire un comando Kubernetes in un cluster Oracle Cloud Infrastructure Kubernetes Engine (OCI Kubernetes Engine o OKE). Utilizzando l'autenticazione OIDC (OpenID Connect) nativa di GitHub, puoi evitare di gestire credenziali di lunga durata e semplificare l'accesso sicuro e automatizzato ai tuoi cluster OKE, consentendo flussi di lavoro CI/CD (Continuous Integration and Continuous Deployment) più efficienti.

Obiettivi

Task 1: creare un repository GitHub

Creare un nuovo repository nell'account GitHub. Immettere il nome dell'account o dell'organizzazione e il nome del repository.

Task 2: configurare un cluster OKE

  1. Creare un cluster Kubernetes con OKE o selezionare un cluster esistente per aggiornare e annotare l'OCID (Oracle Cloud Identifier) del cluster. Per ulteriori informazioni, vedere Creazione di cluster Kubernetes mediante i workflow della console.

  2. Creare un file JSON per aggiornare il cluster con le informazioni riportate di seguito.

    • isOpenIdConnectAuthEnabled: immettere true.
    • issuerUrl: immettere "https://token.actions.githubusercontent.com".
    • clientId: immettere il valore. In questo esempio, viene utilizzato "oke-kubernetes-cluster". Questo valore deve corrispondere all'audience nel flusso di lavoro Azioni GitHub.
    • requiredClaim: immettere ["repository=GH_ACCOUNT/REPO", "workflow=NAME", "ref=refs/heads/main"].
    • usernameClaim: immettere "sub".
    • usernamePrefix: immettere "actions-oidc:".

    Sostituire i valori GH_ACCOUNT e REPO con i valori. Il file JSON dovrebbe essere simile al seguente:

    {
        "openIdConnectTokenAuthenticationConfig": {
          "isOpenIdConnectAuthEnabled": true,
          "clientId": "oke-kubernetes-cluster",
          "issuerUrl": "https://token.actions.githubusercontent.com",
          "usernameClaim": "sub",
          "usernamePrefix": "actions-oidc:",
          "requiredClaim": [
            "repository=gregvers/testoidc",
            "workflow=oke-oidc",
            "ref=refs/heads/main"
          ],
          "caCertificate": null,
          "signingAlgorithms": [
            "RS256"
          ]
        }
    }
    
  3. Eseguire il comando CLI (OCI Command Line Interface) per aggiornare il cluster con il file JSON. Sostituire CLUSTER_OCID con i valori.

    oci ce cluster update --cluster-id CLUSTER_OCID --from-json file://./update.json
    

Task 3: configurare il traffico di rete in uscita

Consentire al server API cluster di esportare il traffico in GitHub aggiungendo una regola di sicurezza alla lista di sicurezza della subnet delle regole di sicurezza assegnate all'endpoint API Kubernetes del cluster.

Task 4: configurare il controllo dell'accesso basato sui ruoli Kubernetes (RBAC)

Configurare RBAC nel cluster Kubernetes per autorizzare il flusso di lavoro GitHub Actions a eseguire determinate operazioni (get/watch/list pod e get/watch/list/create/update/delete deployments) utilizzando il seguente file YAML. Aggiornare la riga seguente: "name: actions-oidc:repo:GH-ACCOUNT/REPO:ref:refs/heads/main" con il nome dell'account GitHub e il nome del repository.

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: default
  name: actions-oidc-role
rules:
  - apiGroups: [""]
    resources: ["pods"]
    verbs: ["get", "watch", "list"]
  - apiGroups: ["apps"]
    resources: ["deployments"]
    verbs: ["get", "watch", "list", "create", "update", "delete"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: actions-oidc-binding
  namespace: default
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: actions-oidc-role
subjects:
  - apiGroup: rbac.authorization.k8s.io
    kind: User
    name: actions-oidc:repo:GH-ACCOUNT/REPO:ref:refs/heads/main

Task 5: configurare le azioni GitHub

Creare un workflow GitHub Actions nel repository GitHub.

  1. Fare clic su Azioni e Impostare un workflow personalmente.

  2. Incollare il codice seguente nell'editor e aggiornare server="https://X.X.X.X:6443" con l'IP pubblico del cluster.

    name: OKE-OIDC
    
    on:
      # Triggers the workflow on push or pull request events but only for the "main" branch
      push:
        branches: [ "main" ]
      pull_request:
        branches: [ "main" ]
    
      # Allows you to run this workflow manually from the Actions tab
      workflow_dispatch:
    
    permissions:
      id-token: write # Required to receive OIDC tokens
    
    # This workflow generates a GitHub Actions OIDC token and runs kubectl command in an OKE cluster
    jobs:
      oke-oidc:
        runs-on: ubuntu-latest
        steps:
          - name: Create OIDC Token
            id: create-oidc-token
            run: |
              AUDIENCE="oke-kubernetes-cluster"
              OIDC_URL_WITH_AUDIENCE="$ACTIONS_ID_TOKEN_REQUEST_URL&audience=$AUDIENCE"
              IDTOKEN=$(curl \
                -H "Authorization: Bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" \
                -H "Accept: application/json; api-version=2.0" \
                "$OIDC_URL_WITH_AUDIENCE" | jq -r .value)
              echo "::add-mask::${IDTOKEN}"
              echo "idToken=${IDTOKEN}" >> $GITHUB_OUTPUT
    
          - name: Check Permissions in Kubernetes
            run: |
              kubectl \
              --token=$ \
              --server="https://X.X.X.X:6443" \
              --insecure-skip-tls-verify \
              auth can-i --list
    

    Il flusso di lavoro Azioni Github verrà eseguito automaticamente. Il comando kubectl restituirà le operazioni autorizzate con la configurazione RBAC (get/watch/list pods e distribuzioni get/watch/list/create/update/delete).

    GitHub Esecuzione del workflow delle azioni

    Descrizione dell'immagine github-actions-workflow.png

Conferme

Altre risorse di apprendimento

Esplora altri laboratori su docs.oracle.com/learn o accedi a più contenuti gratuiti sulla formazione su Oracle Learning YouTube channel. Inoltre, visita education.oracle.com/learning-explorer per diventare un Oracle Learning Explorer.

Per la documentazione del prodotto, visita l'Oracle Help Center.