Repository navigation
feat(runners): record a given commit verification in the sandbox runner #3599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,14 +17,30 @@ package runners | |
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
|
|
||
| schemaapi "github.com/chainloop-dev/chainloop/app/controlplane/api/workflowcontract/v1" | ||
| "github.com/chainloop-dev/chainloop/pkg/attestation/crafter/runners/commitverification" | ||
| ) | ||
|
|
||
| const chainloopSandboxEnvVar = "CHAINLOOP_SANDBOX" | ||
|
|
||
| // ChainloopSandboxCommitVerificationEnvVar holds a SandboxCommitVerification as JSON. | ||
| // The sandbox gets no SCM token, so the platform verifies the commit before it starts | ||
| // the sandbox and gives the result to the runner in this variable. | ||
| const ChainloopSandboxCommitVerificationEnvVar = "CHAINLOOP_SANDBOX_COMMIT_VERIFICATION" | ||
|
|
||
| // SandboxCommitVerification is a commit verification result and the commit that it applies to. | ||
| type SandboxCommitVerification struct { | ||
| // Commit is the full hash of the verified commit. | ||
| Commit string `json:"commit"` | ||
| // Verification is the result of the verification. | ||
| Verification *commitverification.CommitVerification `json:"verification"` | ||
| } | ||
|
|
||
| type ChainloopSandbox struct { | ||
| *Generic | ||
| } | ||
|
|
@@ -67,8 +83,33 @@ func (r *ChainloopSandbox) Environment() RunnerEnvironment { | |
| return Unknown | ||
| } | ||
|
|
||
| func (r *ChainloopSandbox) VerifyCommitSignature(_ context.Context, _ string) *commitverification.CommitVerification { | ||
| return nil | ||
| // VerifyCommitSignature returns the verification that the platform gave in | ||
| // ChainloopSandboxCommitVerificationEnvVar. It returns nil when no verification was | ||
| // given, or when the verification is for a different commit than commitHash. | ||
| func (r *ChainloopSandbox) VerifyCommitSignature(_ context.Context, commitHash string) *commitverification.CommitVerification { | ||
| raw := os.Getenv(ChainloopSandboxCommitVerificationEnvVar) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: An explicitly empty value is treated as though the variable were unset, so malformed input does not produce the promised Prompt for AI agents
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not changed, by decision. An empty variable is the usual way to unset a variable, so the runner treats it as not set and records no verification. The platform never sends an empty value: it omits the variable when it has no result. Neither outcome records a verified commit. |
||
| if raw == "" || commitHash == "" { | ||
| return nil | ||
| } | ||
|
|
||
| // A verification without a status carries no result, the same as a missing one. | ||
| var given SandboxCommitVerification | ||
| if err := json.Unmarshal([]byte(raw), &given); err != nil || given.Verification == nil || | ||
| given.Verification.Status == commitverification.VerificationStatusUnspecified { | ||
| return &commitverification.CommitVerification{ | ||
| Attempted: true, | ||
| Status: commitverification.VerificationStatusUnavailable, | ||
| Reason: fmt.Sprintf("invalid value in %s", ChainloopSandboxCommitVerificationEnvVar), | ||
| } | ||
| } | ||
|
|
||
| // The result is about one commit only. Never apply it to the commit of another | ||
| // checkout, and never match an abbreviated hash. | ||
| if !strings.EqualFold(given.Commit, commitHash) { | ||
| return nil | ||
| } | ||
|
|
||
| return given.Verification | ||
| } | ||
|
|
||
| func (r *ChainloopSandbox) Report(_ []byte, _ string) error { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // | ||
| // Copyright 2026 The Chainloop Authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package commitverification | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestVerificationStatusText(t *testing.T) { | ||
| testCases := []struct { | ||
| status VerificationStatus | ||
| text string | ||
| }{ | ||
| {VerificationStatusUnspecified, "unspecified"}, | ||
| {VerificationStatusVerified, "verified"}, | ||
| {VerificationStatusUnverified, "unverified"}, | ||
| {VerificationStatusUnavailable, "unavailable"}, | ||
| {VerificationStatusNotApplicable, "not_applicable"}, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.text, func(t *testing.T) { | ||
| got, err := tc.status.MarshalText() | ||
| require.NoError(t, err) | ||
| assert.Equal(t, tc.text, string(got)) | ||
| assert.Equal(t, tc.text, tc.status.String()) | ||
|
|
||
| var parsed VerificationStatus | ||
| require.NoError(t, parsed.UnmarshalText([]byte(tc.text))) | ||
| assert.Equal(t, tc.status, parsed) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestVerificationStatusTextErrors(t *testing.T) { | ||
| _, err := VerificationStatus(99).MarshalText() | ||
| require.Error(t, err) | ||
|
|
||
| var parsed VerificationStatus | ||
| require.Error(t, parsed.UnmarshalText([]byte("trusted"))) | ||
| } | ||
|
|
||
| const signatureAlgorithmPGP = "PGP" | ||
|
|
||
| func TestCommitVerificationJSON(t *testing.T) { | ||
| want := &CommitVerification{ | ||
| Attempted: true, | ||
| Status: VerificationStatusVerified, | ||
| Reason: "Commit signed and verified", | ||
| Platform: "gitlab", | ||
| KeyID: "1", | ||
| SignatureAlgorithm: signatureAlgorithmPGP, | ||
| } | ||
|
|
||
| raw, err := json.Marshal(want) | ||
| require.NoError(t, err) | ||
| assert.JSONEq(t, `{"attempted":true,"status":"verified","reason":"Commit signed and verified","platform":"gitlab","key_id":"1","signature_algorithm":"PGP"}`, string(raw)) | ||
|
|
||
| var got CommitVerification | ||
| require.NoError(t, json.Unmarshal(raw, &got)) | ||
| assert.Equal(t, want, &got) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we trust a json struct to decide wether something is verified or not? My understanding is that to show verified, we do rely on a signed JWT that we check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In CI, the commit verification does not come from a signed JWT either. The CLI calls the GitHub or GitLab commit API with
GITHUB_TOKENorGITLAB_TOKENand records the answer (VerifyGitHubCommit,VerifyGitLabCommit). The signed JWT that the CLI checks is the runner OIDC token. It setsrunner.authenticatedand the runner environment, not the commit verification.Here the platform calls the same functions and gives the result to the CLI. The value is not signed, by decision D-009 in the spec (chainloop-dev/platform#6821). Any run with
CHAINLOOP_SANDBOXcan set it, and the sandbox runner recordsauthenticated: false. The attestation content comes from the client: a token holder with a changed CLI can already write anygit.headvalues. The spec lists a signed result as a later hardening: the platform signs the result and the CLI checks the signature.If you want the signed result in this change, I will update the spec and do it in both PRs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure, I just want to make sure that whatever security contraints we have today to define that a commit is verified it's kept, so I am not 100% sure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does not keep it. Today, to record a forged "verified", you must change the CLI or build the attestation by hand. With this PR, two environment variables on the released CLI are enough.
To keep today's constraint, the CLI must accept only a result that it can authenticate. I will change the design: the platform signs the result, and the CLI verifies the signature with a key that it trusts. I will update the spec first (D-009 changes), then this PR and chainloop-dev/platform#6861. I am putting this PR back to draft until then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new design is in the spec PR chainloop-dev/platform#6875, and it is wider than this result. The platform gives each sandbox run a signed identity, with a short-lived certificate from the control plane. The CLI and the control plane verify it. A verified run records an authenticated sandbox runner, and the commit verification travels in the identity. The control plane rejects an attestation that claims an authenticated sandbox runner, or a commit verification, that the identity does not support. This PR stays in draft until the spec merges.