Skip to main content

Configuring OpenID Connect in JFrog

Use OpenID Connect within your workflows to authenticate with JFrog.

Overview

OpenID Connect (OIDC) allows your GitHub Actions workflows to authenticate with JFrog to download and publish artifacts without storing JFrog passwords, tokens, or API keys in GitHub.

This guide gives an overview of how to configure JFrog to trust GitHub's OIDC as a federated identity, and demonstrates how to use this configuration in a GitHub Actions workflow.

For an example GitHub Actions workflow, see Sample GitHub Actions Integration in the JFrog documentation.

For an example GitHub Actions workflow using the JFrog CLI, see build-publish.yml in the jfrog-github-oidc-example repository.

Prerequisites

  • Pour en savoir plus sur les concepts de base décrivant la façon dont GitHub utilise OIDC (OpenID Connect) ainsi que sur son architecture et ses avantages, consultez « À propos du renforcement de la sécurité avec OpenID Connect ».

  • Avant de continuer, vous devez planifier votre stratégie de sécurité pour veiller à ce que les jetons d’accès soient uniquement alloués de manière prévisible. Pour contrôler la façon dont votre fournisseur de cloud émet des jetons d’accès, vous devez définir au moins une condition, afin que les dépôts non approuvés ne puissent pas demander de jetons d’accès à vos ressources cloud. Pour plus d’informations, consultez « À propos du renforcement de la sécurité avec OpenID Connect ».

  • To be secure, you need to set a Claims JSON in JFrog when configuring identity mappings. For more information, see "AUTOTITLE" and "À propos du renforcement de la sécurité avec OpenID Connect."

    For example, you can set iss to https://token.actions.githubusercontent.com, and the repository to something like "octo-org/octo-repo"`. This will ensure only Actions workflows from the specified repository will have access to your JFrog platform. The following is an example Claims JSON when configuring identity mappings.

    JSON
    {
      "iss": "https://token.actions.githubusercontent.com",
      "repository": "octo-org/octo-repo"
    }
    

Adding the identity provider to JFrog

To use OIDC with JFrog, establish a trust relationship between GitHub Actions and the JFrog platform. For more information about this process, see OpenID Connect Integration in the JFrog documentation.

  1. Sign in to your JFrog Platform.
  2. Configure trust between JFrog and your GitHub Actions workflows.
  3. Configure identity mappings.

Updating your GitHub Actions workflow

Once you establish a trust relationship between GitHub Actions and the JFrog platform, you can update your GitHub Actions workflow file.

In your GitHub Actions workflow file, ensure you are using the provider name and audience you configured in the JFrog Platform.

The following example uses the placeholder YOUR_PROVIDER_NAME.

- name: Fetch Access Token from Artifactory
        id: fetch_access_token
        env:
          ID_TOKEN: $
        run: |
          ACCESS_TOKEN=$(curl \
          -X POST \
          -H "Content-type: application/json" \
          https://example.jfrog.io/access/api/v1/oidc/token \
          -d \
          "{\"grant_type\": \"urn:ietf:params:oauth:grant-type:token-exchange\", \"subject_token_type\":\"urn:ietf:params:oauth:token-type:id_token\", \"subject_token\": \"$ID_TOKEN\", \"provider_name\": \"YOUR_PROVIDER_NAME\"}" | jq .access_token | tr -d '"')
          echo ACCESS_TOKEN=$ACCESS_TOKEN >> $GITHUB_OUTPUT

The following example shows part of a GitHub Actions workflow file using cURL.

- name: Get ID Token (cURL method)
        id: idtoken
        run: |
          ID_TOKEN=$(curl -sLS -H "User-Agent: actions/oidc-client" -H "Authorization: Bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" \
          "${ACTIONS_ID_TOKEN_REQUEST_URL}&audience=jfrog-github" | jq .value | tr -d '"')
          echo "ID_TOKEN=${ID_TOKEN}" >> $GITHUB_OUTPUT

Alternatively, you can set the audience as an environment variable using the env context. For more information about the env context, see "Contextes."

Remarque : lorsque des environnements sont utilisés dans des workflows ou dans des stratégies OIDC, nous vous recommandons d’ajouter des règles de protection à l’environnement pour plus de sécurité. Par exemple, vous pouvez configurer des règles de déploiement sur un environnement pour restreindre les branches et balises pouvant être déployées dans l’environnement ou accéder aux secrets d’environnement. Pour plus d’informations, consultez « Utilisation d’environnements pour le déploiement ».

jobs:
  build:
    runs-on: ubuntu-latest
    env:
      OIDC_AUDIENCE: 'YOUR_AUDIENCE'

Then, in your workflow file, retrieve the value of the variables stored in the env context. The following example uses the env context to retrieve the OIDC audience.

- name: Get ID Token (using env context)
        uses: actions/github-script@v6
        id: idtoken
        with:
          script: |
            const coredemo = require('@actions/core');
            let id_token = await coredemo.getIDToken(process.env.OIDC_AUDIENCE);
            coredemo.setOutput('id_token', id_token);