Skip to content

Commit 3d4b0a8

Browse files
Felix Phippsclaude
andcommitted
fix(gcb): pin cmrel install in GCB to the invoking binary's commit
The privileged GCB jobs installed cmrel with `go install .../cmrel@${_RELEASE_REPO_REF}` defaulting to the mutable `master` ref, so any commit landed on cert-manager/release@master would execute inside the build holding GITHUB_TOKEN, the decrypted docker config and KMS signing access (CWE-829). There was no pin, module-sum or signature binding the submitted cloudbuild.yaml to the code that ran. Resolve the ref from cmrel's own build info (runtime/debug.ReadBuildInfo) and set _RELEASE_REPO_REF to the exact commit the invoking binary was built from, so GCB runs the same code the release manager built and ran locally rather than re-resolving master. This is wired into stage, publish and bootstrap-pgp with no change to operator invocation. It fails closed: a modified working tree, an unstamped ("(devel)") build or missing build info returns an error instead of falling back to a mutable ref. The cloudbuild.yaml defaults become a PINNED_CMREL_COMMIT_SHA_REQUIRED placeholder backstop; the manually submitted helm test job documents supplying the SHA via --substitutions. Co-Authored-By: Claude Opus 4.8 <[email protected]> Signed-off-by: Felix Phipps <[email protected]>
1 parent 7d045e0 commit 3d4b0a8

10 files changed

Lines changed: 200 additions & 5 deletions

File tree

‎cmd/cmrel/cmd/bootstrap_pgp.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ func runBootstrapPGP(rootOpts *rootOptions, o *bootstrapPGPOptions) error {
111111

112112
build.Substitutions["_KMS_KEY"] = o.Key
113113

114+
// Pin the GCB build to the commit this cmrel binary was built from, rather than
115+
// installing cmrel from a mutable ref (e.g. "master") inside the privileged build.
116+
repoRef, err := releaseRepoRef()
117+
if err != nil {
118+
return fmt.Errorf("failed to determine cmrel commit to pin GCB build: %w", err)
119+
}
120+
log.Printf("Pinning GCB cmrel install to cert-manager/release@%s", repoRef)
121+
build.Substitutions["_RELEASE_REPO_REF"] = repoRef
122+
114123
log.Printf("DEBUG: building google cloud build API client")
115124

116125
svc, err := cloudbuild.NewService(ctx)

‎cmd/cmrel/cmd/publish.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,15 @@ func runPublish(rootOpts *rootOptions, o *publishOptions) error {
214214
build.Substitutions["_SKIP_SIGNING"] = fmt.Sprintf("%v", o.SkipSigning)
215215
build.Substitutions["_KMS_KEY"] = o.SigningKMSKey
216216

217+
// Pin the GCB build to the commit this cmrel binary was built from, rather than
218+
// installing cmrel from a mutable ref (e.g. "master") inside the privileged build.
219+
repoRef, err := releaseRepoRef()
220+
if err != nil {
221+
return fmt.Errorf("failed to determine cmrel commit to pin GCB build: %w", err)
222+
}
223+
log.Printf("Pinning GCB cmrel install to cert-manager/release@%s", repoRef)
224+
build.Substitutions["_RELEASE_REPO_REF"] = repoRef
225+
217226
log.Printf("DEBUG: building google cloud build API client")
218227
svc, err := cloudbuild.NewService(ctx)
219228
if err != nil {

‎cmd/cmrel/cmd/releaseref.go‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Copyright 2026 The cert-manager Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"fmt"
21+
"runtime/debug"
22+
)
23+
24+
// releaseRepoRef returns the git ref of the cert-manager/release repository that
25+
// GCB build jobs should install cmrel from, i.e. the value of the
26+
// _RELEASE_REPO_REF substitution.
27+
func releaseRepoRef() (string, error) {
28+
info, ok := debug.ReadBuildInfo()
29+
return releaseRepoRefFromBuildInfo(info, ok)
30+
}
31+
32+
// releaseRepoRefFromBuildInfo implements releaseRepoRef against an explicit
33+
// build.BuildInfo so the ref-resolution logic can be unit tested.
34+
func releaseRepoRefFromBuildInfo(info *debug.BuildInfo, ok bool) (string, error) {
35+
if !ok || info == nil {
36+
return "", fmt.Errorf("unable to read build info to determine the cmrel commit; build cmrel with `go build`/`go install` from a clean checkout of cert-manager/release")
37+
}
38+
39+
var revision, modified string
40+
for _, s := range info.Settings {
41+
switch s.Key {
42+
case "vcs.revision":
43+
revision = s.Value
44+
case "vcs.modified":
45+
modified = s.Value
46+
}
47+
}
48+
49+
// Built from a VCS working tree (e.g. `make build` / `go build ./cmd/cmrel`).
50+
if revision != "" {
51+
if modified == "true" {
52+
return "", fmt.Errorf("cmrel was built from a modified working tree (commit %s); GCB cannot install an unpushed build - rebuild from a clean, pushed commit", revision)
53+
}
54+
return revision, nil
55+
}
56+
57+
// Installed as a module (e.g. `go install .../cmrel@<ref>`); Main.Version is a
58+
// tag or pseudo-version that `go install` can resolve. "(devel)" means the
59+
// binary was built from source without VCS stamping, which we cannot pin.
60+
if v := info.Main.Version; v != "" && v != "(devel)" {
61+
return v, nil
62+
}
63+
64+
return "", fmt.Errorf("unable to determine the commit cmrel was built from; build cmrel with `go build`/`go install` from cert-manager/release so its version is stamped")
65+
}

‎cmd/cmrel/cmd/releaseref_test.go‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
Copyright 2026 The cert-manager Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"runtime/debug"
21+
"testing"
22+
)
23+
24+
func TestReleaseRepoRefFromBuildInfo(t *testing.T) {
25+
const commit = "f6da9c76877551ef32503b17189bb178501f59a7"
26+
27+
tests := []struct {
28+
name string
29+
info *debug.BuildInfo
30+
ok bool
31+
want string
32+
wantErr bool
33+
}{
34+
{
35+
name: "no build info",
36+
info: nil,
37+
ok: false,
38+
wantErr: true,
39+
},
40+
{
41+
name: "clean working tree pins to the commit",
42+
ok: true,
43+
info: &debug.BuildInfo{Settings: []debug.BuildSetting{
44+
{Key: "vcs.revision", Value: commit},
45+
{Key: "vcs.modified", Value: "false"},
46+
}},
47+
want: commit,
48+
},
49+
{
50+
name: "modified working tree fails closed",
51+
ok: true,
52+
info: &debug.BuildInfo{Settings: []debug.BuildSetting{
53+
{Key: "vcs.revision", Value: commit},
54+
{Key: "vcs.modified", Value: "true"},
55+
}},
56+
wantErr: true,
57+
},
58+
{
59+
name: "module install pins to the version",
60+
ok: true,
61+
info: &debug.BuildInfo{Main: debug.Module{Version: "v0.0.0-20230101000000-" + commit[:12]}},
62+
want: "v0.0.0-20230101000000-" + commit[:12],
63+
},
64+
{
65+
name: "devel build without vcs stamping fails closed",
66+
ok: true,
67+
info: &debug.BuildInfo{Main: debug.Module{Version: "(devel)"}},
68+
wantErr: true,
69+
},
70+
{
71+
name: "no revision and no version fails closed",
72+
ok: true,
73+
info: &debug.BuildInfo{},
74+
wantErr: true,
75+
},
76+
}
77+
78+
for _, tt := range tests {
79+
t.Run(tt.name, func(t *testing.T) {
80+
got, err := releaseRepoRefFromBuildInfo(tt.info, tt.ok)
81+
if (err != nil) != tt.wantErr {
82+
t.Fatalf("releaseRepoRefFromBuildInfo() error = %v, wantErr %v", err, tt.wantErr)
83+
}
84+
if got != tt.want {
85+
t.Errorf("releaseRepoRefFromBuildInfo() = %q, want %q", got, tt.want)
86+
}
87+
})
88+
}
89+
}

‎cmd/cmrel/cmd/stage.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,15 @@ func runStage(rootOpts *rootOptions, o *stageOptions) error {
217217
build.Substitutions["_TARGET_OSES"] = strings.Join(targetOSes.List(), ",")
218218
build.Substitutions["_TARGET_ARCHES"] = strings.Join(targetArches.List(), ",")
219219

220+
// Pin the GCB build to the commit this cmrel binary was built from, rather than
221+
// installing cmrel from a mutable ref (e.g. "master") inside the privileged build.
222+
repoRef, err := releaseRepoRef()
223+
if err != nil {
224+
return fmt.Errorf("failed to determine cmrel commit to pin GCB build: %w", err)
225+
}
226+
log.Printf("Pinning GCB cmrel install to cert-manager/release@%s", repoRef)
227+
build.Substitutions["_RELEASE_REPO_REF"] = repoRef
228+
220229
outputDir := ""
221230
// If --release-version is not explicitly set, we treat this build as a
222231
// 'devel' build and output into the development directory.

‎gcb/bootstrap-pgp/cloudbuild.yaml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ substitutions:
2727
## Required parameters
2828
_KMS_KEY: ""
2929
## Options controlling the version of the release tooling used in the build.
30-
_RELEASE_REPO_REF: "master"
30+
## Must be an immutable commit SHA: cmrel runs here with KMS access to the signing
31+
## key, so a mutable ref (e.g. "master") would execute unpinned code.
32+
_RELEASE_REPO_REF: "PINNED_CMREL_COMMIT_SHA_REQUIRED"
3133

3234
# we don't specify a machineType manually because we don't need high CPU at all for this
3335
# job; the default machine type seems to be the lowest-power and cheapest so we leave it

‎gcb/publish/cloudbuild.yaml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ substitutions:
9696
## Used as a tag to identify the build more easily later
9797
_TAG_RELEASE_NAME: ""
9898
## Ref for cert-manager/release repo to use when installing cmrel
99-
_RELEASE_REPO_REF: "master"
99+
## Must be an immutable commit SHA: cmrel runs here with release secrets and KMS
100+
## signing access, so a mutable ref (e.g. "master") would execute unpinned code.
101+
_RELEASE_REPO_REF: "PINNED_CMREL_COMMIT_SHA_REQUIRED"
100102
## Version of the cosign tool to install
101103
_COSIGN_REPO_REF: "v1.13.6"
102104

‎gcb/stage/cloudbuild.yaml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ substitutions:
6666
_TARGET_OSES: "*"
6767
_TARGET_ARCHES: "*"
6868
## Options controlling the version of the release tooling used in the build.
69-
_RELEASE_REPO_REF: "master"
69+
## Must be an immutable commit SHA: cmrel runs here with release secrets and KMS
70+
## signing access, so a mutable ref (e.g. "master") would execute unpinned code.
71+
_RELEASE_REPO_REF: "PINNED_CMREL_COMMIT_SHA_REQUIRED"
7072
## Used as a tag to identify the build more easily later
7173
_TAG_RELEASE_BRANCH: ""
7274

‎gcb/test/helm/README.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@ Use this to verify that the [jetstack-release-bot][] user has permission to crea
88
Run the tests using [gcloud builds submit][] :
99

1010
```sh
11-
$ gcloud builds submit gcb/test/helm/ --config=gcb/test/helm/cloudbuild.yaml
11+
$ gcloud builds submit gcb/test/helm/ --config=gcb/test/helm/cloudbuild.yaml \
12+
--substitutions=_RELEASE_REPO_REF=<commit-sha>
1213
```
1314

15+
`_RELEASE_REPO_REF` must be an immutable commit SHA of this repository. The build
16+
checks out that ref and runs the tests with credentials in scope, so it must not be
17+
left as a mutable ref such as `master`; the default value is a placeholder that fails
18+
the build closed if no SHA is supplied.
19+
1420
[jetstack-release-bot]: https://github.com/jetstack-release-bot
1521
[jetstack-charts repo]: https://github.com/jetstack/jetstack-charts
1622
[gcloud builds submit]: https://cloud.google.com/sdk/gcloud/reference/builds/submit

‎gcb/test/helm/cloudbuild.yaml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ steps:
2929
3030
substitutions:
3131
_RELEASE_REPO_URL: https://github.com/cert-manager/release.git
32-
_RELEASE_REPO_REF: "master"
32+
## Must be an immutable commit SHA: tests run here with GITHUB_TOKEN in scope, so a
33+
## mutable ref (e.g. "master") would execute unpinned code.
34+
_RELEASE_REPO_REF: "PINNED_CMREL_COMMIT_SHA_REQUIRED"
3335

3436
secrets:
3537
- kmsKeyName: projects/cert-manager-release/locations/europe-west1/keyRings/cert-manager-release/cryptoKeys/cert-manager-release-secret-key

0 commit comments

Comments
 (0)