package packaging import ( "errors" "fmt" "io/fs" "os" "regexp" "slices" "strings" "testing" "github.com/devvmhq/devvm/internal/minisign" "github.com/devvmhq/devvm/internal/minisign/minisigntest" ) // The old release key is minisign.pub's, a minisign public key, once the // file is there, and none before. func TestMinisignKey(t *testing.T) { _, err := os.Stat("minisign.pub") switch k := MinisignKey(); { case err != nil && k != "": t.Errorf("no minisign.pub, but the key %q", k) case err == nil: if _, err := minisign.ParsePublicKey(k); err != nil { t.Errorf("minisign.pub's key %q: %v", k, err) } } } // releaseKeys are keys/'s release keys, by role: none until the real keys // are made, and a role without a key has no entry. func releaseKeys() map[minisign.Role]string { pub := map[minisign.Role]string{} for _, r := range minisign.Roles() { if k := Key(minisign.KeyFile(minisign.Release, r)); k != "" { pub[r] = k } } return pub } // checkReleaseKeys checks pub, the release keys by role: none, or three // different minisign keys, one for each role, none of them legacy, the old // release key, so its signature and one of theirs are never two of three. // legacy is "" when there's no old release key. func checkReleaseKeys(pub map[minisign.Role]string, legacy string) error { if len(pub) == 0 { return nil } if _, err := minisign.NewKeys(minisign.Release, pub); err != nil { return err } if legacy == "" { return nil } old, err := minisign.ParsePublicKey(legacy) if err != nil { return fmt.Errorf("the old release key: %w", err) } for _, r := range minisign.Roles() { k, _ := minisign.ParsePublicKey(pub[r]) // NewKeys parsed it if k.ID == old.ID || slices.Equal(k.Key, old.Key) { return fmt.Errorf("the %s key is the old release key", r) } } return nil } // checkInstallScriptKeys checks that script, an install.sh, has one // release__key line for each role, with pub's key, or empty when // pub has none. func checkInstallScriptKeys(script string, pub map[minisign.Role]string) error { for _, r := range minisign.Roles() { line := regexp.MustCompile(`(?m)^release_` + string(r) + `_key=(\S*)$`) got := line.FindAllStringSubmatch(script, -1) if len(got) != 1 || got[0][1] != pub[r] { return fmt.Errorf("install.sh's release_%s_key lines are %q, want one with %q", r, got, pub[r]) } } return nil } // The release keys in keys/ are none, until the real keys are made, or // all three, each a different minisign key and none the old release key's // (checkReleaseKeys). Every release-*.pub there is a key Key reads, so a // malformed file never passes as no key. A key is read only from keys/. func TestReleaseKeys(t *testing.T) { pub := releaseKeys() if err := checkReleaseKeys(pub, MinisignKey()); err != nil { t.Error(err) } names, err := fs.Glob(files, "keys/release-*.pub") if err != nil { t.Fatal(err) } if len(names) != len(pub) { t.Errorf("keys/ has %d release key files, %v, but %d keys read from them", len(names), names, len(pub)) } if k := Key("../minisign.pub"); k != "" { t.Errorf("Key read %q from outside keys/", k) } if k := Key("nope.pub"); k != "" { t.Errorf("Key read %q from a file that isn't there", k) } } // checkReleaseKeys passes none or three good keys, and refuses one or // two, a key that isn't one, one key in two roles, and the old release // key as a release key, so the keys, once added, are checked as // TestReleaseKeys checks them. func TestCheckReleaseKeys(t *testing.T) { ci, cosign, recovery, old := minisigntest.NewKey().Public(), minisigntest.NewKey().Public(), minisigntest.NewKey().Public(), minisigntest.NewKey().Public() for _, tc := range []struct { name string pub map[minisign.Role]string ok bool }{ {"none", nil, true}, {"three", map[minisign.Role]string{minisign.CI: ci, minisign.Cosign: cosign, minisign.Recovery: recovery}, true}, {"one", map[minisign.Role]string{minisign.CI: ci}, false}, {"two", map[minisign.Role]string{minisign.CI: ci, minisign.Cosign: cosign}, false}, {"not a key", map[minisign.Role]string{minisign.CI: ci, minisign.Cosign: "nope", minisign.Recovery: recovery}, false}, {"one key twice", map[minisign.Role]string{minisign.CI: ci, minisign.Cosign: cosign, minisign.Recovery: ci}, false}, {"the old key", map[minisign.Role]string{minisign.CI: ci, minisign.Cosign: cosign, minisign.Recovery: old}, false}, } { if err := checkReleaseKeys(tc.pub, old); (err == nil) != tc.ok { t.Errorf("%s: %v", tc.name, err) } } } // A key marked PLACEHOLDER in keys/, such as a stand-in nobody holds the // secret half of, fails here before scripts/release.sh refuses it. func TestReleaseRefusesPlaceholders(t *testing.T) { err := fs.WalkDir(files, "keys", func(path string, d fs.DirEntry, err error) error { if err != nil || d.IsDir() { return err } b, err := files.ReadFile(path) if err == nil && strings.Contains(string(b), "PLACEHOLDER") { t.Errorf("%s is marked PLACEHOLDER, which release.sh refuses", path) } return err }) if err != nil && !errors.Is(err, fs.ErrNotExist) { t.Fatal(err) } } // install.sh checks a release with the same keys as devvm upgrade: its // release__key lines hold keys/'s, each once, or are empty while // keys/ has none. func TestInstallScriptKeys(t *testing.T) { b, err := os.ReadFile("../install.sh") if err != nil { t.Fatal(err) } if err := checkInstallScriptKeys(string(b), releaseKeys()); err != nil { t.Error(err) } pub := map[minisign.Role]string{minisign.CI: "RWci", minisign.Cosign: "RWcosign", minisign.Recovery: "RWrecovery"} for name, tc := range map[string]struct { script string pub map[minisign.Role]string ok bool }{ "no keys, empty lines": {"release_ci_key=\nrelease_cosign_key=\nrelease_recovery_key=\n", nil, true}, "the keys, their lines": {"release_ci_key=RWci\nrelease_cosign_key=RWcosign\nrelease_recovery_key=RWrecovery\n", pub, true}, "no keys, a key's line": {"release_ci_key=RWci\nrelease_cosign_key=\nrelease_recovery_key=\n", nil, false}, "the keys, empty lines": {"release_ci_key=\nrelease_cosign_key=\nrelease_recovery_key=\n", pub, false}, "another key": {"release_ci_key=RWci\nrelease_cosign_key=RWother\nrelease_recovery_key=RWrecovery\n", pub, false}, "a line missing": {"release_ci_key=RWci\nrelease_cosign_key=RWcosign\n", pub, false}, "a line twice": {"release_ci_key=RWci\nrelease_ci_key=RWci\nrelease_cosign_key=RWcosign\nrelease_recovery_key=RWrecovery\n", pub, false}, } { if err := checkInstallScriptKeys(tc.script, tc.pub); (err == nil) != tc.ok { t.Errorf("%s: %v", name, err) } } } // The docs that show the release key show minisign.pub's, so you check a // release against the key that signed it: README.md, SECURITY.md and // the install page's section have it, and no page has another. func TestDocsShowTheKey(t *testing.T) { key := MinisignKey() if key == "" { t.Skip("no minisign.pub") } // A minisign public key as minisign -P takes it: "RW" and the rest of // its 42 bytes in base64. keyLike := regexp.MustCompile(`RW[A-Za-z0-9+/]{54}`) repo := os.DirFS("..") files := []string{"README.md", "SECURITY.md"} err := fs.WalkDir(repo, "docs/content", func(path string, d fs.DirEntry, err error) error { if err == nil && !d.IsDir() && strings.HasSuffix(path, ".md") { files = append(files, path) } return err }) if err != nil { t.Fatal(err) } want := map[string]bool{"README.md": false, "SECURITY.md": false, "docs/content/docs/internals/install.md": false} for _, f := range files { b, err := fs.ReadFile(repo, f) if err != nil { t.Fatal(err) } for _, k := range keyLike.FindAllString(string(b), -1) { if k != key { t.Errorf("%s shows the key %s, not minisign.pub's %s", f, k, key) continue } if _, ok := want[f]; ok { want[f] = true } } } for f, shown := range want { if !shown { t.Errorf("%s doesn't show the release key %s", f, key) } } }