配置 GitHub 操作


Red Hat Trusted Application Pipeline 1.4

了解如何为安全 CI/CD 工作流配置 GitHub 操作。

Red Hat Trusted Application Pipeline Documentation Team

摘要

本文档提供了有关设置 GitHub Actions 以执行基本安全任务的说明,如漏洞扫描、镜像签名和测试生成。

前言

如果您在应用程序中使用 GitHub Actions,则管道运行可能会因为缺少 secret 而失败。如果没有它们,与 Quay、JFrog 和 Red Hat Advanced Cluster Security (ACS)的集成将无法正常工作,破坏了漏洞扫描、镜像签名和 SBOM 生成合规性等安全任务。

要防止这种情况,您需要在 GitHub Actions 中安全地存储 secret。本指南指导您完成这个过程,确保管道平稳运行。

先决条件

在配置 GitHub Actions 前,请确定您有以下内容:

  • 对 GitHub 存储库和 CI/CD 设置的管理员访问权限。
  • 用于从 Quay.io、JFrog Artifactory 或 Sonatype Nexus 中拉取容器镜像的 容器注册表凭证
  • 特定 GitHub Actions 任务的 身份验证详情

    • 对于 ACS 安全任务

      • ROX Central 服务器端点
      • ROX API 令牌
    • 对于 SBOM 和工件签名任务

      • Cosign 签名密钥密码
      • 私钥和公钥
      • Trustification URL
      • 客户端 ID 和 secret
      • 支持的 CycloneDX 版本
    注意

    凭证和其他详情已经经过 Base64 编码,因此您不需要再次编码它们。您可以在您的 private.env 文件中找到这些凭证,该文件在 RHTAP 安装过程中创建。

1.1. 使用 UI 将 secret 添加到 GitHub Actions

流程

  1. 登录 GitHub 并导航到您的源存储库。
  2. 转至 Settings 选项卡。
  3. 在左侧导航窗格中,选择 Secrets 和 variables,然后选择 Actions
  4. 输入以下详情:

    1. 选择 New repository secret
    2. Name 字段中输入 MY_GITHUB_TOKEN
    3. Secret 字段中,输入与您的 GitHub 帐户关联 的令牌
  5. 重复步骤 3-4 以添加所需的变量:

    Expand
    变量描述

    仅为一个镜像 registry 提供镜像 registry 凭证。

    QUAY_IO_CREDS_USR

    用于访问 Quay.io 存储库的用户名。

    QUAY_IO_CREDS_PSW

    用于访问 Quay.io 存储库的密码。

    RTIFACTORY_IO_CREDS_USR

    用于访问 JFrog Artifactory 存储库的用户名。

    RTIFACTORY_IO_CREDS_PSW

    用于访问 JFrog Artifactory 存储库的密码。

    NEXUS_IO_CREDS_USR

    用于访问 Sonatype Nexus 存储库的用户名。

    NEXUS_IO_CREDS_PSW

    用于访问 Sonatype Nexus 存储库的密码。

    如果 GitHub Actions 运行程序不在与 RHTAP 实例相同的集群中运行,则设置这些变量。

    REKOR_HOST

    您的 Rekor 服务器的 URL。

    TUF_MIRROR

    您的 TUF 服务的 URL。

    GitHub 的 GitOps 配置

    GITOPS_AUTH_PASSWORD

    系统用来为新构建镜像更新 GitOps 存储库的令牌。

    GITOPS_AUTH_USERNAME (可选)

    Jenkins 使用 GitHub 所需的参数。您还需要在 Jenkinsfile 中使用此参数取消注释一行: GITOPS_AUTH_USERNAME = credentials ('GITOPS_AUTH_USERNAME')。默认情况下,此行被注释掉。

    ACS 任务所需的变量。

    ROX_CENTRAL_ENDPOINT

    ROX Central 服务器的端点。

    ROX_API_TOKEN

    用于访问 ROX 服务器的 API 令牌。

    SBOM 任务所需的变量。

    COSIGN_SECRET_PASSWORD

    Cosign 签名密钥的密码。

    COSIGN_SECRET_KEY

    Cosign 的私钥。

    COSIGN_PUBLIC_KEY

    Cosign 的公钥。

    TRUSTIFICATION_BOMBASTIC_API_URL

    SBOM 生成中使用的 Trustification Bombastic API 的 URL。

    TRUSTIFICATION_OIDC_ISSUER_URL

    与 Trustification Bombastic API 交互时用于身份验证的 OIDC 签发者 URL。

    TRUSTIFICATION_OIDC_CLIENT_ID

    用于使用 OIDC 向 Trustification Bombastic API 进行身份验证的客户端 ID。

    TRUSTIFICATION_OIDC_CLIENT_SECRET

    与客户端 ID 一起使用的客户端 secret,以向 Trustification Bombastic API 进行身份验证。

    TRUSTIFICATION_SUPPORTED_CYCLONEDX_VERSION

    指定系统支持并生成的 CycloneDX SBOM 版本。

  6. 选择 Add secret
  7. 重新运行最后的管道运行,以验证 secret 是否已正确应用。

    1. 或者,切换到 GitHub 中应用的源存储库,进行次要更改,并提交它以触发新的管道运行。

1.2. 使用 CLI 将 secret 添加到 GitHub

流程

  1. 在首选文本编辑器中使用两个文件创建项目,如 Visual Studio Code:

    • env_vars.sh
    • ghub-set-vars
  2. 使用以下环境变量更新 env_vars.sh 文件:

    # env_vars.sh
    
    
    
    # GitHub credentials
    export MY_GITHUB_TOKEN="your_github_token_here"
    export MY_GITHUB_USER="your_github_username_here"
    
    export GITOPS_AUTH_PASSWORD="your_OpenShift_GitOps_password_here"
    export GITOPS_AUTH_USERNAME="your_OpenShift_GitOps_username_here"
    
    // Provide the credentials for the image registry you use.
    # Quay.io credentials
    export QUAY_IO_CREDS_USR="your_quay_username_here"
    export QUAY_IO_CREDS_PSW="your_quay_password_here"
    
    # JFrog Artifactory credenditals
    export ARTIFACTORY_IO_CREDS_USR="your_artifactory_username_here"
    export ARTIFACTORY_IO_CREDS_PSW="your_artifactory_password_here"
    
    # Sonatype Nexus credentials
    export NEXUS_IO_CREDS_USR="your_nexus_username_here"
    export NEXUS_IO_CREDS_PSW="your_nexus_password_here"
    
    # Rekor and TUF routes
    export REKOR_HOST="your rekor server url here"
    export TUF_MIRROR="your tuf service url here"
    
    // Variables required for ACS tasks
    # ROX variables
    export ROX_CENTRAL_ENDPOINT="your_rox_central_endpoint_here"
    export ROX_API_TOKEN="your_rox_api_token_here"
    
    
    
    // Set these variables if GitHub Actions runners do not run on the same cluster as the {ProductShortName} instance.
    export ROX_CENTRAL_ENDPOINT="your_rox_central_endpoint_here"
    export ROX_API_TOKEN="your_rox_api_token_here"
    
    // Variables required for SBOM tasks.
    # Cosign secrets
    export COSIGN_SECRET_PASSWORD="your_cosign_secret_password_here"
    export COSIGN_SECRET_KEY="your_cosign_secret_key_here"
    export COSIGN_PUBLIC_KEY="your_cosign_public_key_here"
    
    # Trustification credentials
    export TRUSTIFICATION_BOMBASTIC_API_URL="your__BOMBASTIC_API_URL_here"
    export TRUSTIFICATION_OIDC_ISSUER_URL="your_OIDC_ISSUER_URL_here"
    export TRUSTIFICATION_OIDC_CLIENT_ID="your_OIDC_CLIENT_ID_here"
    export TRUSTIFICATION_OIDC_CLIENT_SECRET="your_OIDC_CLIENT_SECRET_here"
    export TRUSTIFICATION_SUPPORTED_CYCLONEDX_VERSION="your_SUPPORTED_CYCLONEDX_VERSION_here"
    Copy to Clipboard Toggle word wrap
  3. 使用以下信息更新 ghub-set-vars 文件:

    #!/bin/bash
    SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" > /dev/null 2>&1 && pwd)"
    
    if [ $# -ne 1 ]; then
        echo "Missing param, provide gitlab repo name"
        echo "Note: This script uses MY_GITHUB_TOKEN and MY_GITHUB_USER env vars"
        exit
    fi
    
    REPO=$1
    HEADER="PRIVATE-TOKEN: $MY_GITHUB_TOKEN"
    URL=https://github.com/api/v4/projects
    
    # Look up the project ID so we can use it below
    PID=$(curl -s -L --header "$HEADER" "$URL/$MY_GITHUB_USER%2F$REPO" | jq ".id")
    
    function setVars() {
        NAME=$1
        VALUE=$2
        MASKED=${3:-true}
        echo "setting $NAME in https://github.com/$MY_GITHUB_USER/$REPO"
    
        # Delete first because if the secret already exists then its value
        # won't be changed by the POST below
        curl -s --request DELETE --header "$HEADER" "$URL/$PID/variables/$NAME"
    
        # Set the new key/value
        curl -s --request POST --header "$HEADER" "$URL/$PID/variables" \
            --form "key=$NAME" --form "value=$VALUE" --form "masked=$MASKED" | jq
    }
    
    setVars ROX_CENTRAL_ENDPOINT $ROX_CENTRAL_ENDPOINT
    setVars ROX_API_TOKEN $ROX_API_TOKEN
    
    setVars GITOPS_AUTH_PASSWORD $MY_GITLAB_TOKEN
    setVars GITOPS_AUTH_USERNAME $MY_GITLAB_USER
    
    setVars QUAY_IO_CREDS_USR $QUAY_IO_CREDS_USR
    setVars QUAY_IO_CREDS_PSW $QUAY_IO_CREDS_PSW
    
    setVars COSIGN_SECRET_PASSWORD $COSIGN_SECRET_PASSWORD
    setVars COSIGN_SECRET_KEY $COSIGN_SECRET_KEY
    setVars COSIGN_PUBLIC_KEY $COSIGN_PUBLIC_KEY
    
    setVars TRUSTIFICATION_BOMBASTIC_API_URL "$TRUSTIFICATION_BOMBASTIC_API_URL"
    setVars TRUSTIFICATION_OIDC_ISSUER_URL "$TRUSTIFICATION_OIDC_ISSUER_URL"
    setVars TRUSTIFICATION_OIDC_CLIENT_ID "$TRUSTIFICATION_OIDC_CLIENT_ID"
    setVars TRUSTIFICATION_OIDC_CLIENT_SECRET "$TRUSTIFICATION_OIDC_CLIENT_SECRET"
    setVars TRUSTIFICATION_SUPPORTED_CYCLONEDX_VERSION "$TRUSTIFICATION_SUPPORTED_CYCLONEDX_VERSION"
    
    setVars ARTIFACTORY_IO_CREDS_USR $ARTIFACTORY_IO_CREDS_USR
    setVars ARTIFACTORY_IO_CREDS_PSW $ARTIFACTORY_IO_CREDS_PSW
    
    setVars NEXUS_IO_CREDS_USR $NEXUS_IO_CREDS_USR
    setVars NEXUS_IO_CREDS_PSW $NEXUS_IO_CREDS_PSW
    
    setVars REKOR_HOST $REKOR_HOST
    setVars TUF_MIRROR $TUF_MIRROR
    Copy to Clipboard Toggle word wrap
  4. (可选)修改 ghub-set-vars 文件,以禁用不需要的变量。例如,要禁用 setVars ROX_API_TOKEN $ROX_API_TOKEN,请在其中添加 false

    ROX_API_TOKEN $ROX_API_TOKEN false
    Copy to Clipboard Toggle word wrap
  5. 将环境变量加载到当前 shell 会话中:

    source env_vars.sh
    Copy to Clipboard Toggle word wrap
  6. 使 ghub-set-vars 脚本可执行,并使用您的存储库名称运行它,以设置 GitHub 存储库中的变量。

    chmod +x ghub-set-vars
    
    ./ghub-set-vars your_repository_name
    Copy to Clipboard Toggle word wrap
  7. 重新运行最后的管道运行,以验证 secret 是否已正确应用。

    1. 或者,切换到 GitLab 中应用的源存储库,进行次要更改,并提交它以触发新的管道运行。





更新于 2025-02-14

法律通告

Copyright © 2025 Red Hat, Inc.
The text of and illustrations in this document are licensed by Red Hat under a Creative Commons Attribution–Share Alike 3.0 Unported license ("CC-BY-SA"). An explanation of CC-BY-SA is available at http://creativecommons.org/licenses/by-sa/3.0/. In accordance with CC-BY-SA, if you distribute this document or an adaptation of it, you must provide the URL for the original version.
Red Hat, as the licensor of this document, waives the right to enforce, and agrees not to assert, Section 4d of CC-BY-SA to the fullest extent permitted by applicable law.
Red Hat, Red Hat Enterprise Linux, the Shadowman logo, the Red Hat logo, JBoss, OpenShift, Fedora, the Infinity logo, and RHCE are trademarks of Red Hat, Inc., registered in the United States and other countries.
Linux® is the registered trademark of Linus Torvalds in the United States and other countries.
Java® is a registered trademark of Oracle and/or its affiliates.
XFS® is a trademark of Silicon Graphics International Corp. or its subsidiaries in the United States and/or other countries.
MySQL® is a registered trademark of MySQL AB in the United States, the European Union and other countries.
Node.js® is an official trademark of Joyent. Red Hat is not formally related to or endorsed by the official Joyent Node.js open source or commercial project.
The OpenStack® Word Mark and OpenStack logo are either registered trademarks/service marks or trademarks/service marks of the OpenStack Foundation, in the United States and other countries and are used with the OpenStack Foundation's permission. We are not affiliated with, endorsed or sponsored by the OpenStack Foundation, or the OpenStack community.
All other trademarks are the property of their respective owners.
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat