diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 6d12085..31512df 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -139,6 +139,13 @@ jobs: if: vars.GCP_PROJECT_NUMBER != '' && env.SCRIPT_PROPERTIES != '' run: ./scripts/set-properties.sh --env SCRIPT_PROPERTIES + # Hygiene, not isolation: keeps the token file away from the post-deploy + # hook, but the job can still mint OIDC tokens (id-token: write), so + # hooks remain trusted, reviewed code like the rest of head_branch. + - name: Remove clasp credentials + if: always() + run: rm -f ~/.clasprc.json .clasp.json + - name: Post-deploy hook if: always() && hashFiles('.github/hooks/post-deploy.sh') != '' run: bash .github/hooks/post-deploy.sh diff --git a/.gitlab/cd.yml b/.gitlab/cd.yml index 7bbb998..d215d78 100644 --- a/.gitlab/cd.yml +++ b/.gitlab/cd.yml @@ -43,6 +43,9 @@ if [[ -n "${GCP_PROJECT_NUMBER:-}" && -n "${SCRIPT_PROPERTIES:-}" ]]; then ./scripts/set-properties.sh --env SCRIPT_PROPERTIES fi + # The shared deploy token must not outlive the deploy steps. + after_script: + - rm -f ~/.clasprc.json .clasp.json cache: key: pnpm-$CI_COMMIT_REF_SLUG paths: diff --git a/README.ja.md b/README.ja.md index b114f33..fe316a4 100644 --- a/README.ja.md +++ b/README.ja.md @@ -78,15 +78,15 @@ Apps Script Fleet は各 Apps Script 機能を独立したリポジトリとし ## 組織セットアップ(初回のみ) -チームが Apps Script Fleet を使い始める前に、組織の管理者が clasp の共有認証情報を **Google Cloud Secret Manager** に格納します。CI は **Workload Identity Federation (OIDC)** でキーレス取得、開発者は個人の `gcloud` ログインで取得します。secret 1つ + WIF プール1つ + org/group 単位 IAM = **リポごとの認証設定ゼロ**。旧方式には無かったローテーション・失効・監査ログが手に入ります。 +チームが Apps Script Fleet を使い始める前に、組織の管理者が clasp の共有認証情報を **Google Cloud Secret Manager** に格納します。CI は **Workload Identity Federation (OIDC)** でキーレス取得します(Permissive モードでは開発者も個人の `gcloud` ログインで取得できます)。secret 1つ + WIF プール1つ + org/group 単位 IAM = **リポごとの認証設定ゼロ**。旧方式には無かったローテーション・失効・監査ログが手に入ります。 -詳細な手順: **[docs/secret-manager.ja.md](docs/secret-manager.ja.md)**。概要: +詳細な手順(**CI-only / Permissive** のデプロイ ID 運用モード選択を含む): **[docs/secret-manager.ja.md](docs/secret-manager.ja.md)**。概要: 1. **認証情報の格納**: デプロイ用アカウントの [Apps Script API を有効化](https://script.google.com/home/usersettings)し、そのアカウントで `clasp login` → `gcloud secrets versions add clasp-credentials --data-file="$HOME/.clasprc.json"` 2. **WIF プール `apps-script-fleet` を作成**し、`github` / `gitlab` プロバイダを org/group に attribute 制限 -3. **`roles/secretmanager.secretAccessor` を付与**: CI の `principalSet://` と開発者 Google グループへ +3. **`roles/secretmanager.secretAccessor` を付与** — **CI-only モード(推奨)**: CD の deploy 環境プリンシパルと、`init.sh` を実行する少数の管理者のみへ。**Permissive モード**: org/group 全体の CI `principalSet://` と開発者 Google グループへ 4. **org/group の CI 変数** `GCP_WIF_PROVIDER` + `CLASPRC_SECRET` を設定(両プラットフォーム同名) -5. **各開発者**はマシンごとに1回 `./scripts/fetch-clasp-credentials.sh` を実行 — `~/.clasprc.json` は全リポで共有されます +5. **開発者** — **CI-only モード**: 共有トークンは取得せず、個人 sandbox(`./scripts/init-sandbox.sh`)で開発。**Permissive モード**: 各開発者がマシンごとに1回 `./scripts/fetch-clasp-credentials.sh` を実行 — `~/.clasprc.json` は全リポで共有されます > **clasp の認証そのものは変わりません。** clasp は今後も `~/.clasprc.json` の OAuth トークンを使います(Apps Script API は push/deploy にサービスアカウント非対応)。変わるのはトークンの保管場所と配送経路だけです。 diff --git a/README.md b/README.md index 77785ab..686c833 100644 --- a/README.md +++ b/README.md @@ -78,15 +78,15 @@ The result — your day looks like this: ## Organization Setup (One-Time) -Before your team can use Apps Script Fleet, an org admin stores the shared clasp credentials in **Google Cloud Secret Manager**. CI fetches them keylessly via **Workload Identity Federation (OIDC)**; developers fetch them with their personal `gcloud` login. One secret + one WIF pool + org/group-wide IAM = **zero per-repo auth setup**, with rotation, revocation, and audit logs the legacy model never had. +Before your team can use Apps Script Fleet, an org admin stores the shared clasp credentials in **Google Cloud Secret Manager**. CI fetches them keylessly via **Workload Identity Federation (OIDC)**; in Permissive mode developers can also fetch them with their personal `gcloud` login. One secret + one WIF pool + org/group-wide IAM = **zero per-repo auth setup**, with rotation, revocation, and audit logs the legacy model never had. -Full step-by-step guide: **[docs/secret-manager.md](docs/secret-manager.md)**. In outline: +Full step-by-step guide, including the **CI-only vs Permissive** deploy identity modes: **[docs/secret-manager.md](docs/secret-manager.md)**. In outline: 1. **Store credentials**: enable the [Apps Script API](https://script.google.com/home/usersettings) for the deploy account, `clasp login` with it → `gcloud secrets versions add clasp-credentials --data-file="$HOME/.clasprc.json"` 2. **Create WIF pool `apps-script-fleet`** with `github` / `gitlab` providers, attribute-restricted to your org/group -3. **Grant `roles/secretmanager.secretAccessor`** to the CI `principalSet://` and to the developer Google group +3. **Grant `roles/secretmanager.secretAccessor`** — **CI-only mode (recommended)**: to the CD deploy-environment principals only, plus the few admins who run `init.sh`; **Permissive mode**: to the org/group-wide CI `principalSet://` and the developer Google group 4. **Set org/group CI variables** `GCP_WIF_PROVIDER` + `CLASPRC_SECRET` (same names on both platforms) -5. **Each developer** runs `./scripts/fetch-clasp-credentials.sh` once per machine — `~/.clasprc.json` is shared by every repo +5. **Developers** — **CI-only mode**: never fetch the shared token; develop against a personal sandbox (`./scripts/init-sandbox.sh`); **Permissive mode**: each developer runs `./scripts/fetch-clasp-credentials.sh` once per machine — `~/.clasprc.json` is shared by every repo > **clasp auth itself is unchanged.** clasp still uses the `~/.clasprc.json` OAuth token (the Apps Script API does not support service accounts for push/deploy). Only the storage and delivery of that token change. diff --git a/docs/secret-manager.ja.md b/docs/secret-manager.ja.md index 7d115e6..74f485e 100644 --- a/docs/secret-manager.ja.md +++ b/docs/secret-manager.ja.md @@ -44,6 +44,149 @@ GCP ネイティブ認証への置き換えは不可能です。変わるのは 存続)。 - どちらも無し → 明示エラー。 +## デプロイ ID の運用モード: CI-only / Permissive + +このフリートのセキュリティ境界はただ1箇所 — **誰が共有トークンを読めるか** +(`roles/secretmanager.secretAccessor` の IAM)です。次の2モードから明示的に +選択してください。どちらも一級のサポート対象構成です: + +| | **CI-only(推奨)** | **Permissive** | +| --- | --- | --- | +| secret を読める主体 | **GitHub**: `main` 上の `cd.yml`(`workflow_run` 起動)から実行された deploy 環境ジョブのみ。**GitLab**: 保護 ref 上の deploy 環境ジョブのみ。 | org/group 内の全ワークフロー + 開発者 Google グループ。 | +| 共有 ID での手元デプロイ | 不可。開発は個人 sandbox で: `./scripts/init-sandbox.sh`(README 参照)。 | `./scripts/fetch-clasp-credentials.sh` 後、従来どおり clasp。 | +| レビューのバイパス | デプロイはブランチ保護されたマージを経た、レビュー済み `cd.yml` 経由のみ。 | 開発者は CI もレビューも経ずに手元から何でもデプロイできる。 | +| 監査粒度 | 「誰が何をデプロイしたか」= PR / パイプライン履歴。secret アクセスログには CI プリンシパルのみ(リポ・環境単位)。 | Secret Manager は取得までは記録するが、その後トークンが手元で何をしたかは見えない。 | +| 緊急デプロイ | CI 経由(revert / roll-forward のマージ)。 | 手元から可能。 | +| セットアップ | 基本セットアップ(§1〜7)**+ 下記の必須要件4つ**。WIF 必須。 | 基本セットアップ(§1〜7)そのまま。 | + +Permissive はこのテンプレの歴史的デフォルトで、今後もサポートされます — +開発者全員が実質管理者の小規模チームなら合理的な選択です。「本番デプロイは +必ずレビューを通る」を慣習ではなく**保証**にしたいなら CI-only を選んで +ください。 + +### CI-only モード: 必須要件 + +4つすべてが必須です — それぞれが実在するバイパス経路を塞ぎます。(1) は +§3a/§3b のプロバイダ条件と §4 の付与を置き換え、(3)(4) は gcloud コマンド +ではなく組織側の前提条件です。 + +#### 1. WIF プロバイダをレビュー済み CD ワークフローに限定 + +GitHub。`workflow_run` 起動のジョブは常にデフォルトブランチの `cd.yml` 定義を +実行するため、`workflow_ref` を `main` に固定すれば「レビュー済みの定義」を +意味します。さらに `environment` クレームで deploy ジョブに絞ります: + +```bash +gcloud iam workload-identity-pools providers update-oidc github \ + --location=global --workload-identity-pool=apps-script-fleet \ + --attribute-mapping="google.subject=assertion.sub,attribute.repository=assertion.repository,attribute.repository_owner=assertion.repository_owner,attribute.environment=assertion.environment" \ + --attribute-condition="assertion.repository_owner == '${ORG}' && assertion.event_name == 'workflow_run' && assertion.workflow_ref.endsWith('/.github/workflows/cd.yml@refs/heads/main') && assertion.environment in ['development', 'production']" +``` + +GitLab。`workflow_run` に相当する仕組みは無いため、「レビュー済み定義」の +保証は `ref_protected`(要件 3 と組み合わせることで保護 ref = レビュー済み +マージのみ)+ `environment` クレームで構成します: + +```bash +gcloud iam workload-identity-pools providers update-oidc gitlab \ + --location=global --workload-identity-pool=apps-script-fleet \ + --attribute-mapping="google.subject=assertion.sub,attribute.project_path=assertion.project_path,attribute.namespace_path=assertion.namespace_path,attribute.environment=assertion.environment" \ + --attribute-condition="(assertion.namespace_path == '${GROUP}' || assertion.namespace_path.startsWith('${GROUP}/')) && assertion.ref_protected == 'true' && assertion.environment in ['development', 'production']" +``` + +`secretAccessor` は org/group 属性ではなく `environment` 属性に付与します +(§4 のバインディングを置き換えます — 既存のものがあれば削除): + +```bash +POOL="projects/${PROJECT_NUMBER}/locations/global/workloadIdentityPools/apps-script-fleet" +for e in development production; do + gcloud projects add-iam-policy-binding "$PROJECT_ID" \ + --role=roles/secretmanager.secretAccessor --condition=None \ + --member="principalSet://iam.googleapis.com/${POOL}/attribute.environment/${e}" +done +``` + +attribute condition がプールの発行する全トークンを既にフィルタしているため、 +environment 単位の付与は多層防御です: 将来プロバイダ設定を誤っても、それ +単独で org 全体へのアクセスが静かに復活することはありません。 + +#### 2. 両 environment の deployment branch policy + +branch policy が無いと、**任意の**ブランチ上のワークフローが +`environment: production` を宣言して、そのクレーム入り OIDC トークンを +入手できてしまいます。 + +- **GitHub**: `workflow_run` 起動のジョブはデフォルトブランチを基準に評価 + されるため、`development` / `production` **両方とも許可ブランチを `main` + のみ**にします(dev / prod の区別は `cd.yml` 内の `head_branch` 判定と + environment 名で行う — 現行どおり・変更なし)。 +- **GitLab**: 従来どおり protected environments。 + +GitHub では `./scripts/configure-environments.sh --branch-policy` が両 +environment のデプロイブランチを `main` のみに制限します。GitLab ではこの +フラグは何も設定しないため、protected branches(`dev`/`main`)と protected +environments で制限してください — 後者は `--development-reviewers` / +`--production-reviewers` を渡すとスクリプトが作成します(GitLab Premium)。 + +#### 3. `dev` / `main` のブランチ保護を org 全体で強制 + +WIF プロバイダは org/group 全体を信頼します。org 全体の保護が無いと、フリート +内外を問わず org 内のどのリポでも、自分の `main` に `cd.yml` を直 push して +要件 1 を満たせてしまいます。`dev` / `main` の PR 必須保護を **org 全体の +ルールセット**(GitHub)/ group レベルの保護ブランチ(GitLab)で強制して +ください。org ルールセットが難しい場合は、代わりにプロバイダの信頼範囲を +fleet 専用 org/group に限定します +(`assertion.repository_owner == ''`)。 + +なお `cd.yml` は `head_branch` のコードを checkout するため、dev で**デプロイ +されるコード**を守るのは `dev` 自身のブランチ保護です。 + +#### 4. WIF 必須 + +legacy の `CLASPRC_JSON` org secret は org 内の任意リポの任意ワークフローから +読め、上記の条件を一切表現できません。したがって CI-only モードは Secret +Manager + WIF 経路が必須です。legacy 経路はエアギャップ環境の Permissive +構成にのみ残してください。 + +> **実機検証が未了**(テンプレリポの issue #193 で追跡):`workflow_run` +> 起動ジョブの `workflow_ref` / `event_name` / `environment` クレームと、 +> deployment branch policy の評価 ref が `main` になる挙動は GitHub の +> ドキュメントどおりですが、実トークンでの確認はまだです。境界に依存する +> 前に下記の検証を実施してください。 + +### CI-only 境界の検証 + +**成功すべきもの** — 既存の `cd.yml`(`workflow_run` 起動): + +1. `dev` への任意の変更のマージ → CI → CD の `development` ジョブが secret を + 取得してデプロイできる。 +2. `dev` を `main` にマージ → CD の `production` ジョブが secret を取得して + デプロイできる。 + +**失敗すべきもの** — 以下はすべて拒否されること(STS 403 "rejected by the +attribute condition"、またはトークン発行前にブロック): + +1. フリートリポの**フィーチャーブランチ**上のワークフローが、フリートの + プロバイダで `google-github-actions/auth` を呼ぶ(`environment` 無し)。 +2. 同じワークフローがフィーチャーブランチで `environment: development` / + `production` を宣言 — deployment branch policy(要件 2)により、トークン + 発行前にブロックされる。 +3. フィーチャーブランチ上の `on: push` + `environment: production` の + ワークフロー — 要件 2 でブロック。policy が無くても + `event_name == 'workflow_run'` で拒否される。 +4. **フィーチャーブランチで改変した `cd.yml`** — `workflow_run` は常に + `main` の定義を実行するため、改変は特権コンテキストでは動かない。改変版を + 直接トリガーしても `event_name` / `workflow_ref` で拒否される。 +5. 同じ org の**フリート外リポ**のワークフローが、未作成の `production` + environment を宣言する(environment は初回使用時に自動作成される)。 + これこそ要件 3 が塞ぐバイパス: org 全体の PR 必須ルールセットがあれば + レビュー無しで `main` に `cd.yml` を置けない。要件 3 が無いとこのケースは + **成功してしまう** — その場合は検証失敗として扱うこと。 + +GitLab 版: 非保護 ref 上のジョブ(`environment` の有無を問わず)は +`ref_protected` で失敗し、保護 ref 上でも `environment:` 無しのジョブは +`environment` 条件で失敗すること。 + ## 一度きりの組織セットアップ コマンドはすべて中央 GCP プロジェクト(Cloud Logging / `clasp run` と同じもの) @@ -114,6 +257,10 @@ gcloud iam workload-identity-pools providers create-oidc github \ --attribute-condition="assertion.repository_owner == '${ORG}'" ``` +> この org 限定のみの条件は **Permissive モード**向けです。CI-only モードでは +> [CI-only モード: 必須要件](#ci-only-モード-必須要件)の、より厳しい条件を +> 使ってください。 + ### 3b. GitLab プロバイダ ```bash @@ -128,7 +275,10 @@ gcloud iam workload-identity-pools providers create-oidc gitlab \ `--allowed-audiences` は CI の JWT の `aud` と一致する必要があります。テンプレは `$CI_SERVER_URL`(スキーム込み・末尾スラッシュ無し)を設定します。 -### 4. CI へのアクセス付与(org/group 単位) +### 4. CI へのアクセス付与(org/group 単位 — Permissive モード) + +CI-only モードでは代わりに `environment` 属性へ付与します — +[CI-only モード: 必須要件](#ci-only-モード-必須要件)を参照。 secret リソースではなく、**プロジェクトレベル**で付与します: @@ -158,7 +308,20 @@ gcloud projects add-iam-policy-binding "$PROJECT_ID" \ > 直後の初回 CI 実行での `PERMISSION_DENIED` は「待ってリトライ」で解決する > ことが多いです。 -### 5. 開発者へのアクセス付与 +### 5. 人間へのアクセス付与 + +**CI-only モード(推奨)**: 開発者グループには付与**しません**。`init.sh` と +ローテーションを担う管理者のみに、個人単位か少人数の管理者グループで付与 +します: + +```bash +gcloud secrets add-iam-policy-binding clasp-credentials \ + --role=roles/secretmanager.secretAccessor \ + --member="user:fleet-admin@yourcompany.com" # または group:apps-script-admins@… +``` + +**Permissive モード**: 開発者グループに付与します — 全員が共有 ID で手元から +デプロイできるようになります: ```bash gcloud secrets add-iam-policy-binding clasp-credentials \ @@ -223,6 +386,10 @@ Cloud Logging に記録されます — `attribute.repository`(GitHub)/ ## 開発者のアクセス +**Permissive モード**(と CI-only モードの管理者)向けです。CI-only モード +では開発者は共有トークンを取得しません — 日常の開発は個人 sandbox で行います: +`./scripts/init-sandbox.sh`(README 参照)。 + ```bash gcloud auth login # 個人アカウント、マシンごとに1回 ./scripts/fetch-clasp-credentials.sh diff --git a/docs/secret-manager.md b/docs/secret-manager.md index f675cf9..cf24c23 100644 --- a/docs/secret-manager.md +++ b/docs/secret-manager.md @@ -43,6 +43,151 @@ audit logs record `attribute.repository` / `project_path` per access. - `GCP_WIF_PROVIDER` unset → legacy `CLASPRC_JSON` (kept for air-gapped GitLab). - Neither → explicit error. +## Deploy Identity Modes: CI-only vs Permissive + +The fleet has exactly one security boundary: **who can read the shared token** +(IAM on `roles/secretmanager.secretAccessor`). Choose one of two modes +explicitly — both are first-class, supported configurations: + +| | **CI-only (recommended)** | **Permissive** | +| --- | --- | --- | +| Who can read the secret | **GitHub**: only deploy-environment jobs executed from `cd.yml` on `main` (started via `workflow_run`). **GitLab**: only deploy-environment jobs on protected refs. | Every workflow in the org/group, plus the developer Google group. | +| Local deploy with the shared identity | Impossible. Develop against a personal sandbox: `./scripts/init-sandbox.sh` (see README). | `./scripts/fetch-clasp-credentials.sh`, then clasp as usual. | +| Review bypass | Deploys happen only through the reviewed `cd.yml` after a branch-protected merge. | A developer can deploy anything from their machine, bypassing CI and review. | +| Audit trail | "Who deployed what" = PR / pipeline history. Secret access logs contain only CI principals (per repo + per environment). | Secret Manager logs the fetch, but what the token does locally afterwards is invisible. | +| Emergency deploys | Also through CI (revert / roll-forward merge). | Possible locally. | +| Setup | Base setup (§1–7) **plus the four required controls below**. WIF is mandatory. | Base setup (§1–7) as written. | + +Permissive is this template's historical default and remains supported — a +small team whose developers are all effectively admins may reasonably keep it. +Pick CI-only whenever "production deploys go through review" is meant to be a +**guarantee** rather than a convention. + +### CI-only mode: required controls + +All four are required — each closes a real bypass. (1) replaces the §3a/§3b +provider conditions and the §4 grants; (3) and (4) are organizational +prerequisites, not gcloud commands. + +#### 1. Restrict the WIF provider to the reviewed CD workflow + +GitHub. A `workflow_run`-started job always executes the `cd.yml` from the +default branch, so pinning `workflow_ref` to `main` means "the reviewed +definition"; the `environment` claim narrows it to the deploy jobs: + +```bash +gcloud iam workload-identity-pools providers update-oidc github \ + --location=global --workload-identity-pool=apps-script-fleet \ + --attribute-mapping="google.subject=assertion.sub,attribute.repository=assertion.repository,attribute.repository_owner=assertion.repository_owner,attribute.environment=assertion.environment" \ + --attribute-condition="assertion.repository_owner == '${ORG}' && assertion.event_name == 'workflow_run' && assertion.workflow_ref.endsWith('/.github/workflows/cd.yml@refs/heads/main') && assertion.environment in ['development', 'production']" +``` + +GitLab. There is no `workflow_run` equivalent; the reviewed-definition +guarantee comes from `ref_protected` (protected refs only accept reviewed +merges when combined with control 3) plus the `environment` claim: + +```bash +gcloud iam workload-identity-pools providers update-oidc gitlab \ + --location=global --workload-identity-pool=apps-script-fleet \ + --attribute-mapping="google.subject=assertion.sub,attribute.project_path=assertion.project_path,attribute.namespace_path=assertion.namespace_path,attribute.environment=assertion.environment" \ + --attribute-condition="(assertion.namespace_path == '${GROUP}' || assertion.namespace_path.startsWith('${GROUP}/')) && assertion.ref_protected == 'true' && assertion.environment in ['development', 'production']" +``` + +Grant `secretAccessor` on the `environment` attribute instead of the org/group +attribute (replaces the §4 bindings — remove those if present): + +```bash +POOL="projects/${PROJECT_NUMBER}/locations/global/workloadIdentityPools/apps-script-fleet" +for e in development production; do + gcloud projects add-iam-policy-binding "$PROJECT_ID" \ + --role=roles/secretmanager.secretAccessor --condition=None \ + --member="principalSet://iam.googleapis.com/${POOL}/attribute.environment/${e}" +done +``` + +The attribute condition already filters every token the pool issues, so the +environment-scoped grant is defense in depth: a later provider +misconfiguration alone does not silently re-open org-wide access. + +#### 2. Deployment branch policies on both environments + +Without a branch policy, a workflow on **any** branch can claim +`environment: production` and mint an OIDC token carrying that claim. + +- **GitHub**: `workflow_run`-started jobs are evaluated against the default + branch, so allow **`main` as the only deployment branch for both** + `development` and `production` (dev vs prod is distinguished inside `cd.yml` + by `head_branch` and the environment name — current behaviour, unchanged). +- **GitLab**: protected environments, as usual. + +On GitHub, `./scripts/configure-environments.sh --branch-policy` sets `main` +as the only deployment branch for both environments. On GitLab the flag is a +no-op; restrict deployments with protected branches (`dev`/`main`) and +protected environments — the script creates the latter when +`--development-reviewers` / `--production-reviewers` are passed (GitLab +Premium). + +#### 3. Branch protection for `dev` and `main`, org-wide + +The WIF provider trusts the whole org/group. Without org-wide protection, any +repo in it — fleet or not — could direct-push a `cd.yml` to its own `main` +and satisfy control 1. Enforce PR-required protection for `dev` and `main` +via **org-wide rulesets** (GitHub) / group-level protected branches (GitLab). +If org-wide rulesets are not feasible, narrow the provider trust to a +fleet-only org/group instead (`assertion.repository_owner == ''`). + +Note that `cd.yml` checks out the `head_branch` code, so what protects the +**deployed code** on dev is `dev`'s own branch protection. + +#### 4. WIF is mandatory + +The legacy `CLASPRC_JSON` org secret is readable by any workflow in any repo +of the org and cannot express any of the conditions above. CI-only mode +therefore requires the Secret Manager + WIF path. Keep the legacy path only +for air-gapped Permissive setups. + +> **To be field-verified** (tracked in the template repo, issue #193): the +> `workflow_ref` / `event_name` / `environment` claims of a +> `workflow_run`-started job and the deployment-branch-policy evaluation ref +> (`main`) match GitHub's documented behaviour but have not yet been confirmed +> against live tokens. Run the verification below before relying on the +> boundary. + +### Verifying the CI-only boundary + +**Must succeed** — the existing `cd.yml`, started via `workflow_run`: + +1. Merge any change to `dev` → CI → the CD `development` job fetches the + secret and deploys. +2. Merge `dev` into `main` → the CD `production` job fetches the secret and + deploys. + +**Must fail** — each of these must be rejected (STS 403 "rejected by the +attribute condition", or blocked before a token exists): + +1. A workflow on a **feature branch** of a fleet repo calling + `google-github-actions/auth` with the fleet provider (no `environment`). +2. The same workflow claiming `environment: development` / `production` — + blocked by the deployment branch policy (control 2) before any token is + issued. +3. An `on: push` workflow on a feature branch with `environment: production` + — blocked by control 2, and by `event_name == 'workflow_run'` even if the + policy were missing. +4. A **modified `cd.yml` on a feature branch** — `workflow_run` always + executes the `main` definition, so the modification never runs in the + privileged context; triggering the modified copy directly fails + `event_name` / `workflow_ref`. +5. A workflow in a repo **outside the fleet** (same org) claiming a + not-yet-created `production` environment (environments auto-create on + first use). This is exactly the bypass control 3 closes: with org-wide + PR-required rulesets the attacker cannot get a `cd.yml` onto `main` + without review; without control 3, this case **succeeds** — treat that as + a failed verification. + +GitLab mirror: a job on an unprotected ref (any `environment`) must fail +`ref_protected`; a job on a protected ref without `environment:` must fail the +`environment` condition. + ## One-Time Organization Setup All commands run against the central GCP project (the same one used for Cloud @@ -114,6 +259,10 @@ gcloud iam workload-identity-pools providers create-oidc github \ --attribute-condition="assertion.repository_owner == '${ORG}'" ``` +> This org-only condition is the **Permissive-mode** form. In CI-only mode use +> the stricter condition from +> [CI-only mode: required controls](#ci-only-mode-required-controls) instead. + ### 3b. GitLab provider ```bash @@ -128,7 +277,10 @@ gcloud iam workload-identity-pools providers create-oidc gitlab \ `--allowed-audiences` must equal the `aud` of the CI JWT, which the template sets to `$CI_SERVER_URL` (scheme included, no trailing slash). -### 4. Grant CI access (org/group-wide) +### 4. Grant CI access (org/group-wide — Permissive mode) + +In CI-only mode, grant on the `environment` attribute instead — see +[CI-only mode: required controls](#ci-only-mode-required-controls). Grant at the **project level**, not on the secret resource: @@ -157,7 +309,20 @@ gcloud projects add-iam-policy-binding "$PROJECT_ID" \ > propagate. A `PERMISSION_DENIED` on the very first CI run right after this > step usually just means "wait and retry". -### 5. Grant developer access +### 5. Grant human access + +**CI-only mode (recommended)**: do **not** grant the developer group. Grant +only the admins who run `init.sh` and rotations — individually, or via a small +admin group: + +```bash +gcloud secrets add-iam-policy-binding clasp-credentials \ + --role=roles/secretmanager.secretAccessor \ + --member="user:fleet-admin@yourcompany.com" # or group:apps-script-admins@… +``` + +**Permissive mode**: grant the developer group — every member can then deploy +locally with the shared identity: ```bash gcloud secrets add-iam-policy-binding clasp-credentials \ @@ -222,6 +387,10 @@ org-wide grant. ## Developer Access +Applies to **Permissive mode** (and to admins in CI-only mode). In CI-only +mode, developers never fetch the shared token — day-to-day development uses a +personal sandbox instead: `./scripts/init-sandbox.sh` (see README). + ```bash gcloud auth login # personal account, once per machine ./scripts/fetch-clasp-credentials.sh diff --git a/scripts/fetch-clasp-credentials.sh b/scripts/fetch-clasp-credentials.sh index 83d19f2..ad46678 100755 --- a/scripts/fetch-clasp-credentials.sh +++ b/scripts/fetch-clasp-credentials.sh @@ -3,6 +3,10 @@ set -euo pipefail # Apps Script Fleet — fetch shared clasp credentials from Secret Manager. # +# FOR FLEET ADMINS and Permissive-mode fleets. In CI-only mode (recommended — +# see docs/secret-manager.md "Deploy Identity Modes") developers have no +# access to this secret; use ./scripts/init-sandbox.sh for local development. +# # Writes the fleet's shared ~/.clasprc.json (clasp OAuth token) from Google # Cloud Secret Manager using your personal gcloud credentials. Replaces the # legacy "copy from the org password manager" flow. @@ -10,7 +14,7 @@ set -euo pipefail # ~/.clasprc.json lives in $HOME, so one fetch covers every repo on this # machine. Access is governed by IAM (roles/secretmanager.secretAccessor). # -# Usage: +# Usage (admin / Permissive mode): # ./scripts/fetch-clasp-credentials.sh [--project ] [--secret ] [--force] # # Project resolution order: @@ -78,6 +82,8 @@ if [[ -z "$PROJECT" ]]; then fi [[ -n "$PROJECT" ]] || die "Cannot resolve the GCP project. Pass --project , set \$CLASP_SECRET_PROJECT, or run from a repo whose .clasp-dev.json has a projectId." +echo "Note: this fetches the fleet's SHARED deploy identity token (admin / Permissive-mode use)." >&2 +echo " For day-to-day development, prefer a personal sandbox: ./scripts/init-sandbox.sh" >&2 echo "Fetching secret '${SECRET_NAME}' from project '${PROJECT}' as ${active_account}..." umask 077 tmp=$(mktemp "${CLASPRC}.XXXXXX")