gunk-dev/the-valley

compile.go

sourceef0e6a83672emain's published tip history
3.6 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 grantsFile := fs.String("grants", "", "where the grants the pre-receive hook checks are written")
22 now := fs.String("now", "", "the day expiry is judged against")
23 if err := fs.Parse(args); err != nil {
24 return err
25 }
26 for _, required := range []struct{ flag, value string }{
27 {"repo", *repo},
28 {"schema", *schema},
29 {"known-signers", *knownSigners},
30 {"authorized-keys", *authorizedKeys},
31 {"grants", *grantsFile},
32 } {
33 if required.value == "" {
34 return fmt.Errorf("--%s is required", required.flag)
35 }
36 }
37
38 // The day expiry is judged against. A flag rather than the clock alone,
39 // so a compilation can be reproduced and a check can pin one.
40 day := time.Now().UTC().Truncate(24 * time.Hour)
41 if *now != "" {
42 parsed, err := time.Parse(dateLayout, *now)
43 if err != nil {
44 return fmt.Errorf("--now %q is not a calendar day", *now)
45 }
46 day = parsed
47 }
48
49 r, commit, err := readRegistry(*repo, *ref, *dir, *schema)
50 if err != nil {
51 return err
52 }
53 a, err := render(r, day)
54 if err != nil {
55 return fmt.Errorf("the registry at %s does not compile: %w", commit, err)
56 }
57
58 // The omissions, before the artifacts are written: an entry that has
59 // aged out is the compiler doing its job, and it is not a silent one.
60 for _, note := range a.notes {
61 fmt.Fprintf(os.Stderr, "identity: %s\n", note)
62 }
63
64 signersWritten, err := writeArtifact(*knownSigners, a.knownSigners)
65 if err != nil {
66 return err
67 }
68 keysWritten, err := writeArtifact(*authorizedKeys, a.authorizedKeys)
69 if err != nil {
70 return err
71 }
72 grantsWritten, err := writeArtifact(*grantsFile, a.grants)
73 if err != nil {
74 return err
75 }
76
77 fmt.Fprintf(os.Stderr, "identity: %s at %s: %d verifier key(s)%s, %d authorized key(s)%s, %d grant(s)%s\n",
78 *ref, commit, a.signers, changed(signersWritten), a.authorized, changed(keysWritten),
79 a.granted, changed(grantsWritten))
80 return nil
81}
82
83func changed(written bool) string {
84 if written {
85 return " written"
86 }
87 return " unchanged"
88}
89
90// writeArtifact replaces one file by rename, and only when its content
91// differs. Every artifact is rendered before any is written, so the only
92// way to see one of them newer than another is a crash between two renames
93// — and none can ever be seen half-written.
94func writeArtifact(path string, content []byte) (bool, error) {
95 if old, err := os.ReadFile(path); err == nil && bytes.Equal(old, content) {
96 return false, nil
97 }
98 dir := filepath.Dir(path)
99 f, err := os.CreateTemp(dir, filepath.Base(path)+".tmp")
100 if err != nil {
101 return false, err
102 }
103 defer os.Remove(f.Name())
104 if _, err := f.Write(content); err != nil {
105 f.Close()
106 return false, err
107 }
108 // sshd reads the authorized_keys file and refuses one anybody but its
109 // owner can write, so the mode is stated rather than left to CreateTemp.
110 if err := f.Chmod(0o644); err != nil {
111 f.Close()
112 return false, err
113 }
114 if err := f.Close(); err != nil {
115 return false, err
116 }
117 return true, os.Rename(f.Name(), path)
118}