diff --git a/hack/seaweedfs-object-lock-actions.bats b/hack/seaweedfs-object-lock-actions.bats new file mode 100644 index 000000000..f209caaa7 --- /dev/null +++ b/hack/seaweedfs-object-lock-actions.bats @@ -0,0 +1,302 @@ +#!/usr/bin/env bats + +load test_helper + +SCRIPT="$PWD/packages/system/seaweedfs/files/object-lock-actions.sh" + +setup() { + strict_setup + BIN="$BATS_TEST_TMPDIR/bin" + mkdir -p "$BIN" + export STATE="$BATS_TEST_TMPDIR/iam.json" + export CALLS="$BATS_TEST_TMPDIR/calls" + export KUBECTL_LOG="$BATS_TEST_TMPDIR/kubectl" + : > "$CALLS" + : > "$KUBECTL_LOG" + cat > "$BIN/weed" <<'EOF' +#!/bin/bash +set -eu +[ "$1" = shell ] || exit 2 +[ -n "${WEED_SLEEP:-}" ] && sleep "$WEED_SLEEP" +drop=false +if [ -n "${WEED_DROP_SESSIONS:-}" ]; then + n=$(cat "$STATE.sessions" 2>/dev/null || echo 0) + # Only sessions that apply count. + input=$(cat) + case $input in *" -apply"*) + n=$((n + 1)); echo "$n" > "$STATE.sessions" + [ "$n" -le "$WEED_DROP_SESSIONS" ] && drop=true ;; + esac + exec <<< "$input" +fi +while read -r line; do + case $line in + "s3.configure") + if [ -n "${WEED_READS_OK:-}" ]; then + reads=$(cat "$STATE.reads" 2>/dev/null || echo 0) + echo $((reads + 1)) > "$STATE.reads" + [ "$reads" -lt "$WEED_READS_OK" ] || exit 1 + fi + if [ -n "${IDENTITIES_DIR:-}" ]; then + jq --slurp '{identities: .}' "$IDENTITIES_DIR"/*.json + else + cat "$STATE" + fi + ;; + "s3.configure -user="*) + echo "$line" >> "$CALLS" + user=${line#s3.configure -user=}; user=${user%% *} + rest=${line#* -actions=} + actions=${rest%% *} + apply=false + case $rest in *" -apply") apply=true ;; esac + if [ "$user" = "${WEED_UNRESOLVED:-}" ]; then + current='{"name":"'"$user"'","credentials":[],"actions":[]}' + elif [ -n "${IDENTITIES_DIR:-}" ]; then + current=$(cat "$IDENTITIES_DIR/$user.json") + else + current=$(jq -c --arg u "$user" '.identities[] | select(.name == $u)' "$STATE") + fi + updated=$(jq -c --arg a "$actions" '.actions = reduce ($a | split(","))[] as $x (.actions; if index([$x]) then . else . + [$x] end)' <<< "$current") + if $apply; then + { [ -n "${WEED_NOOP_APPLY:-}" ] || $drop; } && continue + if [ -n "${IDENTITIES_DIR:-}" ]; then + printf '%s\n' "$updated" > "$IDENTITIES_DIR/$user.json" + else + jq --arg u "$user" --argjson id "$updated" \ + '.identities |= map(if .name == $u then $id else . end)' "$STATE" > "$STATE.new" + mv "$STATE.new" "$STATE" + fi + echo "$updated" + else + echo "$updated" + echo 'Running in simulation mode. Use "-apply" option to apply the changes.' + fi + ;; + *) + exit 3 + ;; + esac +done +EOF + cat > "$BIN/kubectl" <<'EOF' +#!/bin/sh +case $1 in + get) echo "ARGS: $*" >> "$KUBECTL_LOG" ;; + *) { echo "ARGS: $*"; cat; } >> "$KUBECTL_LOG" ;; +esac +[ -z "${KUBECTL_SLEEP:-}" ] || exec sleep "$KUBECTL_SLEEP" +[ "$1" != get ] || printf 0b5e1a7e-0000-4000-8000-000000000001 +EOF + chmod +x "$BIN/weed" "$BIN/kubectl" + export PATH="$BIN:$PATH" + export MASTER=seaweedfs-master:9333 FILER=seaweedfs-filer-client:8888 + export NAMESPACE=tenant-foo RELEASE=seaweedfs-system + export ATTEMPTS=2 RETRY_DELAY=0 SHELL_TIMEOUT=5 +} + +identity() { + printf '{"name":"%s","credentials":[{"accessKey":"AK-%s","secretKey":"SECRET-%s"}],"actions":%s,"isStatic":%s}' \ + "$1" "$1" "$1" "$2" "${3:-false}" +} + +state() { + printf '{"identities":[%s]}\n' "$(IFS=,; echo "$*")" > "$STATE" +} + +event() { + sed '1,/^ARGS: create --filename -$/d' "$KUBECTL_LOG" | jq --exit-status "$@" > /dev/null +} + +actions_of() { + jq -c --arg u "$1" '.identities[] | select(.name == $u) | .actions | sort' "$STATE" +} + +@test "readwrite identity gains the Get and Put Object Lock actions for its bucket" { + state "$(identity rw '["Read:b1","Write:b1","List:b1","Tagging:b1"]')" + "$SCRIPT" + [ "$(actions_of rw)" = '["GetBucketObjectLockConfiguration:b1","GetObjectLegalHold:b1","GetObjectRetention:b1","List:b1","PutBucketObjectLockConfiguration:b1","PutObjectLegalHold:b1","PutObjectRetention:b1","Read:b1","Tagging:b1","Write:b1"]' ] +} + +@test "per-identity IAM migration preserves old credentials and leaves an already upgraded grant unchanged" { + export IDENTITIES_DIR="$BATS_TEST_TMPDIR/identities" + mkdir -p "$IDENTITIES_DIR" + state + identity old '["Read:b1","Write:b1","List:b1","Tagging:b1"]' > "$IDENTITIES_DIR/old.json" + identity new '["Read:b2","List:b2","GetObjectRetention:b2","GetObjectLegalHold:b2","GetBucketObjectLockConfiguration:b2"]' > "$IDENTITIES_DIR/new.json" + old_credentials=$(jq -c '.credentials' "$IDENTITIES_DIR/old.json") + new_identity=$(cat "$IDENTITIES_DIR/new.json") + "$SCRIPT" + [ "$(jq -c '.credentials' "$IDENTITIES_DIR/old.json")" = "$old_credentials" ] + [ "$(jq -c '.actions | sort' "$IDENTITIES_DIR/old.json")" = '["GetBucketObjectLockConfiguration:b1","GetObjectLegalHold:b1","GetObjectRetention:b1","List:b1","PutBucketObjectLockConfiguration:b1","PutObjectLegalHold:b1","PutObjectRetention:b1","Read:b1","Tagging:b1","Write:b1"]' ] + [ "$(cat "$IDENTITIES_DIR/new.json")" = "$new_identity" ] + [ "$(cat "$STATE")" = '{"identities":[]}' ] + [ "$(grep -c -- ' -apply$' "$CALLS")" -eq 1 ] + [ ! -s "$KUBECTL_LOG" ] + : > "$CALLS" + "$SCRIPT" + [ ! -s "$CALLS" ] +} + +@test "readonly identity gains only the Get Object Lock actions" { + state "$(identity ro '["Read:b1","List:b1"]')" + "$SCRIPT" + [ "$(actions_of ro)" = '["GetBucketObjectLockConfiguration:b1","GetObjectLegalHold:b1","GetObjectRetention:b1","List:b1","Read:b1"]' ] +} + +@test "the suffix of each grant is carried over verbatim, bare grants stay bare" { + state "$(identity w '["Write","Read:logs/*"]')" + "$SCRIPT" + [ "$(actions_of w)" = '["GetBucketObjectLockConfiguration:logs/*","GetObjectLegalHold:logs/*","GetObjectRetention:logs/*","PutBucketObjectLockConfiguration","PutObjectLegalHold","PutObjectRetention","Read:logs/*","Write"]' ] +} + +@test "actions that only start with Read or Write imply nothing" { + state "$(identity acp '["ReadAcp:b1","WriteAcp:b1","List:b1"]')" + "$SCRIPT" + [ "$(actions_of acp)" = '["List:b1","ReadAcp:b1","WriteAcp:b1"]' ] + [ ! -s "$CALLS" ] +} + +@test "admin and static identities are left alone" { + state "$(identity admin '["Admin"]')" "$(identity static '["Read","Write"]' true)" + "$SCRIPT" + [ "$(actions_of admin)" = '["Admin"]' ] + [ "$(actions_of static)" = '["Read","Write"]' ] + [ ! -s "$CALLS" ] +} + +@test "a second run changes nothing" { + state "$(identity rw '["Read:b1","Write:b1"]')" "$(identity ro '["Read:b2"]')" + "$SCRIPT" + [ "$(grep -c -- ' -apply$' "$CALLS")" -eq 2 ] + : > "$CALLS" + "$SCRIPT" + [ ! -s "$CALLS" ] +} + +@test "secret keys never reach the output" { + state "$(identity rw '["Read:b1","Write:b1"]')" + out=$("$SCRIPT" 2>&1) + case $out in + *SECRET-*) echo "FAIL: secret key printed: $out"; false ;; + esac + case $out in + *rw*) ;; + *) echo "FAIL: the identity it changed is not named: $out"; false ;; + esac +} + +@test "a grant that does not stick ends in a Warning Event on the HelmRelease and exit 0" { + state "$(identity rw '["Read:b1","Write:b1"]')" + WEED_NOOP_APPLY=1 "$SCRIPT" + [ "$(grep -c -- ' -apply$' "$CALLS")" -eq 2 ] + grep -q '^ARGS: create --filename -$' "$KUBECTL_LOG" + event '.type == "Warning"' + event '.involvedObject.kind == "HelmRelease"' + event '.involvedObject.name == "seaweedfs-system"' + event '.involvedObject.namespace == "tenant-foo"' + event '.involvedObject.uid == "0b5e1a7e-0000-4000-8000-000000000001"' + grep -q 'rw' "$KUBECTL_LOG" +} + +@test "an unreachable filer ends in a Warning Event and exit 0" { + rm -f "$STATE" + "$SCRIPT" + event '.type == "Warning"' + grep -q 'could not be read' "$KUBECTL_LOG" +} + +@test "a weed shell that never returns is cut off and still ends in an Event" { + state "$(identity rw '["Read:b1","Write:b1"]')" + SHELL_TIMEOUT=3 WEED_SLEEP=60 "$SCRIPT" + event '.type == "Warning"' +} + +@test "an identity s3.configure cannot resolve is never applied over" { + state "$(identity rw '["Read:b1","Write:b1"]')" "$(identity lost '["Read:b2","Write:b2"]')" + WEED_UNRESOLVED=lost "$SCRIPT" + [ "$(jq -c '.identities[] | select(.name == "lost")' "$STATE")" = '{"name":"lost","credentials":[{"accessKey":"AK-lost","secretKey":"SECRET-lost"}],"actions":["Read:b2","Write:b2"],"isStatic":false}' ] + if grep -q -- '-user=lost .* -apply$' "$CALLS"; then echo "FAIL: applied over an unresolved identity"; false; fi + [ "$(actions_of rw)" = '["GetBucketObjectLockConfiguration:b1","GetObjectLegalHold:b1","GetObjectRetention:b1","PutBucketObjectLockConfiguration:b1","PutObjectLegalHold:b1","PutObjectRetention:b1","Read:b1","Write:b1"]' ] + event '.type == "Warning"' + grep -q 'lost' "$KUBECTL_LOG" +} + +@test "output without an IAM configuration counts as a failed read" { + echo 'error: connection refused' > "$STATE" + "$SCRIPT" + grep -q 'could not be read' "$KUBECTL_LOG" +} + +@test "no Event is written when every grant sticks" { + state "$(identity rw '["Read:b1","Write:b1"]')" + "$SCRIPT" + [ ! -s "$KUBECTL_LOG" ] +} + +@test "JSON that is not an IAM configuration counts as a failed read" { + echo '{"error":"not ready"}' > "$STATE" + "$SCRIPT" + grep -q 'could not be read' "$KUBECTL_LOG" +} + +@test "grants that stick only on the last attempt raise no Event" { + state "$(identity rw '["Read:b1","Write:b1"]')" + WEED_DROP_SESSIONS=1 "$SCRIPT" + [ "$(grep -c -- ' -apply$' "$CALLS")" -eq 2 ] + [ ! -s "$KUBECTL_LOG" ] +} + +@test "a name or action weed shell would split never reaches a command line and is reported" { + state "$(identity rw '["Read:b1","Write:b1"]')" "$(identity 'x;s3.configure -user=rw -actions=Admin -apply' '["Read:b2"]')" "$(identity y '["Read:b3;s3.configure -user=rw -actions=Admin"]')" + "$SCRIPT" + if grep -q -- 'x;' "$CALLS"; then echo "FAIL: unsafe name reached weed shell"; false; fi + if grep -q -- 'Admin' "$CALLS"; then echo "FAIL: injected action reached weed shell"; false; fi + [ "$(actions_of rw)" = '["GetBucketObjectLockConfiguration:b1","GetObjectLegalHold:b1","GetObjectRetention:b1","PutBucketObjectLockConfiguration:b1","PutObjectLegalHold:b1","PutObjectRetention:b1","Read:b1","Write:b1"]' ] + event '.type == "Warning"' +} + +@test "a final read that fails reports the outcome as unverified, not the last plan" { + state "$(identity rw '["Read:b1","Write:b1"]')" + WEED_READS_OK=1 "$SCRIPT" + [ "$(actions_of rw)" = '["GetBucketObjectLockConfiguration:b1","GetObjectLegalHold:b1","GetObjectRetention:b1","PutBucketObjectLockConfiguration:b1","PutObjectLegalHold:b1","PutObjectRetention:b1","Read:b1","Write:b1"]' ] + grep -q 'could not be read' "$KUBECTL_LOG" + if grep -q 'missing them' "$KUBECTL_LOG"; then echo "FAIL: reported a stale plan"; false; fi +} + +# The stub logs its call before it hangs, so SHELL_TIMEOUT has to leave a +# loaded runner time to start it. +@test "a kubectl that never returns is cut off and the Job still exits 0" { + state "$(identity rw '["Read:b1","Write:b1"]')" + start=$SECONDS + SHELL_TIMEOUT=3 KUBECTL_SLEEP=60 WEED_NOOP_APPLY=1 "$SCRIPT" 2> "$BATS_TEST_TMPDIR/stderr" + [ $((SECONDS - start)) -lt 60 ] + grep -q '^ARGS: get helmrelease' "$KUBECTL_LOG" + grep -q '^ARGS: create --filename -$' "$KUBECTL_LOG" + grep -q 'could not record the Warning Event' "$BATS_TEST_TMPDIR/stderr" +} + +@test "a name or action ending in a newline never reaches a command line and is reported" { + state "$(identity rw '["Read:b1","Write:b1"]')" "$(identity 'foo\n' '["Read:b2"]')" "$(identity y '["Read:b3\n"]')" + "$SCRIPT" + if grep -q -- '-user=foo' "$CALLS"; then echo "FAIL: name with a trailing newline reached weed shell"; false; fi + if grep -q -- '-user=y' "$CALLS"; then echo "FAIL: action with a trailing newline reached weed shell"; false; fi + [ "$(actions_of rw)" = '["GetBucketObjectLockConfiguration:b1","GetObjectLegalHold:b1","GetObjectRetention:b1","PutBucketObjectLockConfiguration:b1","PutObjectLegalHold:b1","PutObjectRetention:b1","Read:b1","Write:b1"]' ] + event '.type == "Warning"' + grep -q 'foo' "$KUBECTL_LOG" +} + +@test "a document marker or a control character in an identity name stays inside the Event message" { + state "$(identity rw '["Read:b1","Write:b1"]')" "$(identity 'x\n--- \"y\\\u0080' '["Read:b2"]')" + "$SCRIPT" + [ "$(sed '1,/^ARGS: create --filename -$/d' "$KUBECTL_LOG" | jq --slurp length)" -eq 1 ] + event --arg name $'x\n--- "y\\\xc2\x80' '.message | contains($name)' +} + +@test "the identity public-read buckets share is never granted" { + state "$(identity rw '["Read:b1","Write:b1"]')" "$(identity anonymous '["Read:b2"]')" + "$SCRIPT" + [ "$(actions_of anonymous)" = '["Read:b2"]' ] + if grep -q -- '-user=anonymous' "$CALLS"; then echo "FAIL: anonymous reached weed shell"; false; fi + [ ! -s "$KUBECTL_LOG" ] +} diff --git a/packages/system/seaweedfs/Chart.yaml b/packages/system/seaweedfs/Chart.yaml index b6aee5353..1d8beac8d 100644 --- a/packages/system/seaweedfs/Chart.yaml +++ b/packages/system/seaweedfs/Chart.yaml @@ -1,3 +1,7 @@ apiVersion: v2 name: cozy-seaweedfs version: 0.0.0 # Placeholder, the actual version will be automatically set during the build process +dependencies: + - name: seaweedfs + version: "*" + repository: file://charts/seaweedfs diff --git a/packages/system/seaweedfs/files/object-lock-actions.sh b/packages/system/seaweedfs/files/object-lock-actions.sh new file mode 100755 index 000000000..415dfc6a1 --- /dev/null +++ b/packages/system/seaweedfs/files/object-lock-actions.sh @@ -0,0 +1,122 @@ +#!/bin/sh +# SeaweedFS 4.48 requires dedicated Object Lock actions. Upgrading the COSI +# driver does not re-grant access for Ready BucketAccesses, so old identities +# still need those actions. Governance bypass was never implied by Write. +# The IAM listing and apply output contain secret keys; neither is printed. +# Grant migration is best-effort: handled errors must not fail the Helm action. +set -eu + +: "${MASTER:?}" "${FILER:?}" "${NAMESPACE:?}" "${RELEASE:?}" +JOB=${JOB:-$RELEASE-object-lock-actions} +# weed shell waits for the master without a deadline, and kubectl waits for the +# API server without one. +ATTEMPTS=${ATTEMPTS:-3} +RETRY_DELAY=${RETRY_DELAY:-20} +SHELL_TIMEOUT=${SHELL_TIMEOUT:-30} + +weed_shell() { + timeout "$SHELL_TIMEOUT" weed shell -master="$MASTER" -filer="$FILER" +} + +# Public-read buckets share this identity, and the COSI driver revokes public +# read by removing only Read:; derived actions would outlive it. +ANONYMOUS=anonymous + +# Static identities come from a config file the IAM API cannot change. +plan_from_config() { + sed -n '/^{/,$p' | jq -c --arg anonymous "$ANONYMOUS" ' + def implied: + if test("^Write(:|$)") then + sub("^Write"; "") as $s + | ["PutObjectRetention", "PutObjectLegalHold", "PutBucketObjectLockConfiguration"] | map(. + $s) + elif test("^Read(:|$)") then + sub("^Read"; "") as $s + | ["GetObjectRetention", "GetObjectLegalHold", "GetBucketObjectLockConfiguration"] | map(. + $s) + else [] end; + if has("identities") | not then error("no IAM configuration in the output") else . end + | [.identities[]? + | select(.isStatic != true and .name != $anonymous) + | (.actions // []) as $have + | {name, have: $have, missing: ([$have[] | implied[]] | unique - $have)} + | select(.missing | length > 0)]' +} + +# s3.configure resolves the user on its own, and creates a fresh identity +# with no keys when it cannot, which would then replace the one listed. The +# dry run shows what it resolved; only an identity that still carries every +# action the listing showed is applied. +safe_commands() { + sed '/^Running in simulation mode/d' | jq -rs --argjson plan "$1" ' + (map({key: .name, value: (.actions // [])}) | from_entries) as $resolved + | $plan[] + | select(($resolved[.name] // []) as $r | .have - $r | length == 0) + | "s3.configure -user=\(.name) -actions=\(.missing | join(",")) -apply"' +} + +# weed shell splits a line on ";" and on whitespace, so an identity whose name +# or actions carry anything else is never put on a command line. It stays in +# the plan and is reported. +dry_run_commands() { + jq -r '.[] + | select(.name | test("^[A-Za-z0-9._@+=-]+\\z")) + | select(.missing | all(test("^[A-Za-z0-9._@+=:/*?-]+\\z"))) + | "s3.configure -user=\(.name) -actions=\(.missing | join(","))"' +} + +# Each attempt reads, then grants; the read after the last attempt is what +# decides the outcome. A failed read keeps the previous plan for the next +# grant, but the outcome is never reported from it. +plan='[]' +attempt=0 +while :; do + read_ok=false + if config=$(echo s3.configure | weed_shell) && fresh=$(printf '%s\n' "$config" | plan_from_config) && [ -n "$fresh" ]; then + read_ok=true + plan=$fresh + if [ "$plan" = '[]' ]; then + echo "every identity carries the Object Lock actions its grants imply" + exit 0 + fi + fi + [ "$attempt" -ge "$ATTEMPTS" ] && break + attempt=$((attempt + 1)) + if [ -n "$plan" ] && dry=$(printf '%s\n' "$plan" | dry_run_commands) && [ -n "$dry" ] && + commands=$(printf '%s\n' "$dry" | weed_shell | safe_commands "$plan") && [ -n "$commands" ]; then + printf '%s\n' "$commands" | sed 's/^s3.configure -user=\([^ ]*\) -actions=\([^ ]*\) -apply$/granting \2 to \1/' + printf '%s\n' "$commands" | weed_shell >/dev/null || true + fi + sleep "$RETRY_DELAY" +done + +if $read_ok; then + pending=$(printf '%s\n' "$plan" | jq -r '[.[].name] | join(" ")') + message="Object Lock actions not granted after $ATTEMPTS attempts; identities still missing them: $pending. See the logs of Job $JOB." +else + message="Object Lock actions unverified after $ATTEMPTS attempts: the IAM configuration could not be read. See the logs of Job $JOB." +fi +echo "$message" >&2 +now=$(date -u +%Y-%m-%dT%H:%M:%SZ) +# kubectl describe matches events by uid as well as name. +uid=$(timeout "$SHELL_TIMEOUT" kubectl get helmrelease "$RELEASE" --namespace "$NAMESPACE" --output jsonpath='{.metadata.uid}') || uid= +# JSON, not YAML: kubectl splits a YAML stream on document markers and its YAML +# reader rejects control characters, either of which an identity name can carry. +jq -n --arg job "$JOB" --arg namespace "$NAMESPACE" --arg release "$RELEASE" \ + --arg uid "$uid" --arg message "$message" --arg now "$now" '{ + apiVersion: "v1", + kind: "Event", + metadata: {generateName: "\($job)-", namespace: $namespace}, + involvedObject: { + apiVersion: "helm.toolkit.fluxcd.io/v2", + kind: "HelmRelease", + name: $release, + namespace: $namespace, + uid: $uid + }, + type: "Warning", + reason: "ObjectLockActionsNotGranted", + message: $message, + source: {component: $job}, + firstTimestamp: $now, + lastTimestamp: $now, + count: 1 +}' | timeout "$SHELL_TIMEOUT" kubectl create --filename - || echo "could not record the Warning Event" >&2 diff --git a/packages/system/seaweedfs/templates/object-lock-actions-hook.yaml b/packages/system/seaweedfs/templates/object-lock-actions-hook.yaml new file mode 100644 index 000000000..a45825609 --- /dev/null +++ b/packages/system/seaweedfs/templates/object-lock-actions-hook.yaml @@ -0,0 +1,165 @@ +{{- /* Install also covers Flux reinstalling a release whose filer kept its identities. */}} +{{- if .Values.seaweedfs.cosi.enabled }} +{{- $name := printf "%s-object-lock-actions" .Release.Name | trunc 63 | trimSuffix "-" }} +{{- $env := .Values.global.seaweedfs.extraEnvironmentVars }} +{{- $security := .Values.global.seaweedfs.enableSecurity }} +{{- $fullname := include "seaweedfs.fullname" .Subcharts.seaweedfs }} +--- +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ $name }} + annotations: + helm.sh/hook: post-install,post-upgrade + helm.sh/hook-weight: "-1" + helm.sh/hook-delete-policy: before-hook-creation,hook-succeeded +data: + object-lock-actions.sh: | + {{- .Files.Get "files/object-lock-actions.sh" | nindent 4 }} +--- +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ $name }} + annotations: + helm.sh/hook: post-install,post-upgrade + helm.sh/hook-weight: "-1" + helm.sh/hook-delete-policy: before-hook-creation,hook-succeeded +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: Role +metadata: + name: {{ $name }} + annotations: + helm.sh/hook: post-install,post-upgrade + helm.sh/hook-weight: "-1" + helm.sh/hook-delete-policy: before-hook-creation,hook-succeeded +rules: +- apiGroups: [""] + resources: ["events"] + verbs: ["create"] +- apiGroups: ["helm.toolkit.fluxcd.io"] + resources: ["helmreleases"] + resourceNames: [{{ .Release.Name | quote }}] + verbs: ["get"] +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: RoleBinding +metadata: + name: {{ $name }} + annotations: + helm.sh/hook: post-install,post-upgrade + helm.sh/hook-weight: "-1" + helm.sh/hook-delete-policy: before-hook-creation,hook-succeeded +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: Role + name: {{ $name }} +subjects: +- kind: ServiceAccount + name: {{ $name }} + namespace: {{ .Release.Namespace | quote }} +--- +apiVersion: batch/v1 +kind: Job +metadata: + name: {{ $name }} + annotations: + helm.sh/hook: post-install,post-upgrade + helm.sh/hook-weight: "0" + helm.sh/hook-delete-policy: before-hook-creation +spec: + backoffLimit: 0 + template: + metadata: + labels: + policy.cozystack.io/allow-to-apiserver: "true" + spec: + serviceAccountName: {{ $name }} + restartPolicy: Never + securityContext: + runAsNonRoot: true + runAsUser: 65534 + seccompProfile: + type: RuntimeDefault + initContainers: + - name: weed + image: {{ include "seaweedfs.s3.image" .Subcharts.seaweedfs }} + command: ["cp", "/usr/bin/weed", "/opt/weed/weed"] + resources: + requests: + cpu: 10m + memory: 64Mi + limits: + memory: 256Mi + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: ["ALL"] + volumeMounts: + - name: weed + mountPath: /opt/weed + containers: + - name: grant + image: {{ .Values.seaweedfs.volume.resizeHook.image }} + command: ["/bin/sh", "/opt/script/object-lock-actions.sh"] + resources: + requests: + cpu: 10m + memory: 64Mi + limits: + memory: 256Mi + env: + - name: PATH + value: /opt/weed:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin + - name: MASTER + value: {{ $env.WEED_CLUSTER_SW_MASTER | quote }} + - name: FILER + value: {{ $env.WEED_CLUSTER_SW_FILER | quote }} + - name: RELEASE + value: {{ .Release.Name | quote }} + - name: JOB + value: {{ $name | quote }} + - name: NAMESPACE + valueFrom: + fieldRef: + fieldPath: metadata.namespace + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: ["ALL"] + volumeMounts: + - name: weed + mountPath: /opt/weed + - name: script + mountPath: /opt/script + {{- if $security }} + - name: security-config + mountPath: /etc/seaweedfs/security.toml + subPath: security.toml + readOnly: true + - name: ca-cert + mountPath: /usr/local/share/ca-certificates/ca/ + readOnly: true + - name: client-cert + mountPath: /usr/local/share/ca-certificates/client/ + readOnly: true + {{- end }} + volumes: + - name: weed + emptyDir: {} + - name: script + configMap: + name: {{ $name }} + {{- if $security }} + - name: security-config + configMap: + name: {{ $fullname }}-security-config + - name: ca-cert + secret: + secretName: {{ $fullname }}-ca-cert + - name: client-cert + secret: + secretName: {{ $fullname }}-client-cert + {{- end }} +{{- end }} diff --git a/packages/system/seaweedfs/tests/object_lock_actions_hook_test.yaml b/packages/system/seaweedfs/tests/object_lock_actions_hook_test.yaml new file mode 100644 index 000000000..b98bb950a --- /dev/null +++ b/packages/system/seaweedfs/tests/object_lock_actions_hook_test.yaml @@ -0,0 +1,167 @@ +suite: seaweedfs Object Lock actions hook + +templates: + - templates/object-lock-actions-hook.yaml + +release: + name: seaweedfs-system + namespace: tenant-foo + +tests: + - it: renders nothing without COSI + set: + seaweedfs.cosi.enabled: false + asserts: + - hasDocuments: + count: 0 + + - it: every object is a hook on both install and upgrade + asserts: + - hasDocuments: + count: 5 + - equal: + path: metadata.annotations["helm.sh/hook"] + value: post-install,post-upgrade + + - it: the Job outlives a run so its logs stay readable + documentSelector: + path: kind + value: Job + asserts: + - equal: + path: metadata.annotations["helm.sh/hook-delete-policy"] + value: before-hook-creation + - equal: + path: metadata.annotations["helm.sh/hook-weight"] + value: "0" + + - it: everything the Job uses is created before it + documentSelector: + path: kind + value: ConfigMap + asserts: + - equal: + path: metadata.annotations["helm.sh/hook-weight"] + value: "-1" + + - it: the weed binary comes from the image the s3 gateway runs + documentSelector: + path: kind + value: Job + set: + seaweedfs.image.repository: registry.example.test/seaweedfs + seaweedfs.image.tag: "9.99" + asserts: + - equal: + path: spec.template.spec.initContainers[0].image + value: registry.example.test/seaweedfs:9.99 + + - it: the grant container runs the package's kubectl image + documentSelector: + path: kind + value: Job + set: + seaweedfs.volume.resizeHook.image: registry.example.test/k8s:9.99 + asserts: + - equal: + path: spec.template.spec.containers[0].image + value: registry.example.test/k8s:9.99 + + - it: ships the script and points it at the cluster + documentSelector: + path: kind + value: Job + asserts: + - equal: + path: spec.template.spec.containers[0].command + value: ["/bin/sh", "/opt/script/object-lock-actions.sh"] + - contains: + path: spec.template.spec.containers[0].env + content: + name: MASTER + value: seaweedfs-master:9333 + - contains: + path: spec.template.spec.containers[0].env + content: + name: FILER + value: seaweedfs-filer-client:8888 + - contains: + path: spec.template.spec.containers[0].env + content: + name: RELEASE + value: seaweedfs-system + + - it: both containers set a memory limit so a namespace default cannot pick one + documentSelector: + path: kind + value: Job + asserts: + - equal: + path: spec.template.spec.initContainers[0].resources.limits.memory + value: 256Mi + - equal: + path: spec.template.spec.containers[0].resources.limits.memory + value: 256Mi + + - it: the script in the ConfigMap is the file under test + documentSelector: + path: kind + value: ConfigMap + asserts: + - matchRegex: + path: data["object-lock-actions.sh"] + pattern: 'safe_commands\(\)' + + - it: mounts the gRPC client credentials when security is on + documentSelector: + path: kind + value: Job + asserts: + - contains: + path: spec.template.spec.volumes + content: + name: client-cert + secret: + secretName: seaweedfs-client-cert + - contains: + path: spec.template.spec.volumes + content: + name: ca-cert + secret: + secretName: seaweedfs-ca-cert + - contains: + path: spec.template.spec.volumes + content: + name: security-config + configMap: + name: seaweedfs-security-config + + - it: mounts no credentials when security is off + documentSelector: + path: kind + value: Job + set: + global.seaweedfs.enableSecurity: false + asserts: + - lengthEqual: + path: spec.template.spec.volumes + count: 2 + - lengthEqual: + path: spec.template.spec.containers[0].volumeMounts + count: 2 + + - it: the Job name fits a label value for the longest release name + release: + name: a-tenant-seaweedfs-instance-with-a-long-name-xx-syste + documentSelector: + path: kind + value: Job + asserts: + - matchRegex: + path: metadata.name + pattern: ^.{1,63}$ + - contains: + path: spec.template.spec.containers[0].env + content: + name: JOB + value: a-tenant-seaweedfs-instance-with-a-long-name-xx-syste-object-lo