From 547d46363bf536d8199610861cbf1149f9311392 Mon Sep 17 00:00:00 2001 From: Irving Popovetsky Date: Mon, 23 Nov 2026 10:08:54 -0800 Subject: [PATCH 1/3] CI: require maintainer approval for Docker image pushes PRs (same-repo, fork, Dependabot) now build the image only, with no AWS credentials. The push job runs only on push to master (environment ecr-main, master branch only) and on dispatch or re-run (environment ecr-approval, maintainer reviewers). github.ref no longer goes straight into shell code. Branch and PR pushes no longer publish :staging automatically. A maintainer dispatches and approves instead. .github/SETUP.md gives the new role GitHubActions-ECR-Push-pybot, its trust (two exact immutable environment subjects) and ECR policy, the environment rules, and the safe switch-over order. Co-Authored-By: Claude Opus 5.5 --- .github/SETUP.md | 226 +++++++++++++++++++++++++++++++++++++++ .github/workflows/ci.yml | 56 ++++++---- 2 files changed, 263 insertions(+), 19 deletions(-) create mode 100644 .github/SETUP.md diff --git a/.github/SETUP.md b/.github/SETUP.md new file mode 100644 index 0000000..400d9f3 --- /dev/null +++ b/.github/SETUP.md @@ -0,0 +1,226 @@ +# GitHub Actions Docker Push Setup + +This guide gives the setup that CI must have to push Docker images to ECR. It also gives the security rules for that setup. + +## How pushes are gated + +Only maintainers can cause CI to push an image. The workflow file does not enforce this, because a writer can change the file on a branch. GitHub Environments and the AWS trust policy enforce it. + +``` +pull_request (any author) --> docker-build build only, no AWS credentials +push to master, first attempt --> docker-build-push environment ecr-main no wait +workflow_dispatch (any ref) --> docker-build-push environment ecr-approval maintainer approves +re-run of any push run --> docker-build-push environment ecr-approval maintainer approves + | + v + AWS role GitHubActions-ECR-Push-pybot trusts ONLY the ecr-main and ecr-approval subjects +``` + +- `ecr-main` can skip the wait because the org ruleset on `master` requires a pull request with code-owner review. +- Image tags: `:prod` and `:latest` when the ref is `master`, and `:staging` for all other refs. All tags are in the same ECR repository, `pybot`. +- Pull requests, Dependabot and forks included, never get AWS credentials. Branch and PR pushes do not publish `:staging`. A maintainer pushes `:staging` with a dispatch (see [Push a Staging Image](#push-a-staging-image-maintainers)). + +## Required Secret + +- **`AWS_ROLE_ARN`**: the ARN of the IAM role that the push job assumes: `arn:aws:iam::633607774026:role/GitHubActions-ECR-Push-pybot`. + +```bash +gh secret set AWS_ROLE_ARN --repo OperationCode/operationcode-pybot --body "arn:aws:iam::633607774026:role/GitHubActions-ECR-Push-pybot" +``` + +## Required Environments + +Create the two environments in Settings > Environments. Create them **before** a workflow uses them. If a workflow uses an environment that does not exist, GitHub creates it with no protection rules. + +| Environment | Required reviewers | Deployment branches and tags | +|---|---|---| +| `ecr-main` | None | "Selected branches and tags", with one **branch** rule: `master`. Do not add a tag rule. Do not use "Protected branches only". | +| `ecr-approval` | The code owner `irvingpop` and the maintainers | No restriction | + +Get the reviewers from GitHub. Do not copy them from an old list: + +```bash +# Code owners +gh api repos/OperationCode/operationcode-pybot/contents/.github/CODEOWNERS --jq .content | base64 -d +# Maintainers: users with the admin or maintain role on the repository +gh api repos/OperationCode/operationcode-pybot/collaborators --paginate \ + --jq '.[] | select(.role_name == "admin" or .role_name == "maintain") | .login' +``` + +Do not add a team that has only write access (for example `contributors` or `core-contributors`) as a reviewer. + +Check the environments after you create them: + +```bash +gh api repos/OperationCode/operationcode-pybot/environments \ + --jq '.environments[] | {name, protection_rules, deployment_branch_policy}' +gh api repos/OperationCode/operationcode-pybot/environments/ecr-main/deployment-branch-policies \ + --jq '.branch_policies[] | {name, type}' +# Expect exactly one policy: {"name":"master","type":"branch"} +``` + +The old environments `oc-pybot-production` and `oc-pybot-staging` are not used by any workflow. The AWS role does not trust them. + +## AWS Role `GitHubActions-ECR-Push-pybot` + +Pybot has its own role. The shared role `GitHubActions-ECR-Push` is for `back-end` only, and must not trust pybot or allow `repository/pybot`. + +### Trust policy + +The repository uses immutable OIDC subjects. To get the prefix, run this command: + +```bash +gh api repos/OperationCode/operationcode-pybot/actions/oidc/customization/sub --jq .sub_claim_prefix +# repo:OperationCode@10479364/operationcode-pybot@146210805 +``` + +The trust must use `StringEquals` and these two subjects only: + +```json +{ + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Principal": { + "Federated": "arn:aws:iam::633607774026:oidc-provider/token.actions.githubusercontent.com" + }, + "Action": "sts:AssumeRoleWithWebIdentity", + "Condition": { + "StringEquals": { + "token.actions.githubusercontent.com:aud": "sts.amazonaws.com", + "token.actions.githubusercontent.com:sub": [ + "repo:OperationCode@10479364/operationcode-pybot@146210805:environment:ecr-main", + "repo:OperationCode@10479364/operationcode-pybot@146210805:environment:ecr-approval" + ] + } + } + } + ] +} +``` + +Rules: +- Do not use `StringLike` or `*`. +- Do not add `:pull_request` or `:ref:refs/heads/...` subjects. A writer can get those subjects without approval. +- The role must have only this one statement. Examine it with this command: + `aws iam get-role --role-name GitHubActions-ECR-Push-pybot --query Role.AssumeRolePolicyDocument` + +### Permission policy + +ECR push to `repository/pybot` only. `GetAuthorizationToken` does not support a resource, so it has its own statement. + +```json +{ + "Version": "2012-10-17", + "Statement": [ + { + "Sid": "PushPybot", + "Effect": "Allow", + "Action": [ + "ecr:BatchCheckLayerAvailability", + "ecr:BatchGetImage", + "ecr:GetDownloadUrlForLayer", + "ecr:InitiateLayerUpload", + "ecr:UploadLayerPart", + "ecr:CompleteLayerUpload", + "ecr:PutImage" + ], + "Resource": "arn:aws:ecr:us-east-2:633607774026:repository/pybot" + }, + { + "Sid": "EcrLogin", + "Effect": "Allow", + "Action": "ecr:GetAuthorizationToken", + "Resource": "*" + } + ] +} +``` + +## Safe Switch-over + +Use this procedure to move from the shared role to `GitHubActions-ECR-Push-pybot`. It makes sure that no unapproved credential path stays open during the change. + +1. Create the two environments, as given in [Required Environments](#required-environments). +2. Create the role `GitHubActions-ECR-Push-pybot` with the trust and permission policies above. +3. Get the pull request that adds the environments to `ci.yml` approved and green. Do not merge it yet. +4. Set the secret `AWS_ROLE_ARN` to the new role, as given in [Required Secret](#required-secret). +5. Remove pybot from the shared role `GitHubActions-ECR-Push`, in one change for each policy: + - Trust: remove every `operationcode-pybot` subject (all spellings, the name form and the ID form). If the back-end switch-over is already done, the trust has only the back-end environment subjects. Examine it: + `aws iam get-role --role-name GitHubActions-ECR-Push --query Role.AssumeRolePolicyDocument` + - Permission: remove `repository/pybot` from `GitHubActions-ECR-Push-Policy`. + + Write down the time of this change (T). After T, the shared role cannot issue a new session to pybot CI. +6. Cancel every pybot CI run that is in progress or queued. These runs can hold credentials from before T. + ```bash + # List the runs, then cancel each one. + for s in in_progress queued; do + gh run list --repo OperationCode/operationcode-pybot --status $s --limit 100 --json databaseId --jq '.[].databaseId' + done | xargs -n 1 gh run cancel --repo OperationCode/operationcode-pybot + ``` + Run the list again until it is empty. +7. Revoke the sessions of the shared role: IAM > Roles > `GitHubActions-ECR-Push` > "Revoke active sessions". Write down the time of this revoke (R). This also stops running back-end push jobs. Do it when no back-end push is running. +8. Immediately merge the pull request. The push to `master` that the merge causes uses `ecr-main` and pushes `:prod` and `:latest`. + - Between step 4 and step 8, every push run of the old workflow fails at "Configure AWS credentials". Nobody must push to `master` during this time. + - If the first push fails, correct the trust subject (see the rules above). Then re-run the job. The re-run uses `ecr-approval`, so a maintainer must approve it. + - When the push run is complete, write down the image digest of `:prod`. You compare with it in step 9: + `aws ecr describe-images --region us-east-2 --repository-name pybot --image-ids imageTag=prod --query 'imageDetails[0].imageDigest' --output text` +9. Check for ECR login tokens from before R. A session issued before T can call ECR `GetAuthorizationToken` until the revoke at R. An ECR login token stays valid for 12 hours, and AWS does not document that "Revoke active sessions" stops it. Thus such a token can push until R + 12 h. An unapproved push during this window is detected, not prevented. When you follow these steps, you accept this risk. + - At R + 12 h plus approximately 30 minutes (CloudTrail can deliver events late), list the ECR write events from T - 12 h to R + 12 h: + ```bash + for e in InitiateLayerUpload PutImage BatchDeleteImage; do + aws cloudtrail lookup-events --region us-east-2 \ + --lookup-attributes AttributeKey=EventName,AttributeValue=$e \ + --start-time --end-time --output json | + jq -r '.Events[] | (.CloudTrailEvent | fromjson) as $c + | [.EventTime, .EventName, .Username, + ($c.userIdentity.sessionContext.sessionIssuer.arn // "-"), + ($c.requestParameters.repositoryName // "-"), + ($c.sourceIPAddress // "-")] | @tsv' + done + ``` + The columns are: time, event, `Username`, session issuer ARN, ECR repository, source IP address. Use the session issuer ARN to find the events of the CI roles (`role/GitHubActions-ECR-Push` and `role/GitHubActions-ECR-Push-pybot`). Do not use `Username` for this: for a role session it is the session name, and the caller chooses it. Each event of a CI role on the `pybot` repository must match a pybot CI run that you know about (time and run). + - The `PutImage` events are the real control. The tag check below shows only the current state of each tag. + - At the same time, make sure that the `:prod`, `:latest` and `:staging` digests are the digests that known CI runs pushed. For `:prod` and `:latest`, this is the digest from step 8, unless a later known run pushed them: + ```bash + for t in prod latest staging; do + aws ecr describe-images --region us-east-2 --repository-name pybot \ + --image-ids imageTag=$t --query 'imageDetails[].[imageTags,imageDigest,imagePushedAt]' + done + ``` + An `ImageNotFoundException` for a tag means that the tag does not exist. This is not a failure of the check. + - If an event or a digest does not match, push a known-good image again (re-run the master build with an `ecr-approval` approval) and investigate. + +## Push a Staging Image (maintainers) + +Pull requests do not push images. To push `:staging` from a branch: + +1. Go to Actions > CI > "Run workflow". Select the branch. You can also run `gh workflow run ci.yml --repo OperationCode/operationcode-pybot --ref `. +2. The `Build and Push Docker Image` job waits for the `ecr-approval` review. +3. Before you approve, read the commit **and** `.github/workflows/ci.yml` on that branch. The approved job runs with the branch's workflow file. Its credentials can also write `:prod` and `:latest`. +4. Approve the deployment. The job pushes `pybot:staging`. + +A dispatch on `master` pushes `:prod` and `:latest`, and it also needs approval. + +## Troubleshooting + +### Build fails with "Error assuming role" +- Make sure that the `AWS_ROLE_ARN` secret is the ARN of `GitHubActions-ECR-Push-pybot`. +- Make sure that the job ran in `ecr-main` or `ecr-approval` (the job page shows the environment). +- Compare the trust subjects with the prefix from `gh api .../actions/oidc/customization/sub`. The subject is `:environment:`. + +### Build fails with "AccessDenied" to ECR +- Make sure that the role has the permission policy above. +- Make sure that the repository is `633607774026.dkr.ecr.us-east-2.amazonaws.com/pybot`. + +### Production build runs even when tests fail +- This must not occur. The push job depends on the `ci-success` job. +- Make sure that `ci-success` fails when lint or tests fail. + +## Additional Resources + +- [GitHub Actions secrets](https://docs.github.com/en/actions/security-guides/encrypted-secrets) +- [GitHub deployment environments](https://docs.github.com/en/actions/reference/workflows-and-actions/deployments-and-environments) +- [GitHub OIDC reference](https://docs.github.com/en/actions/reference/security/oidc) +- [AWS OIDC with GitHub](https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-amazon-web-services) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f734052..7455e07 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -141,13 +141,45 @@ jobs: - name: Display Bandit results run: poetry run bandit -r pybot -x pybot/_vendor --skip B101 -f txt || true + # Every PR (same-repo, fork, Dependabot): build only, no AWS credentials + docker-build: + name: Build Docker Image + runs-on: ubuntu-24.04-arm + if: github.event_name == 'pull_request' + needs: [ci-success] + permissions: + contents: read + steps: + - name: Checkout code + uses: actions/checkout@v7 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v4 + with: + platforms: linux/arm64 + + - name: Build Docker image + uses: docker/build-push-action@v7 + with: + context: . + file: docker/Dockerfile + target: prod + platforms: linux/arm64 + push: false + provenance: false + cache-from: type=gha,scope=arm64 + cache-to: type=gha,mode=max,scope=arm64 + + # Push to master and manual dispatch only. The environment's protection rules, + # not this file, are the gate: the AWS role must trust only these environments. docker-build-push: name: Build and Push Docker Image runs-on: ubuntu-24.04-arm - # Run on push to master (build+push), manual dispatch (build+push) and on PRs (build only) - if: github.event_name == 'push' || github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch' - # Wait for CI checks to pass + if: github.event_name == 'push' || github.event_name == 'workflow_dispatch' needs: [ci-success] + # First attempt of a push to master: reviewed by a code owner before merge, so no wait. + # Dispatch and re-runs (any ref, any writer): a maintainer must approve. + environment: ${{ (github.event_name == 'push' && github.ref == 'refs/heads/master' && github.run_attempt == '1') && 'ecr-main' || 'ecr-approval' }} permissions: id-token: write # Required for OIDC authentication contents: read # Required to checkout code @@ -155,22 +187,11 @@ jobs: - name: Checkout code uses: actions/checkout@v7 - - name: Determine push eligibility - id: can-push - run: | - if [ "${{ github.event_name }}" == "push" ] || [ "${{ github.event_name }}" == "workflow_dispatch" ]; then - echo "push=true" >> "$GITHUB_OUTPUT" - elif [ "${{ github.event.pull_request.head.repo.full_name }}" == "${{ github.repository }}" ]; then - echo "push=true" >> "$GITHUB_OUTPUT" - else - echo "push=false" >> "$GITHUB_OUTPUT" - fi - - name: Determine Docker tags id: docker-tag run: | ECR=633607774026.dkr.ecr.us-east-2.amazonaws.com/pybot - if [ "${{ github.ref }}" == "refs/heads/master" ]; then + if [ "$GITHUB_REF" == "refs/heads/master" ]; then echo "images=${ECR}:prod,${ECR}:latest" >> "$GITHUB_OUTPUT" echo "environment=Production" >> "$GITHUB_OUTPUT" else @@ -184,7 +205,6 @@ jobs: platforms: linux/arm64 - name: Configure AWS credentials - if: steps.can-push.outputs.push == 'true' uses: aws-actions/configure-aws-credentials@v6 with: role-to-assume: ${{ secrets.AWS_ROLE_ARN }} @@ -193,7 +213,6 @@ jobs: - name: Login to Amazon ECR id: login-ecr - if: steps.can-push.outputs.push == 'true' uses: aws-actions/amazon-ecr-login@v2 - name: Build and push Docker image @@ -203,7 +222,7 @@ jobs: file: docker/Dockerfile target: prod platforms: linux/arm64 - push: ${{ steps.can-push.outputs.push == 'true' }} + push: true tags: ${{ steps.docker-tag.outputs.images }} provenance: false no-cache: ${{ inputs.no_cache == true }} @@ -212,7 +231,6 @@ jobs: cache-to: type=gha,mode=max,scope=arm64 - name: Output image URI - if: steps.can-push.outputs.push == 'true' run: | echo "Successfully pushed ${{ steps.docker-tag.outputs.environment }} images:" echo "${{ steps.docker-tag.outputs.images }}" From 8be03c0cb222b6db9b9250fba4633a04106f531e Mon Sep 17 00:00:00 2001 From: Irving Popovetsky Date: Mon, 23 Nov 2026 10:13:57 -0800 Subject: [PATCH 2/3] SETUP.md: follow the shared-role contract, set ecr-approval reviewers Pybot no longer changes the old shared role GitHubActions-ECR-Push: no trust edit, no permission edit, no session revoke. The guide says the old role is not gated and stays open to branch workflows of both repos until it is deleted. Back-end SETUP.md owns that deletion and the CloudTrail check of both ECR repos; this guide only points to it. The move now ends with a check of one master push through the new role (CloudTrail AssumeRoleWithWebIdentity or the run log). The new role uses an inline policy and create commands, as in back-end. ecr-approval reviewers are irvingpop and kylemh, the owner's choice. Co-Authored-By: Claude Opus 5.5 --- .github/SETUP.md | 143 ++++++++++++++++++++++++----------------------- 1 file changed, 74 insertions(+), 69 deletions(-) diff --git a/.github/SETUP.md b/.github/SETUP.md index 400d9f3..d9c84e5 100644 --- a/.github/SETUP.md +++ b/.github/SETUP.md @@ -4,7 +4,7 @@ This guide gives the setup that CI must have to push Docker images to ECR. It al ## How pushes are gated -Only maintainers can cause CI to push an image. The workflow file does not enforce this, because a writer can change the file on a branch. GitHub Environments and the AWS trust policy enforce it. +Only maintainers can cause CI to push an image through the pybot role. The workflow file does not enforce this, because a writer can change the file on a branch. GitHub Environments and the AWS trust policy enforce it. ``` pull_request (any author) --> docker-build build only, no AWS credentials @@ -13,16 +13,17 @@ workflow_dispatch (any ref) --> docker-build-push environment ecr-approval re-run of any push run --> docker-build-push environment ecr-approval maintainer approves | v - AWS role GitHubActions-ECR-Push-pybot trusts ONLY the ecr-main and ecr-approval subjects + role GitHubActions-ECR-Push-pybot trusts ONLY the ecr-main and ecr-approval subjects ``` - `ecr-main` can skip the wait because the org ruleset on `master` requires a pull request with code-owner review. - Image tags: `:prod` and `:latest` when the ref is `master`, and `:staging` for all other refs. All tags are in the same ECR repository, `pybot`. - Pull requests, Dependabot and forks included, never get AWS credentials. Branch and PR pushes do not publish `:staging`. A maintainer pushes `:staging` with a dispatch (see [Push a Staging Image](#push-a-staging-image-maintainers)). +- The gate is not complete until the old shared role is deleted. See [The Old Shared Role](#the-old-shared-role). ## Required Secret -- **`AWS_ROLE_ARN`**: the ARN of the IAM role that the push job assumes: `arn:aws:iam::633607774026:role/GitHubActions-ECR-Push-pybot`. +- **`AWS_ROLE_ARN`** (repository secret): the ARN of the IAM role that the push job assumes. ```bash gh secret set AWS_ROLE_ARN --repo OperationCode/operationcode-pybot --body "arn:aws:iam::633607774026:role/GitHubActions-ECR-Push-pybot" @@ -35,19 +36,10 @@ Create the two environments in Settings > Environments. Create them **before** a | Environment | Required reviewers | Deployment branches and tags | |---|---|---| | `ecr-main` | None | "Selected branches and tags", with one **branch** rule: `master`. Do not add a tag rule. Do not use "Protected branches only". | -| `ecr-approval` | The code owner `irvingpop` and the maintainers | No restriction | +| `ecr-approval` | `irvingpop` and `kylemh` | No restriction | -Get the reviewers from GitHub. Do not copy them from an old list: - -```bash -# Code owners -gh api repos/OperationCode/operationcode-pybot/contents/.github/CODEOWNERS --jq .content | base64 -d -# Maintainers: users with the admin or maintain role on the repository -gh api repos/OperationCode/operationcode-pybot/collaborators --paginate \ - --jq '.[] | select(.role_name == "admin" or .role_name == "maintain") | .login' -``` - -Do not add a team that has only write access (for example `contributors` or `core-contributors`) as a reviewer. +- The owner chose the `ecr-approval` reviewers. The owner can change them later in Settings > Environments > `ecr-approval`. GitHub allows at most 6 users or teams. Add only maintainers. Do not add a team that has only write access (for example `contributors` or `core-contributors`). +- One approval from a reviewer is sufficient. Check the environments after you create them: @@ -59,11 +51,11 @@ gh api repos/OperationCode/operationcode-pybot/environments/ecr-main/deployment- # Expect exactly one policy: {"name":"master","type":"branch"} ``` -The old environments `oc-pybot-production` and `oc-pybot-staging` are not used by any workflow. The AWS role does not trust them. +The old environments `oc-pybot-production` and `oc-pybot-staging` are not used by any workflow, and no role trusts them. -## AWS Role `GitHubActions-ECR-Push-pybot` +## AWS Role -Pybot has its own role. The shared role `GitHubActions-ECR-Push` is for `back-end` only, and must not trust pybot or allow `repository/pybot`. +The pybot repository has its own role, `GitHubActions-ECR-Push-pybot`. No other repository uses it. ### Trust policy @@ -74,7 +66,7 @@ gh api repos/OperationCode/operationcode-pybot/actions/oidc/customization/sub -- # repo:OperationCode@10479364/operationcode-pybot@146210805 ``` -The trust must use `StringEquals` and these two subjects only: +Save this as `pybot-trust.json`: ```json { @@ -101,26 +93,26 @@ The trust must use `StringEquals` and these two subjects only: ``` Rules: -- Do not use `StringLike` or `*`. +- Use `StringEquals` and these two subjects only. Do not use `StringLike` or `*`. - Do not add `:pull_request` or `:ref:refs/heads/...` subjects. A writer can get those subjects without approval. -- The role must have only this one statement. Examine it with this command: +- Do not add subjects of another repository. Each repository gets its own role. +- The role must have only this one `Allow` statement. Examine all statements with this command: `aws iam get-role --role-name GitHubActions-ECR-Push-pybot --query Role.AssumeRolePolicyDocument` -### Permission policy +### Permissions policy (inline) -ECR push to `repository/pybot` only. `GetAuthorizationToken` does not support a resource, so it has its own statement. +Save this as `pybot-ecr-push.json`. The first statement has the ECR actions of the push job, on the `pybot` repository only. `ecr:GetAuthorizationToken` (used by `amazon-ecr-login`) cannot be limited to a repository, so it has its own statement on `*`. ```json { "Version": "2012-10-17", "Statement": [ { - "Sid": "PushPybot", "Effect": "Allow", "Action": [ "ecr:BatchCheckLayerAvailability", - "ecr:BatchGetImage", "ecr:GetDownloadUrlForLayer", + "ecr:BatchGetImage", "ecr:InitiateLayerUpload", "ecr:UploadLayerPart", "ecr:CompleteLayerUpload", @@ -129,7 +121,6 @@ ECR push to `repository/pybot` only. `GetAuthorizationToken` does not support a "Resource": "arn:aws:ecr:us-east-2:633607774026:repository/pybot" }, { - "Sid": "EcrLogin", "Effect": "Allow", "Action": "ecr:GetAuthorizationToken", "Resource": "*" @@ -138,59 +129,73 @@ ECR push to `repository/pybot` only. `GetAuthorizationToken` does not support a } ``` -## Safe Switch-over +### Create the role + +```bash +aws iam create-role \ + --role-name GitHubActions-ECR-Push-pybot \ + --assume-role-policy-document file://pybot-trust.json \ + --max-session-duration 3600 \ + --description "GitHub Actions OperationCode/operationcode-pybot: push to ECR repository pybot" + +aws iam put-role-policy \ + --role-name GitHubActions-ECR-Push-pybot \ + --policy-name ecr-push-pybot \ + --policy-document file://pybot-ecr-push.json + +# Examine the result. +aws iam get-role --role-name GitHubActions-ECR-Push-pybot \ + --query 'Role.[Arn,MaxSessionDuration,AssumeRolePolicyDocument]' +aws iam list-role-policies --role-name GitHubActions-ECR-Push-pybot # only ecr-push-pybot +aws iam list-attached-role-policies --role-name GitHubActions-ECR-Push-pybot # empty +``` -Use this procedure to move from the shared role to `GitHubActions-ECR-Push-pybot`. It makes sure that no unapproved credential path stays open during the change. +## The Old Shared Role -1. Create the two environments, as given in [Required Environments](#required-environments). -2. Create the role `GitHubActions-ECR-Push-pybot` with the trust and permission policies above. -3. Get the pull request that adds the environments to `ci.yml` approved and green. Do not merge it yet. -4. Set the secret `AWS_ROLE_ARN` to the new role, as given in [Required Secret](#required-secret). -5. Remove pybot from the shared role `GitHubActions-ECR-Push`, in one change for each policy: - - Trust: remove every `operationcode-pybot` subject (all spellings, the name form and the ID form). If the back-end switch-over is already done, the trust has only the back-end environment subjects. Examine it: - `aws iam get-role --role-name GitHubActions-ECR-Push --query Role.AssumeRolePolicyDocument` - - Permission: remove `repository/pybot` from `GitHubActions-ECR-Push-Policy`. +Before this change, pybot used `arn:aws:iam::633607774026:role/GitHubActions-ECR-Push`. That role is shared with `OperationCode/back-end`: +- Its trust uses `StringLike` with `repo:...:*` subjects for back-end and pybot. Any branch or pull request of either repository can assume it. +- Its policy allows push to `repository/back-end` and to `repository/pybot`. Thus a session of either repository can overwrite the images of the other. - Write down the time of this change (T). After T, the shared role cannot issue a new session to pybot CI. -6. Cancel every pybot CI run that is in progress or queued. These runs can hold credentials from before T. +**The old role is not gated.** It stays open to branch workflows of both repositories until it is deleted. A writer can edit a workflow on a branch, put the old role ARN in it, and get credentials without an approval. These credentials can push `pybot:prod` and `back-end:prod`. The environments do not stop this. + +**Do not change the old role during the move.** Do not edit its trust, do not edit its permissions, and do not use "Revoke active sessions" on it. The other repository uses it until its own move is done. + +The old role is deleted one time, after both repositories have done step 6 of their move. The back-end guide owns the deletion and the CloudTrail check of ECR writes to both repositories: see "The Old Shared Role" and the steps after it in [back-end `.github/SETUP.md`](https://github.com/OperationCode/back-end/blob/main/.github/SETUP.md). Do not do those steps from this guide. + +## Safe Change to the New Role + +Use this procedure to move pybot from the old shared role to its own role. Do not change the old role during this procedure (see above). + +1. Create the two environments, as given in [Required Environments](#required-environments). Write down the time when you start this step (S). The back-end CloudTrail check uses the earlier S of the two repositories. +2. Create the new role, as given in [Create the role](#create-the-role). Get the pull request that adds the environments to `ci.yml` approved and green. Do not merge it yet. +3. Set the secret to the new role ARN, as given in [Required Secret](#required-secret). + - Between step 3 and step 5, every push run of the old workflow fails at "Configure AWS credentials", because the new role does not trust its subject. Nobody must push to `master` during this time. +4. Cancel every pybot CI run that is in progress or queued. These runs can hold the old ARN or credentials of the old role. ```bash # List the runs, then cancel each one. for s in in_progress queued; do gh run list --repo OperationCode/operationcode-pybot --status $s --limit 100 --json databaseId --jq '.[].databaseId' done | xargs -n 1 gh run cancel --repo OperationCode/operationcode-pybot ``` - Run the list again until it is empty. -7. Revoke the sessions of the shared role: IAM > Roles > `GitHubActions-ECR-Push` > "Revoke active sessions". Write down the time of this revoke (R). This also stops running back-end push jobs. Do it when no back-end push is running. -8. Immediately merge the pull request. The push to `master` that the merge causes uses `ecr-main` and pushes `:prod` and `:latest`. - - Between step 4 and step 8, every push run of the old workflow fails at "Configure AWS credentials". Nobody must push to `master` during this time. - - If the first push fails, correct the trust subject (see the rules above). Then re-run the job. The re-run uses `ecr-approval`, so a maintainer must approve it. - - When the push run is complete, write down the image digest of `:prod`. You compare with it in step 9: + To cancel one run: `gh run cancel --repo OperationCode/operationcode-pybot`. Run the list again until it is empty. +5. Immediately merge the pull request. The push to `master` that the merge causes uses `ecr-main` and the new role, and pushes `:prod` and `:latest`. + - If the first push fails, correct the new role (see the rules above). Then re-run the job. The re-run uses `ecr-approval`, so a maintainer must approve it. + - When the push run is complete, write down the image digest of `:prod`. Give it to the person who does the back-end CloudTrail check: `aws ecr describe-images --region us-east-2 --repository-name pybot --image-ids imageTag=prod --query 'imageDetails[0].imageDigest' --output text` -9. Check for ECR login tokens from before R. A session issued before T can call ECR `GetAuthorizationToken` until the revoke at R. An ECR login token stays valid for 12 hours, and AWS does not document that "Revoke active sessions" stops it. Thus such a token can push until R + 12 h. An unapproved push during this window is detected, not prevented. When you follow these steps, you accept this risk. - - At R + 12 h plus approximately 30 minutes (CloudTrail can deliver events late), list the ECR write events from T - 12 h to R + 12 h: - ```bash - for e in InitiateLayerUpload PutImage BatchDeleteImage; do - aws cloudtrail lookup-events --region us-east-2 \ - --lookup-attributes AttributeKey=EventName,AttributeValue=$e \ - --start-time --end-time --output json | - jq -r '.Events[] | (.CloudTrailEvent | fromjson) as $c - | [.EventTime, .EventName, .Username, - ($c.userIdentity.sessionContext.sessionIssuer.arn // "-"), - ($c.requestParameters.repositoryName // "-"), - ($c.sourceIPAddress // "-")] | @tsv' - done - ``` - The columns are: time, event, `Username`, session issuer ARN, ECR repository, source IP address. Use the session issuer ARN to find the events of the CI roles (`role/GitHubActions-ECR-Push` and `role/GitHubActions-ECR-Push-pybot`). Do not use `Username` for this: for a role session it is the session name, and the caller chooses it. Each event of a CI role on the `pybot` repository must match a pybot CI run that you know about (time and run). - - The `PutImage` events are the real control. The tag check below shows only the current state of each tag. - - At the same time, make sure that the `:prod`, `:latest` and `:staging` digests are the digests that known CI runs pushed. For `:prod` and `:latest`, this is the digest from step 8, unless a later known run pushed them: +6. Confirm one successful push to `master` through the new role. Use one of these: + - CloudTrail: find the `AssumeRoleWithWebIdentity` event for the new role ARN, at the time of the push run from step 5. ```bash - for t in prod latest staging; do - aws ecr describe-images --region us-east-2 --repository-name pybot \ - --image-ids imageTag=$t --query 'imageDetails[].[imageTags,imageDigest,imagePushedAt]' - done + aws cloudtrail lookup-events --region us-east-2 \ + --lookup-attributes AttributeKey=EventName,AttributeValue=AssumeRoleWithWebIdentity \ + --start-time --output json | + jq -r '.Events[] | (.CloudTrailEvent | fromjson) as $c + | select($c.requestParameters.roleArn == "arn:aws:iam::633607774026:role/GitHubActions-ECR-Push-pybot") + | [.EventTime, $c.requestParameters.roleArn, ($c.userIdentity.userName // "-"), ($c.errorCode // "ok")] | @tsv' ``` - An `ImageNotFoundException` for a tag means that the tag does not exist. This is not a failure of the check. - - If an event or a digest does not match, push a known-good image again (re-run the master build with an `ecr-approval` approval) and investigate. + Expect a line with `ok` and the subject `...:environment:ecr-main`. + - Run log: the push run from step 5 shows environment `ecr-main`, and the "Output image URI" step prints `Successfully pushed Production images`. + + The pybot move is done after this step. Tell the back-end owner, because the old role is deleted only after both repositories finish step 6. ## Push a Staging Image (maintainers) @@ -208,10 +213,10 @@ A dispatch on `master` pushes `:prod` and `:latest`, and it also needs approval. ### Build fails with "Error assuming role" - Make sure that the `AWS_ROLE_ARN` secret is the ARN of `GitHubActions-ECR-Push-pybot`. - Make sure that the job ran in `ecr-main` or `ecr-approval` (the job page shows the environment). -- Compare the trust subjects with the prefix from `gh api .../actions/oidc/customization/sub`. The subject is `:environment:`. +- Compare the trust subjects with the prefix from `gh api .../actions/oidc/customization/sub`. The subject is `:environment:`. Never add an old subject back. ### Build fails with "AccessDenied" to ECR -- Make sure that the role has the permission policy above. +- Make sure that the role has the inline policy `ecr-push-pybot`. - Make sure that the repository is `633607774026.dkr.ecr.us-east-2.amazonaws.com/pybot`. ### Production build runs even when tests fail From becbb368ba7b33fc6cf6ac61380c7f7efd82041a Mon Sep 17 00:00:00 2001 From: Irving Popovetsky Date: Mon, 23 Nov 2026 10:24:23 -0800 Subject: [PATCH 3/3] Docs: new ECR push role is managed in operationcode_infra Terraform Co-Authored-By: Claude Opus 5.5 --- .github/SETUP.md | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/.github/SETUP.md b/.github/SETUP.md index d9c84e5..6d83261 100644 --- a/.github/SETUP.md +++ b/.github/SETUP.md @@ -131,19 +131,13 @@ Save this as `pybot-ecr-push.json`. The first statement has the ECR actions of t ### Create the role +The role is defined in [operationcode_infra](https://github.com/OperationCode/operationcode_infra), file `terraform/github_actions_ecr.tf`. Apply it with Terraform. Do not create the role by hand, and do not edit it by hand. + +The trust policy and permissions policy above are a reference only. The Terraform file is the source of truth. + +To examine the result after the apply: + ```bash -aws iam create-role \ - --role-name GitHubActions-ECR-Push-pybot \ - --assume-role-policy-document file://pybot-trust.json \ - --max-session-duration 3600 \ - --description "GitHub Actions OperationCode/operationcode-pybot: push to ECR repository pybot" - -aws iam put-role-policy \ - --role-name GitHubActions-ECR-Push-pybot \ - --policy-name ecr-push-pybot \ - --policy-document file://pybot-ecr-push.json - -# Examine the result. aws iam get-role --role-name GitHubActions-ECR-Push-pybot \ --query 'Role.[Arn,MaxSessionDuration,AssumeRolePolicyDocument]' aws iam list-role-policies --role-name GitHubActions-ECR-Push-pybot # only ecr-push-pybot