| 1 | // valley-web-publish publishes a valley host's public projects to a web |
| 2 | // receiver. One run is one pass. It reads the valley through the CLI's JSON |
| 3 | // commands, projects what it read onto the public export (package export), |
| 4 | // signs a manifest for it, and uploads it by the protocol package export |
| 5 | // defines. The receiver never connects to the host; its replies are all the |
| 6 | // publisher hears from it. |
| 7 | // |
| 8 | // A pass, in order: |
| 9 | // |
| 10 | // 1. take the lock in the state directory, so two passes never overlap; |
| 11 | // 2. read the export key from the state directory, or make one on the |
| 12 | // first start, and say its verifier line whenever it changes; |
| 13 | // 3. fetch main of the instance repository into the publisher's own bare |
| 14 | // repository, whose origin it is, and read the instance's policy/web; |
| 15 | // 4. run valley plan --all --json and valley pending --all --json in that |
| 16 | // repository, which discover the projects and the signers through the |
| 17 | // host's own publications; |
| 18 | // 5. for each project the instance allows, fetch main into a mirror under |
| 19 | // the cache and read the project's own policy/web there; |
| 20 | // 6. run valley knot show --project P <id> --json for every knot of every |
| 21 | // public project, and build the export with export.Build, reading |
| 22 | // again when the inputs were read at different graph tips; |
| 23 | // 7. upload it, unless the receiver acknowledged the same content digest |
| 24 | // less than --recheck ago, or write it to --out instead. |
| 25 | // |
| 26 | // A project is public only when the instance's policy/web and its own both |
| 27 | // say public (schema/web.cue). A declaration that is missing, or that the |
| 28 | // schema refuses, means private. |
| 29 | package main |
| 30 | |
| 31 | import ( |
| 32 | "context" |
| 33 | "crypto/ed25519" |
| 34 | "errors" |
| 35 | "flag" |
| 36 | "fmt" |
| 37 | "io" |
| 38 | "net/http" |
| 39 | "os" |
| 40 | "path/filepath" |
| 41 | "strings" |
| 42 | "time" |
| 43 | |
| 44 | "the-valley/webui/export" |
| 45 | ) |
| 46 | |
| 47 | const usage = `usage: valley-web-publish --instance-repo URL --instance NAME --destination NAME |
| 48 | (--receiver URL | --out DIR) [--principals FILE] |
| 49 | [--state DIR] [--schema FILE] [--recheck DURATION] |
| 50 | |
| 51 | One pass of the web publisher: read the valley's public projects, and |
| 52 | upload them to the receiver at URL, or write them to DIR. |
| 53 | |
| 54 | --instance-repo URL the instance repository: the origin of the |
| 55 | publisher's own bare repository, which the valley |
| 56 | CLI discovers projects through |
| 57 | --instance NAME the instance the manifest names, and the export |
| 58 | key's signing name, NAME/web-export |
| 59 | --destination NAME the receiver the manifest is for |
| 60 | --receiver URL the receiver's base URL, http or https |
| 61 | --out DIR write the export to DIR, which must not exist or be |
| 62 | empty, instead of uploading it: manifest, export.json |
| 63 | and bundles/<project>.bundle |
| 64 | --principals FILE the display mapping: a JSON object from each |
| 65 | principal's name to the name an export shows; a |
| 66 | name it does not map is shown as contributor |
| 67 | --state DIR the key, its verifier line, the lock and the last |
| 68 | acknowledgement (default |
| 69 | ${XDG_STATE_HOME:-$HOME/.local/state}/valley/web-publish) |
| 70 | --schema FILE schema/web.cue (default $VALLEY_WEB_SCHEMA) |
| 71 | --recheck DURATION how long an acknowledgement is trusted: a pass whose |
| 72 | digest the receiver acknowledged less than this ago |
| 73 | sends nothing (default 1h) |
| 74 | |
| 75 | The cache is $XDG_CACHE_HOME (default $HOME/.cache): the valley CLI's |
| 76 | clones under valley/, the publisher's repository and mirrors under |
| 77 | valley-web-publish/.` |
| 78 | |
| 79 | func main() { |
| 80 | os.Exit(run(context.Background(), os.Args[1:], os.Stdout, os.Stderr)) |
| 81 | } |
| 82 | |
| 83 | func run(ctx context.Context, args []string, stdout, stderr io.Writer) int { |
| 84 | cfg, err := parseArgs(args, stderr) |
| 85 | if errors.Is(err, flag.ErrHelp) { |
| 86 | fmt.Fprintln(stdout, usage) |
| 87 | return 0 |
| 88 | } |
| 89 | if err != nil { |
| 90 | fmt.Fprintf(stderr, "valley-web-publish: %v\n%s\n", err, usage) |
| 91 | return 2 |
| 92 | } |
| 93 | p, err := newPublisher(cfg, stdout, stderr) |
| 94 | if err == nil { |
| 95 | err = p.pass(ctx) |
| 96 | } |
| 97 | if err != nil { |
| 98 | fmt.Fprintf(stderr, "valley-web-publish: %v\n", err) |
| 99 | return 1 |
| 100 | } |
| 101 | return 0 |
| 102 | } |
| 103 | |
| 104 | // config is a pass's flags. |
| 105 | type config struct { |
| 106 | instanceRepo string |
| 107 | instance string |
| 108 | destination string |
| 109 | receiver string |
| 110 | out string |
| 111 | principals string |
| 112 | state string |
| 113 | schema string |
| 114 | recheck time.Duration |
| 115 | } |
| 116 | |
| 117 | func parseArgs(args []string, stderr io.Writer) (config, error) { |
| 118 | var c config |
| 119 | fs := flag.NewFlagSet("valley-web-publish", flag.ContinueOnError) |
| 120 | fs.SetOutput(io.Discard) |
| 121 | fs.StringVar(&c.instanceRepo, "instance-repo", "", "") |
| 122 | fs.StringVar(&c.instance, "instance", "", "") |
| 123 | fs.StringVar(&c.destination, "destination", "", "") |
| 124 | fs.StringVar(&c.receiver, "receiver", "", "") |
| 125 | fs.StringVar(&c.out, "out", "", "") |
| 126 | fs.StringVar(&c.principals, "principals", "", "") |
| 127 | fs.StringVar(&c.state, "state", "", "") |
| 128 | fs.StringVar(&c.schema, "schema", os.Getenv("VALLEY_WEB_SCHEMA"), "") |
| 129 | fs.DurationVar(&c.recheck, "recheck", time.Hour, "") |
| 130 | if err := fs.Parse(args); err != nil { |
| 131 | return c, err |
| 132 | } |
| 133 | switch { |
| 134 | case fs.NArg() > 0: |
| 135 | return c, fmt.Errorf("unexpected argument %q", fs.Arg(0)) |
| 136 | case c.instanceRepo == "": |
| 137 | return c, errors.New("--instance-repo is required") |
| 138 | case !export.ValidName(c.instance): |
| 139 | return c, fmt.Errorf("--instance %q is not a name: it is 1 to 64 letters, digits, dots, dashes and underscores, opening with a letter or a digit", c.instance) |
| 140 | case !export.ValidName(c.destination): |
| 141 | return c, fmt.Errorf("--destination %q is not a name: it is 1 to 64 letters, digits, dots, dashes and underscores, opening with a letter or a digit", c.destination) |
| 142 | case (c.receiver == "") == (c.out == ""): |
| 143 | return c, errors.New("give exactly one of --receiver and --out") |
| 144 | case c.schema == "": |
| 145 | return c, errors.New("no schema: give --schema, or set VALLEY_WEB_SCHEMA") |
| 146 | case c.recheck < 0: |
| 147 | return c, errors.New("--recheck is not negative") |
| 148 | } |
| 149 | if c.receiver != "" { |
| 150 | if err := checkReceiver(c.receiver); err != nil { |
| 151 | return c, err |
| 152 | } |
| 153 | } |
| 154 | if c.state == "" { |
| 155 | base := os.Getenv("XDG_STATE_HOME") |
| 156 | if base == "" { |
| 157 | home, err := os.UserHomeDir() |
| 158 | if err != nil { |
| 159 | return c, errors.New("no state directory: give --state, or set XDG_STATE_HOME or HOME") |
| 160 | } |
| 161 | base = filepath.Join(home, ".local", "state") |
| 162 | } |
| 163 | c.state = filepath.Join(base, "valley", "web-publish") |
| 164 | } |
| 165 | return c, nil |
| 166 | } |
| 167 | |
| 168 | // publisher is one pass's configuration and what it reads and writes |
| 169 | // through: its state and cache directories, its key, the receiver, and |
| 170 | // the clock. |
| 171 | type publisher struct { |
| 172 | cfg config |
| 173 | stdout, stderr io.Writer |
| 174 | |
| 175 | // cache is the publisher's own cache, under $XDG_CACHE_HOME. origin is |
| 176 | // its bare repository, whose origin is the instance repository. |
| 177 | cache string |
| 178 | origin string |
| 179 | |
| 180 | principals export.Principals |
| 181 | signer string |
| 182 | key ed25519.PrivateKey |
| 183 | |
| 184 | client *http.Client |
| 185 | now func() time.Time |
| 186 | sleep func(context.Context, time.Duration) error |
| 187 | } |
| 188 | |
| 189 | func newPublisher(cfg config, stdout, stderr io.Writer) (*publisher, error) { |
| 190 | base := os.Getenv("XDG_CACHE_HOME") |
| 191 | if base == "" { |
| 192 | home, err := os.UserHomeDir() |
| 193 | if err != nil { |
| 194 | return nil, errors.New("no cache directory: set XDG_CACHE_HOME or HOME") |
| 195 | } |
| 196 | base = filepath.Join(home, ".cache") |
| 197 | } |
| 198 | if !filepath.IsAbs(base) { |
| 199 | return nil, fmt.Errorf("XDG_CACHE_HOME is %q, which is not an absolute path", base) |
| 200 | } |
| 201 | principals, err := readPrincipals(cfg.principals) |
| 202 | if err != nil { |
| 203 | return nil, err |
| 204 | } |
| 205 | cache := filepath.Join(base, "valley-web-publish") |
| 206 | return &publisher{ |
| 207 | cfg: cfg, stdout: stdout, stderr: stderr, |
| 208 | cache: cache, origin: filepath.Join(cache, "origin.git"), |
| 209 | principals: principals, |
| 210 | signer: cfg.instance + "/web-export", |
| 211 | client: newClient(), |
| 212 | now: time.Now, |
| 213 | sleep: sleepContext, |
| 214 | }, nil |
| 215 | } |
| 216 | |
| 217 | // pass is one run of the publisher. |
| 218 | func (p *publisher) pass(ctx context.Context) error { |
| 219 | if err := os.MkdirAll(p.cfg.state, 0o700); err != nil { |
| 220 | return err |
| 221 | } |
| 222 | unlock, err := lockFile(filepath.Join(p.cfg.state, "lock")) |
| 223 | if err != nil { |
| 224 | return err |
| 225 | } |
| 226 | defer unlock() |
| 227 | if p.key, err = p.loadKey(); err != nil { |
| 228 | return err |
| 229 | } |
| 230 | read, err := p.collect(ctx) |
| 231 | if err != nil { |
| 232 | return err |
| 233 | } |
| 234 | digest, err := read.export.Digest() |
| 235 | if err != nil { |
| 236 | return err |
| 237 | } |
| 238 | p.sayVisibility(read) |
| 239 | if p.cfg.out != "" { |
| 240 | return p.writeOut(ctx, read) |
| 241 | } |
| 242 | ack, err := p.readAcknowledged() |
| 243 | if err != nil { |
| 244 | return err |
| 245 | } |
| 246 | if ack.covers(p.cfg, digest, p.now(), p.cfg.recheck) { |
| 247 | p.say("the receiver acknowledged digest %s at %s, and nothing has changed since; nothing is sent", short(digest), ack.At) |
| 248 | return nil |
| 249 | } |
| 250 | return p.upload(ctx, read, digest, ack) |
| 251 | } |
| 252 | |
| 253 | // sayVisibility says which projects this pass exports, and which it keeps |
| 254 | // private. |
| 255 | func (p *publisher) sayVisibility(r *reading) { |
| 256 | var public []string |
| 257 | for _, proj := range r.export.Projects { |
| 258 | public = append(public, proj.Name) |
| 259 | } |
| 260 | list := func(names []string) string { |
| 261 | if len(names) == 0 { |
| 262 | return "none" |
| 263 | } |
| 264 | return strings.Join(names, ", ") |
| 265 | } |
| 266 | p.say("public: %s; private: %s", list(public), list(r.private)) |
| 267 | } |
| 268 | |
| 269 | func (p *publisher) say(format string, args ...any) { |
| 270 | fmt.Fprintf(p.stdout, "valley-web-publish: "+format+"\n", args...) |
| 271 | } |
| 272 | |
| 273 | func (p *publisher) warn(format string, args ...any) { |
| 274 | fmt.Fprintf(p.stderr, "valley-web-publish: "+format+"\n", args...) |
| 275 | } |
| 276 | |
| 277 | // short is the first twelve characters of a digest or an object ID, for |
| 278 | // messages. |
| 279 | func short(s string) string { |
| 280 | if len(s) > 12 { |
| 281 | return s[:12] |
| 282 | } |
| 283 | return s |
| 284 | } |
| 285 | |
| 286 | func sleepContext(ctx context.Context, d time.Duration) error { |
| 287 | t := time.NewTimer(d) |
| 288 | defer t.Stop() |
| 289 | select { |
| 290 | case <-ctx.Done(): |
| 291 | return ctx.Err() |
| 292 | case <-t.C: |
| 293 | return nil |
| 294 | } |
| 295 | } |