gunk-dev/the-valley

compile.go

source79a631efa975main's published tip history
3.3 KiB
1package main
2
3import (
4 "bytes"
5 "flag"
6 "fmt"
7 "os"
8 "path/filepath"
9 "time"
10)
11
12func compile(args []string) error {
13 fs := flag.NewFlagSet("compile", flag.ExitOnError)
14 fs.Usage = func() { fmt.Fprint(os.Stderr, usage) }
15 repo := fs.String("repo", "", "the bare instance repository carrying the registry")
16 ref := fs.String("ref", "refs/heads/main", "the ref whose tip is read")
17 dir := fs.String("dir", "identity", "the registry's directory in that tip")
18 schema := fs.String("schema", os.Getenv("VALLEY_IDENTITY_SCHEMA"), "the identity schema")
19 knownSigners := fs.String("known-signers", "", "where the verifier keys are written")
20 authorizedKeys := fs.String("authorized-keys", "", "where the tagged authorized_keys is written")
21 now := fs.String("now", "", "the day expiry is judged against")
22 if err := fs.Parse(args); err != nil {
23 return err
24 }
25 for _, required := range []struct{ flag, value string }{
26 {"repo", *repo},
27 {"schema", *schema},
28 {"known-signers", *knownSigners},
29 {"authorized-keys", *authorizedKeys},
30 } {
31 if required.value == "" {
32 return fmt.Errorf("--%s is required", required.flag)
33 }
34 }
35
36 // The day expiry is judged against. A flag rather than the clock alone,
37 // so a compilation can be reproduced and a check can pin one.
38 day := time.Now().UTC().Truncate(24 * time.Hour)
39 if *now != "" {
40 parsed, err := time.Parse(dateLayout, *now)
41 if err != nil {
42 return fmt.Errorf("--now %q is not a calendar day", *now)
43 }
44 day = parsed
45 }
46
47 r, commit, err := readRegistry(*repo, *ref, *dir, *schema)
48 if err != nil {
49 return err
50 }
51 a, err := render(r, day)
52 if err != nil {
53 return fmt.Errorf("the registry at %s does not compile: %w", commit, err)
54 }
55
56 // The omissions, before the artifacts are written: an entry that has
57 // aged out is the compiler doing its job, and it is not a silent one.
58 for _, note := range a.notes {
59 fmt.Fprintf(os.Stderr, "identity: %s\n", note)
60 }
61
62 signersWritten, err := writeArtifact(*knownSigners, a.knownSigners)
63 if err != nil {
64 return err
65 }
66 keysWritten, err := writeArtifact(*authorizedKeys, a.authorizedKeys)
67 if err != nil {
68 return err
69 }
70
71 fmt.Fprintf(os.Stderr, "identity: %s at %s: %d verifier key(s)%s, %d authorized key(s)%s\n",
72 *ref, commit, a.signers, changed(signersWritten), a.authorized, changed(keysWritten))
73 return nil
74}
75
76func changed(written bool) string {
77 if written {
78 return " written"
79 }
80 return " unchanged"
81}
82
83// writeArtifact replaces one file by rename, and only when its content
84// differs. Both artifacts are rendered before either is written, so the
85// only way to see one of them newer than the other is a crash between the
86// two renames — and neither can ever be seen half-written.
87func writeArtifact(path string, content []byte) (bool, error) {
88 if old, err := os.ReadFile(path); err == nil && bytes.Equal(old, content) {
89 return false, nil
90 }
91 dir := filepath.Dir(path)
92 f, err := os.CreateTemp(dir, filepath.Base(path)+".tmp")
93 if err != nil {
94 return false, err
95 }
96 defer os.Remove(f.Name())
97 if _, err := f.Write(content); err != nil {
98 f.Close()
99 return false, err
100 }
101 // sshd reads the authorized_keys file and refuses one anybody but its
102 // owner can write, so the mode is stated rather than left to CreateTemp.
103 if err := f.Chmod(0o644); err != nil {
104 f.Close()
105 return false, err
106 }
107 if err := f.Close(); err != nil {
108 return false, err
109 }
110 return true, os.Rename(f.Name(), path)
111}