以 headless 方式管理劳动力(Identity Center)用户(M2M)

从后端服务管理你组织的 劳动力(Identity Center)用户,无需交互式登录 — 仅使用服务 access key(AK/SK)和 STS。该模式忠实于 AWS:你的 key assumes a role,而该 role 的临时凭证携带管理 IdC 用户的权限。

AK/SK  →  assume-role  →  ASIA temp creds  →  FGAI-HMAC + X-FGAI-Security-Token
       →  /identity/v1/organizations/{orgId}/identity-center/users…

为什么使用角色(而不是直接使用你的密钥)?Workforce-user 管理在每次调用时都针对 identitycenter:User:{verb} 授权。该权限授予一个位于你组织的 management account 中的角色;你 assume 该角色来以其身份操作。这会让你的长期 service key 保持较少权限,并遵循与 AWS IAM + STS 相同的形态。

Prerequisites: a service access key (see Integrate an external app with AK/SK) and the ability to sign requests with FGAI-HMAC-SHA256 (see IAM roles and STS AssumeRole). All calls below are one signed HTTPS request to https://api.flexgalaxy.ai.

角色位于管理账号中

WorkforceAdminRole 始终位于你的组织的管理账号({mgmtAccountId})中,因为 Identity Center 目录锚定在那里。因此其 FRN 是 frn:{mgmtAccountId}:iam:role/WorkforceAdminRole,其权限策略授予 identitycenter:User:* 到 frn:{mgmtAccountId}:identitycenter:user/*。你应选择哪条路径取决于你的服务主体位于哪里:

路径

你的 service principal 位于…

你通过…获取该角色

Primary — platform-seeded(external integrator)

你的自有 tenant account(不是 management account)

platform 会在 management account 中为你预置该 role;其 trust 允许你的 principal 的 FRN 跨 accounts 访问,并要求 sts:ExternalId。你可跨 account assume 它。

Secondary — 自写入(仅 management-account)

management account 本身

你需要自行编写该 role、它的 managed policy 以及 attachment,然后 assume 它。

主路径是外部集成方的常规路径:服务 key 被固定到其自己的账号,因此它不能在管理账号中创建角色 — 平台会播种该角色。self-author 路径仅当你的主体已经位于管理账号中时才可用。

两个关键约束

先阅读这些 — 任一错误都会产生静默的 403 IMPLICIT_DENY。它们在两条路径上都成立(平台预置 role 已满足它们)。

  1. 权限通过 MANAGED policy 授予,并 attach 到 role。 role 的 inline policy 在此路径中不会生效。grant 是通过 POST …/roles/{roleId}/policies attach 的 customer-managed iam-policy。

  2. policy Resource 必须匹配 Identity Center user FRN。 它必须是 frn:{mgmtAccountId}:identitycenter:user/*(或 *)。更窄或错误的 Resource 不会匹配,调用会被拒绝。

主路径 — assume 平台预置的 role(外部集成方)

你的 service principal 位于你自己的 tenant account 中。在 onboarding 期间,平台会在你组织的 management account 中 seed WorkforceAdminRole,其 trust policy 会跨账号接纳你的 principal FRN,并要求共享密钥 sts:ExternalId。你无需编写任何内容 — 只需 assume 并执行。

onboarding 会给你什么:

  • role FRN — frn:{mgmtAccountId}:iam:role/WorkforceAdminRole

  • trust condition 使用的 externalId shared secret

  • 你的 {orgId}(及其 {mgmtAccountId})

1. Assume the role → temporary credentials

POST /identity/v1/sts/assume-role — 使用你的静态服务密钥(AKIA)签名,即使该角色位于不同的(管理)账号中也是如此。这是跨账号 assume-role;角色的 trust policy 是门控条件。

{
  "roleFrn": "frn:{mgmtAccountId}:iam:role/WorkforceAdminRole",
  "roleSessionName": "workforce-batch-42",
  "durationSeconds": 3600,
  "externalId": "shared-secret-xyz"
}

响应返回 ASIA… 凭证(accessKeyId、secretAccessKey、sessionToken、expiration)。durationSeconds 被限制在 [900, role.maxSessionDuration]。错误或缺失的 externalId 返回 403,且不返回凭证。

2. Manage workforce users with the temporary credentials

使用 ASIA 凭证(FGAI-HMAC-SHA256)为每次调用签名,并添加 X-FGAI-Security-Token header,其值设为 sessionToken。

… /identity/v1/organizations/{orgId}/identity-center/users

方法

路径

目的

GET

…/identity-center/users

列出劳动力用户

POST

…/identity-center/users

邀请劳动力用户

GET

…/identity-center/users/{userId}

读取劳动力用户

PUT

…/identity-center/users/{userId}

更新 workforce user

POST

…/identity-center/users/{userId}/disable | /enable

禁用 / 启用

POST

…/identity-center/users/{userId}/reset-password

重置登录凭证

DELETE

…/identity-center/users/{userId}

移除劳动力用户

每个操作都会根据 identitycenter:User:{verb} 检查;使用 seeded role 的 managed policy 时,检查返回 ALLOW。

次要路径 — 自行创建角色(仅限管理账号主体)

仅当你的 service principal 已经位于 management account 中时才使用此项。在这种情况下,下面路径中的 {accountId} 就是 {mgmtAccountId},因此你可以通过公共路由使用静态密钥编写整条链,然后像上面的步骤 1 一样 assume 该角色。

1. Create the managed permission policy

POST /identity/v1/accounts/{mgmtAccountId}/policies

{
  "name": "WorkforceAdmin",
  "policyDocument": {
    "Version": "2012-10-17",
    "Statement": [
      {
        "Effect": "Allow",
        "Action": "identitycenter:User:*",
        "Resource": "frn:{mgmtAccountId}:identitycenter:user/*"
      }
    ]
  }
}

如果你愿意,可以将 Action 收窄到你需要的确切 verbs:identitycenter:User:List、:Read、:Create、:Update、:Delete、:Disable、:Enable、:ResetPassword。响应会返回 policy id。

2. Create the role with a trust policy that admits your service principal

POST /identity/v1/accounts/{mgmtAccountId}/roles

trust policy 回答“谁可以 assume 我”。请通过 FRN 允许你自己的服务主体,并要求 sts:AssumeRole。推荐: 添加 sts:ExternalId 条件(confused-deputy hardening),并在 assume-role 时传入相同值。

{
  "name": "WorkforceAdminRole",
  "maxSessionDuration": 3600,
  "trustPolicy": {
    "Version": "2012-10-17",
    "Statement": [
      {
        "Effect": "Allow",
        "Principal": { "FGAI": ["frn:{mgmtAccountId}:iam:user/{yourPrincipalId}"] },
        "Action": "sts:AssumeRole",
        "Condition": { "StringEquals": { "sts:ExternalId": "shared-secret-xyz" } }
      }
    ]
  }
}

响应返回角色 id;角色 FRN 是 frn:{mgmtAccountId}:iam:role/WorkforceAdminRole。

3. Attach the managed policy to the role

POST /identity/v1/accounts/{mgmtAccountId}/roles/{role_id}/policies

{ "policyId": "a1b2c3d4-…" }

这一步让该角色能够管理 IdC 用户。(将策略以内联方式附加到角色上不起作用 — 请参见约束 1。)

4. Assume the role and manage users

assume 该 role 并按 primary path(上方步骤 1–2)完全相同的方式管理 users,使用 roleFrn = frn:{mgmtAccountId}:iam:role/WorkforceAdminRole。

如何查找你的 mgmtAccountId

角色 FRN、策略 Resource 和 {orgId} 路径都指向你的组织的管理账号。从组织解析它:读取你的组织(GET /organizations/v1/{orgId})— 其管理账号 id 是 mgmtAccountId。对于 self-author(管理账号)主体,这就是你自己的账号 id。

长时间运行的作业:会话时长和重新 assume

Assumed-role credentials 是短期的 — durationSeconds 会被限制到 [900, role.maxSessionDuration](默认 3600)。长时间运行的 headless batch 必须在当前 expiration 之前重新 assume 该 role,并继续使用新的 ASIA credentials 签名。请在到期前稍早刷新(例如生命周期的 80%),而不是等待 401。

失败

  • 签名缺失/无效 → 401。

  • Role 缺失 或 其 trust policy 拒绝你(错误 principal 或错误 externalId)→ 403(不可区分,因此调用方无法探测另一个账户中的 roles),且 不会 返回 credentials。

  • 权限未授予(未附加 managed policy,或 Resource 不匹配)→ identity-center/users 调用返回 403。

另请参见