gunk-dev/the-valley

cue-vet.sh

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