main.go raw
1 // Command nostrino-release signs a release manifest, or checks one.
2 //
3 // The private key never leaves the machine that signs: operators sign on an offline machine and
4 // publish the signed JSON, and a gateway verifies it against the public key compiled into its
5 // configuration.
6 package main
7
8 import (
9 "crypto/ed25519"
10 "crypto/rand"
11 "encoding/hex"
12 "encoding/json"
13 "flag"
14 "fmt"
15 "os"
16 "path/filepath"
17 "strings"
18
19 "git.smesh.lol/nostrino/pkg/release"
20 )
21
22 func main() {
23 var (
24 artifacts = flag.String("artifacts", "", "directory of built binaries to describe")
25 version = flag.String("version", "", "release version, for example 0.3.0")
26 key = flag.String("key", "", "Ed25519 private key file (64 hex characters)")
27 out = flag.String("out", "", "write the signed manifest here (default standard output)")
28 base = flag.String("base-url", "", "URL prefix the artifacts are served from")
29 notes = flag.String("notes", "", "release notes")
30 minVer = flag.String("min-version", "", "oldest version allowed to take this release")
31 genkey = flag.Bool("genkey", false, "print a new Ed25519 release seed, then its public key")
32 pubFor = flag.String("pubkey-for", "", "print the public key for a private key file")
33 check = flag.String("check", "", "verify a signed manifest file")
34 pub = flag.String("pubkey", "", "public key for -check (64 hex characters)")
35 )
36 flag.Parse()
37
38 if *genkey {
39 seed := make([]byte, ed25519.SeedSize)
40
41 if _, err := rand.Read(seed); err != nil {
42 fail(err)
43 }
44
45 private := ed25519.NewKeyFromSeed(seed)
46
47 fmt.Println(hex.EncodeToString(seed))
48 fmt.Println(hex.EncodeToString(private.Public().(ed25519.PublicKey)))
49
50 return
51 }
52
53 if *pubFor != "" {
54 body, err := os.ReadFile(*pubFor)
55 if err != nil {
56 fail(err)
57 }
58 private, err := parsePrivate(string(body))
59 if err != nil {
60 fail(err)
61 }
62 fmt.Println(hex.EncodeToString(private.Public().(ed25519.PublicKey)))
63 return
64 }
65
66 if *check != "" {
67 body, err := os.ReadFile(*check)
68 if err != nil {
69 fail(err)
70 }
71 public, err := parsePublic(*pub)
72 if err != nil {
73 fail(err)
74 }
75 manifest, _, err := release.Verify(body, public)
76 if err != nil {
77 fail(err)
78 }
79 enc := json.NewEncoder(os.Stdout)
80 enc.SetIndent("", " ")
81 if err := enc.Encode(manifest); err != nil {
82 fail(err)
83 }
84 return
85 }
86
87 if *artifacts == "" || *version == "" || *key == "" {
88 fail(fmt.Errorf("need -artifacts, -version and -key (or -check and -pubkey)"))
89 }
90
91 private, err := parsePrivate(*key)
92 if err != nil {
93 fail(err)
94 }
95
96 entries, err := os.ReadDir(*artifacts)
97 if err != nil {
98 fail(err)
99 }
100
101 var list []release.Artifact
102
103 for _, entry := range entries {
104 if entry.IsDir() || !strings.HasPrefix(entry.Name(), "nostrino-gateway-") {
105 continue
106 }
107
108 // nostrino-gateway-<version>-<os>-<arch>
109 parts := strings.Split(strings.TrimSuffix(entry.Name(), ".exe"), "-")
110 if len(parts) < 5 {
111 continue
112 }
113
114 path := filepath.Join(*artifacts, entry.Name())
115 body, err := os.ReadFile(path)
116 if err != nil {
117 fail(err)
118 }
119 sum, err := release.SHA256Hex(body)
120 if err != nil {
121 fail(err)
122 }
123 list = append(list, release.Artifact{
124 OS: parts[len(parts)-2], Arch: parts[len(parts)-1],
125 URL: *base + "/" + entry.Name(), SHA256: sum, Size: int64(len(body)),
126 })
127 }
128
129 if len(list) == 0 {
130 fail(fmt.Errorf("no nostrino-gateway-<version>-<os>-<arch> files in %s", *artifacts))
131 }
132
133 manifest := release.Manifest{Version: *version, Artifacts: list, Notes: *notes, MinVersion: *minVer}
134 body, err := json.Marshal(manifest)
135 if err != nil {
136 fail(err)
137 }
138
139 // Compact, and compact for a reason: the signature covers the manifest bytes exactly as they
140 // appear in the envelope, and an indenting encoder would re-space those bytes and invalidate
141 // the signature it is writing.
142 envelope, err := json.Marshal(release.Signed{
143 Manifest: body, Signature: hex.EncodeToString(ed25519.Sign(private, body)),
144 })
145 if err != nil {
146 fail(err)
147 }
148
149 if *out == "" {
150 fmt.Println(string(envelope))
151 return
152 }
153
154 if err := os.WriteFile(*out, append(envelope, '\n'), 0o644); err != nil {
155 fail(err)
156 }
157 }
158
159 func parsePrivate(text string) (ed25519.PrivateKey, error) {
160 raw, err := hex.DecodeString(strings.TrimSpace(text))
161 if err != nil {
162 return nil, err
163 }
164 switch len(raw) {
165 case ed25519.PrivateKeySize:
166 return ed25519.PrivateKey(raw), nil
167 case ed25519.SeedSize:
168 return ed25519.NewKeyFromSeed(raw), nil
169 }
170 return nil, fmt.Errorf("key must be %d or %d bytes of hex", ed25519.SeedSize, ed25519.PrivateKeySize)
171 }
172
173 func parsePublic(text string) (ed25519.PublicKey, error) {
174 raw, err := hex.DecodeString(strings.TrimSpace(text))
175 if err != nil {
176 return nil, err
177 }
178 if len(raw) != ed25519.PublicKeySize {
179 return nil, fmt.Errorf("public key must be %d bytes of hex", ed25519.PublicKeySize)
180 }
181 return ed25519.PublicKey(raw), nil
182 }
183
184 func fail(err error) {
185 fmt.Fprintln(os.Stderr, "nostrino-release:", err)
186 os.Exit(1)
187 }
188