mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-09-08 15:41:15 +02:00
* filer: require a read token for the root listing maybeCheckJwtAuthorization waved through every GET/HEAD on "/", so a filer with jwt.filer_signing.read.key set still served its root directory listing -- entry names, sizes and chunks[].file_id -- to a caller holding no token at all, and served the same listing to a token restricted by allowed_prefixes. The exemption was added for health checks before the filer had /healthz and /readyz. Both are registered on the default and read-only muxes ahead of the "/" handler and answer without a token, so drop it. Point the mTLS harness at /healthz, which is what it was probing for. * filer: keep the jwt query parameter out of a proxied chunk request The proxy stripped "jwt" from the forwarded query on reads only, on the grounds that a writer's own credential travels there. It does not: an uploader carries its AssignVolume token in the Authorization header, and the query parameter on this path holds a filer credential. Strip it for every method. A volume server has no business seeing a filer token, and because security.GetJwt reads the query before the header, relaying one would hide the writer's own token behind it. * filer: dispatch the chunk proxy after the JWT gate The ?proxyChunkId= branch returned before maybeCheckJwtAuthorization ran, so GET, PUT, POST and DELETE against any needle in the cluster were reachable on the filer's HTTP port with no filer credential, on a filer where every other request answered 401. An anonymous caller read a stored object, replaced its bytes, or deleted the needle, which the master's next vacuum makes permanent. #10434 stopped the filer from minting a volume write token for that caller, which closes the write half only where the volume server has a jwt.signing.key of its own -- not the shipped default, and not what scaffold/security.toml recommends for a filer deployment. The read half stayed open in every configuration, because the filer mints the read token itself. Move the dispatch below the gate. A file id carries no path, so a token restricted by allowed_prefixes cannot be scoped against one and is refused here; every consumer of this endpoint holds an unrestricted token. * filer: mint the volume credential for a proxied write too The proxy minted a volume token on reads and forwarded whatever the caller sent on writes. #10434 made it that way because the branch ran ahead of the JWT gate, so a token minted here would have been signed for an unauthenticated caller; the branch now runs behind the gate, and the credential the caller presents there is a filer one, which a volume server cannot validate and has no business seeing. Mint at the access level the request needs, and drop the caller's Authorization when there is no key to mint from. A proxied uploader then needs only the filer credential, instead of holding one for each hop with a single header to put them in. * mount, mq, filer.sync: send the filer credential for a proxied chunk Every in-tree consumer of ?proxyChunkId= reached the filer anonymously: mount and the broker put the AssignVolume token in the Authorization header, which is a volume credential, and filer.sync sent nothing at all. That was enough only while the branch ran ahead of the filer's JWT gate. Build the URL through one helper, and pick the credential from the URL it returns: a chunk proxied through a filer is a request to the filer, which authorizes it and attaches the volume credential itself, so the token there is a filer one at the access level the request needs. * filer: honor -exposeDirectoryData The flag was declared on all three commands that start a filer and read by none of them: FilerOption.ExposeDirectoryData was only ever assigned from filer.expose_directory_metadata in security.toml, so -exposeDirectoryData=false silently left the listing exposed. Only the TOML key had any effect. Plumb the flag through and let either switch turn the listing off. * filer: count a proxied chunk request once Moving the dispatch below the gate put it after the deferred request observation, so every proxied chunk now landed in FilerRequestHistogram twice, once under its HTTP method and once under chunkProxy. Name the deferred one after the proxy instead, the way the unsupported-method branch already does, which also gives the endpoint the status codes FilerRequestCounter records.
129 lines
5.1 KiB
Bash
Executable File
129 lines
5.1 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Generate mTLS material + security.toml with Terraform, then run a real
|
|
# master+volume+filer cluster with mTLS enabled and assert it works.
|
|
# ./run_local_secure.sh # render + run + assert + teardown
|
|
# KEEP=1 ./run_local_secure.sh
|
|
set -u
|
|
|
|
HERE="$(cd "$(dirname "$0")" && pwd)"
|
|
export PATH="/opt/homebrew/bin:$PATH"
|
|
TOFU="${TOFU:-tofu}"
|
|
WEED="${WEED:-$(go env GOPATH 2>/dev/null || echo "$HOME/go")/bin/weed}"
|
|
WORKDIR="${WORKDIR:-/tmp/seaweedfs-tftest-secure}"
|
|
LOGDIR="$WORKDIR/logs"
|
|
RUNDIR="$WORKDIR/run"
|
|
|
|
PASS=0
|
|
FAIL=0
|
|
ok() { echo " PASS: $1"; PASS=$((PASS + 1)); }
|
|
bad() { echo " FAIL: $1"; FAIL=$((FAIL + 1)); }
|
|
info() { echo "==> $1"; }
|
|
|
|
cleanup() {
|
|
info "tearing down"
|
|
if [ -d "$RUNDIR" ]; then
|
|
for pf in "$RUNDIR"/*.pid; do [ -f "$pf" ] && kill "$(cat "$pf")" 2>/dev/null; done
|
|
sleep 1
|
|
for pf in "$RUNDIR"/*.pid; do [ -f "$pf" ] && kill -9 "$(cat "$pf")" 2>/dev/null; done
|
|
fi
|
|
}
|
|
|
|
info "cleaning $WORKDIR"
|
|
case "$WORKDIR" in "" | "/" | "$HOME") echo "refusing to delete '$WORKDIR'" >&2; exit 2 ;; esac
|
|
rm -rf "$WORKDIR"
|
|
mkdir -p "$LOGDIR" "$RUNDIR" "$WORKDIR/.seaweedfs"
|
|
[ -x "$WEED" ] || { echo "weed not found at $WEED" >&2; exit 2; }
|
|
|
|
info "generating certs + rendering config with OpenTofu"
|
|
cd "$HERE"
|
|
"$TOFU" init -backend=false -input=false -no-color >/dev/null 2>&1 || { echo "tofu init failed"; exit 2; }
|
|
if ! "$TOFU" apply -auto-approve -input=false -no-color \
|
|
-var "weed_binary=$WEED" -var "workdir=$WORKDIR" >"$LOGDIR/tofu.log" 2>&1; then
|
|
echo "tofu apply failed"; tail -30 "$LOGDIR/tofu.log"; exit 2
|
|
fi
|
|
|
|
# write certs to their on-host paths
|
|
"$TOFU" output -json certs | jq -c '.[]' | while IFS= read -r c; do
|
|
p="$(echo "$c" | jq -r '.path')"; m="$(echo "$c" | jq -r '.mode')"
|
|
mkdir -p "$(dirname "$p")"
|
|
echo "$c" | jq -r '.content' > "$p"
|
|
chmod "$m" "$p"
|
|
done
|
|
# place security.toml where weed searches ($HOME/.seaweedfs)
|
|
"$TOFU" output -raw security_toml > "$WORKDIR/.seaweedfs/security.toml"
|
|
info "security.toml + $(ls "$WORKDIR"/certs | wc -l | tr -d ' ') cert dirs written"
|
|
|
|
OUT="$("$TOFU" output -json cluster)"
|
|
|
|
# derive ports from the rendered config (high range; never hardcoded)
|
|
port_of() { echo "$OUT" | jq -r --arg r "$1" '.[] | select(.role==$r) | .http_port' | head -1; }
|
|
MPORT="$(port_of master)"; VPORT="$(port_of volume)"; FPORT="$(port_of filer)"
|
|
|
|
busy=""
|
|
for p in $(echo "$OUT" | jq -r '.[].http_port'); do
|
|
lsof -nP -iTCP:"$p" -sTCP:LISTEN >/dev/null 2>&1 && busy="$busy $p"
|
|
done
|
|
[ -n "$busy" ] && { echo "Required port(s) already in use:$busy -- is another SeaweedFS running?" >&2; exit 3; }
|
|
|
|
[ "${KEEP:-0}" = "1" ] || trap cleanup EXIT INT TERM
|
|
|
|
# launch a node with HOME pointed at $WORKDIR so weed loads security.toml
|
|
launch() {
|
|
n="$1"
|
|
for d in $(echo "$OUT" | jq -r --arg n "$n" '.[$n].data_dirs[]?'); do mkdir -p "$d"; done
|
|
ENVS=("HOME=$WORKDIR")
|
|
while IFS= read -r e; do [ -n "$e" ] && ENVS+=("$e"); done \
|
|
< <(echo "$OUT" | jq -r --arg n "$n" '.[$n].env | to_entries[] | "\(.key)=\(.value)"')
|
|
ARGV=()
|
|
while IFS= read -r a; do ARGV+=("$a"); done \
|
|
< <(echo "$OUT" | jq -r --arg n "$n" '.[$n].argv[]')
|
|
info "launching $n (mTLS)"
|
|
env "${ENVS[@]}" "$WEED" "${ARGV[@]}" >"$LOGDIR/$n.log" 2>&1 &
|
|
echo "$!" > "$RUNDIR/$n.pid"
|
|
}
|
|
|
|
wait_http() {
|
|
url="$1"; t="${2:-30}"; i=0
|
|
while [ "$i" -lt "$t" ]; do
|
|
curl -fsS -o /dev/null --max-time 2 "$url" 2>/dev/null && return 0
|
|
i=$((i + 1)); sleep 1
|
|
done
|
|
return 1
|
|
}
|
|
|
|
launch master-m0
|
|
QOK=0; i=0
|
|
while [ "$i" -lt 40 ]; do
|
|
st="$(curl -fsS --max-time 2 "http://127.0.0.1:$MPORT/cluster/status" 2>/dev/null)" || { i=$((i+1)); sleep 1; continue; }
|
|
[ "$(echo "$st" | jq -r '.IsLeader // false')" = "true" ] && { QOK=1; break; }
|
|
i=$((i + 1)); sleep 1
|
|
done
|
|
[ "$QOK" -eq 1 ] && ok "master elected leader under mTLS" || bad "master leader (mTLS)"
|
|
|
|
launch volume-v0
|
|
# registration (master gRPC over mTLS) confirms the volume joined the cluster
|
|
AOK=0; i=0
|
|
while [ "$i" -lt 40 ]; do
|
|
fid="$(curl -fsS --max-time 2 "http://127.0.0.1:$MPORT/dir/assign" 2>/dev/null | jq -r '.fid // ""')"
|
|
[ -n "$fid" ] && { AOK=1; break; }
|
|
i=$((i + 1)); sleep 1
|
|
done
|
|
[ "$AOK" -eq 1 ] && ok "volume registered via mTLS gRPC (fid $fid)" || bad "volume registration over mTLS"
|
|
# /healthz flips to 200 after the first heartbeat completes (slower under mTLS)
|
|
wait_http "http://127.0.0.1:$VPORT/healthz" 60 && ok "volume /healthz up (mTLS)" || bad "volume /healthz"
|
|
|
|
launch filer-f0
|
|
wait_http "http://127.0.0.1:$FPORT/healthz" 30 && ok "filer /healthz up (mTLS)" || bad "filer /healthz"
|
|
# [jwt.filer_signing] is active, so the filer requires a signed JWT for writes.
|
|
# An unsigned write MUST be rejected with 401 -- this proves the JWT signing key
|
|
# rendered into security.toml is enforced (positive security assertion).
|
|
echo "smoke" > "$WORKDIR/hello.txt"
|
|
code="$(curl -s -o /dev/null -w '%{http_code}' --max-time 5 -X POST \
|
|
-F "file=@$WORKDIR/hello.txt" "http://127.0.0.1:$FPORT/smoke/hello.txt" 2>/dev/null)"
|
|
[ "$code" = "401" ] && ok "filer rejects unsigned write (HTTP 401) => JWT signing enforced" \
|
|
|| bad "filer JWT enforcement (expected 401, got HTTP $code)"
|
|
|
|
echo
|
|
info "RESULTS: $PASS passed, $FAIL failed"
|
|
[ "$FAIL" -eq 0 ]
|