Skip to content

Commit 7c4158f

Browse files
authored
Merge pull request #1404 from besteffects/fips_140_autotests
Updated verification of GOFIPS140=1.0.0 and improved containers clean up
2 parents 5e0de83 + 13acc7f commit 7c4158f

1 file changed

Lines changed: 30 additions & 10 deletions

File tree

.github/scripts/verify_fips_image.sh

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
# * grep "FIPS 140-3: true"
1313
# 3. Verify the embedded Go binary was actually linked against the FIPS module
1414
# * go version -m <binary>
15-
# * check it shows GOFIPS140, the fips140 build tag, DefaultGODEBUG=fips140=on and CGO_ENABLED=0
15+
# * check it shows GOFIPS140=v1.0.0 (certified version), the fips140v1.0 build
16+
# tag, DefaultGODEBUG=fips140=on and CGO_ENABLED=0
1617
#
1718
# Usage:
1819
# verify_fips_image.sh --image <docker-image-ref> [--binary <path-to-fips-binary>]
@@ -62,8 +63,10 @@ fail() { echo "FIPS VERIFY FAIL: $*" >&2; exit 1; }
6263
ok() { echo "FIPS VERIFY OK: $*"; }
6364

6465
CLEANUP_BINARY=""
66+
CLEANUP_CONTAINER=""
6567
cleanup() {
6668
[[ -n "${CLEANUP_BINARY}" ]] && rm -f "${CLEANUP_BINARY}" || true
69+
[[ -n "${CLEANUP_CONTAINER}" ]] && docker rm -f "${CLEANUP_CONTAINER}" >/dev/null 2>&1 || true
6770
}
6871
trap cleanup EXIT
6972

@@ -79,7 +82,7 @@ verify_image_runtime() {
7982
if ! env_json="$(docker image inspect --format '{{json .Config.Env}}' "${image}" 2>&1)"; then
8083
fail "unable to inspect image ${image}: ${env_json}"
8184
fi
82-
grep -q "${godebug_marker}" <<<"${env_json}" \
85+
grep -qF "${godebug_marker}" <<<"${env_json}" \
8386
|| fail "image ${image} does not set ${godebug_marker} (Env: ${env_json})"
8487
ok "image env enforces ${godebug_marker}"
8588

@@ -90,9 +93,9 @@ verify_image_runtime() {
9093
fi
9194
printf '%s\n' "${version_out}"
9295
local fips_line
93-
fips_line="$(grep "${version_label}" <<<"${version_out}" || true)"
96+
fips_line="$(grep -F "${version_label}" <<<"${version_out}" || true)"
9497
[[ -n "${fips_line}" ]] || fail "'${version_label}' not present in --version output"
95-
grep -qi 'true' <<<"${fips_line}" \
98+
grep -qiF 'true' <<<"${fips_line}" \
9699
|| fail "image ${image} reports non-FIPS build: ${fips_line}"
97100
ok "image reports ${version_label} true"
98101
}
@@ -101,20 +104,24 @@ resolve_binary_from_image() {
101104
local image="$1"
102105
local tmp
103106
tmp="$(mktemp "${TMPDIR:-/tmp}/clickhouse-backup-fips.XXXXXX")"
107+
# Register temp file and container for cleanup immediately, so the EXIT
108+
# removes both even if `docker cp` fails (set -e aborts before later lines).
109+
CLEANUP_BINARY="${tmp}"
104110
local cid
105111
cid="$(docker create "${image}")"
112+
CLEANUP_CONTAINER="${cid}"
106113
docker cp "${cid}:/bin/clickhouse-backup" "${tmp}" >/dev/null
107114
docker rm -f "${cid}" >/dev/null
108-
CLEANUP_BINARY="${tmp}"
115+
CLEANUP_CONTAINER=""
109116
echo "${tmp}"
110117
}
111118

112119
verify_binary_metadata() {
113120
local binary="$1"
114-
# Markers emitted by `go version -m` for a binary linked against the Go FIPS module (GOFIPS140=...)
121+
# Build settings that `go version -m` must report for a binary linked against
122+
# the certified Go FIPS module.
115123
local required_markers=(
116-
"GOFIPS140="
117-
"fips140"
124+
"fips140v1.0"
118125
"DefaultGODEBUG=fips140=on"
119126
"CGO_ENABLED=0"
120127
)
@@ -128,11 +135,24 @@ verify_binary_metadata() {
128135
fail "'go version -m ${binary}' failed: ${meta}"
129136
fi
130137

138+
# The certified FIPS module version must be v1.0.0. A pinned snapshot suffix is
139+
# allowed (e.g. v1.0.0-c2097c7c), but any other version (v1.0.01, v1.1.0, ...)
140+
# must be rejected, so we compare the exact value instead of a substring.
141+
local gofips_value
142+
gofips_value="$(grep -oE 'GOFIPS140=[^[:space:]]+' <<<"${meta}" | head -n1 | cut -d= -f2)"
143+
case "${gofips_value}" in
144+
v1.0.0 | v1.0.0-*)
145+
ok "binary build metadata contains GOFIPS140=v1.0.0" ;;
146+
*)
147+
printf '%s\n' "${meta}" >&2
148+
fail "binary ${binary} has GOFIPS140=${gofips_value:-<missing>}; expected v1.0.0 or v1.0.0-<suffix>" ;;
149+
esac
150+
131151
local marker
132152
for marker in "${required_markers[@]}"; do
133-
if ! grep -q "${marker}" <<<"${meta}"; then
153+
if ! grep -qF "${marker}" <<<"${meta}"; then
134154
printf '%s\n' "${meta}" >&2
135-
fail "binary ${binary} missing FIPS build marker: ${marker}"
155+
fail "binary ${binary} missing required FIPS build marker: ${marker}"
136156
fi
137157
ok "binary build metadata contains: ${marker}"
138158
done

0 commit comments

Comments
 (0)