Skip to content

Commit 9bb11a8

Browse files
committed
demo code change and introduce compile.sh -docker option
1 parent cd0543b commit 9bb11a8

4 files changed

Lines changed: 78 additions & 50 deletions

File tree

README.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -468,28 +468,32 @@ Produces `libsrvguard.a` for static linking into any native application.
468468

469469
## Building srvguard
470470

471-
### Development binary
471+
Use `compile.sh` — output always lands in `bin/`.
472472

473-
```bash
474-
go build -o srvguard .
475-
```
476-
477-
### Production static binary
473+
### Native (requires Go)
478474

479475
```bash
480-
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o srvguard .
476+
./compile.sh # current platform → bin/srvguard
477+
./compile.sh -amd64 # → bin/srvguard-linux-amd64
478+
./compile.sh -arm64 # → bin/srvguard-linux-arm64
479+
./compile.sh -all # both amd64 + arm64
481480
```
482481

483-
### Container — test image (Wolfi runtime)
482+
### Without Go — build inside a container
484483

485484
```bash
486-
docker build --target test -t srvguard:test .
485+
./compile.sh -docker # uses golang:alpine, no local Go required
487486
```
488487

489-
### Container — release image (Chainguard static)
488+
Output is written to `bin/srvguard` on the host via a volume mount.
489+
Override the image with `GO_IMAGE=golang:1.22-alpine ./compile.sh -docker`.
490+
491+
### Container image (Docker)
490492

491493
```bash
492-
docker build --target release -t srvguard:latest .
494+
./build.sh # release image, local platform → docker load
495+
./build.sh -docker test # test image
496+
REGISTRY=myregistry.example.com/srvguard ./build.sh push # multi-arch push
493497
```
494498

495499
## Vault Secret Format

bin/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
Local build output directory.
44

55
Run `../compile.sh` to build the `srvguard` binary here.
6+
Run `../compile.sh -docker` if Go is not installed locally.

compile.sh

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
#!/bin/bash
1+
#!/bin/sh
22
# compile.sh — build srvguard binary
33
#
44
# Usage:
5-
# ./compile.sh — build for current platform
6-
# ./compile.sh all — build for all supported platforms (amd64, arm64)
5+
# ./compile.sh — build for current platform (requires Go)
6+
# ./compile.sh -all — build amd64 + arm64 (requires Go)
7+
# ./compile.sh -amd64 — build linux/amd64 (requires Go)
8+
# ./compile.sh -arm64 — build linux/arm64 (requires Go)
9+
# ./compile.sh -docker — build for current platform inside golang:alpine
710

811
set -e
912

@@ -14,34 +17,59 @@ git config core.hooksPath .githooks 2>/dev/null || true
1417
SRC_DIR="${SCRIPT_DIR}/src"
1518
BIN_DIR="${SCRIPT_DIR}/bin"
1619

20+
GO_IMAGE="${GO_IMAGE:-golang:alpine}"
21+
22+
usage() { printf "Usage: %s [-docker|-all|-amd64|-arm64]\n" "$0"; exit 1; }
23+
24+
# ── Argument parsing ──────────────────────────────────────────────────────────
25+
26+
OPT_DOCKER=false
27+
OPT_ALL=false
28+
OPT_ARCH=""
29+
30+
for arg in "$@"; do
31+
case "$arg" in
32+
-docker) OPT_DOCKER=true ;;
33+
-all) OPT_ALL=true ;;
34+
-amd64) OPT_ARCH=amd64 ;;
35+
-arm64) OPT_ARCH=arm64 ;;
36+
*) usage ;;
37+
esac
38+
done
39+
40+
# ── Docker mode ───────────────────────────────────────────────────────────────
41+
42+
if $OPT_DOCKER; then
43+
command -v docker &>/dev/null || { printf "ERROR: docker not found\n" >&2; exit 1; }
44+
printf "Building inside container (%s) → bin/srvguard ...\n" "${GO_IMAGE}"
45+
docker run --rm \
46+
-v "${SCRIPT_DIR}:/work" \
47+
-w /work \
48+
"${GO_IMAGE}" \
49+
sh -c "sh compile.sh"
50+
exit $?
51+
fi
52+
53+
# ── Native build ──────────────────────────────────────────────────────────────
54+
1755
build_target()
1856
{
19-
local arch="$1"
20-
local out="$2"
57+
local arch="$1"
58+
local out="$2"
2159

22-
printf "Building %s ...\n" "${out}"
23-
( cd "${SRC_DIR}" && CGO_ENABLED=0 GOOS=linux GOARCH="${arch}" \
24-
go build -buildvcs=false -ldflags="-s -w" -o "${out}" . )
60+
printf "Building %s ...\n" "${out}"
61+
( cd "${SRC_DIR}" && CGO_ENABLED=0 GOOS=linux GOARCH="${arch}" \
62+
go build -buildvcs=false -ldflags="-s -w" -o "${out}" . )
2563
}
2664

27-
TARGET="${1:-native}"
28-
29-
case "${TARGET}" in
30-
native)
31-
NATIVE_ARCH=$(go env GOARCH)
32-
build_target "${NATIVE_ARCH}" "${BIN_DIR}/srvguard"
33-
;;
34-
all)
65+
if $OPT_ALL; then
3566
build_target amd64 "${BIN_DIR}/srvguard-linux-amd64"
3667
build_target arm64 "${BIN_DIR}/srvguard-linux-arm64"
37-
;;
38-
amd64|arm64)
39-
build_target "${TARGET}" "${BIN_DIR}/srvguard-linux-${TARGET}"
40-
;;
41-
*)
42-
printf "Usage: %s [all|amd64|arm64]\n" "$0"
43-
exit 1
44-
;;
45-
esac
68+
elif [ -n "$OPT_ARCH" ]; then
69+
build_target "${OPT_ARCH}" "${BIN_DIR}/srvguard-linux-${OPT_ARCH}"
70+
else
71+
NATIVE_ARCH=$(go env GOARCH)
72+
build_target "${NATIVE_ARCH}" "${BIN_DIR}/srvguard"
73+
fi
4674

4775
printf "Done.\n"

consumers/cpp/demo.sh

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,15 @@ SYSTEMD_VER=$(systemctl --version | awk 'NR==1{print $2}')
5858
[[ "$SYSTEMD_VER" -ge 250 ]] || die "systemd v250+ required (have $SYSTEMD_VER)"
5959
log "systemd version: $SYSTEMD_VER"
6060

61-
# Verify credential support is available on this machine
62-
if ! systemd-creds has-tpm2 &>/dev/null && \
63-
[[ ! -f /var/lib/systemd/credential.secret ]]; then
64-
log "creating /var/lib/systemd/credential.secret ..."
61+
# Verify credential support and report which backend is active
62+
if systemd-creds has-tpm2 &>/dev/null; then
63+
log "credential backend: TPM2 (hardware-bound)"
64+
elif [[ -f /var/lib/systemd/credential.secret ]]; then
65+
log "credential backend: credential.secret (machine-specific, no TPM2)"
66+
else
67+
log "no credential backend found — running systemd-creds setup ..."
6568
systemd-creds setup
69+
log "credential backend: credential.secret (created)"
6670
fi
6771

6872
# ── Build example binary ──────────────────────────────────────────────────────
@@ -123,13 +127,4 @@ sleep 1
123127
journalctl -u "${UNIT_NAME}.service" --no-pager -o cat | grep -v "^$"
124128

125129
echo
126-
if journalctl -u "${UNIT_NAME}.service" --no-pager -o cat | grep -q "keyring: got password"; then
127-
echo " ✓ systemd credential decrypted into \$CREDENTIALS_DIRECTORY"
128-
echo " ✓ srvguard loaded it into the kernel keyring"
129-
echo " ✓ example consumer read it — key revoked, memory zeroed"
130-
echo
131-
echo " Next step: demo-transient.sh — same flow with Vault in the middle"
132-
else
133-
echo " ✗ check output above"
134-
exit 1
135-
fi
130+
echo " Next step: demo-transient.sh — same flow with Vault in the middle"

0 commit comments

Comments
 (0)