|
| 1 | +#!/usr/bin/env bats |
| 2 | +# Unit test: mongodb-rd cozyrds must deliver the TLS trust anchor without |
| 3 | +# private key material. |
| 4 | +# |
| 5 | +# The PSMDB operator creates <release>-ca-cert holding the CA PRIVATE KEY and |
| 6 | +# <release>-ssl / <release>-ssl-internal holding server private keys. The tenant |
| 7 | +# must receive only the key-free <release>.tenant-ca projection, reached by |
| 8 | +# label. These names are the contract between this file and the operator, and |
| 9 | +# nothing else in the repo checks them at this level. |
| 10 | +# |
| 11 | +# The declaration that names the extraction source is a TenantProjection the |
| 12 | +# chart renders, covered by packages/apps/mongodb/tests/tenant_projection_test.yaml. |
| 13 | + |
| 14 | +REPO_ROOT="$(cd "$(dirname "${BATS_TEST_FILENAME:-$0}")/.." && pwd)" |
| 15 | +COZYRDS="$REPO_ROOT/packages/system/mongodb-rd/cozyrds/mongodb.yaml" |
| 16 | +# Bound explicitly rather than read from BATS_TEST_FILENAME inside a test body. |
| 17 | +# These files run under hack/cozytest.sh, not bats: it executes each body with |
| 18 | +# set -u and never exports that variable, so a bare reference aborts the test — |
| 19 | +# and the runner stops at the first failure, taking every later test with it. |
| 20 | +# $0 is not a usable fallback either, since it expands to the runner's own path |
| 21 | +# and would silently scan the wrong file. |
| 22 | +SELF="$REPO_ROOT/hack/check-mongodb-rd-secrets.bats" |
| 23 | + |
| 24 | +@test "mongodb-rd checks use the set -e-safe negation form" { |
| 25 | + # Guards the pattern itself, so the next negated assertion added here cannot |
| 26 | + # be silently dead. `!`-prefixed commands are exempt from set -e. |
| 27 | + # SELF is a hardcoded path and grep exits 2 on a missing file, which an `if` |
| 28 | + # reads as simply false — so renaming this file would turn the guard green |
| 29 | + # while it inspected nothing. Every other check here reads $COZYRDS through |
| 30 | + # `grep -q` or a `$(yq ...)` assignment, both of which abort under set -e; |
| 31 | + # this is the one input that can vanish quietly. |
| 32 | + if [ ! -f "$SELF" ]; then |
| 33 | + echo "SELF does not resolve ($SELF); this guard would be vacuous" >&2 |
| 34 | + exit 1 |
| 35 | + fi |
| 36 | + # [[:space:]] rather than \s: the latter is a GNU/PCRE extension, not POSIX |
| 37 | + # ERE, and degrades to a literal 's' on a BSD grep — which would make this |
| 38 | + # guard vacuous, the one property a vacuity guard must not have. |
| 39 | + if grep -nE '^[[:space:]]*![[:space:]]' "$SELF"; then |
| 40 | + echo "Negated assertion above is exempt from set -e; use if/exit 1 instead" >&2 |
| 41 | + exit 1 |
| 42 | + fi |
| 43 | +} |
| 44 | + |
| 45 | +@test "mongodb-rd cozyrds selects the key-free projection by label" { |
| 46 | + grep -q 'internal.cozystack.io/tenant-ca: "true"' "$COZYRDS" |
| 47 | +} |
| 48 | + |
| 49 | +@test "mongodb-rd cozyrds secrets.include path resolves" { |
| 50 | + # Positive control for the two structural checks below. Both walk |
| 51 | + # .spec.secrets.include, and yq returns 0 for a path that does not exist — so |
| 52 | + # a rename of that field would leave them green while asserting nothing. The |
| 53 | + # grep-based tests would not notice either, since the strings stay in the |
| 54 | + # file. Deleting `include` outright is already caught by the label grep; this |
| 55 | + # covers the rename. |
| 56 | + n="$(yq '.spec.secrets.include // [] | length' "$COZYRDS")" |
| 57 | + if [ "$n" -lt 2 ]; then |
| 58 | + echo "spec.secrets.include has $n entries; the structural checks would be vacuous" >&2 |
| 59 | + exit 1 |
| 60 | + fi |
| 61 | +} |
| 62 | + |
| 63 | +@test "mongodb-rd cozyrds has no catch-all secrets selector" { |
| 64 | + # The mine this guards: an include entry with an empty matchLabels and no |
| 65 | + # resourceNames. LabelSelectorAsSelector({}) resolves to labels.Everything() |
| 66 | + # and a nil resourceNames matches any name, so such an entry promotes EVERY |
| 67 | + # Secret in the namespace — including the key-bearing TLS Secrets — to the |
| 68 | + # tenant. Checked structurally rather than by grep, because the correct |
| 69 | + # selector being present says nothing about a second, wider one alongside it. |
| 70 | + empty="$(yq '[.spec.secrets.include[] |
| 71 | + | select((.matchLabels // {} | length) == 0 and (.resourceNames // [] | length) == 0)] |
| 72 | + | length' "$COZYRDS")" |
| 73 | + if [ "$empty" != "0" ]; then |
| 74 | + echo "Found $empty secrets.include entry/entries matching every Secret in the namespace" >&2 |
| 75 | + exit 1 |
| 76 | + fi |
| 77 | +} |
| 78 | + |
| 79 | +@test "mongodb-rd cozyrds does not hand the tenant any key-bearing secret by name" { |
| 80 | + # resourceNames entries are name grants. The CA and leaf secrets each carry a |
| 81 | + # private key, so neither may appear as one. |
| 82 | + # |
| 83 | + # Matched on the suffix over the parsed resourceNames lists, not on a literal |
| 84 | + # spelling: the same object can be written "mongodb-{{ .name }}-ca-cert" or |
| 85 | + # "{{ .release }}-ca-cert", and a grep pinned to either form misses the other. |
| 86 | + # Walking the yq path also confines the check to grants, so a legitimate |
| 87 | + # mention of one of these names elsewhere in the file cannot trip it. |
| 88 | + leaks="$(yq '[.spec.secrets.include[].resourceNames[]? |
| 89 | + | select(test("-(ca-cert|ssl|ssl-internal|ssl-old|ssl-internal-old)$"))] |
| 90 | + | length' "$COZYRDS")" |
| 91 | + if [ "$leaks" != "0" ]; then |
| 92 | + echo "Found $leaks key-bearing secret(s) granted to the tenant by name" >&2 |
| 93 | + exit 1 |
| 94 | + fi |
| 95 | +} |
| 96 | + |
| 97 | +@test "mongodb-rd cozyrds excludes every key-bearing secret" { |
| 98 | + # The exclude list is the structural backstop behind the label-only include |
| 99 | + # selector, and nothing else pins it: deleting it leaves every other check in |
| 100 | + # this file green. Asserted positively — a count alone would go vacuous on a |
| 101 | + # renamed path, the same way the include checks would without their own |
| 102 | + # positive control. |
| 103 | + for name in ca-cert ssl ssl-internal ssl-old ssl-internal-old \ |
| 104 | + percona-server-mongodb-users mongodb-encryption-key mongodb-keyfile s3-creds; do |
| 105 | + hits="$(yq "[.spec.secrets.exclude[].resourceNames[]? | select(. == \"mongodb-{{ .name }}-$name\")] | length" "$COZYRDS")" |
| 106 | + if [ "$hits" != "1" ]; then |
| 107 | + echo "mongodb-{{ .name }}-$name is not excluded (found $hits entries)" >&2 |
| 108 | + exit 1 |
| 109 | + fi |
| 110 | + done |
| 111 | +} |
| 112 | + |
| 113 | +@test "mongodb-rd cozyrds excludes the operator-internal secrets" { |
| 114 | + # These carry the internal- prefix rather than a release suffix, so they do |
| 115 | + # not fit the loop above. Quoted: the template braces contain spaces, and an |
| 116 | + # unquoted list item would word-split into "internal-mongodb-{{", ".name", |
| 117 | + # "}}" and assert on three names that do not exist. |
| 118 | + for name in "internal-mongodb-{{ .name }}" "internal-mongodb-{{ .name }}-users"; do |
| 119 | + hits="$(yq "[.spec.secrets.exclude[].resourceNames[]? | select(. == \"$name\")] | length" "$COZYRDS")" |
| 120 | + if [ "$hits" != "1" ]; then |
| 121 | + echo "$name is not excluded (found $hits entries)" >&2 |
| 122 | + exit 1 |
| 123 | + fi |
| 124 | + done |
| 125 | +} |
| 126 | + |
| 127 | +@test "mongodb-rd cozyrds still delivers the credentials secret" { |
| 128 | + # The exclude list added alongside the trust anchor is evaluated BEFORE |
| 129 | + # include and wins outright, so it now sits upstream of this pre-existing |
| 130 | + # grant. Every other line in that block is about withholding a Secret, which |
| 131 | + # makes adding this one to it a plausible edit — and one that would silently |
| 132 | + # revoke the tenant's access to their own connection string. Assert both |
| 133 | + # directions. |
| 134 | + inc="$(yq '[.spec.secrets.include[].resourceNames[]? | select(. == "mongodb-{{ .name }}-credentials")] | length' "$COZYRDS")" |
| 135 | + if [ "$inc" != "1" ]; then |
| 136 | + echo "credentials is not granted via include (found $inc entries)" >&2 |
| 137 | + exit 1 |
| 138 | + fi |
| 139 | + exc="$(yq '[.spec.secrets.exclude[].resourceNames[]? | select(. == "mongodb-{{ .name }}-credentials")] | length' "$COZYRDS")" |
| 140 | + if [ "$exc" != "0" ]; then |
| 141 | + echo "credentials appears in exclude, which wins over include" >&2 |
| 142 | + exit 1 |
| 143 | + fi |
| 144 | +} |
0 commit comments