|
1 | | -self_signed_issuer="${MDB_RESOURCE_NAME}-selfsigned-issuer" |
2 | | -ca_cert_name="${MDB_RESOURCE_NAME}-ca" |
3 | | -ca_issuer="${MDB_RESOURCE_NAME}-ca-issuer" |
| 1 | +#!/usr/bin/env bash |
4 | 2 |
|
5 | | -kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<EOF_MANIFEST |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +# Bootstrap a self-signed ClusterIssuer that will mint the CA material consumed by |
| 6 | +# the MongoDBCommunity deployment. |
| 7 | +kubectl apply --context "${K8S_CTX}" -f - <<EOF_MANIFEST |
6 | 8 | apiVersion: cert-manager.io/v1 |
7 | | -kind: Issuer |
| 9 | +kind: ClusterIssuer |
8 | 10 | metadata: |
9 | | - name: ${self_signed_issuer} |
10 | | - namespace: ${MDB_NS} |
| 11 | + name: ${MDB_TLS_SELF_SIGNED_ISSUER} |
11 | 12 | spec: |
12 | 13 | selfSigned: {} |
13 | | ---- |
| 14 | +EOF_MANIFEST |
| 15 | + |
| 16 | +kubectl --context "${K8S_CTX}" wait --for=condition=Ready clusterissuer "${MDB_TLS_SELF_SIGNED_ISSUER}" |
| 17 | + |
| 18 | +# Create the CA certificate and secret in the cert-manager namespace. |
| 19 | +kubectl apply --context "${K8S_CTX}" -f - <<EOF_MANIFEST |
14 | 20 | apiVersion: cert-manager.io/v1 |
15 | 21 | kind: Certificate |
16 | 22 | metadata: |
17 | | - name: ${ca_cert_name} |
18 | | - namespace: ${MDB_NS} |
| 23 | + name: ${MDB_TLS_CA_CERT_NAME} |
| 24 | + namespace: ${CERT_MANAGER_NAMESPACE} |
19 | 25 | spec: |
20 | 26 | isCA: true |
| 27 | + commonName: ${MDB_TLS_CA_CERT_NAME} |
21 | 28 | secretName: ${MDB_TLS_CA_SECRET_NAME} |
22 | | - commonName: ${MDB_RESOURCE_NAME}-svc.${MDB_NS}.svc.cluster.local |
23 | 29 | privateKey: |
24 | | - algorithm: RSA |
25 | | - size: 2048 |
| 30 | + algorithm: ECDSA |
| 31 | + size: 256 |
26 | 32 | issuerRef: |
27 | | - kind: Issuer |
28 | | - name: ${self_signed_issuer} |
29 | | - duration: 240h0m0s |
30 | | - renewBefore: 120h0m0s |
31 | | ---- |
| 33 | + name: ${MDB_TLS_SELF_SIGNED_ISSUER} |
| 34 | + kind: ClusterIssuer |
| 35 | +EOF_MANIFEST |
| 36 | + |
| 37 | +kubectl --context "${K8S_CTX}" wait --for=condition=Ready -n "${CERT_MANAGER_NAMESPACE}" certificate "${MDB_TLS_CA_CERT_NAME}" |
| 38 | + |
| 39 | +# Publish a cluster-scoped issuer that fronts the generated CA secret so all namespaces can reuse it. |
| 40 | +kubectl apply --context "${K8S_CTX}" -f - <<EOF_MANIFEST |
32 | 41 | apiVersion: cert-manager.io/v1 |
33 | | -kind: Issuer |
| 42 | +kind: ClusterIssuer |
34 | 43 | metadata: |
35 | | - name: ${ca_issuer} |
36 | | - namespace: ${MDB_NS} |
| 44 | + name: ${MDB_TLS_CA_ISSUER} |
37 | 45 | spec: |
38 | 46 | ca: |
39 | 47 | secretName: ${MDB_TLS_CA_SECRET_NAME} |
40 | 48 | EOF_MANIFEST |
41 | 49 |
|
42 | | -kubectl --context "${K8S_CTX}" -n "${MDB_NS}" wait --for=condition=Ready issuer "${self_signed_issuer}" --timeout=120s |
43 | | -kubectl --context "${K8S_CTX}" -n "${MDB_NS}" wait --for=condition=Ready certificate "${ca_cert_name}" --timeout=300s |
44 | | -kubectl --context "${K8S_CTX}" -n "${MDB_NS}" wait --for=condition=Ready issuer "${ca_issuer}" --timeout=120s |
| 50 | +kubectl --context "${K8S_CTX}" wait --for=condition=Ready clusterissuer "${MDB_TLS_CA_ISSUER}" |
| 51 | + |
| 52 | +TMP_CA_CERT="$(mktemp)" |
| 53 | +trap 'rm -f "${TMP_CA_CERT}"' EXIT |
| 54 | + |
| 55 | +kubectl --context "${K8S_CTX}" \ |
| 56 | + get secret "${MDB_TLS_CA_SECRET_NAME}" -n "${CERT_MANAGER_NAMESPACE}" \ |
| 57 | + -o jsonpath="{.data['ca\\.crt']}" | base64 --decode > "${TMP_CA_CERT}" |
| 58 | + |
| 59 | +# Expose the CA bundle through a ConfigMap for workloads and the MongoDBCommunity resource. |
| 60 | +kubectl --context "${K8S_CTX}" create configmap "${MDB_TLS_CA_CONFIGMAP}" -n "${MDB_NS}" \ |
| 61 | + --from-file=ca-pem="${TMP_CA_CERT}" --from-file=mms-ca.crt="${TMP_CA_CERT}" \ |
| 62 | + --from-file=ca.crt="${TMP_CA_CERT}" \ |
| 63 | + --dry-run=client -o yaml | kubectl --context "${K8S_CTX}" apply -f - |
45 | 64 |
|
46 | | -echo "cert-manager issuer ${ca_issuer} is ready to sign certificates." |
| 65 | +echo "Cluster-wide CA issuer ${MDB_TLS_CA_ISSUER} is ready." |
0 commit comments