-
Notifications
You must be signed in to change notification settings - Fork 23
[draft] Community TLS and cert manager #564
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3ec055e
6593b18
b791c36
53e9334
cdc55d4
83e9b33
14bedcf
8dc3d81
f4a1a34
75c83de
83d91a0
0c6fd48
63813e3
843fd3b
ab1718f
cb83dbc
326ad85
aa0dc82
0b8bff0
fcb92ca
dae55bd
fda22ee
7ef1ae2
4c38389
060b7be
5231723
714b2cc
8d95b3f
1b80dd1
86d13a7
2010b55
0a13ee9
a5fc3fc
648facd
548fc39
bdf65ef
286e079
a2906ab
2a09806
f3f2c22
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| required=( | ||
| K8S_CTX | ||
| MDB_NS | ||
| MDB_RESOURCE_NAME | ||
| MDB_VERSION | ||
| MDB_MEMBERS | ||
| CERT_MANAGER_NAMESPACE | ||
| MDB_TLS_CA_SECRET_NAME | ||
| MDB_TLS_SERVER_CERT_SECRET_NAME | ||
| MDB_SEARCH_TLS_SECRET_NAME | ||
| MDB_ADMIN_USER_PASSWORD | ||
| MDB_SEARCH_SYNC_USER_PASSWORD | ||
| MDB_USER_PASSWORD | ||
| OPERATOR_HELM_CHART | ||
| ) | ||
|
|
||
| missing_req=() | ||
| for v in "${required[@]}"; do [[ -n "${!v:-}" ]] || missing_req+=("$v"); done | ||
| if (( ${#missing_req[@]} )); then | ||
| echo "ERROR: Missing required environment variables:" >&2 | ||
| for m in "${missing_req[@]}"; do echo " - $m" >&2; done | ||
| fi | ||
|
|
||
| echo "All required environment variables present." |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,16 @@ | ||
| kubectl --context "${K8S_CTX}" --namespace "${MDB_NS}" \ | ||
| create secret generic mdb-admin-user-password \ | ||
| --from-literal=password="${MDB_ADMIN_USER_PASSWORD}" | ||
| # Create admin user secret | ||
| kubectl create secret generic mdb-admin-user-password \ | ||
| --from-literal=password="${MDB_ADMIN_USER_PASSWORD}" \ | ||
| --dry-run=client -o yaml | kubectl apply --context "${K8S_CTX}" --namespace "${MDB_NS}" -f - | ||
|
|
||
| kubectl --context "${K8S_CTX}" --namespace "${MDB_NS}" \ | ||
| create secret generic mdbc-rs-search-sync-source-password \ | ||
| --from-literal=password="${MDB_SEARCH_SYNC_USER_PASSWORD}" | ||
| # Create search sync source user secret | ||
| kubectl create secret generic "${MDB_RESOURCE_NAME}-search-sync-source-password" \ | ||
| --from-literal=password="${MDB_SEARCH_SYNC_USER_PASSWORD}" \ | ||
| --dry-run=client -o yaml | kubectl apply --context "${K8S_CTX}" --namespace "${MDB_NS}" -f - | ||
|
|
||
| kubectl --context "${K8S_CTX}" --namespace "${MDB_NS}" \ | ||
| create secret generic mdb-user-password \ | ||
| --from-literal=password="${MDB_USER_PASSWORD}" | ||
| # Create regular user secret | ||
| kubectl create secret generic mdb-user-password \ | ||
| --from-literal=password="${MDB_USER_PASSWORD}" \ | ||
| --dry-run=client -o yaml | kubectl apply --context "${K8S_CTX}" --namespace "${MDB_NS}" -f - | ||
|
|
||
| echo "User secrets created." | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| helm upgrade --install \ | ||
| cert-manager \ | ||
| oci://quay.io/jetstack/charts/cert-manager \ | ||
| --kube-context "${K8S_CTX}" \ | ||
| --namespace "${CERT_MANAGER_NAMESPACE}" \ | ||
| --create-namespace \ | ||
| --set crds.enabled=true | ||
|
|
||
| for deployment in cert-manager cert-manager-cainjector cert-manager-webhook; do | ||
| kubectl --context "${K8S_CTX}" \ | ||
| -n "${CERT_MANAGER_NAMESPACE}" \ | ||
| wait --for=condition=Available "deployment/${deployment}" --timeout=300s | ||
| done | ||
|
|
||
| echo "cert-manager is ready in namespace ${CERT_MANAGER_NAMESPACE}." |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| # Bootstrap a self-signed ClusterIssuer that will mint the CA material consumed by | ||
| # the MongoDBCommunity deployment. | ||
| kubectl apply --context "${K8S_CTX}" -f - <<EOF_MANIFEST | ||
| apiVersion: cert-manager.io/v1 | ||
| kind: ClusterIssuer | ||
| metadata: | ||
| name: ${MDB_TLS_SELF_SIGNED_ISSUER} | ||
| spec: | ||
| selfSigned: {} | ||
| EOF_MANIFEST | ||
|
|
||
| kubectl --context "${K8S_CTX}" wait --for=condition=Ready clusterissuer "${MDB_TLS_SELF_SIGNED_ISSUER}" | ||
|
|
||
| # Create the CA certificate and secret in the cert-manager namespace. | ||
| kubectl apply --context "${K8S_CTX}" -f - <<EOF_MANIFEST | ||
| apiVersion: cert-manager.io/v1 | ||
| kind: Certificate | ||
| metadata: | ||
| name: ${MDB_TLS_CA_CERT_NAME} | ||
| namespace: ${CERT_MANAGER_NAMESPACE} | ||
| spec: | ||
| isCA: true | ||
| commonName: ${MDB_TLS_CA_CERT_NAME} | ||
| secretName: ${MDB_TLS_CA_SECRET_NAME} | ||
| privateKey: | ||
| algorithm: ECDSA | ||
| size: 256 | ||
| issuerRef: | ||
| name: ${MDB_TLS_SELF_SIGNED_ISSUER} | ||
| kind: ClusterIssuer | ||
| EOF_MANIFEST | ||
|
|
||
| kubectl --context "${K8S_CTX}" wait --for=condition=Ready -n "${CERT_MANAGER_NAMESPACE}" certificate "${MDB_TLS_CA_CERT_NAME}" | ||
|
|
||
| # Publish a cluster-scoped issuer that fronts the generated CA secret so all namespaces can reuse it. | ||
| kubectl apply --context "${K8S_CTX}" -f - <<EOF_MANIFEST | ||
| apiVersion: cert-manager.io/v1 | ||
| kind: ClusterIssuer | ||
| metadata: | ||
| name: ${MDB_TLS_CA_ISSUER} | ||
| spec: | ||
| ca: | ||
| secretName: ${MDB_TLS_CA_SECRET_NAME} | ||
| EOF_MANIFEST | ||
|
|
||
| kubectl --context "${K8S_CTX}" wait --for=condition=Ready clusterissuer "${MDB_TLS_CA_ISSUER}" | ||
|
|
||
| TMP_CA_CERT="$(mktemp)" | ||
|
|
||
| kubectl --context "${K8S_CTX}" \ | ||
| get secret "${MDB_TLS_CA_SECRET_NAME}" -n "${CERT_MANAGER_NAMESPACE}" \ | ||
| -o jsonpath="{.data['ca\\.crt']}" | base64 --decode > "${TMP_CA_CERT}" | ||
|
|
||
| # Expose the CA bundle through a ConfigMap for workloads and the MongoDBCommunity resource. | ||
| kubectl --context "${K8S_CTX}" create configmap "${MDB_TLS_CA_CONFIGMAP}" -n "${MDB_NS}" \ | ||
| --from-file=ca-pem="${TMP_CA_CERT}" --from-file=mms-ca.crt="${TMP_CA_CERT}" \ | ||
| --from-file=ca.crt="${TMP_CA_CERT}" \ | ||
| --dry-run=client -o yaml | kubectl --context "${K8S_CTX}" apply -f - | ||
|
|
||
| echo "Cluster-wide CA issuer ${MDB_TLS_CA_ISSUER} is ready." |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| server_certificate="${MDB_RESOURCE_NAME}-server-tls" | ||
| search_certificate="${MDB_RESOURCE_NAME}-search-tls" | ||
|
|
||
| mongo_dns_names=() | ||
| for ((member = 0; member < ${MDB_MEMBERS}; member++)); do | ||
| mongo_dns_names+=("${MDB_RESOURCE_NAME}-${member}") | ||
| mongo_dns_names+=("${MDB_RESOURCE_NAME}-${member}.${MDB_RESOURCE_NAME}-svc.${MDB_NS}.svc.cluster.local") | ||
| done | ||
| mongo_dns_names+=( | ||
| "${MDB_RESOURCE_NAME}-svc.${MDB_NS}.svc.cluster.local" | ||
| "*.${MDB_RESOURCE_NAME}-svc.${MDB_NS}.svc.cluster.local" | ||
| ) | ||
|
|
||
| search_dns_names=( | ||
| "${MDB_RESOURCE_NAME}-search-svc.${MDB_NS}.svc.cluster.local" | ||
| ) | ||
|
|
||
| render_dns_list() { | ||
| local dns_list=("$@") | ||
| for dns in "${dns_list[@]}"; do | ||
| printf " - \"%s\"\n" "${dns}" | ||
| done | ||
| } | ||
|
|
||
| kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<EOF_MANIFEST | ||
| apiVersion: cert-manager.io/v1 | ||
| kind: Certificate | ||
| metadata: | ||
| name: ${server_certificate} | ||
| namespace: ${MDB_NS} | ||
| spec: | ||
| secretName: ${MDB_TLS_SERVER_CERT_SECRET_NAME} | ||
| issuerRef: | ||
| name: ${MDB_TLS_CA_ISSUER} | ||
| kind: ClusterIssuer | ||
| duration: 240h0m0s | ||
| renewBefore: 120h0m0s | ||
| usages: | ||
| - digital signature | ||
| - key encipherment | ||
| - server auth | ||
| - client auth | ||
| dnsNames: | ||
| $(render_dns_list "${mongo_dns_names[@]}") | ||
| --- | ||
| apiVersion: cert-manager.io/v1 | ||
| kind: Certificate | ||
| metadata: | ||
| name: ${search_certificate} | ||
| namespace: ${MDB_NS} | ||
| spec: | ||
| secretName: ${MDB_SEARCH_TLS_SECRET_NAME} | ||
| issuerRef: | ||
| name: ${MDB_TLS_CA_ISSUER} | ||
| kind: ClusterIssuer | ||
| duration: 240h0m0s | ||
| renewBefore: 120h0m0s | ||
| usages: | ||
| - digital signature | ||
| - key encipherment | ||
| - server auth | ||
| - client auth | ||
| dnsNames: | ||
| $(render_dns_list "${search_dns_names[@]}") | ||
| EOF_MANIFEST | ||
|
|
||
| kubectl --context "${K8S_CTX}" -n "${MDB_NS}" wait --for=condition=Ready certificate "${server_certificate}" --timeout=300s | ||
| kubectl --context "${K8S_CTX}" -n "${MDB_NS}" wait --for=condition=Ready certificate "${search_certificate}" --timeout=300s | ||
|
|
||
| echo "MongoDB TLS certificates have been issued." |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| echo "Waiting for MongoDBSearch resource to reach Running phase..." | ||
| kubectl --context "${K8S_CTX}" -n "${MDB_NS}" wait \ | ||
| --for=jsonpath='{.status.phase}'=Running mdbs/mdbc-rs --timeout=300s | ||
| --for=jsonpath='{.status.phase}'=Running mdbs/"${MDB_RESOURCE_NAME}" --timeout=300s |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,7 @@ | ||
| echo "Waiting for MongoDBCommunity resource to reach Running phase..." | ||
| kubectl --context "${K8S_CTX}" -n "${MDB_NS}" wait \ | ||
| --for=jsonpath='{.status.phase}'=Running mdbc/mdbc-rs --timeout=400s | ||
| echo; echo "MongoDBCommunity resource" | ||
| kubectl --context "${K8S_CTX}" -n "${MDB_NS}" get mdbc/mdbc-rs | ||
| echo; echo "Pods running in cluster ${K8S_CTX}" | ||
| kubectl --context "${K8S_CTX}" -n "${MDB_NS}" get pods |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| kubectl exec --context "${K8S_CTX}" -n "${MDB_NS}" mongodb-tools-pod -- \ | ||
| mongosh --quiet \ | ||
| "${MDB_CONNECTION_STRING}" \ | ||
| mongosh --quiet "${MDB_CONNECTION_STRING}" \ | ||
| --eval "use sample_mflix" \ | ||
| --eval 'db.movies.createSearchIndex("default", { mappings: { dynamic: true } });' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,14 @@ | |
| # user only for the connection string in MDB_CONNECTION_STRING env var below | ||
| #export MDB_RESOURCE_NAME="mdbc-rs" | ||
|
|
||
| # TLS-related resources used by the snippets in this module | ||
| #export MDB_TLS_CA_SECRET_NAME="${MDB_RESOURCE_NAME}-ca" | ||
| #export MDB_TLS_CA_CONFIGMAP="${MDB_RESOURCE_NAME}-ca-configmap" | ||
| #export MDB_TLS_SERVER_CERT_SECRET_NAME="${MDB_RESOURCE_NAME}-tls" | ||
| #export MDB_SEARCH_TLS_SECRET_NAME="${MDB_RESOURCE_NAME}-search-tls" | ||
|
|
||
| # default connection string if MongoDB database is deployed using the operator | ||
| #export MDB_CONNECTION_STRING="mongodb://mdb-user:${MDB_USER_PASSWORD}@${MDB_RESOURCE_NAME}-0.${MDB_RESOURCE_NAME}-svc.${MDB_NS}.svc.cluster.local:27017/?replicaSet=${MDB_RESOURCE_NAME}" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if we're going with tls params in connection string (I'd recommend that), then let's put an example here for tls as well The assumption is that whatever connection string is defined in previous snippet modules (community, enterprise, external) should work in this module keeping it generic |
||
| # To enable TLS for the shared snippets, append driver options directly to the connection string, for example: | ||
| #export MDB_CONNECTION_STRING="mongodb://mdb-user:${MDB_USER_PASSWORD}@${MDB_RESOURCE_NAME}-0.${MDB_RESOURCE_NAME}-svc.${MDB_NS}.svc.cluster.local:27017/?replicaSet=${MDB_RESOURCE_NAME}&tls=true&tlsCAFile=%2Ftls%2Fca.crt" | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added these logs so that it is easier for LLMs to debug issues