gunk-dev/the-valley

cue-vet.sh

source4d6ef35777b0main's published tip history
14.1 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 and the integrator emit …
95while read -r def event; do
96 cue vet -d "$def" "$eventSchema" "$events/$event.json"
97done <<'EOF'
98#RefUpdated ref-updated
99#IntegrationSucceeded integration-succeeded
100#RequestStale request-stale
101EOF
102# … and every event stays closed: a field outside the git-derivable
103# identity — wall-clock time, a hostname — must be rejected,
104# or replay determinism silently dies. An abbreviated object
105# id is rejected for the same reason.
106while read -r def bad names; do
107 if cue vet -d "$def" "$eventSchema" "$events/rejected/$bad.json" > "$bad.err" 2>&1; then
108 echo "cue-vet: expected events/rejected/$bad.json to be rejected" >&2
109 exit 1
110 fi
111 grep -qF -- "$names" "$bad.err" || {
112 echo "cue-vet: events/rejected/$bad.json was rejected without naming $names" >&2
113 cat "$bad.err" >&2
114 exit 1
115 }
116done <<'EOF'
117#RefUpdated short-object-id new:
118#RefUpdated timestamped time:
119#IntegrationSucceeded integration-succeeded-timestamped time:
120#RequestStale request-stale-timestamped time:
121EOF
122[ "$(find "$events/rejected" -name '*.json' | wc -l)" = 4 ] || {
123 echo "cue-vet: events/rejected/ holds a case the list above does not name" >&2
124 exit 1
125}
126
127# The rejected declarations, and the words each rejection must name.
128while IFS=$'\t' read -r name names; do
129 case "$name" in ''|'#'*) continue ;; esac
130 if cue vet -c "$schema" "$hosts/rejected/$name.cue" > "$name.err" 2>&1; then
131 echo "cue-vet: expected invalid declaration '$name' to be rejected" >&2
132 exit 1
133 fi
134 if [ -n "$names" ] && ! grep -qF -- "$names" "$name.err"; then
135 echo "cue-vet: '$name' was rejected without naming $names" >&2
136 cat "$name.err" >&2
137 exit 1
138 fi
139done < "$rejectedCases"
140
141# The worked policy example: its instance floor and templates composed with
142# its project layer, as schema/verification.cue's header shows. The
143# deriver's own fixtures are policy-deriver's.
144cue vet -c "$verificationSchema" "$policies"/instance/*.cue \
145 "$policies"/project/*.cue
146
147# The carry modes, case by case. Each fixture under examples/policy/carry/
148# composes a floor, a template and a project in one document, or refines
149# `policy` itself. Each must vet, and each class must resolve to the mode
150# the layers mean: the strictest mode whose key is true, and same-paths when
151# none is.
152carry="$policies/carry"
153while read -r fixture class mode; do
154 cue vet -c "$verificationSchema" "$carry/$fixture.cue"
155 got="$(cue eval --out text -e "_mode.$class" "$verificationSchema" "$carry/$fixture.cue")"
156 [ "$got" = "$mode" ] || {
157 echo "cue-vet: carry/$fixture.cue resolves $class to '$got', not $mode" >&2
158 exit 1
159 }
160done <<'EOF'
161floor-pins-exact guarded exact
162template-default-declined guarded same-paths
163template-default-declined kept same-class
164no-carry guarded same-paths
165project-adds-exact guarded exact
166policy-refines-carry guarded exact
167policy-refines-carry added same-class
168EOF
169
170# The restacker's cap is 64 unless a layer sets it.
171[ "$(cue export -e policy.restack.maxAutomaticVersions "$verificationSchema" \
172 "$carry/no-carry.cue")" = 64 ]
173[ "$(cue export -e policy.restack.maxAutomaticVersions "$verificationSchema" \
174 "$carry/project-adds-exact.cue")" = 16 ]
175
176# The compositions the schema must reject, and the field each rejection must
177# name: a project dropping the floor's mode, a carry key that is not a
178# mode, a cap below 1, a cap outside every layer, and a pattern that would
179# forge a line of the policy `valley checks` renders.
180while read -r fixture names; do
181 if cue vet -c "$verificationSchema" "$carry/rejected/$fixture.cue" \
182 > "$fixture.err" 2>&1; then
183 echo "cue-vet: expected carry/rejected/$fixture.cue to be rejected" >&2
184 exit 1
185 fi
186 grep -qF -- "$names" "$fixture.err" || {
187 echo "cue-vet: carry/rejected/$fixture.cue was rejected without naming $names" >&2
188 cat "$fixture.err" >&2
189 exit 1
190 }
191done <<'EOF'
192project-drops-floor-exact policy.classes.guarded.carry.exact
193not-a-mode policy.classes.guarded.carry."same-file"
194cap-below-one policy.restack.maxAutomaticVersions
195restack-outside-project restack: conflicting values
196restack-outside-project "INVALID: unknown top-level field
197forged-carry-row policy.classes.injected.paths."unused\ncarry\tguarded\tsame-paths": field not allowed
198EOF
199
200# The hardware requirement, case by case. Each fixture under
201# examples/policy/approval/ composes the three layers in one document. Each
202# must vet, and the composed policy must say what the layers mean: true when
203# the floor or the project requires a hardware key, false when a project
204# turned a template's default off, and no `approval` field at all when no
205# layer wrote one.
206approval="$policies/approval"
207while read -r fixture hardware; do
208 cue vet -c "$verificationSchema" "$approval/$fixture.cue"
209 cue export --out json -e policy "$verificationSchema" "$approval/$fixture.cue" \
210 > "$fixture.json"
211 if [ "$hardware" = absent ]; then
212 if grep -q '"approval"' "$fixture.json"; then
213 echo "cue-vet: approval/$fixture.cue exports an approval field no layer wrote" >&2
214 exit 1
215 fi
216 continue
217 fi
218 got="$(cue export -e policy.approval.hardware "$verificationSchema" "$approval/$fixture.cue")"
219 [ "$got" = "$hardware" ] || {
220 echo "cue-vet: approval/$fixture.cue composes approval.hardware to '$got', not $hardware" >&2
221 exit 1
222 }
223done <<'EOF'
224floor-requires-hardware true
225project-requires-hardware true
226template-default-declined false
227no-requirement absent
228EOF
229
230# The approval requirements the schema must reject, and the field each
231# rejection must name: a project dropping the floor's hardware or approval
232# requirement, a field `approval` does not have, and a requirement that is
233# not a boolean.
234while read -r fixture names; do
235 if cue vet -c "$verificationSchema" "$approval/rejected/$fixture.cue" \
236 > "$fixture.err" 2>&1; then
237 echo "cue-vet: expected approval/rejected/$fixture.cue to be rejected" >&2
238 exit 1
239 fi
240 grep -qF -- "$names" "$fixture.err" || {
241 echo "cue-vet: approval/rejected/$fixture.cue was rejected without naming $names" >&2
242 cat "$fixture.err" >&2
243 exit 1
244 }
245done <<'EOF'
246project-drops-floor-hardware policy.approval.hardware: conflicting values false and true
247not-a-requirement policy.approval.touch: field not allowed
248not-a-bool policy.approval.hardware: conflicting values "yes" and bool
249project-drops-floor-required policy.approval.required: conflicting values false and true
250required-not-a-bool policy.approval.required: conflicting values "yes" and bool
251EOF
252
253# No control character reaches a string of the policy: a path pattern, a
254# class name, a check name in the catalogue, in a class's requirements or
255# in the unclassified set, an attribute, or a command line. Each case is written once with a dash where the character
256# goes, and must vet, so that its failure with the character is the
257# character's doing. The last case refines `policy` itself rather than a
258# layer.
259while read -r field doc; do
260 printf 'package verification\n%s\n' "${doc//@/-}" > control.cue
261 cue vet -c "$verificationSchema" control.cue || {
262 echo "cue-vet: the $field case does not vet without a control character, so it proves nothing" >&2
263 exit 1
264 }
265 for char in '\n' '\r' '\t' '\u0000' '\u001b' '\u007f'; do
266 printf 'package verification\n%s\n' "${doc//@/"$char"}" > control.cue
267 if cue vet -c "$verificationSchema" control.cue > control.err 2>&1; then
268 echo "cue-vet: a $field holding $char vetted" >&2
269 exit 1
270 fi
271 done
272done <<'EOF'
273pattern project: classes: c: paths: "a@b": true
274class project: classes: "a@b": paths: "a": true
275check project: checks: "a@b": {runner: "nix", attribute: "a"}
276requirement project: {checks: "a-b": {runner: "nix", attribute: "a"}, classes: c: {paths: "a": true, requires: "a@b": true}}
277unclassified project: {checks: "a-b": {runner: "nix", attribute: "a"}, unclassified: "a@b": true}
278attribute project: checks: c: {runner: "nix", attribute: "a@b"}
279command project: checks: c: {runner: "command", command: "a@b"}
280refined-pattern policy: classes: c: paths: "a@b": true
281EOF
282
283# The read policies (schema/read.cue): the worked project's; the one a new
284# project starts from, which names the operator alone; and the-valley's
285# own, which lets anyone and the operator read it.
286cue vet -c "$readSchema" "$readPolicies"/project/*.cue
287cue vet -c "$readSchema" "$readPolicies"/new-project/*.cue
288[ "$(cue export -e readers.operator "$readSchema" "$readPolicies"/new-project/*.cue)" = true ]
289[ "$(cue export -e 'len(readers)' "$readSchema" "$readPolicies"/new-project/*.cue)" = 1 ]
290[ "$(cue export -e project "$readSchema" "$ownRead"/*.cue)" = '"the-valley"' ]
291[ "$(cue export -e readers.anyone "$readSchema" "$ownRead"/*.cue)" = true ]
292[ "$(cue export -e readers.operator "$readSchema" "$ownRead"/*.cue)" = true ]
293
294# The policies the schema must reject, and the field each rejection must
295# name. A refused policy consents to nothing, so a schema that stopped
296# refusing one would let read whom no project named.
297while read -r fixture names; do
298 if cue vet -c "$readSchema" "$readPolicies/rejected/$fixture.cue" > "$fixture.err" 2>&1; then
299 echo "cue-vet: expected read/rejected/$fixture.cue to be rejected" >&2
300 exit 1
301 fi
302 grep -qF -- "$names" "$fixture.err" || {
303 echo "cue-vet: read/rejected/$fixture.cue was rejected without naming $names" >&2
304 cat "$fixture.err" >&2
305 exit 1
306 }
307done <<'EOF'
308top-level-reader reader: conflicting values
309top-level-reader "INVALID: unknown top-level field
310no-readers readers: field is required but not present
311no-identity identity: field is required but not present
312short-identity identity: invalid value "9da6809f"
313unsafe-project-name project: invalid value "../app"
314unsafe-reader-name readers."../operator": field not allowed
315reader-declined readers.anyone: conflicting values true and false
316redeclared-principal readers."two words": field not allowed
317web-declaration project: conflicting values
318EOF
319[ "$(find "$readPolicies/rejected" -name '*.cue' | wc -l)" = 9 ] || {
320 echo "cue-vet: read/rejected/ holds a case the list above does not name" >&2
321 exit 1
322}
323touch "$out"