|
| 1 | +""" |
| 2 | +CLOUDP-351614: Test that TLS can be disabled on AppDB without breaking monitoring. |
| 3 | +
|
| 4 | +This is a dedicated test file to verify that when TLS is disabled on AppDB, |
| 5 | +the operator correctly clears stale TLS params from the monitoring configuration. |
| 6 | +""" |
| 7 | +from typing import Optional |
| 8 | + |
| 9 | +from kubetester import create_or_update_secret, try_load |
| 10 | +from kubetester.certs import create_ops_manager_tls_certs |
| 11 | +from kubetester.kubetester import fixture as yaml_fixture |
| 12 | +from kubetester.opsmanager import MongoDBOpsManager |
| 13 | +from kubetester.phase import Phase |
| 14 | +from pytest import fixture, mark |
| 15 | +from tests.common.cert.cert_issuer import create_appdb_certs |
| 16 | +from tests.conftest import is_multi_cluster |
| 17 | +from tests.opsmanager.withMonitoredAppDB.conftest import enable_multi_cluster_deployment |
| 18 | + |
| 19 | + |
| 20 | +def get_monitoring_tls_params(ops_manager: MongoDBOpsManager) -> dict: |
| 21 | + """ |
| 22 | + Extract TLS-related additionalParams from monitoring config via OM API. |
| 23 | +
|
| 24 | + Returns a dict with the additionalParams for all monitoring agents, |
| 25 | + keyed by hostname. An empty dict means no TLS params are present. |
| 26 | +
|
| 27 | + Note: We use get_om_tester() to access the full automation config from |
| 28 | + Ops Manager API, which includes monitoringVersions. The K8s secret-based |
| 29 | + get_automation_config_tester() only has the cluster config for agents. |
| 30 | + """ |
| 31 | + om_tester = ops_manager.get_om_tester(ops_manager.app_db_name()) |
| 32 | + ac = om_tester.get_automation_config_tester() |
| 33 | + monitoring_versions = ac.automation_config.get("monitoringVersions", []) |
| 34 | + |
| 35 | + tls_params_by_host = {} |
| 36 | + for mv in monitoring_versions: |
| 37 | + hostname = mv.get("hostname", "unknown") |
| 38 | + additional_params = mv.get("additionalParams", {}) |
| 39 | + if additional_params: |
| 40 | + tls_params_by_host[hostname] = additional_params |
| 41 | + |
| 42 | + return tls_params_by_host |
| 43 | + |
| 44 | +OM_NAME = "om-tls-disable-test" |
| 45 | +APPDB_NAME = f"{OM_NAME}-db" |
| 46 | + |
| 47 | + |
| 48 | +@fixture(scope="module") |
| 49 | +def ops_manager_certs(namespace: str, issuer: str): |
| 50 | + return create_ops_manager_tls_certs(issuer, namespace, OM_NAME) |
| 51 | + |
| 52 | + |
| 53 | +@fixture(scope="module") |
| 54 | +def appdb_certs(namespace: str, issuer: str): |
| 55 | + return create_appdb_certs(namespace, issuer, APPDB_NAME) |
| 56 | + |
| 57 | + |
| 58 | +@fixture(scope="module") |
| 59 | +@mark.usefixtures("appdb_certs", "ops_manager_certs", "issuer_ca_configmap") |
| 60 | +def ops_manager( |
| 61 | + namespace: str, |
| 62 | + issuer_ca_configmap: str, |
| 63 | + appdb_certs: str, |
| 64 | + ops_manager_certs: str, |
| 65 | + custom_version: Optional[str], |
| 66 | + custom_appdb_version: str, |
| 67 | +) -> MongoDBOpsManager: |
| 68 | + """Create an Ops Manager with TLS-enabled AppDB for testing TLS disable.""" |
| 69 | + om = MongoDBOpsManager.from_yaml(yaml_fixture("om_appdb_tls_disable.yaml"), namespace=namespace) |
| 70 | + om.set_version(custom_version) |
| 71 | + om.set_appdb_version(custom_appdb_version) |
| 72 | + |
| 73 | + if try_load(om): |
| 74 | + return om |
| 75 | + |
| 76 | + if is_multi_cluster(): |
| 77 | + enable_multi_cluster_deployment(om) |
| 78 | + |
| 79 | + om.update() |
| 80 | + return om |
| 81 | + |
| 82 | + |
| 83 | +@mark.e2e_om_appdb_tls_disable |
| 84 | +def test_om_created_with_tls(ops_manager: MongoDBOpsManager): |
| 85 | + """Verify OM and AppDB are running with TLS enabled.""" |
| 86 | + ops_manager.om_status().assert_reaches_phase(Phase.Running, timeout=900) |
| 87 | + ops_manager.appdb_status().assert_reaches_phase(Phase.Running, timeout=600) |
| 88 | + |
| 89 | + |
| 90 | +@mark.e2e_om_appdb_tls_disable |
| 91 | +def test_appdb_monitoring_works_with_tls(ops_manager: MongoDBOpsManager): |
| 92 | + """Verify monitoring works with TLS enabled before we disable it.""" |
| 93 | + ops_manager.assert_appdb_monitoring_group_was_created() |
| 94 | + ops_manager.assert_monitoring_data_exists(timeout=600, all_hosts=False) |
| 95 | + |
| 96 | + |
| 97 | +@mark.e2e_om_appdb_tls_disable |
| 98 | +def test_monitoring_config_has_tls_params_when_tls_enabled(ops_manager: MongoDBOpsManager): |
| 99 | + """ |
| 100 | + CLOUDP-351614: Verify that monitoring config contains TLS params when TLS is enabled. |
| 101 | +
|
| 102 | + When TLS is enabled on AppDB, the monitoring agents should be configured with |
| 103 | + TLS parameters in additionalParams, including: |
| 104 | + - useSslForAllConnections: "true" |
| 105 | + - sslTrustedServerCertificates: path to CA file |
| 106 | + - sslClientCertificate: path to client certificate (for x509 auth) |
| 107 | + """ |
| 108 | + tls_params = get_monitoring_tls_params(ops_manager) |
| 109 | + |
| 110 | + # Monitoring agents should have TLS params configured |
| 111 | + assert len(tls_params) > 0, "Expected TLS params in monitoring config when TLS is enabled" |
| 112 | + |
| 113 | + # Verify TLS params contain expected keys |
| 114 | + for hostname, params in tls_params.items(): |
| 115 | + assert params.get("useSslForAllConnections") == "true", ( |
| 116 | + f"Expected useSslForAllConnections=true for {hostname}, got {params}" |
| 117 | + ) |
| 118 | + assert "sslTrustedServerCertificates" in params, ( |
| 119 | + f"Expected sslTrustedServerCertificates in params for {hostname}" |
| 120 | + ) |
| 121 | + |
| 122 | + |
| 123 | +@mark.e2e_om_appdb_tls_disable |
| 124 | +def test_disable_tls_on_appdb(ops_manager: MongoDBOpsManager): |
| 125 | + """ |
| 126 | + CLOUDP-351614: Disable TLS on AppDB and verify the operator correctly handles |
| 127 | + the transition without leaving stale TLS params in monitoring config. |
| 128 | +
|
| 129 | + This test disables TLS by setting tls.enabled = False. The operator handles |
| 130 | + the TLS mode transition: requireTLS -> preferTLS -> allowTLS -> disabled. |
| 131 | +
|
| 132 | + Note: We keep the certsSecretPrefix in place because: |
| 133 | + 1. The cert files are needed during the TLS transition |
| 134 | + 2. Removing certsSecretPrefix while TLS mode is still transitioning causes failures |
| 135 | + 3. The certs are harmless to keep after TLS is disabled (just unused) |
| 136 | + """ |
| 137 | + ops_manager.load() |
| 138 | + |
| 139 | + # Disable TLS mode (keeping certs for the transition) |
| 140 | + # The operator will handle the TLS mode transition: requireTLS -> preferTLS -> allowTLS -> disabled |
| 141 | + ops_manager["spec"]["applicationDatabase"]["security"]["tls"]["enabled"] = False |
| 142 | + ops_manager.update() |
| 143 | + |
| 144 | + # Wait for AppDB to reach Running state after TLS mode is fully disabled |
| 145 | + # Use a longer timeout as the transition goes through multiple TLS modes |
| 146 | + ops_manager.appdb_status().assert_reaches_phase(Phase.Running, timeout=1800) |
| 147 | + |
| 148 | + |
| 149 | +@mark.e2e_om_appdb_tls_disable |
| 150 | +def test_monitoring_config_tls_params_cleared_after_tls_disabled(ops_manager: MongoDBOpsManager): |
| 151 | + """ |
| 152 | + CLOUDP-351614: Verify that TLS params are CLEARED from monitoring config after TLS is disabled. |
| 153 | +
|
| 154 | + THIS IS THE KEY TEST FOR THE BUG FIX: |
| 155 | + Before the fix in CLOUDP-351614, the operator would leave stale TLS params |
| 156 | + (useSslForAllConnections, sslClientCertificate, etc.) in the monitoring config |
| 157 | + even after TLS was disabled. This caused monitoring agents to fail because they |
| 158 | + would try to use TLS certificate files that are no longer valid for authentication. |
| 159 | +
|
| 160 | + After the fix, the operator correctly clears these params by calling: |
| 161 | + delete(monitoringVersion, "additionalParams") |
| 162 | + """ |
| 163 | + tls_params = get_monitoring_tls_params(ops_manager) |
| 164 | + |
| 165 | + # After TLS is disabled, monitoring config should NOT have TLS params |
| 166 | + # This assertion would have FAILED before the fix because stale TLS params remained |
| 167 | + assert len(tls_params) == 0, ( |
| 168 | + f"CLOUDP-351614 BUG: TLS params should be cleared from monitoring config after " |
| 169 | + f"TLS is disabled, but found stale params: {tls_params}" |
| 170 | + ) |
| 171 | + |
| 172 | + |
| 173 | +@mark.e2e_om_appdb_tls_disable |
| 174 | +def test_monitoring_works_after_tls_disable(ops_manager: MongoDBOpsManager): |
| 175 | + """ |
| 176 | + CLOUDP-351614: Verify monitoring data can still be collected after TLS disable. |
| 177 | +
|
| 178 | + After TLS is disabled, monitoring agents switch from x509 to SCRAM authentication. |
| 179 | + This test verifies that: |
| 180 | + 1. The operator correctly cleared stale TLS params from monitoring config |
| 181 | + 2. Monitoring agents can reconnect and collect data |
| 182 | + """ |
| 183 | + # Use a longer timeout as monitoring agents need time to reconnect with SCRAM auth |
| 184 | + ops_manager.assert_monitoring_data_exists(timeout=1200, all_hosts=False) |
0 commit comments