gunk-dev/the-valley

cue-vet.sh

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