Repository navigation
tufclient: authenticate repo requests as this machine #82
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
joaquimrocha
wants to merge
3
commits into
main
Choose a base branch
from
eng-233-wit-signed-fetch
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.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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
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
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
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
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,184 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // Copyright (C) 2026 Amutable GmbH | ||
|
|
||
| //go:build fixtures | ||
|
|
||
| // acp-sample-gen regenerates the control plane's | ||
| // deploy/cloudflare/quarry-worker/test/fixtures/go-signed-read.json, which | ||
| // pins the Worker's verifier to what Transport puts on the wire: | ||
| // | ||
| // go run -tags fixtures ./internal/fixtures/acp-sample-gen \ | ||
| // <control-plane>/deploy/cloudflare/quarry-worker/test/fixtures/go-signed-read.json | ||
| // | ||
| // Transport gets its WIT from a fake identity agent, which mints one with a | ||
| // throwaway tenant key and binds the key Transport asked for. | ||
| package main | ||
|
|
||
| import ( | ||
| "crypto/ecdsa" | ||
| "crypto/elliptic" | ||
| "crypto/rand" | ||
| "crypto/sha256" | ||
| "encoding/base64" | ||
| "encoding/json" | ||
| "fmt" | ||
| "log" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "os" | ||
| "time" | ||
|
|
||
| jose "github.com/go-jose/go-jose/v4" | ||
| "snai.pe/go-varlink" | ||
|
|
||
| "go.amutable.dev/quarry/internal/wimseagent" | ||
| ) | ||
|
|
||
| const ( | ||
| broker = "https://wit-broker.test" | ||
| tenant = "acme" | ||
| stamp = "test.example" | ||
| machine = "0123456789abcdef0123456789abcdef" | ||
| audience = "urn:amutable:quarry-worker" | ||
| kid = "tenant-key" | ||
| ) | ||
|
|
||
| var b64 = base64.RawURLEncoding.EncodeToString | ||
|
|
||
| func es256JWT(key *ecdsa.PrivateKey, header, claims any) (string, error) { | ||
| h, err := json.Marshal(header) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| c, err := json.Marshal(claims) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| signing := b64(h) + "." + b64(c) | ||
| digest := sha256.Sum256([]byte(signing)) | ||
| r, s, err := ecdsa.Sign(rand.Reader, key, digest[:]) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| sig := make([]byte, 64) | ||
| r.FillBytes(sig[:32]) | ||
| s.FillBytes(sig[32:]) | ||
| return signing + "." + b64(sig), nil | ||
| } | ||
|
|
||
| // p256JWK returns the JWK to bind into the WIT for jwk. Like the real agent, | ||
| // it only accepts P-256 public keys with "alg" ES256, which WIMSE requires. | ||
| func p256JWK(jwk jose.JSONWebKey) (jose.JSONWebKey, error) { | ||
| pub, ok := jwk.Key.(*ecdsa.PublicKey) | ||
| if !ok || pub.Curve != elliptic.P256() || !jwk.Valid() { | ||
| return jose.JSONWebKey{}, fmt.Errorf("not a P-256 public key: %T", jwk.Key) | ||
| } | ||
| if jwk.Algorithm != string(jose.ES256) { | ||
| return jose.JSONWebKey{}, fmt.Errorf("alg %q does not match a P-256 key", jwk.Algorithm) | ||
| } | ||
| return jose.JSONWebKey{Key: pub, Algorithm: string(jose.ES256)}, nil | ||
| } | ||
|
|
||
| // startAgent serves GetWorkloadIdentityToken from a socket in dir, replying | ||
| // with a WIT signed by tenantKey and bound to the requested key. | ||
| func startAgent(dir string, tenantKey *ecdsa.PrivateKey, now time.Time) (string, error) { | ||
| uri := "unix:" + dir + "/agent" | ||
| listener, err := varlink.Listen(uri) | ||
| if err != nil { | ||
| return "", fmt.Errorf("listen on fake agent socket: %w", err) | ||
| } | ||
| var mux varlink.ServeMux | ||
| mux.HandleFunc("com.amutable.IdentityAgent.GetWorkloadIdentityToken", func(w varlink.ReplyWriter, c *varlink.Call) { | ||
| var p struct { | ||
| Audience string `json:"aud"` | ||
| PublicKey jose.JSONWebKey `json:"jwk_pub"` | ||
| } | ||
| if err := c.Unmarshal(&p); err != nil { | ||
| _ = w.WriteError(err) | ||
| return | ||
| } | ||
| pub, err := p256JWK(p.PublicKey) | ||
| if err != nil { | ||
| _ = w.WriteError(varlink.NewError("org.varlink.service.InvalidParameter", "parameter", "jwk_pub")) | ||
| return | ||
| } | ||
| wit, err := es256JWT(tenantKey, | ||
| map[string]string{"alg": "ES256", "typ": "wit+jwt", "kid": kid}, | ||
| map[string]any{ | ||
| "iss": broker + "/tenants/" + tenant, | ||
| "sub": "spiffe://" + tenant + "." + stamp + "/machine/" + machine, | ||
| "aud": []string{p.Audience}, | ||
| "iat": now.Unix(), | ||
| "exp": now.Add(time.Hour).Unix(), | ||
| "jti": "fixture", | ||
| "cnf": map[string]any{"jwk": pub}, | ||
| }) | ||
| if err != nil { | ||
| _ = w.WriteError(varlink.NewError("com.amutable.IdentityAgent.Failed", "error", err.Error())) | ||
| return | ||
| } | ||
| _ = w.WriteReply(map[string]any{"wit": wit, "expires_in": int64(time.Hour / time.Second)}) | ||
| }) | ||
| srv := &varlink.Server{Handler: &mux} | ||
| go func() { _ = srv.Serve(listener) }() | ||
| return uri, nil | ||
| } | ||
|
|
||
| func run(out string) error { | ||
| tenantKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| now := time.Now() | ||
|
|
||
| dir, err := os.MkdirTemp("", "quarry-worker-fixture-") //nolint:forbidigo // fresh private directory | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer func() { _ = os.RemoveAll(dir) }() //nolint:forbidigo // our own temporary directory | ||
| agent, err := startAgent(dir, tenantKey, now) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| var header http.Header | ||
| srv := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { | ||
| header = r.Header.Clone() | ||
| })) | ||
| defer srv.Close() | ||
| cred, err := wimseagent.NewCred(wimseagent.ID{WitAudience: audience, Provider: agent}, nil) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| client := &http.Client{Transport: &wimseagent.Transport{WitCred: cred}} | ||
| reqURL := srv.URL + "/machine-id/" + machine + "/1.root.json" | ||
| resp, err := client.Get(reqURL) //nolint:noctx // one-shot script | ||
| if err != nil { | ||
| return err | ||
| } | ||
| _ = resp.Body.Close() // empty body from the capture handler | ||
|
|
||
| jwk := jose.JSONWebKey{Key: &tenantKey.PublicKey, KeyID: kid, Algorithm: "ES256", Use: "sig"} | ||
| data, err := json.MarshalIndent(map[string]any{ | ||
| "now": now.Unix(), | ||
| "url": reqURL, | ||
| "jwks": map[string]any{"keys": []any{jwk}}, | ||
| "wit": header.Get("Workload-Identity-Token"), | ||
| "signatureInput": header.Get("Signature-Input"), | ||
| "signature": header.Get("Signature"), | ||
| }, "", " ") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return os.WriteFile(out, append(data, '\n'), 0o644) //nolint:forbidigo // path given by the user | ||
| } | ||
|
|
||
| func main() { | ||
| if len(os.Args) != 2 { | ||
| fmt.Fprintf(os.Stderr, "usage: %s <go-signed-read.json>\n", os.Args[0]) | ||
| os.Exit(2) | ||
| } | ||
| if err := run(os.Args[1]); err != nil { | ||
| log.Fatal(err) | ||
| } | ||
| } |
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,13 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // Copyright (C) 2026 Amutable GmbH | ||
|
|
||
| package generics | ||
|
|
||
| // TakeOne takes the first value out of a slice and returns the remaining slice. | ||
| // If the slice is empty or nil then (nil, s) is returned. | ||
| func TakeOne[S ~[]E, E any](s S) (*E, []E) { | ||
| if len(s) > 0 { | ||
| return Ptr(s[0]), s[1:] | ||
| } | ||
| return nil, s | ||
| } |
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.
This is quite unfortunate, though since only the method and URI are included in the signature I guess we can eventually pre-compute the signed headers and provide them to
sd-pullonce it switches to UAPI.16 (and a UAPI.16 extension is added to support it).@davidstrauss Do we plan to extend the scope of things we would sign for WIMSE/WIT? If we start signing other headers then we would just need to accept that we are going to be proxying data a lot. (Though we could of course choose to proxy based n what headers are needed to be signed...)
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 might need more context, but I wasn't expecting we'd use WIT/WIMSE for repo auth at all. I thought we only wanted to rely on authZ mechanisms that (1) we can delegate to the delivery system itself (mainly for cost, DoS protection, and maybe some basic infrastructure disclosure control) or (2) we can achieve via sealing the data going through Quarry (for load-bearing secret delivery to specific machines).
In short, I assumed our entire model would work fine with publicly readable buckets.
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.
This is the "front door" access control we discussed in January, BTW.