| 1 | package main |
| 2 | |
| 3 | // The policy bridge. The integrator does not derive required checks; it |
| 4 | // asks the deriver, which is `valley checks` (dcr-f41f718). The whole of |
| 5 | // the mandatory-versus-overridable distinction is CUE unification, so a |
| 6 | // second implementation of the composition here would be a second copy of |
| 7 | // the decision. |
| 8 | // |
| 9 | // Both layers are resolved from the integrator's own side and never from |
| 10 | // the submitted tree (bd-eaefe82): the project layer from a checkout of the |
| 11 | // target tip, the instance layer from where the integrator is configured to |
| 12 | // find it. A change therefore never supplies the policy that gates it. |
| 13 | |
| 14 | import ( |
| 15 | "the-valley/integrator/verdict" |
| 16 | |
| 17 | "encoding/json" |
| 18 | "fmt" |
| 19 | "os/exec" |
| 20 | "path/filepath" |
| 21 | "strings" |
| 22 | "time" |
| 23 | ) |
| 24 | |
| 25 | // policySource is where the two layers are read from, and the schema they |
| 26 | // are composed under. |
| 27 | type policySource struct { |
| 28 | schema string // the verification schema |
| 29 | instance string // the instance layer, resolved from the instance repository |
| 30 | project string // the project layer, relative to the target tip's tree |
| 31 | valley string // the deriver |
| 32 | cue string // the composer, for the catalogue the deriver does not print |
| 33 | } |
| 34 | |
| 35 | // derived is one run of the deriver over a diff: which classes the diff |
| 36 | // matched, and which checks that made required. |
| 37 | type derived struct { |
| 38 | classes map[string]bool |
| 39 | requires map[string][]string // check name -> the classes requiring it |
| 40 | } |
| 41 | |
| 42 | // derive runs the deriver over a diff, in a checkout of the target tip. The |
| 43 | // checkout supplies the project layer and the commits the diff names; the |
| 44 | // instance layer comes from outside it. |
| 45 | func (p policySource) derive(tip *worktree, from, to string) (derived, error) { |
| 46 | cmd := exec.Command(p.valley, "checks", |
| 47 | "--schema", p.schema, |
| 48 | "--instance", p.instance, |
| 49 | "--project", filepath.Join(tip.dir, p.project), |
| 50 | from, to) |
| 51 | cmd.Dir = tip.dir |
| 52 | out, err := cmd.CombinedOutput() |
| 53 | if err != nil { |
| 54 | return derived{}, fmt.Errorf("deriving required checks for %s..%s: %w\n%s", verdict.Short(from), verdict.Short(to), err, strings.TrimSpace(string(out))) |
| 55 | } |
| 56 | return parseDerived(string(out)) |
| 57 | } |
| 58 | |
| 59 | // parseDerived reads what `valley checks` printed. The deriver reports for |
| 60 | // a person, so this is the one place the two programs are coupled by |
| 61 | // output; the coupling is pinned by the integrator's own checks rather |
| 62 | // than assumed. |
| 63 | func parseDerived(out string) (derived, error) { |
| 64 | d := derived{classes: map[string]bool{}, requires: map[string][]string{}} |
| 65 | lines := strings.Split(out, "\n") |
| 66 | for i := 0; i < len(lines); i++ { |
| 67 | line := lines[i] |
| 68 | switch { |
| 69 | case strings.HasPrefix(line, "classes matched:"): |
| 70 | rest := strings.TrimSpace(strings.TrimPrefix(line, "classes matched:")) |
| 71 | if rest == "none" { |
| 72 | continue |
| 73 | } |
| 74 | for _, c := range strings.Split(rest, ",") { |
| 75 | if c = strings.TrimSpace(c); c != "" { |
| 76 | d.classes[c] = true |
| 77 | } |
| 78 | } |
| 79 | case line == "required checks:": |
| 80 | for i++; i < len(lines); i++ { |
| 81 | body := lines[i] |
| 82 | if !strings.HasPrefix(body, " ") || strings.TrimSpace(body) == "" { |
| 83 | i-- |
| 84 | break |
| 85 | } |
| 86 | // " <name> <mandatory|default> <class>[, <class>…]" |
| 87 | fields := strings.Fields(body) |
| 88 | if len(fields) < 2 { |
| 89 | return derived{}, fmt.Errorf("the deriver printed %q, which is not a required-check line", body) |
| 90 | } |
| 91 | name := fields[0] |
| 92 | var classes []string |
| 93 | for _, c := range strings.Split(strings.Join(fields[2:], " "), ",") { |
| 94 | if c = strings.TrimSpace(c); c != "" { |
| 95 | classes = append(classes, c) |
| 96 | } |
| 97 | } |
| 98 | d.requires[name] = classes |
| 99 | } |
| 100 | case strings.HasPrefix(line, "required checks: none"): |
| 101 | // Nothing owed; the map stays empty. |
| 102 | } |
| 103 | } |
| 104 | return d, nil |
| 105 | } |
| 106 | |
| 107 | // catalogueEntry is one check as the composed policy defines it: how it is |
| 108 | // run, and how long an observation of it stays good. |
| 109 | type catalogueEntry struct { |
| 110 | Runner string `json:"runner"` |
| 111 | Attribute string `json:"attribute"` |
| 112 | Command string `json:"command"` |
| 113 | Validity string `json:"validity"` |
| 114 | } |
| 115 | |
| 116 | // catalogue reads the composed policy's check definitions. The deriver |
| 117 | // prints which checks are required and not what a check is, and the |
| 118 | // integrator needs the latter: a pure check's attribute is what it |
| 119 | // evaluates a closure over, and an effectful check's window is Δ. |
| 120 | // |
| 121 | // This is the same composition the deriver ran, over the same layers and |
| 122 | // the same schema — `cue export` of a field the deriver does not print, |
| 123 | // not a second opinion about what the policy says. |
| 124 | func (p policySource) catalogue(tip *worktree) (map[string]catalogueEntry, string, error) { |
| 125 | args := []string{"export", "--out", "json", "-e", "policy", p.schema} |
| 126 | for _, dir := range []string{p.instance, filepath.Join(tip.dir, p.project)} { |
| 127 | docs, err := filepath.Glob(filepath.Join(dir, "*.cue")) |
| 128 | if err != nil || len(docs) == 0 { |
| 129 | return nil, "", fmt.Errorf("no policy layer at %s", dir) |
| 130 | } |
| 131 | args = append(args, docs...) |
| 132 | } |
| 133 | cmd := exec.Command(p.cue, args...) |
| 134 | out, err := cmd.CombinedOutput() |
| 135 | if err != nil { |
| 136 | return nil, "", fmt.Errorf("composing the policy: %w\n%s", err, strings.TrimSpace(string(out))) |
| 137 | } |
| 138 | var composed struct { |
| 139 | Checks map[string]catalogueEntry `json:"checks"` |
| 140 | } |
| 141 | if err := json.Unmarshal(out, &composed); err != nil { |
| 142 | return nil, "", fmt.Errorf("reading the composed policy: %w", err) |
| 143 | } |
| 144 | return composed.Checks, sha256hex(out), nil |
| 145 | } |
| 146 | |
| 147 | // required turns a derivation and a catalogue into the check set the |
| 148 | // verdict is computed over. |
| 149 | func (p policySource) required(d derived, cat map[string]catalogueEntry) ([]verdict.Required, error) { |
| 150 | var out []verdict.Required |
| 151 | for name, classes := range d.requires { |
| 152 | entry, ok := cat[name] |
| 153 | if !ok { |
| 154 | return nil, fmt.Errorf("the policy requires %q and defines no check by that name", name) |
| 155 | } |
| 156 | window, err := parseWindow(entry.Validity) |
| 157 | if err != nil { |
| 158 | return nil, fmt.Errorf("check %s: %w", name, err) |
| 159 | } |
| 160 | out = append(out, verdict.Required{ |
| 161 | Name: name, |
| 162 | Classes: classes, |
| 163 | Runner: entry.Runner, |
| 164 | Attribute: entry.Attribute, |
| 165 | Validity: window, |
| 166 | }) |
| 167 | } |
| 168 | return out, nil |
| 169 | } |
| 170 | |
| 171 | // parseWindow reads a #Duration from the policy. The schema admits whole |
| 172 | // seconds, minutes, hours and days; an empty window is no window, which is |
| 173 | // what makes the integrator re-demand the check. |
| 174 | func parseWindow(s string) (time.Duration, error) { |
| 175 | if s == "" { |
| 176 | return 0, nil |
| 177 | } |
| 178 | unit := s[len(s)-1] |
| 179 | if unit == 'd' { |
| 180 | d, err := time.ParseDuration(s[:len(s)-1] + "h") |
| 181 | return d * 24, err |
| 182 | } |
| 183 | return time.ParseDuration(s) |
| 184 | } |