// Command nostrino-release signs a release manifest, or checks one. // // The private key never leaves the machine that signs: operators sign on an offline machine and // publish the signed JSON, and a gateway verifies it against the public key compiled into its // configuration. package main import ( "crypto/ed25519" "crypto/rand" "encoding/hex" "encoding/json" "flag" "fmt" "os" "path/filepath" "strings" "git.smesh.lol/nostrino/pkg/release" ) func main() { var ( artifacts = flag.String("artifacts", "", "directory of built binaries to describe") version = flag.String("version", "", "release version, for example 0.3.0") key = flag.String("key", "", "Ed25519 private key file (64 hex characters)") out = flag.String("out", "", "write the signed manifest here (default standard output)") base = flag.String("base-url", "", "URL prefix the artifacts are served from") notes = flag.String("notes", "", "release notes") minVer = flag.String("min-version", "", "oldest version allowed to take this release") genkey = flag.Bool("genkey", false, "print a new Ed25519 release seed, then its public key") pubFor = flag.String("pubkey-for", "", "print the public key for a private key file") check = flag.String("check", "", "verify a signed manifest file") pub = flag.String("pubkey", "", "public key for -check (64 hex characters)") ) flag.Parse() if *genkey { seed := make([]byte, ed25519.SeedSize) if _, err := rand.Read(seed); err != nil { fail(err) } private := ed25519.NewKeyFromSeed(seed) fmt.Println(hex.EncodeToString(seed)) fmt.Println(hex.EncodeToString(private.Public().(ed25519.PublicKey))) return } if *pubFor != "" { body, err := os.ReadFile(*pubFor) if err != nil { fail(err) } private, err := parsePrivate(string(body)) if err != nil { fail(err) } fmt.Println(hex.EncodeToString(private.Public().(ed25519.PublicKey))) return } if *check != "" { body, err := os.ReadFile(*check) if err != nil { fail(err) } public, err := parsePublic(*pub) if err != nil { fail(err) } manifest, _, err := release.Verify(body, public) if err != nil { fail(err) } enc := json.NewEncoder(os.Stdout) enc.SetIndent("", " ") if err := enc.Encode(manifest); err != nil { fail(err) } return } if *artifacts == "" || *version == "" || *key == "" { fail(fmt.Errorf("need -artifacts, -version and -key (or -check and -pubkey)")) } private, err := parsePrivate(*key) if err != nil { fail(err) } entries, err := os.ReadDir(*artifacts) if err != nil { fail(err) } var list []release.Artifact for _, entry := range entries { if entry.IsDir() || !strings.HasPrefix(entry.Name(), "nostrino-gateway-") { continue } // nostrino-gateway--- parts := strings.Split(strings.TrimSuffix(entry.Name(), ".exe"), "-") if len(parts) < 5 { continue } path := filepath.Join(*artifacts, entry.Name()) body, err := os.ReadFile(path) if err != nil { fail(err) } sum, err := release.SHA256Hex(body) if err != nil { fail(err) } list = append(list, release.Artifact{ OS: parts[len(parts)-2], Arch: parts[len(parts)-1], URL: *base + "/" + entry.Name(), SHA256: sum, Size: int64(len(body)), }) } if len(list) == 0 { fail(fmt.Errorf("no nostrino-gateway--- files in %s", *artifacts)) } manifest := release.Manifest{Version: *version, Artifacts: list, Notes: *notes, MinVersion: *minVer} body, err := json.Marshal(manifest) if err != nil { fail(err) } // Compact, and compact for a reason: the signature covers the manifest bytes exactly as they // appear in the envelope, and an indenting encoder would re-space those bytes and invalidate // the signature it is writing. envelope, err := json.Marshal(release.Signed{ Manifest: body, Signature: hex.EncodeToString(ed25519.Sign(private, body)), }) if err != nil { fail(err) } if *out == "" { fmt.Println(string(envelope)) return } if err := os.WriteFile(*out, append(envelope, '\n'), 0o644); err != nil { fail(err) } } func parsePrivate(text string) (ed25519.PrivateKey, error) { raw, err := hex.DecodeString(strings.TrimSpace(text)) if err != nil { return nil, err } switch len(raw) { case ed25519.PrivateKeySize: return ed25519.PrivateKey(raw), nil case ed25519.SeedSize: return ed25519.NewKeyFromSeed(raw), nil } return nil, fmt.Errorf("key must be %d or %d bytes of hex", ed25519.SeedSize, ed25519.PrivateKeySize) } func parsePublic(text string) (ed25519.PublicKey, error) { raw, err := hex.DecodeString(strings.TrimSpace(text)) if err != nil { return nil, err } if len(raw) != ed25519.PublicKeySize { return nil, fmt.Errorf("public key must be %d bytes of hex", ed25519.PublicKeySize) } return ed25519.PublicKey(raw), nil } func fail(err error) { fmt.Fprintln(os.Stderr, "nostrino-release:", err) os.Exit(1) }