trust: the verifying half of publisher signatures (own module)
Moved from yet/internal/trust, behavior and file formats unchanged, so yacota (images) checks against the same trust-policy.json and known-keys.json as yet (packages): the signature payload and its check (Verify over fetched referrers), known keys with TOFU pinning (Reconcile; refusals are ErrRefused), the trust policy (Evaluate, AllowsCapability), Curated, Pinned and Home (yet's data directory). Signing, key storage and the registry side stay in yet. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
764b6f36ea
commit
35f8b7d18a
11 files changed
+1921
-3
No files matched your search
@@ -0,0 +1,76 @@
|
||||
package trust
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"yetano.dev/yetano/core"
|
||||
)
|
||||
|
||||
func TestVerifyPayloads(t *testing.T) {
|
||||
kp, err := generateKey()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
digest := "sha256:" + onesDigest()
|
||||
sig, err := testSign(kp, digest)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
good, _ := json.Marshal(sig)
|
||||
got := Verify(digest, []Payload{{Data: good}, {Err: errors.New("fetch failed")}, {Data: []byte("{")}})
|
||||
if len(got) != 3 || !got[0].Valid || got[0].KeyID != ID(kp.Public) || got[1].Error != "fetch failed" || got[2].Valid || got[2].Error == "" {
|
||||
t.Fatalf("%+v", got)
|
||||
}
|
||||
if v := Verify("sha256:"+zeros(), []Payload{{Data: good}}); v[0].Valid {
|
||||
t.Error("a signature over another digest is valid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRefused(t *testing.T) {
|
||||
err := fmt.Errorf("pull: %w", Refusedf("refusing %s", "x"))
|
||||
if !errors.Is(err, ErrRefused) || err.Error() != "pull: refusing x" {
|
||||
t.Fatalf("%v", err)
|
||||
}
|
||||
if errors.Is(errors.New("other"), ErrRefused) {
|
||||
t.Fatal("any error is refused")
|
||||
}
|
||||
// What Reconcile returns for a changed key.
|
||||
path := filepath.Join(t.TempDir(), "known-keys.json")
|
||||
a, _ := generateKey()
|
||||
b, _ := generateKey()
|
||||
d := "sha256:" + onesDigest()
|
||||
sa, _ := testSign(a, d)
|
||||
sb, _ := testSign(b, d)
|
||||
if err := Reconcile(path, "r/x", []VerifiedSignature{VerifySignature(d, sa)}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := Reconcile(path, "r/x", []VerifiedSignature{VerifySignature(d, sb)}); !errors.Is(err, ErrRefused) {
|
||||
t.Fatalf("a changed key: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCuratedAndPinned(t *testing.T) {
|
||||
p := Policy{Rules: []Rule{{Namespace: "yetano/*", AllowedKeys: []KeyID{"1"}}, {Namespace: "library/nginx"}}}
|
||||
for name, want := range map[string]bool{"yetano/yet": true, "yetano": false, "library/nginx": true, "library/alpine": false} {
|
||||
if p.Curated(name) != want {
|
||||
t.Errorf("Curated(%s) = %v", name, !want)
|
||||
}
|
||||
}
|
||||
keys := KnownKeys{"docker.io/library/nginx": {{KeyID: "1"}}}
|
||||
if !keys.Pinned("docker.io/library/nginx") || keys.Pinned("docker.io/library/alpine") {
|
||||
t.Error("Pinned")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHome(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
t.Setenv("YET_DATA_HOME", dir)
|
||||
got, err := Home(core.UserScope)
|
||||
if err != nil || got != dir {
|
||||
t.Fatalf("Home = %q, %v; want %q (yet's data directory)", got, err, dir)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user