工作负载身份联合 — 从 CI 调用 FlexGalaxy.AI API,无需长期密钥

⚠️ 过时 — 描述的是未在 neo 中采用的设计;待重写。

下面记录的 GitHub Actions / GitLab CI federation 流程不是当前运行中部署已接线的机制。在线 federation trust-root 接受来自 FlexGalaxy.AI 自有集群内 workload 的 OIDC token,而不是来自公共 GitHub 或 GitLab issuer。如果你需要从 CI 调用 FlexGalaxy.AI API 且不使用长期 secret,请在依赖以下步骤前联系 FlexGalaxy.AI support 了解当前集成路径。

如果你的 CI/CD pipeline 调用 FlexGalaxy.AI APIs,或以其他方式作为非人类 principal 行事,请 不要 在 GITHUB_SECRETS 或 GitLab CI variables 中发布长期 API key。使用 workload identity federation:你的 workload 向 DotID 提供其 IdP 颁发的 OIDC token,DotID 验证它,并返回一个限定到某个 FlexGalaxy.AI service 的短期(15 min)access token。

本页汇总了调用点所需的全部内容。

TL;DR. Your GitHub Actions or GitLab CI job requests an OIDC token with audience dotid-token-exchange, posts it to https://iam.flexgalaxy.ai/oauth2/token as an RFC 8693 token exchange, receives a 15-minute access token, and presents that to the target FlexGalaxy.AI service as Authorization: Bearer <token>. No refresh tokens. No long-lived secrets in your CI.

先决条件 — 您的 FlexGalaxy.AI 账户管理员必须首先设置的内容

此流程需要在您的 FlexGalaxy.AI 账户中完成两项一次性设置。请将此列表转发给您的账户根用户或授权账户管理员:

  1. Create a workload identity in the account that owns the target service. The account admin opens https://console.flexgalaxy.ai/account → Workload identities → New and gives it a descriptive name (e.g. bazaar-publish-syrius).

  2. Bind the federated subject to that workload identity. For GitHub Actions, the bound subject is the (issuer, sub) tuple from the GitHub OIDC token — typically repo:<owner>/<repo>:ref:refs/heads/<branch> (or :environment:<env>, :pull_request, etc.) under issuer https://token.actions.githubusercontent.com. For GitLab CI, the bound subject is project_path:<group>/<project>:ref_type:branch:ref:<branch> under issuer https://gitlab.com (or your self-hosted GitLab URL). The account admin sends a POST /identity/v1/accounts/{account_id}/workload-identities/federated with:

    {
      "workload_identity_id": "<id from step 1>",
      "issuer": "https://token.actions.githubusercontent.com",
      "subject": "repo:syrius/bazaar:ref:refs/heads/main"
    }
    
  3. 编写 IAM 策略,允许工作负载身份为目标服务交换令牌。操作为 iam:ExchangeToken;资源为 arn:fg:dotid::service/<target-service>:

    {
      "Effect": "Allow",
      "Action": "iam:ExchangeToken",
      "Resource": "arn:fg:dotid::service/bazaar-publisher"
    }
    

    通过标准 IAM 策略 UI 将其附加到工作负载身份(或其所属的组)。

一旦这三个步骤完成,您可以在 CI 中运行本指南的其余部分,而无需进一步的账户管理员参与。

GitHub Actions — 示例工作流

name: publish-to-bazaar
on:
  push:
    branches: [main]

permissions:
  id-token: write    # required for OIDC token request
  contents: read

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Request OIDC token from GitHub
        id: oidc
        run: |
          OIDC_TOKEN=$(curl -sLS \
            -H "Authorization: bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" \
            "$ACTIONS_ID_TOKEN_REQUEST_URL&audience=dotid-token-exchange" \
            | jq -r '.value')
          echo "oidc_token=$OIDC_TOKEN" >> "$GITHUB_OUTPUT"

      - name: Exchange for a DotID access token
        id: exchange
        run: |
          RESPONSE=$(curl -sS -X POST https://iam.flexgalaxy.ai/oauth2/token \
            -H "Content-Type: application/x-www-form-urlencoded" \
            --data-urlencode "grant_type=urn:ietf:params:oauth:grant-type:token-exchange" \
            --data-urlencode "subject_token=${{ steps.oidc.outputs.oidc_token }}" \
            --data-urlencode "subject_token_type=urn:ietf:params:oauth:token-type:jwt" \
            --data-urlencode "audience=bazaar-publisher")
          ACCESS_TOKEN=$(echo "$RESPONSE" | jq -r '.access_token')
          echo "::add-mask::$ACCESS_TOKEN"
          echo "access_token=$ACCESS_TOKEN" >> "$GITHUB_OUTPUT"

      - name: Call the target FlexGalaxy.AI service
        env:
          SERVICE_TOKEN: ${{ steps.exchange.outputs.access_token }}
        run: |
          # The target path and payload are defined by whichever FlexGalaxy.AI
          # service you call. Substitute <your-service> and the real route.
          curl -sS -X POST "https://api.flexgalaxy.ai/<your-service>/v1/..." \
            -H "Authorization: Bearer $SERVICE_TOKEN" \
            -F "artifact=@./build/my-artifact-1.0.0.tar.gz"

关键点:

  • permissions: id-token: write — 如果没有这个,ACTIONS_ID_TOKEN_REQUEST_TOKEN 将不会导出到作业中,步骤 1 将静默失败。

  • &audience=dotid-token-exchange — 必须是这个字面字符串。这是 anti-confused-deputy audience pin。具有任何其他 audience 的 token 都会被拒绝。

  • 对访问令牌使用 ::add-mask:: — 防止意外日志泄漏。令牌有效期很短,但掩码是低成本的保障。

  • 交换得到的令牌的 aud 是 bazaar-publisher(请求 audience 参数的值)。每个下游服务都会验证自己的受众。

GitLab CI — 示例作业

publish-to-bazaar:
  image: alpine:3.20
  before_script:
    - apk add --no-cache curl jq
  id_tokens:
    DOTID_TOKEN:
      aud: dotid-token-exchange     # MUST be this literal string
  script:
    - |
      RESPONSE=$(curl -sS -X POST https://iam.flexgalaxy.ai/oauth2/token \
        -H "Content-Type: application/x-www-form-urlencoded" \
        --data-urlencode "grant_type=urn:ietf:params:oauth:grant-type:token-exchange" \
        --data-urlencode "subject_token=$DOTID_TOKEN" \
        --data-urlencode "subject_token_type=urn:ietf:params:oauth:token-type:jwt" \
        --data-urlencode "audience=bazaar-publisher")
      ACCESS_TOKEN=$(echo "$RESPONSE" | jq -r '.access_token')
    - |
      # Substitute <your-service> and the real route for your target service.
      curl -sS -X POST "https://api.flexgalaxy.ai/<your-service>/v1/..." \
        -H "Authorization: Bearer $ACCESS_TOKEN" \
        -F "artifact=@./build/my-artifact-1.0.0.tar.gz"
  rules:
    - if: $CI_COMMIT_BRANCH == "main"

id_tokens is the GitLab CI keyword that requests an OIDC token from GitLab's built-in issuer (https://gitlab.com for hosted GitLab, the self-hosted instance URL otherwise). The aud field must be the literal string dotid-token-exchange.

交换调用(原始)

如果您是从除 GitHub Actions 或 GitLab CI 之外的 CI 系统进行此操作,则底层的 HTTP 交换是相同的:

POST /oauth2/token HTTP/1.1
Host: iam.flexgalaxy.ai
Content-Type: application/x-www-form-urlencoded

grant_type=urn:ietf:params:oauth:grant-type:token-exchange
&subject_token=<your federated OIDC JWT>
&subject_token_type=urn:ietf:params:oauth:token-type:jwt
&audience=<target-service-id>

必需参数

参数

值

grant_type

urn:ietf:params:oauth:grant-type:token-exchange (RFC 8693,固定)

subject_token

您的联合 OIDC JWT

subject_token_type

urn:ietf:params:oauth:token-type:jwt (唯一接受的值)

audience

目标 FlexGalaxy.AI 服务标识符(无默认值;您必须提供一个)。示例:bazaar-publisher、wms-fleet、pspec-write

可选 / 被忽略的参数

参数

DotID 的功能

requested_token_type

默认为 urn:ietf:params:oauth:token-type:access_token;仅接受该值。发送任何其他值将返回 400 unsupported_token_type。

scope

被忽略 — scope 根据您的 IAM 策略授权计算。发送 scope 对响应没有影响。

resource

被忽略 — 重复 audience。

actor_token

已忽略 — delegation chains 当前不支持。

成功响应 (200)

{
  "access_token": "eyJraWQiOiJ...<DotID-signed JWT>",
  "issued_token_type": "urn:ietf:params:oauth:token-type:access_token",
  "token_type": "Bearer",
  "expires_in": 900,
  "scope": "bazaar:Artifact:Publish"
}
  • expires_in 始终为 900(15 分钟)。不提供更长的 TTL。

  • 不存在 refresh_token 字段。每次 CI 运行时,请向你的 IdP 请求新的 OIDC token 并重新 exchange。这是有意设计 — workload tokens 本来就是短期的。

  • scope 是 PDP 计算的授权集。请将其视为信息性字段;实际授权发生在目标服务验证 JWT 时。

错误解码器 — 当 invalid_grant 返回时

DotID 有意将每个 subject-token rejection 折叠为带通用消息的 401 invalid_grant。这是有意设计的(它防止探测有效 issuers、subjects 或 claim shapes),但这意味着你必须从自己一侧消歧。

以下是规范解码器。每次遇到 invalid_grant 时,都应从上到下逐项处理。

症状

最可能的原因

需要检查/修复的内容

401 invalid_grant,并且您刚刚首次完成此设置

主体令牌上存在受众不匹配

您的 CI 正在请求 OIDC,但受众错误。在 GitHub Actions 中,请检查传递给 ACTIONS_ID_TOKEN_REQUEST_URL 的 URL 是否确实以 &audience=dotid-token-exchange 结尾。在 GitLab CI 中,请检查 id_tokens: DOTID_TOKEN: aud: dotid-token-exchange。任何其他值(包括 dotid、flexgalaxy、您自己的服务名称)将被拒绝。

401 invalid_grant 在一个正常工作的管道开始失败后

联合主体已更改

GitHub OIDC subjects encode the branch (refs/heads/main), environment, or PR shape. A workflow that worked on main will fail if it runs on a feature branch unless the account admin bound that branch's subject too. Decode your subject token at https://www.jwt.io/ and confirm the sub claim matches what your admin bound.

来自全新签发者的 401 invalid_grant

签发者不在信任根中

federation trust root 是固定的 OIDC issuer allowlist。不在 allowlist 上的 issuer 会被拒绝。新增 issuer 需要 FlexGalaxy.AI 侧的配置变更 — 请带上你的 issuer URL 联系 FlexGalaxy.AI support。

401 invalid_grant 在已知 JWKS 轮换后不久

DotID 上的过期 JWKS 缓存

DotID 在签名失败时会刷新一次你的 IdP 的 JWKS 缓存副本。如果你在轮换后的几秒内遇到这种情况,请在 30 秒后使用新的 OIDC token 重试。若失败持续数分钟,则表示存在真实问题 — 请联系 FlexGalaxy.AI support,并引用你的 federated-subject (iss, sub) 以便追踪问题。

401 invalid_grant,并且主体令牌的 exp 已超过 5 分钟

主体令牌已过期

联合 OIDC 令牌的有效期为几分钟。在每次交换调用之前立即请求新的 OIDC 令牌;不要在步骤之间缓存它们。

403 access_denied(不是 invalid_grant!)

主体令牌有效,但 IAM 策略拒绝了交换

你的 subject token 已成功验证。可能是 IAM policy 缺失、命名了错误的 resource,或携带了优先级更高的 Deny。resource 必须是与所发送 audience 匹配的 arn:fg:dotid::service/<audience>。请让你的 account admin 重新检查 policy。此次拒绝已记录在你账号的 audit trail 中。

其他状态码

状态

RFC 8693 error

发生了什么

400

invalid_request

缺少必需的参数或参数格式错误(例如,audience 为空,或 grant_type 错误)。错误描述中列出了有问题的参数。

400

unsupported_token_type

您发送的 subject_token_type 不是 urn:ietf:params:oauth:token-type:jwt,或者请求的 requested_token_type 不是 urn:ietf:params:oauth:token-type:access_token。

403

access_denied

PDP 拒绝了 iam:ExchangeToken。请参见上一行。

5xx

(无正文)

DotID 端问题。请使用退避策略重试;如果问题持续存在,请查看状态页面并联系支持。

不支持的内容

这些是有意省略,不是 bug。如果你需要其中之一,请联系 FlexGalaxy.AI support,而不是提交 issue。

  • 刷新 token。 Workload token 刻意设置为短生命周期。每次 CI 运行时重新 exchange。根据 RFC 8693 §2.2.1,DotID 可以省略 refresh_token,实际也会省略。

  • actor_token delegation chains。 不支持。

  • resource parameter(RFC 8707)。接受但忽略 — audience 是唯一 routing input。

  • Token introspection endpoint(RFC 7662)。消费者会基于 DotID JWKS 在本地校验 JWT signature(见 api-auth-jwt-validation.md)。

  • AWS STS / Azure AD / corporate-SSO federation。 不支持。

  • 与你团队运营的自定义 OIDC IdP 进行 federation。 trust-root issuer set 是固定的;如果需要添加 issuer,请联系 FlexGalaxy.AI support。

处理交换的令牌

DotID 返回的令牌是普通的 DotID 签发 JWT。要在目标服务中使用它,请遵循 api-auth-jwt-validation.md 中的标准模式:

  • iss 是 DotID 领域 URL。

  • aud 与您发送的 audience 参数匹配(bazaar-publisher 等)。

  • sub 是您的工作负载身份 ID。

  • scope 是 PDP 计算的授权集。

  • 自定义声明 federated_subject 携带输入 OIDC 令牌中的 {issuer, sub} — 您的服务可以将其用于审计关联,但 不得 用于授权决策(aud/sub/scope 声明才是授权依据)。

根据领域 JWKS 验证 JWT 签名。按照其 Cache-Control 标头缓存 JWKS 响应。根据 exp 拒绝过期令牌。

重新交换频率

朴素的“在作业开始时交换一次”模式,会让一个 15 分钟有效期的访问令牌在作业余下时间里一直留在作业环境中。可以做两点改进:

  • 每个调用点一次交换 如果您的作业调用多个 FlexGalaxy.AI 服务。为每个 audience 单独交换(每个目标服务一个令牌),并让每个令牌自行过期。

  • 在目标服务返回 401 时重新交换。当令牌过期时,目标服务返回 401;您的客户端捕获到它,重新请求来自 IdP 的新 OIDC 令牌并重新交换,然后重试原始调用。将重试次数限制为 1。

对于大多数 CI 作业(总时长 < 15 分钟),交换一次即可。

不使用 GitHub / GitLab 的本地测试

你无法从真实工作流之外的 trusted issuer 生成有效的 OIDC token — 这正是 trust root 的意义。如果你在接入实时 pipeline 之前需要帮助演练集成的 FlexGalaxy.AI 侧,请联系 FlexGalaxy.AI support。

另见

  • oidc-client-setup.md — 注册 OIDC 客户端(接受用户登录的浏览器/应用)是 不同 的事情。工作负载联合不注册客户端;它将外部 OIDC 主体绑定到 DotID 工作负载身份。

  • api-auth-jwt-validation.md — 如何在目标服务中验证任何由 DotID 颁发的 JWT。

  • api-overview.md — FlexGalaxy.AI API 概述。

  • frn-format.md — FRN(资源 ARN)格式。iam:ExchangeToken 资源模式为 arn:fg:dotid::service/<target>。