gunk-dev/the-valley

cue-vet.sh

source03252b3b26acmain's published tip history
11.3 KiB
1# shellcheck shell=bash
2# Paths and fixtures come from the derivation environment.
3# shellcheck disable=SC2154
4cue vet -c "$schema" "$example"
5cue export "$schema" "$example" > example.json
6grep -q 'mirror.example.org:the-valley' example.json
7grep -q 'restic-sftp' example.json
8[ "$(cue export -e 'projects."the-valley".group' "$schema" "$example")" = '"valley"' ]
9[ "$(cue export -e 'groups.valley.name' "$schema" "$example")" = '"valley"' ]
10
11# A declaration without groups exports none, and its projects no group,
12# and no aliases.
13[ "$(cue export -e groups "$schema" "$hosts/no-backup.cue")" = '{}' ]
14[ "$(cue export -e 'projects."the-valley".aliases' "$schema" "$hosts/no-backup.cue")" = '[]' ]
15
16# A declaration with no optional field set must stay valid —
17# consumers written before backup or protection existed keep
18# vetting.
19cue vet -c "$schema" "$hosts/no-backup.cue"
20
21# What the installer renders a hook from. A project that
22# leaves the set out gets the default, a project that names
23# patterns gets them in order, and a project that declares no
24# protection exports none at all — which is how the installer
25# tells a protected project from an open one.
26protected=("$schema" "$hosts/protected.cue")
27[ "$(cue export -e 'projects.guarded.protection.refs[0]' "${protected[@]}")" \
28 = '"refs/heads/main"' ]
29cue export -e 'projects.released.protection.refs' "${protected[@]}" > released.json
30grep -q '"refs/heads/release/\*"' released.json
31if cue export -e 'projects.open.protection' "${protected[@]}" > open.err 2>&1; then
32 echo "cue-vet: a project declaring no protection exported one" >&2
33 exit 1
34fi
35
36# The norm: protection that names no writer. It exports an
37# empty list rather than nothing, so the installer renders a
38# hook whose exception list is empty rather than missing.
39[ "$(cue export -e 'projects.sealed.protection.writers' "${protected[@]}")" = '[]' ]
40
41# The same for grants: none by default, so the hook reads a
42# project that opens nothing as exactly that, whether or not it
43# declares protection. A grant exports its patterns and its
44# writers as declared.
45[ "$(cue export -e 'projects.sealed.grants' "${protected[@]}")" = '{}' ]
46[ "$(cue export -e 'projects.open.grants' "${protected[@]}")" = '{}' ]
47cue export -e 'projects.released.grants' "${protected[@]}" > grants.json
48grep -q '"refs/tags/release/\*"' grants.json
49
50# The event schema accepts what the publisher hook emits …
51cue vet -d '#RefUpdated' "$eventSchema" "$events/ref-updated.json"
52# … and stays closed: a field outside the git-derivable
53# identity — wall-clock time, a hostname — must be rejected,
54# or replay determinism silently dies. An abbreviated object
55# id is rejected for the same reason.
56for bad in "$events"/rejected/*.json; do
57 if cue vet -d '#RefUpdated' "$eventSchema" "$bad"; then
58 echo "cue-vet: expected $(basename "$bad") to be rejected" >&2
59 exit 1
60 fi
61done
62
63# The rejected declarations, and the words each rejection must name.
64while IFS=$'\t' read -r name names; do
65 case "$name" in ''|'#'*) continue ;; esac
66 if cue vet -c "$schema" "$hosts/rejected/$name.cue" > "$name.err" 2>&1; then
67 echo "cue-vet: expected invalid declaration '$name' to be rejected" >&2
68 exit 1
69 fi
70 if [ -n "$names" ] && ! grep -qF -- "$names" "$name.err"; then
71 echo "cue-vet: '$name' was rejected without naming $names" >&2
72 cat "$name.err" >&2
73 exit 1
74 fi
75done < "$rejectedCases"
76
77# The worked policy example: its instance floor and templates composed with
78# its project layer, as schema/verification.cue's header shows. The
79# deriver's own fixtures are policy-deriver's.
80cue vet -c "$verificationSchema" "$policies"/instance/*.cue \
81 "$policies"/project/*.cue
82
83# The carry modes, case by case. Each fixture under examples/policy/carry/
84# composes a floor, a template and a project in one document, or refines
85# `policy` itself. Each must vet, and each class must resolve to the mode
86# the layers mean: the strictest mode whose key is true, and same-paths when
87# none is.
88carry="$policies/carry"
89while read -r fixture class mode; do
90 cue vet -c "$verificationSchema" "$carry/$fixture.cue"
91 got="$(cue eval --out text -e "_mode.$class" "$verificationSchema" "$carry/$fixture.cue")"
92 [ "$got" = "$mode" ] || {
93 echo "cue-vet: carry/$fixture.cue resolves $class to '$got', not $mode" >&2
94 exit 1
95 }
96done <<'EOF'
97floor-pins-exact guarded exact
98template-default-declined guarded same-paths
99template-default-declined kept same-class
100no-carry guarded same-paths
101project-adds-exact guarded exact
102policy-refines-carry guarded exact
103policy-refines-carry added same-class
104EOF
105
106# The restacker's cap is 64 unless a layer sets it.
107[ "$(cue export -e policy.restack.maxAutomaticVersions "$verificationSchema" \
108 "$carry/no-carry.cue")" = 64 ]
109[ "$(cue export -e policy.restack.maxAutomaticVersions "$verificationSchema" \
110 "$carry/project-adds-exact.cue")" = 16 ]
111
112# The compositions the schema must reject, and the field each rejection must
113# name: a project dropping the floor's mode, a carry key that is not a
114# mode, a cap below 1, a cap outside every layer, and a pattern that would
115# forge a line of the policy `valley checks` renders.
116while read -r fixture names; do
117 if cue vet -c "$verificationSchema" "$carry/rejected/$fixture.cue" \
118 > "$fixture.err" 2>&1; then
119 echo "cue-vet: expected carry/rejected/$fixture.cue to be rejected" >&2
120 exit 1
121 fi
122 grep -qF -- "$names" "$fixture.err" || {
123 echo "cue-vet: carry/rejected/$fixture.cue was rejected without naming $names" >&2
124 cat "$fixture.err" >&2
125 exit 1
126 }
127done <<'EOF'
128project-drops-floor-exact policy.classes.guarded.carry.exact
129not-a-mode policy.classes.guarded.carry."same-file"
130cap-below-one policy.restack.maxAutomaticVersions
131restack-outside-project restack: conflicting values "INVALID: unknown top-level field
132forged-carry-row policy.classes.injected.paths."unused\ncarry\tguarded\tsame-paths": field not allowed
133EOF
134
135# The hardware requirement, case by case. Each fixture under
136# examples/policy/approval/ composes the three layers in one document. Each
137# must vet, and the composed policy must say what the layers mean: true when
138# the floor or the project requires a hardware key, false when a project
139# turned a template's default off, and no `approval` field at all when no
140# layer wrote one.
141approval="$policies/approval"
142while read -r fixture hardware; do
143 cue vet -c "$verificationSchema" "$approval/$fixture.cue"
144 cue export --out json -e policy "$verificationSchema" "$approval/$fixture.cue" \
145 > "$fixture.json"
146 if [ "$hardware" = absent ]; then
147 if grep -q '"approval"' "$fixture.json"; then
148 echo "cue-vet: approval/$fixture.cue exports an approval field no layer wrote" >&2
149 exit 1
150 fi
151 continue
152 fi
153 got="$(cue export -e policy.approval.hardware "$verificationSchema" "$approval/$fixture.cue")"
154 [ "$got" = "$hardware" ] || {
155 echo "cue-vet: approval/$fixture.cue composes approval.hardware to '$got', not $hardware" >&2
156 exit 1
157 }
158done <<'EOF'
159floor-requires-hardware true
160project-requires-hardware true
161template-default-declined false
162no-requirement absent
163EOF
164
165# The hardware requirements the schema must reject, and the field each
166# rejection must name: a project dropping the floor's requirement, a field
167# `approval` does not have, and a requirement that is not a boolean.
168while read -r fixture names; do
169 if cue vet -c "$verificationSchema" "$approval/rejected/$fixture.cue" \
170 > "$fixture.err" 2>&1; then
171 echo "cue-vet: expected approval/rejected/$fixture.cue to be rejected" >&2
172 exit 1
173 fi
174 grep -qF -- "$names" "$fixture.err" || {
175 echo "cue-vet: approval/rejected/$fixture.cue was rejected without naming $names" >&2
176 cat "$fixture.err" >&2
177 exit 1
178 }
179done <<'EOF'
180project-drops-floor-hardware policy.approval.hardware: conflicting values false and true
181not-a-requirement policy.approval.touch: field not allowed
182not-a-bool policy.approval.hardware: conflicting values "yes" and bool
183EOF
184
185# No control character reaches a string `valley checks` renders as a field
186# of a line: a path pattern, a class name, a check name in the catalogue,
187# in a class's requirements or in the unclassified set, an attribute, or a
188# command line. Each case is written once with a dash where the character
189# goes, and must vet, so that its failure with the character is the
190# character's doing. The last case refines `policy` itself rather than a
191# layer.
192while read -r field doc; do
193 printf 'package verification\n%s\n' "${doc//@/-}" > control.cue
194 cue vet -c "$verificationSchema" control.cue || {
195 echo "cue-vet: the $field case does not vet without a control character, so it proves nothing" >&2
196 exit 1
197 }
198 for char in '\n' '\r' '\t' '\u0000' '\u001b' '\u007f'; do
199 printf 'package verification\n%s\n' "${doc//@/"$char"}" > control.cue
200 if cue vet -c "$verificationSchema" control.cue > control.err 2>&1; then
201 echo "cue-vet: a $field holding $char vetted" >&2
202 exit 1
203 fi
204 done
205done <<'EOF'
206pattern project: classes: c: paths: "a@b": true
207class project: classes: "a@b": paths: "a": true
208check project: checks: "a@b": {runner: "nix", attribute: "a"}
209requirement project: {checks: "a-b": {runner: "nix", attribute: "a"}, classes: c: {paths: "a": true, requires: "a@b": true}}
210unclassified project: {checks: "a-b": {runner: "nix", attribute: "a"}, unclassified: "a@b": true}
211attribute project: checks: c: {runner: "nix", attribute: "a@b"}
212command project: checks: c: {runner: "command", command: "a@b"}
213refined-pattern policy: classes: c: paths: "a@b": true
214EOF
215
216# The read policies (schema/read.cue): the worked project's; the one a new
217# project starts from, which names the operator alone; and the-valley's
218# own, which lets anyone and the operator read it.
219cue vet -c "$readSchema" "$readPolicies"/project/*.cue
220cue vet -c "$readSchema" "$readPolicies"/new-project/*.cue
221[ "$(cue export -e readers.operator "$readSchema" "$readPolicies"/new-project/*.cue)" = true ]
222[ "$(cue export -e 'len(readers)' "$readSchema" "$readPolicies"/new-project/*.cue)" = 1 ]
223[ "$(cue export -e project "$readSchema" "$ownRead"/*.cue)" = '"the-valley"' ]
224[ "$(cue export -e readers.anyone "$readSchema" "$ownRead"/*.cue)" = true ]
225[ "$(cue export -e readers.operator "$readSchema" "$ownRead"/*.cue)" = true ]
226
227# The policies the schema must reject, and the field each rejection must
228# name. A refused policy consents to nothing, so a schema that stopped
229# refusing one would let read whom no project named.
230while read -r fixture names; do
231 if cue vet -c "$readSchema" "$readPolicies/rejected/$fixture.cue" > "$fixture.err" 2>&1; then
232 echo "cue-vet: expected read/rejected/$fixture.cue to be rejected" >&2
233 exit 1
234 fi
235 grep -qF -- "$names" "$fixture.err" || {
236 echo "cue-vet: read/rejected/$fixture.cue was rejected without naming $names" >&2
237 cat "$fixture.err" >&2
238 exit 1
239 }
240done <<'EOF'
241top-level-reader reader: conflicting values "INVALID: unknown top-level field
242no-readers readers: field is required but not present
243no-identity identity: field is required but not present
244short-identity identity: invalid value "9da6809f"
245unsafe-project-name project: invalid value "../app"
246unsafe-reader-name readers."../operator": field not allowed
247reader-declined readers.anyone: conflicting values true and false
248redeclared-principal readers."two words": field not allowed
249web-declaration project: cannot use
250EOF
251[ "$(find "$readPolicies/rejected" -name '*.cue' | wc -l)" = 9 ] || {
252 echo "cue-vet: read/rejected/ holds a case the list above does not name" >&2
253 exit 1
254}
255touch "$out"