-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary-bundle
More file actions
executable file
·301 lines (279 loc) · 9.66 KB
/
Copy pathlibrary-bundle
File metadata and controls
executable file
·301 lines (279 loc) · 9.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/env bash
# Deterministic validation and directory-bundle tooling for the one-file
# library boundary. This script never concatenates or rewrites a library.
repo_root="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd -P)" || exit 1
usage() {
cat >&2 << 'EOF'
Usage:
scripts/library-bundle check
scripts/library-bundle bundle DIRECTORY
scripts/library-bundle verify DIRECTORY
EOF
}
error() {
printf 'Library bundle error: %s\n' "$*" >&2
}
hash_file() {
local path="$1"
if command -v sha256sum > /dev/null 2>&1; then
sha256sum -- "$path" | awk '{print $1}'
else
shasum -a 256 -- "$path" | awk '{print $1}'
fi
}
file_list() {
local manifest
manifest="$("$repo_root/scripts/api-manifest" artifact-paths)" || return $?
{
printf '%s\n' VERSION base_api_manifest.yaml
printf '%s\n' "$manifest"
} | LC_ALL=C sort -u
}
bundle_inventory() {
local inventory
inventory="$(file_list)" || return $?
{
printf '%s\n' BUNDLE.release
printf '%s\n' "$inventory"
} | LC_ALL=C sort -u
}
check_one_file_boundary() {
local source_path directory count source_paths
source_paths="$("$repo_root/scripts/api-manifest" source-paths)" || return $?
while IFS= read -r source_path; do
[[ "$source_path" == lib/bash/*/lib_*.sh ]] || continue
directory="${source_path%/*}"
count="$(find "$repo_root/$directory" -maxdepth 1 -type f -name 'lib_*.sh' -print | wc -l | tr -d '[:space:]')"
if [[ "$count" != 1 ]]; then
error "public library directory '$directory' has $count canonical lib_*.sh files (expected exactly one)"
return 1
fi
if grep -Eq '^[[:space:]]*(source[[:space:]]+|\.[[:space:]]+[^|])' "$repo_root/$source_path"; then
error "public library '$source_path' contains a source-time implementation import"
return 1
fi
done <<< "$source_paths"
}
check_repo() {
local inventory
"$repo_root/scripts/api-manifest" check > /dev/null || return $?
check_one_file_boundary || return $?
inventory="$(file_list)" || return $?
while IFS= read -r path; do
[[ -n "$path" && -f "$repo_root/$path" ]] || {
error "manifest artifact is missing: $path"
return 1
}
done <<< "$inventory"
printf 'Library bundle contract is valid: one canonical file per public library.\n'
}
write_hash_manifest() {
local root="$1" path inventory
inventory="$(bundle_inventory)" || return $?
: > "$root/MANIFEST.sha256" || return 1
while IFS= read -r path; do
[[ -n "$path" ]] || continue
printf '%s %s\n' "$(hash_file "$root/$path")" "$path" >> "$root/MANIFEST.sha256" || return 1
done <<< "$inventory"
}
bundle_repo() {
local destination="${1:-}" temporary path inventory inventory_status
[[ -n "$destination" ]] || {
usage
return 2
}
check_repo || return $?
[[ ! -e "$destination" && ! -L "$destination" ]] || {
error "refusing to overwrite existing bundle '$destination'"
return 2
}
temporary="$(mktemp -d "${destination}.tmp.XXXXXX")" || {
error "unable to create a private staging directory beside '$destination'"
return 1
}
inventory="$(file_list)" || {
inventory_status=$?
rm -rf -- "$temporary"
return "$inventory_status"
}
while IFS= read -r path; do
[[ -n "$path" ]] || continue
if ! mkdir -p "$temporary/$(dirname -- "$path")" ||
! cp -- "$repo_root/$path" "$temporary/$path"; then
rm -rf -- "$temporary"
return 1
fi
done <<< "$inventory"
if ! mkdir -p "$temporary/lib/bash"; then
rm -rf -- "$temporary"
return 1
fi
if [[ -r "$repo_root/lib/bash/base-bash-libs.release" ]]; then
if ! cp -- "$repo_root/lib/bash/base-bash-libs.release" "$temporary/lib/bash/base-bash-libs.release"; then
rm -rf -- "$temporary"
return 1
fi
fi
printf 'bundle_format=1\nsource_version=%s\nsource_commit=%s\nprovenance=deterministic-directory-bundle\n' \
"$(< "$repo_root/VERSION")" \
"$(sed -n 's/^commit=//p' "$repo_root/lib/bash/base-bash-libs.release" | sed -n '1p')" \
> "$temporary/BUNDLE.release" || {
rm -rf -- "$temporary"
return 1
}
if ! write_hash_manifest "$temporary"; then
rm -rf -- "$temporary"
return 1
fi
if ! mv -- "$temporary" "$destination"; then
rm -rf -- "$temporary"
return 1
fi
printf 'Created deterministic bundle: %s\n' "$destination"
}
verify_bundle() {
local root="${1:-}" expected actual path line line_number=0
local checksum_pattern inventory
local -A checksum_seen=() expected_seen=()
[[ -n "$root" && -d "$root" && ! -L "$root" && -f "$root/MANIFEST.sha256" &&
! -L "$root/MANIFEST.sha256" && -f "$root/BUNDLE.release" && ! -L "$root/BUNDLE.release" ]] || {
error "bundle is missing MANIFEST.sha256: $root"
return 1
}
inventory="$(bundle_inventory)" || return $?
checksum_pattern='^([0-9a-f]{64}) ([A-Za-z0-9_./-]+)$'
while IFS= read -r line || [[ -n "$line" ]]; do
line_number=$((line_number + 1))
[[ "$line" =~ $checksum_pattern ]] || {
error "malformed checksum entry at line $line_number"
return 1
}
expected="${BASH_REMATCH[1]}"
path="${BASH_REMATCH[2]}"
[[ "$path" != /* && "$path" != ./* && "$path" != */ && "$path" != *'//'* &&
"$path" != *'/../'* && "$path" != ../* && "$path" != *'/./'* && "$path" != ./ ]] || {
error "unsafe checksum path: $path"
return 1
}
[[ -z "${checksum_seen[$path]+set}" ]] || {
error "duplicate checksum entry: $path"
return 1
}
checksum_seen["$path"]=1
[[ -f "$root/$path" ]] || {
error "bundle file is missing: $path"
return 1
}
[[ ! -L "$root/$path" ]] || {
error "bundle file must not be a symlink: $path"
return 1
}
actual="$(hash_file "$root/$path")"
[[ "$actual" == "$expected" ]] || {
error "bundle hash mismatch: $path"
return 1
}
done < "$root/MANIFEST.sha256"
while IFS= read -r path; do
[[ -n "$path" ]] || continue
expected_seen["$path"]=1
[[ -n "${checksum_seen[$path]+set}" ]] || {
error "bundle checksum manifest omits expected file: $path"
return 1
}
done <<< "$inventory"
for path in "${!checksum_seen[@]}"; do
[[ -n "${expected_seen[$path]+set}" ]] || {
error "bundle checksum manifest contains unexpected file: $path"
return 1
}
done
# Reject symlink payloads and regular files not represented by the exact
# inventory. A vendor destination may additionally carry its lock file.
while IFS= read -r path; do
path="${path#./}"
[[ "$path" == MANIFEST.sha256 ]] && continue
[[ "$path" == base-bash-libs.lock && "${BUNDLE_VERIFY_ALLOW_LOCK:-0}" == 1 ]] && continue
[[ -n "${expected_seen[$path]+set}" ]] || {
error "bundle contains unexpected file: $path"
return 1
}
done < <(cd -- "$root" && find . -type f -print | LC_ALL=C sort)
if find "$root" -type l -print -quit | grep -q .; then
error 'bundle contains a symlink payload'
return 1
fi
local metadata_key metadata_value metadata_line metadata_line_count=0
local -A metadata_seen=()
while IFS= read -r metadata_line || [[ -n "$metadata_line" ]]; do
metadata_line_count=$((metadata_line_count + 1))
[[ "$metadata_line" =~ ^([a-z_][a-z_]*)=([^[:space:]]+)$ ]] || {
error "malformed BUNDLE.release field at line $metadata_line_count"
return 1
}
metadata_key="${BASH_REMATCH[1]}"
metadata_value="${BASH_REMATCH[2]}"
[[ -z "${metadata_seen[$metadata_key]+set}" ]] || {
error "duplicate BUNDLE.release field: $metadata_key"
return 1
}
metadata_seen["$metadata_key"]=1
case "$metadata_key" in
bundle_format) [[ "$metadata_value" == 1 ]] || {
error 'unsupported bundle format'
return 1
} ;;
source_version) [[ "$metadata_value" =~ ^[0-9]+\.[0-9]+\.[0-9]+([.-][A-Za-z0-9.-]+)?$ ]] || {
error 'invalid source version metadata'
return 1
} ;;
source_commit) [[ "$metadata_value" =~ ^(unknown|[0-9a-f]{40})$ ]] || {
error 'invalid source commit metadata'
return 1
} ;;
provenance) [[ "$metadata_value" == deterministic-directory-bundle || "$metadata_value" == release-artifact ]] || {
error 'invalid provenance metadata'
return 1
} ;;
*)
error "unknown BUNDLE.release field: $metadata_key"
return 1
;;
esac
done < "$root/BUNDLE.release"
((metadata_line_count == 4)) || {
error 'BUNDLE.release must contain exactly four metadata fields'
return 1
}
printf 'Bundle provenance and hashes are valid: %s\n' "$root"
}
main() {
case "${1:-}" in
check)
(($# == 1)) || {
usage
return 2
}
check_repo
;;
bundle)
(($# == 2)) || {
usage
return 2
}
bundle_repo "$2"
;;
verify)
(($# == 2)) || {
usage
return 2
}
verify_bundle "$2"
;;
*)
usage
return 2
;;
esac
}
main "$@"