gunk-dev/the-valley

cue-vet.sh

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