|
| 1 | +# ./tests/tls_cacert_test.yaml |
| 2 | +suite: tls caCertSecret helper |
| 3 | + |
| 4 | +templates: |
| 5 | + - templates/tests/tls-cacert.yaml |
| 6 | + - templates/tests/tls-cacert-params.yaml |
| 7 | + - templates/tests/tls-cacert-nondict.yaml |
| 8 | + |
| 9 | +release: |
| 10 | + name: myrelease |
| 11 | + namespace: tenant-acme |
| 12 | + revision: 1 |
| 13 | + isUpgrade: false |
| 14 | + |
| 15 | +tests: |
| 16 | + - it: renders an Opaque Secret carrying only ca.crt |
| 17 | + templates: |
| 18 | + - templates/tests/tls-cacert.yaml |
| 19 | + values: |
| 20 | + - tls_cacert_values.yaml |
| 21 | + asserts: |
| 22 | + - hasDocuments: |
| 23 | + count: 1 |
| 24 | + - isKind: |
| 25 | + of: Secret |
| 26 | + - equal: |
| 27 | + path: apiVersion |
| 28 | + value: v1 |
| 29 | + - equal: |
| 30 | + path: metadata.name |
| 31 | + value: myrelease-ca-cert |
| 32 | + - equal: |
| 33 | + path: metadata.namespace |
| 34 | + value: tenant-acme |
| 35 | + - equal: |
| 36 | + path: type |
| 37 | + value: Opaque |
| 38 | + - isNotNullOrEmpty: |
| 39 | + path: stringData["ca.crt"] |
| 40 | + # The trust anchor must never carry private key material. The helper |
| 41 | + # only ever emits stringData, so these are the meaningful assertions. |
| 42 | + - notExists: |
| 43 | + path: stringData["tls.key"] |
| 44 | + - notExists: |
| 45 | + path: stringData["tls.crt"] |
| 46 | + |
| 47 | + - it: carries the tenantresource label so it is delivered via core.cozystack.io/tenantsecrets |
| 48 | + templates: |
| 49 | + - templates/tests/tls-cacert.yaml |
| 50 | + values: |
| 51 | + - tls_cacert_values.yaml |
| 52 | + asserts: |
| 53 | + # This label is the entire RBAC delivery mechanism: the tenantsecret |
| 54 | + # registry surfaces only Secrets bearing it, and the base tenant roles |
| 55 | + # grant read on core.cozystack.io/tenantsecrets (not raw core secrets). |
| 56 | + - equal: |
| 57 | + path: metadata.labels["internal.cozystack.io/tenantresource"] |
| 58 | + value: "true" |
| 59 | + |
| 60 | + - it: merges caller-supplied labels and annotations |
| 61 | + templates: |
| 62 | + - templates/tests/tls-cacert.yaml |
| 63 | + values: |
| 64 | + - tls_cacert_values.yaml |
| 65 | + asserts: |
| 66 | + - equal: |
| 67 | + path: metadata.labels["app.kubernetes.io/instance"] |
| 68 | + value: myrelease |
| 69 | + - equal: |
| 70 | + path: metadata.annotations["cozystack.io/example"] |
| 71 | + value: "yes" |
| 72 | + |
| 73 | + - it: keeps the mandatory tenantresource label even when a caller tries to override it |
| 74 | + templates: |
| 75 | + - templates/tests/tls-cacert.yaml |
| 76 | + values: |
| 77 | + - tls_cacert_values.yaml |
| 78 | + set: |
| 79 | + # A caller (or a compromised values override) attempting to opt the object |
| 80 | + # out of tenant delivery — or worse, flip the label onto a key-bearing |
| 81 | + # Secret — must not win. The helper's mandatory label always overrides. |
| 82 | + callerLabels: |
| 83 | + internal.cozystack.io/tenantresource: "false" |
| 84 | + asserts: |
| 85 | + - equal: |
| 86 | + path: metadata.labels["internal.cozystack.io/tenantresource"] |
| 87 | + value: "true" |
| 88 | + |
| 89 | + - it: fails closed when caCert is empty |
| 90 | + templates: |
| 91 | + - templates/tests/tls-cacert.yaml |
| 92 | + set: |
| 93 | + caCert: "" |
| 94 | + asserts: |
| 95 | + - failedTemplate: |
| 96 | + errorMessage: "cozy-lib.tls.caCertSecret: caCert is required and must be a non-empty PEM" |
| 97 | + |
| 98 | + - it: refuses to publish private key material to tenants |
| 99 | + templates: |
| 100 | + - templates/tests/tls-cacert.yaml |
| 101 | + set: |
| 102 | + caCert: | |
| 103 | + -----BEGIN EC PRIVATE KEY----- |
| 104 | + MHcCAQEEexampleprivatekeymaterialthatmustneverreachtenants00000000 |
| 105 | + -----END EC PRIVATE KEY----- |
| 106 | + asserts: |
| 107 | + - failedTemplate: |
| 108 | + errorMessage: "cozy-lib.tls.caCertSecret: caCert must not contain private key material" |
| 109 | + |
| 110 | + - it: refuses private key material regardless of PEM header casing |
| 111 | + templates: |
| 112 | + - templates/tests/tls-cacert.yaml |
| 113 | + set: |
| 114 | + # RFC 7468 mandates uppercase headers, but the guard must also catch |
| 115 | + # malformed / hand-pasted input where the casing differs. |
| 116 | + caCert: | |
| 117 | + -----begin private key----- |
| 118 | + MIIExampleLowercaseHeaderPrivateKeyMustStillBeRejected0000000000 |
| 119 | + -----end private key----- |
| 120 | + asserts: |
| 121 | + - failedTemplate: |
| 122 | + errorMessage: "cozy-lib.tls.caCertSecret: caCert must not contain private key material" |
| 123 | + |
| 124 | + - it: refuses a certificate bundle that also carries a private key block |
| 125 | + templates: |
| 126 | + - templates/tests/tls-cacert.yaml |
| 127 | + set: |
| 128 | + # The realistic accident: a full keypair pasted into the CA field. The |
| 129 | + # guard must scan the entire blob, not merely reject key-only input — a |
| 130 | + # valid leading certificate block must not let a trailing private key |
| 131 | + # slip through to a tenant-readable trust anchor. |
| 132 | + caCert: | |
| 133 | + -----BEGIN CERTIFICATE----- |
| 134 | + MIIBExampleCertificateBodyOnlyNoPrivateKeyHeaderPresent0000000000 |
| 135 | + -----END CERTIFICATE----- |
| 136 | + -----BEGIN PRIVATE KEY----- |
| 137 | + MIIExamplePrivateKeyThatMustNeverReachTenantsViaCABundle00000000 |
| 138 | + -----END PRIVATE KEY----- |
| 139 | + asserts: |
| 140 | + - failedTemplate: |
| 141 | + errorMessage: "cozy-lib.tls.caCertSecret: caCert must not contain private key material" |
| 142 | + |
| 143 | + - it: rejects input that is not a PEM certificate |
| 144 | + templates: |
| 145 | + - templates/tests/tls-cacert.yaml |
| 146 | + set: |
| 147 | + caCert: | |
| 148 | + this is not a certificate at all |
| 149 | + asserts: |
| 150 | + - failedTemplate: |
| 151 | + errorMessage: "cozy-lib.tls.caCertSecret: caCert must contain a PEM certificate (BEGIN CERTIFICATE)" |
| 152 | + |
| 153 | + - it: accepts a certificate even when the literal text PRIVATE KEY appears outside a PEM header |
| 154 | + templates: |
| 155 | + - templates/tests/tls-cacert-params.yaml |
| 156 | + set: |
| 157 | + useParams: true |
| 158 | + name: my-ca-cert |
| 159 | + namespace: tenant-acme |
| 160 | + # "PRIVATE KEY" here is in a friendlyName line, not a PEM header — a free |
| 161 | + # substring guard would wrongly reject this valid, key-free certificate. |
| 162 | + caCert: | |
| 163 | + Bag Attributes: friendlyName=acme PRIVATE KEY authority |
| 164 | + -----BEGIN CERTIFICATE----- |
| 165 | + MIIBExampleCertificateBodyOnlyNoPrivateKeyHeaderPresent0000000000 |
| 166 | + -----END CERTIFICATE----- |
| 167 | + asserts: |
| 168 | + - isKind: |
| 169 | + of: Secret |
| 170 | + - isNotNullOrEmpty: |
| 171 | + path: stringData["ca.crt"] |
| 172 | + |
| 173 | + - it: fails closed when name is empty |
| 174 | + templates: |
| 175 | + - templates/tests/tls-cacert-params.yaml |
| 176 | + set: |
| 177 | + useParams: true |
| 178 | + name: "" |
| 179 | + namespace: tenant-acme |
| 180 | + caCert: | |
| 181 | + -----BEGIN CERTIFICATE----- |
| 182 | + MIIBExampleCertificateBodyOnlyNoPrivateKeyHeaderPresent0000000000 |
| 183 | + -----END CERTIFICATE----- |
| 184 | + asserts: |
| 185 | + - failedTemplate: |
| 186 | + errorMessage: "cozy-lib.tls.caCertSecret: name is required" |
| 187 | + |
| 188 | + - it: omits metadata.namespace when namespace is not supplied |
| 189 | + templates: |
| 190 | + - templates/tests/tls-cacert-params.yaml |
| 191 | + set: |
| 192 | + useParams: true |
| 193 | + name: my-ca-cert |
| 194 | + caCert: | |
| 195 | + -----BEGIN CERTIFICATE----- |
| 196 | + MIIBExampleCertificateBodyOnlyNoPrivateKeyHeaderPresent0000000000 |
| 197 | + -----END CERTIFICATE----- |
| 198 | + asserts: |
| 199 | + - equal: |
| 200 | + path: metadata.name |
| 201 | + value: my-ca-cert |
| 202 | + - notExists: |
| 203 | + path: metadata.namespace |
| 204 | + |
| 205 | + - it: fails closed when invoked with a non-dict argument |
| 206 | + templates: |
| 207 | + - templates/tests/tls-cacert-nondict.yaml |
| 208 | + # A valid caCert keeps the other fixtures rendering cleanly so the only |
| 209 | + # failure is the non-dict guard on this fixture. |
| 210 | + values: |
| 211 | + - tls_cacert_values.yaml |
| 212 | + set: |
| 213 | + nonDict: true |
| 214 | + asserts: |
| 215 | + - failedTemplate: |
| 216 | + errorMessage: "cozy-lib.tls.caCertSecret: expected a single dict argument" |
0 commit comments