Repository navigation
feat(sandboxidentity): verify the identity of Chainloop sandboxes #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
Open
javirln
wants to merge
11
commits into
main
Choose a base branch
from
pfm-7678-sandbox-commit-verification
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+3,430
−163
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e56ae90
feat(runners): record a given commit verification in the sandbox runner
javirln b5617ce
fix(runners): record unavailable for a given verification without a s…
javirln 104f18b
feat(sandboxidentity): add the identity token of a Chainloop sandbox
javirln 5149d1f
feat(controlplane): issue and enforce sandbox identities
javirln 309914a
feat(runners): authenticate the sandbox runner with its verified iden…
javirln a3f7184
fix(crafter): keep the verified sandbox identity after init
javirln 2566fc6
fix(controlplane): compare the signature algorithm of a sandbox commi…
javirln 79b0474
fix(controlplane): keep the sandbox identity out of the request log
javirln 2b6fdf2
fix(sandboxidentity): check the organization of the certificate at is…
javirln 1ed44fd
test(controlplane): cover the sandbox identity permission at token cr…
javirln efb870b
refactor(controlplane): name the dependent run in SaveAttestation
javirln File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
app/cli/pkg/action/attestation_init_sandboxidentity_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| // | ||
| // 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 action | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/ecdsa" | ||
| "crypto/elliptic" | ||
| "crypto/rand" | ||
| "crypto/sha256" | ||
| "crypto/x509" | ||
| "crypto/x509/pkix" | ||
| "encoding/hex" | ||
| "math/big" | ||
| "net" | ||
| "testing" | ||
| "time" | ||
|
|
||
| pb "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1" | ||
| "github.com/chainloop-dev/chainloop/pkg/attestation/crafter/runners" | ||
| "github.com/chainloop-dev/chainloop/pkg/attestation/crafter/runners/commitverification" | ||
| "github.com/chainloop-dev/chainloop/pkg/attestation/sandboxidentity" | ||
| "github.com/golang-jwt/jwt/v5" | ||
| "github.com/google/uuid" | ||
| "github.com/rs/zerolog" | ||
| "github.com/sigstore/sigstore/pkg/cryptoutils" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "google.golang.org/grpc" | ||
| "google.golang.org/grpc/credentials/insecure" | ||
| "google.golang.org/grpc/test/bufconn" | ||
| ) | ||
|
|
||
| func newIdentityTestCA(t *testing.T) (*x509.Certificate, *ecdsa.PrivateKey) { | ||
| t.Helper() | ||
|
|
||
| key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) | ||
| require.NoError(t, err) | ||
|
|
||
| tmpl := &x509.Certificate{ | ||
| SerialNumber: big.NewInt(1), Subject: pkix.Name{CommonName: "test root"}, | ||
| NotBefore: time.Now().Add(-time.Hour), NotAfter: time.Now().Add(time.Hour), | ||
| IsCA: true, BasicConstraintsValid: true, KeyUsage: x509.KeyUsageCertSign, SubjectKeyId: []byte("root"), | ||
| } | ||
|
|
||
| der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key) | ||
| require.NoError(t, err) | ||
| cert, err := x509.ParseCertificate(der) | ||
| require.NoError(t, err) | ||
|
|
||
| return cert, key | ||
| } | ||
|
|
||
| // trustedRootConn serves the CA as the trusted root of a fake control plane. | ||
| func trustedRootConn(t *testing.T, root *x509.Certificate) *grpc.ClientConn { | ||
| t.Helper() | ||
|
|
||
| rootPEM, err := cryptoutils.MarshalCertificateToPEM(root) | ||
| require.NoError(t, err) | ||
| keyID := sha256.Sum256(root.SubjectKeyId) | ||
|
|
||
| svc := &fakeSigningService{trustedRoot: &pb.GetTrustedRootResponse{ | ||
| Keys: map[string]*pb.CertificateChain{hex.EncodeToString(keyID[:]): {Certificates: []string{string(rootPEM)}}}, | ||
| }} | ||
|
|
||
| l := bufconn.Listen(1 << 20) | ||
| srv := grpc.NewServer() | ||
| pb.RegisterSigningServiceServer(srv, svc) | ||
| go func() { _ = srv.Serve(l) }() | ||
|
|
||
| conn, err := grpc.NewClient("passthrough:///bufnet", | ||
| grpc.WithContextDialer(func(ctx context.Context, _ string) (net.Conn, error) { return l.DialContext(ctx) }), | ||
| grpc.WithTransportCredentials(insecure.NewCredentials()), | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| t.Cleanup(func() { | ||
| conn.Close() | ||
| srv.Stop() | ||
| }) | ||
|
|
||
| return conn | ||
| } | ||
|
|
||
| func TestApplySandboxIdentity(t *testing.T) { | ||
| const commit = "ea3a974660a030f0314aa1505a64792bf2633b33" | ||
| wf := &pb.WorkflowItem{Id: uuid.NewString(), ProjectId: uuid.NewString()} | ||
| root, rootKey := newIdentityTestCA(t) | ||
|
|
||
| // A certificate with the mark, as the control plane CA gives it to the platform | ||
| key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) | ||
| require.NoError(t, err) | ||
| leafDER, err := x509.CreateCertificate(rand.Reader, &x509.Certificate{ | ||
| SerialNumber: big.NewInt(2), | ||
| Subject: pkix.Name{Organization: []string{"org-id"}, OrganizationalUnit: []string{sandboxidentity.CertificateMark}}, | ||
| NotBefore: time.Now().Add(-time.Minute), NotAfter: time.Now().Add(10 * time.Minute), | ||
| KeyUsage: x509.KeyUsageDigitalSignature, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageCodeSigning}, | ||
| }, root, &key.PublicKey, rootKey) | ||
| require.NoError(t, err) | ||
| leaf, err := x509.ParseCertificate(leafDER) | ||
| require.NoError(t, err) | ||
|
|
||
| sign := func(t *testing.T, workflowID string) string { | ||
| t.Helper() | ||
| raw, err := sandboxidentity.Sign(key, []*x509.Certificate{leaf}, &sandboxidentity.Claims{ | ||
| RegisteredClaims: jwt.RegisteredClaims{IssuedAt: jwt.NewNumericDate(time.Now())}, | ||
| OrganizationID: "org-id", ProjectID: wf.GetProjectId(), WorkflowID: workflowID, ChainloopTokenID: uuid.NewString(), | ||
| Data: &sandboxidentity.Data{CommitVerification: &sandboxidentity.CommitVerification{ | ||
| Commit: commit, | ||
| Verification: &commitverification.CommitVerification{Attempted: true, Status: commitverification.VerificationStatusVerified}, | ||
| }}, | ||
| }) | ||
| require.NoError(t, err) | ||
| return raw | ||
| } | ||
|
|
||
| verified := &pb.AttestationServiceInitResponse_SandboxIdentityResult{Verified: true} | ||
|
|
||
| testCases := []struct { | ||
| name string | ||
| raw string | ||
| cpResult *pb.AttestationServiceInitResponse_SandboxIdentityResult | ||
| conn *grpc.ClientConn | ||
| wantAuth bool | ||
| }{ | ||
| {name: "verified by the control plane and the CLI", raw: sign(t, wf.GetId()), cpResult: verified, conn: trustedRootConn(t, root), wantAuth: true}, | ||
| {name: "a control plane that does not know the identity", raw: sign(t, wf.GetId()), conn: trustedRootConn(t, root)}, | ||
| {name: "rejected by the control plane", raw: sign(t, wf.GetId()), cpResult: &pb.AttestationServiceInitResponse_SandboxIdentityResult{Reason: "another token"}, conn: trustedRootConn(t, root)}, | ||
| {name: "an identity for another workflow", raw: sign(t, uuid.NewString()), cpResult: verified, conn: trustedRootConn(t, root)}, | ||
| {name: "another trusted root", raw: sign(t, wf.GetId()), cpResult: verified, conn: newSigningServiceConn(t, true)}, | ||
| {name: "no trusted root", raw: sign(t, wf.GetId()), cpResult: verified, conn: newSigningServiceConn(t, false)}, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| action := &AttestationInit{ActionsOpts: &ActionsOpts{CPConnection: tc.conn, Logger: zerolog.Nop()}} | ||
| runner := runners.NewChainloopSandbox() | ||
|
|
||
| action.applySandboxIdentity(context.Background(), runner, tc.raw, tc.cpResult, wf) | ||
|
|
||
| assert.Equal(t, tc.wantAuth, runner.IsAuthenticated()) | ||
| if tc.wantAuth { | ||
| assert.NotNil(t, runner.VerifyCommitSignature(context.Background(), commit)) | ||
| } else { | ||
| assert.Nil(t, runner.VerifyCommitSignature(context.Background(), commit)) | ||
| } | ||
| }) | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I understood from the spec that the sandbox identity token was going to be put into the attestation, and verified from the attestation contents. This way, we could verify past attestations completely, not only on
att init.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 implementation follows the merged spec here. The spec keeps the attestation format as it is. The token goes in the attestation init request, and the control plane records the verified identity on the run (R-006, and "Other runners and the attestation format" in the Proposal). At push, the control plane compares the attestation with that record and rejects an authenticated sandbox runner without a verified identity (D-010).
The spec does not keep the raw token after init, so a later check relies on the record of the control plane. If you want to verify past attestations from their content alone, the CLI would record the token in the predicate. That changes the attestation format, so I would add it as a new decision in the spec, in this PR or as a follow-up. Which do you prefer?