Skip to content

Commit 0a13ee9

Browse files
committed
use cluster issuer
1 parent 2010b55 commit 0a13ee9

File tree

4 files changed

+55
-32
lines changed

4 files changed

+55
-32
lines changed
Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,65 @@
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
42

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
68
apiVersion: cert-manager.io/v1
7-
kind: Issuer
9+
kind: ClusterIssuer
810
metadata:
9-
name: ${self_signed_issuer}
10-
namespace: ${MDB_NS}
11+
name: ${MDB_TLS_SELF_SIGNED_ISSUER}
1112
spec:
1213
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
1420
apiVersion: cert-manager.io/v1
1521
kind: Certificate
1622
metadata:
17-
name: ${ca_cert_name}
18-
namespace: ${MDB_NS}
23+
name: ${MDB_TLS_CA_CERT_NAME}
24+
namespace: ${CERT_MANAGER_NAMESPACE}
1925
spec:
2026
isCA: true
27+
commonName: ${MDB_TLS_CA_CERT_NAME}
2128
secretName: ${MDB_TLS_CA_SECRET_NAME}
22-
commonName: ${MDB_RESOURCE_NAME}-svc.${MDB_NS}.svc.cluster.local
2329
privateKey:
24-
algorithm: RSA
25-
size: 2048
30+
algorithm: ECDSA
31+
size: 256
2632
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
3241
apiVersion: cert-manager.io/v1
33-
kind: Issuer
42+
kind: ClusterIssuer
3443
metadata:
35-
name: ${ca_issuer}
36-
namespace: ${MDB_NS}
44+
name: ${MDB_TLS_CA_ISSUER}
3745
spec:
3846
ca:
3947
secretName: ${MDB_TLS_CA_SECRET_NAME}
4048
EOF_MANIFEST
4149

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 -
4564

46-
echo "cert-manager issuer ${ca_issuer} is ready to sign certificates."
65+
echo "Cluster-wide CA issuer ${MDB_TLS_CA_ISSUER} is ready."

docs/search/01-search-community-deploy/code_snippets/01_0308_issue_tls_certificates.sh

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
ca_issuer="${MDB_RESOURCE_NAME}-ca-issuer"
21
server_certificate="${MDB_RESOURCE_NAME}-server-tls"
32
search_certificate="${MDB_RESOURCE_NAME}-search-tls"
43

@@ -32,8 +31,8 @@ metadata:
3231
spec:
3332
secretName: ${MDB_TLS_SERVER_CERT_SECRET_NAME}
3433
issuerRef:
35-
kind: Issuer
36-
name: ${ca_issuer}
34+
name: ${MDB_TLS_CA_ISSUER}
35+
kind: ClusterIssuer
3736
duration: 240h0m0s
3837
renewBefore: 120h0m0s
3938
usages:
@@ -52,8 +51,8 @@ metadata:
5251
spec:
5352
secretName: ${MDB_SEARCH_TLS_SECRET_NAME}
5453
issuerRef:
55-
kind: Issuer
56-
name: ${ca_issuer}
54+
name: ${MDB_TLS_CA_ISSUER}
55+
kind: ClusterIssuer
5756
duration: 240h0m0s
5857
renewBefore: 120h0m0s
5958
usages:

docs/search/01-search-community-deploy/code_snippets/01_0310_create_mongodb_community_resource.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ spec:
1212
enabled: true
1313
certificateKeySecretRef:
1414
name: ${MDB_TLS_SERVER_CERT_SECRET_NAME}
15-
caCertificateSecretRef:
16-
name: ${MDB_TLS_CA_SECRET_NAME}
15+
caConfigMapRef:
16+
name: ${MDB_TLS_CA_CONFIGMAP}
1717
authentication:
1818
ignoreUnknownUsers: true
1919
modes:

docs/search/01-search-community-deploy/env_variables.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ export MDB_TLS_CA_SECRET_NAME="${MDB_RESOURCE_NAME}-ca"
1414
export MDB_TLS_SERVER_CERT_SECRET_NAME="${MDB_RESOURCE_NAME}-tls"
1515
export MDB_SEARCH_TLS_SECRET_NAME="${MDB_RESOURCE_NAME}-search-tls"
1616

17+
export MDB_TLS_CA_CONFIGMAP="${MDB_RESOURCE_NAME}-ca-configmap"
18+
export MDB_TLS_SELF_SIGNED_ISSUER="${MDB_RESOURCE_NAME}-selfsigned-cluster-issuer"
19+
export MDB_TLS_CA_CERT_NAME="${MDB_RESOURCE_NAME}-selfsigned-ca"
20+
export MDB_TLS_CA_ISSUER="${MDB_RESOURCE_NAME}-cluster-issuer"
21+
1722
# minimum required MongoDB version for running MongoDB Search is 8.2.0
1823
export MDB_VERSION="8.2.0"
1924

0 commit comments

Comments
 (0)