Binary Authorization and Artifact Registry for PCDOE: deploy only images built by Cloud Build

13 min read

Section 2 of the Professional Cloud DevOps Engineer exam (~25%) is CI/CD. In the exam guide, 2.1 covers artifact management with Artifact Registry, pipeline triggers and approval flows, and 2.4 covers securing the deployment pipeline and software supply chain security, naming Binary Authorization directly. The three newest questions in the free preview of the question bank land right here: deploy only images built by Cloud Build, cache images from public registries, and set up a Cloud Build trigger from GitHub with an approval step and no static credentials. This post explains how each piece works, with gcloud commands, config files, a decision table and example practice questions. For the exam format and scope, see the PCDOE study guide.

Key takeaways

  • Deploy only images built by Cloud Build with the least effort: enable Binary Authorization on the cluster and require the built-by-cloud-build attestor; Cloud Build creates the attestor and signs the attestations for you.
  • Cache images from public registries: one remote repository per upstream, grouped behind one virtual repository; the pipeline pulls only through the virtual repository.
  • Build on every new tag with a required approval: a Cloud Build GitHub trigger with --require-approval, and roles/cloudbuild.builds.approver for the approving group.
  • "No static credentials, no federation" rules out service account keys, SSH deploy keys and also Workload Identity Federation for GitHub Actions.

The software supply chain: where the risks sit in a pipeline

The "software supply chain" is everything between the code you write and the container running in production: the source code, the base images and libraries pulled from the internet, the build system, where images are stored, and the deploy step. An attacker does not need to break your application if they can slip into one of those links, for example by deploying an image they built themselves to your cluster, or by planting malicious code in a public image that your pipeline pulls on every build.

PCDOE questions usually split the problem into four questions, each matching one tool:

  • Who can start a build, and from which source? A Cloud Build trigger is tied to a connected repository, filters on branches or tags, and can require an approver.
  • Where does the pipeline get its dependencies? Artifact Registry, with remote repositories as a cache for Docker Hub and other public registries, and a virtual repository as a single front door.
  • Does the image have known vulnerabilities? Artifact Analysis scans images in Artifact Registry.
  • Will the cluster run an unknown image? Binary Authorization checks at deploy time and blocks images that fail the policy.

A common mix-up: IAM on Artifact Registry only answers "who can push images to this registry". It does not stop someone from deploying an image from another registry to the cluster. To block on the cluster side you need a check at deploy time, and that is the job of Binary Authorization.

The exam guide also mentions SLSA (Supply-chain Levels for Software Artifacts), a framework that describes how trustworthy a build process is. For the exam, remember the core idea: build on a managed system, record provenance (metadata saying which source an image was built from and how), and verify it at deploy time.

Binary Authorization: blocking unknown images at deploy time

Policies, rules and attestors

Binary Authorization enforces constraints at deploy time. On GKE, when you create a Pod, the cluster asks Binary Authorization whether the image is allowed to run. The answer comes from the project's policy, which has:

  • A default rule (defaultAdmissionRule): applies to every deploy request that does not match a more specific rule.
  • Specific rules: for example clusterAdmissionRules per cluster, or rules per Kubernetes service account. That way a dev cluster can be looser than a prod cluster in the same policy.
  • Exempt images: a list of images that are always allowed. The docs recommend not using this list for Google's system images; instead, turn on trust for Google-maintained system images (globalPolicyEvaluationMode: ENABLE) so images that GKE needs are not blocked by mistake.

Each rule has an evaluation mode: allow all images, disallow all images, or require attestations. An attestation is a digital signature over the image digest (the sha256:... string), created by a trusted party. An attestor holds the public key used to verify that signature at deploy time. Because the signature is tied to the digest and not to a tag, if someone points the latest tag at a different image, the new image has no valid attestation.

Each rule also has an enforcement mode: block and write an audit log, or dry run (audit log only, the deployment still goes ahead), which lets you test a policy in production before you actually start blocking.

Enabling it on a cluster

Enable enforcement on an existing cluster with gcloud (for a zonal cluster, replace --region with --zone):

gcloud container clusters update prod-cluster \
  --region=us-central1 \
  --binauthz-evaluation-mode=PROJECT_SINGLETON_POLICY_ENFORCE

The older --enable-binauthz flag is deprecated, but older questions may still use that name. The docs note that the policy can take a few minutes to take effect, so do not conclude that "the policy is not working" if you test a deployment right after enabling it.

Breakglass and logs

When you urgently need to run an image that fails the policy, use breakglass: add the label image-policy.k8s.io/break-glass: "true" to the Pod spec. The Pod is deployed, but the event is written to Cloud Audit Logs, where you can review it or trigger an alert automatically. Do not treat breakglass as a way to "temporarily turn off" Binary Authorization; put an alert on breakglass log entries.

The built-by-cloud-build attestor: the least-effort option

Cloud Build does the signing

The traditional approach is to build it yourself: create a key in Cloud KMS, create an attestor, and add a step to cloudbuild.yaml that signs the image after it is pushed (the docs use a custom build step from the Cloud Build community repository, and the Cloud Build service account needs extra roles such as roles/cloudkms.signerVerifier). It is flexible, but you own the key and the signing step.

The lower-effort approach: according to the docs, Cloud Build automatically creates the built-by-cloud-build attestor in your project when you run a build that produces images. After each successful build, Cloud Build signs and creates attestations for the images. All you do is update the policy to require that attestor:

defaultAdmissionRule:
  evaluationMode: REQUIRE_ATTESTATION
  enforcementMode: ENFORCED_BLOCK_AND_AUDIT_LOG
  requireAttestationsBy:
  - projects/PROJECT_ID/attestors/built-by-cloud-build
name: projects/PROJECT_ID/policy

Images without a valid attestation are blocked, and the failure is logged to Cloud Audit Logs. No Cloud KMS, no custom step, no function to maintain.

The limits that break the setup

These are the common reasons why "it is enabled, but a freshly built image is still blocked":

  • Push images with the images field, not a separate docker push step. The docs say Cloud Build does not generate attestations (or provenance) when the image is pushed with an explicit docker push step.
  • Builds that specify a location or run in a private pool get attestations only if you set requestedVerifyOption: VERIFIED. With that option, a build is only marked SUCCESS once attestations and provenance have been generated.
  • Use separate build and deploy config files. The attestation exists only after the build pipeline completes, so if the deployment runs inside the same build, the image has no attestation yet at deploy time.
  • Same project: Cloud Build and Binary Authorization must be in the same project. If the cluster lives in another project, set up IAM for a multi-project setup and reference the attestor in the project where Cloud Build runs.

A minimal build file that follows those rules (a tag trigger provides the $TAG_NAME variable):

steps:
- name: gcr.io/cloud-builders/docker
  args:
  - build
  - -t
  - us-central1-docker.pkg.dev/$PROJECT_ID/app/web:$TAG_NAME
  - .
images:
- us-central1-docker.pkg.dev/$PROJECT_ID/app/web:$TAG_NAME
options:
  requestedVerifyOption: VERIFIED

Artifact Registry: standard, remote and virtual repositories

Three modes, three roles

Artifact Registry repositories come in different modes. You pick the mode when you create the repository and cannot change it afterwards (the same goes for the format, the location and the encryption type):

  • Standard: the default mode, where you push your own images and packages.
  • Remote: a proxy for an upstream source such as Docker Hub. The first time you request a version, Artifact Registry downloads and caches it; the next time it serves the cached copy. If the upstream does not have that version, the request fails.
  • Virtual: a single access point for several upstream repositories of the same format, which can be standard or remote repositories. A virtual repository stores no data, so listing images in it shows nothing.

The connector mode (Preview) proxies without caching, so it does not fit a requirement to reduce reliance on public registries.

Why remote repositories make builds steadier and safer

According to the docs, caching copies of public dependencies in Artifact Registry reduces latency when Google Cloud services pull images, and cached artifacts stay available when the public registry has an outage. Keeping the remote repository in the same region as your runtimes also reduces data transfer costs. There is a DOCKER-HUB preset; other registries such as GitHub Container Registry (https://ghcr.io) or https://registry.k8s.io are added as custom URLs, which must use HTTPS. One limit to know: upstreams must be reachable over the internet, and remote repositories do not support on-premises or VPC upstreams without a public IP address. For Docker, the cached tag list is refreshed after 1 hour by default, so a tag just pushed upstream may not show up through the remote repository right away.

gcloud artifacts repositories create dockerhub \
  --repository-format=docker \
  --location=us-central1 \
  --mode=remote-repository \
  --remote-docker-repo=DOCKER-HUB

Virtual repositories and priority

A virtual repository is created from an upstream policy file in JSON. Each upstream has an integer priority; a higher number wins (it is a weight, not a 1, 2, 3 ranking). When an artifact exists in more than one upstream, Artifact Registry serves it from the upstream with the highest priority.

[
 {"id": "private", "priority": 100,
  "repository": "projects/P/locations/L/repositories/app"},
 {"id": "dockerhub", "priority": 10,
  "repository": "projects/P/locations/L/repositories/dh"}
]
gcloud artifacts repositories create docker-all \
  --repository-format=docker \
  --location=us-central1 \
  --mode=virtual-repository \
  --upstream-policy-file=upstreams.json

Two operational details people often miss: standard upstream repositories must be in the same region (or multi-region) as the virtual repository, though they can be in other projects; and the virtual repository uses the Artifact Registry service agent to read its upstreams, so that service agent needs read access (roles/artifactregistry.reader) in each project that holds an upstream.

The biggest security benefit is protection against dependency confusion: an attacker publishes a package to a public repository with the same name as an internal package but a higher version number, hoping the build tool picks it. The docs recommend a remote repository for the public source, a standard repository for your private packages, a virtual repository that prioritizes the standard one, and configuring your tools to read only from the virtual repository.

Applied to a question about caching all containerized dependencies with little maintenance: remote repositories cache automatically, so nothing is downloaded by hand, and the virtual repository gives the pipeline one endpoint, so adding a new upstream does not mean editing the pipeline.

Cloud Build triggers from GitHub, approvals and no static keys

Connecting GitHub without keys

With 2nd gen repositories you create a connection to GitHub. This step asks you, in the browser, to install and authorize the Cloud Build GitHub App; Cloud Build stores the resulting authorization token as a secret in Secret Manager in your project and uses the Cloud Build Service Agent to read it. You do not create a service account key or an SSH deploy key, and you do not set up federation with another system. A connection must have a region; it cannot be global.

A tag trigger with a required approval

gcloud builds triggers create github \
  --name=release-on-tag \
  --region=us-central1 \
  --repository=REPO_RESOURCE \
  --tag-pattern='^v[0-9]+\.[0-9]+\.[0-9]+$' \
  --build-config=cloudbuild.yaml \
  --require-approval \
  --service-account=SA_RESOURCE

REPO_RESOURCE is the full name of the linked repository (projects/.../connections/gh/repositories/app), and SA_RESOURCE is the service account that runs the build (projects/PROJECT_ID/serviceAccounts/EMAIL). Note that --tag-pattern is an RE2 regular expression and matches partially: the pattern foo also matches foobar. Without ^ and $, a test tag such as v1.2.3-test can also start a release build.

With --require-approval, the build does not start right away; it waits in a pending state. Someone with the Cloud Build Approver role (roles/cloudbuild.builds.approver) approves it in the console or from the command line; if it is rejected, the build never starts:

gcloud projects add-iam-policy-binding my-proj \
  --member=group:release-approvers@example.com \
  --role=roles/cloudbuild.builds.approver

gcloud builds approve BUILD_ID --project=my-proj

A few practical points. Users with the Cloud Build Editor role can turn the approval requirement on or off for a trigger, but approving builds needs the Approver role, so do not hand Editor to the approving group just so they can click a button. Pending builds older than 2 months can no longer be approved or rejected. And if the organization policy constraints/cloudbuild.allowedIntegrations denies the repository, the policy wins even after a build is approved.

Finally, the trigger's service account needs permission to write images. Grant roles/artifactregistry.writer on the repository rather than the whole project to keep it least-privilege.

When GitHub Actions is the answer

If the question allows GitHub Actions, the keyless way is Workload Identity Federation (the workflow exchanges GitHub's OIDC token for short-lived Google Cloud access). But that is still federation. When the question says avoid using static credentials or federating with any other system, both the service account key option and the workload identity pool option are out; an SSH deploy key is a static credential too.

Decision table: protecting the pipeline by requirement

Requirement in the questionChooseWhy / signal
Deploy only images built by Cloud Build, with little effortBinary Authorization + the built-by-cloud-build attestorCloud Build creates the attestor and signs; no KMS or function
Test a policy in production before blockingDry run enforcement modeDeploys go ahead, audit log is written
Emergency deploy of an image that fails the policyBreakglass (label on the Pod)Still leaves a trail in Cloud Audit Logs
Cache images from public registries, survive upstream outagesRemote repositoryCached on first request, served from cache after
One endpoint for many repositories, stop dependency confusionVirtual repository with prioritiesHigher number wins; clients read only from the virtual repo
Build on a new tag, from GitHub, no static keysCloud Build GitHub trigger, --tag-patternToken stored in Secret Manager via the GitHub App
Builds need an approver--require-approval + roles/cloudbuild.builds.approverNot the Editor role
GitHub Actions without static keysWorkload Identity FederationOut if the question forbids federation

Common traps

  • Scenario: only images built by the Cloud Build pipeline in the production project may be deployed to GKE, with the least effort. Answer: enable Binary Authorization on the cluster and configure the policy to require the projects/PROJECT_ID/attestors/built-by-cloud-build attestor. Creating a KMS keyring and signing yourself is extra work; a function that checks after the push cannot block a deployment (practice question #229).
  • Scenario: a containerized CI pipeline must rely less on public registries, build reproducibly, cache all dependencies and need little maintenance. Answer: one remote repository per upstream, a central virtual repository in front of them, and the pipeline pulls through the virtual repository. Standard repositories with manual uploads are wrong because they take effort and do not update themselves (question #230).
  • Scenario: build from GitHub on every new tag, approved by a Google group, no static credentials and no federation. Answer: a Cloud Build GitHub trigger on tags, an approval requirement, roles/cloudbuild.builds.approver for the group and roles/artifactregistry.writer for the trigger's service account. GitHub Actions with a key or with a workload identity pool both break the requirements (question #232).
  • Scenario: the policy requires the built-by-cloud-build attestor, but a freshly built image is still blocked. What to check: whether the build pushes the image with a separate docker push step, whether the build specifies a location or runs in a private pool without requestedVerifyOption: VERIFIED, and whether the deployment runs inside the same build.

Reading the question

  • only images built by ... are deployed: think Binary Authorization, a check at deploy time, not at push time.
  • minimize manual effort / maintenance: prefer built-in features (the built-by-cloud-build attestor, self-caching remote repositories) over a home-made KMS setup, custom functions or manual uploads.
  • reduce reliance on public registries / reproducible builds: remote repositories; add single endpoint / central and you also need a virtual repository.
  • approved by a group: Cloud Build approvals and the Approver role; watch for options that swap in the Editor role.
  • no static credentials: rule out service account keys and SSH keys; add no federation and Workload Identity Federation is out too.

Start practicing

Try 60 free Professional Cloud DevOps Engineer questions, then a timed exam (Pro). For an overview of the five exam sections, see the PCDOE study guide.

Official sources