|
1 |
| -# Copyright 2023 Canonical Ltd. |
| 1 | +# Copyright 2024 Canonical Ltd. |
2 | 2 | # See LICENSE file for licensing details.
|
3 | 3 |
|
4 |
| -# flake8: noqa |
5 |
| -# TODO: enable & remove noqa |
6 |
| -# import asyncio |
7 |
| -# import logging |
8 |
| -# |
9 |
| -# import pytest |
10 |
| -# from pytest_operator.plugin import OpsTest |
11 |
| -# |
12 |
| -# logger = logging.getLogger(__name__) |
13 |
| -# |
14 |
| -# MYSQL_APP_NAME = "mysql" |
15 |
| -# MYSQL_ROUTER_APP_NAME = "mysqlrouter" |
16 |
| -# TEST_APP_NAME = "mysql-test-app" |
17 |
| -# TLS_APP_NAME = "tls-certificates-operator" |
18 |
| -# SLOW_TIMEOUT = 15 * 60 |
19 |
| -# MODEL_CONFIG = {"logging-config": "<root>=INFO;unit=DEBUG"} |
20 |
| -# |
21 |
| -# |
22 |
| -# @pytest.mark.group(1) |
23 |
| -# @pytest.mark.abort_on_fail |
24 |
| -# async def test_build_deploy_and_relate(ops_test: OpsTest, mysql_router_charm_series: str) -> None: |
25 |
| -# """Test encryption when backend database is using TLS.""" |
26 |
| -# # Deploy TLS Certificates operator. |
27 |
| -# await ops_test.model.set_config(MODEL_CONFIG) |
28 |
| -# logger.info("Deploy and relate all applications") |
29 |
| -# async with ops_test.fast_forward(): |
30 |
| -# # deploy mysql first |
31 |
| -# await ops_test.model.deploy( |
32 |
| -# MYSQL_APP_NAME, channel="8.0/edge", config={"profile": "testing"}, num_units=3 |
33 |
| -# ) |
34 |
| -# tls_config = {"generate-self-signed-certificates": "true", "ca-common-name": "Test CA"} |
35 |
| -# |
36 |
| -# # ROUTER |
37 |
| -# mysqlrouter_charm = await ops_test.build_charm(".") |
38 |
| -# |
39 |
| -# # tls, test app and router |
40 |
| -# await asyncio.gather( |
41 |
| -# ops_test.model.deploy( |
42 |
| -# mysqlrouter_charm, |
43 |
| -# application_name=MYSQL_ROUTER_APP_NAME, |
44 |
| -# num_units=None, |
45 |
| -# series=mysql_router_charm_series, |
46 |
| -# ), |
47 |
| -# ops_test.model.deploy( |
48 |
| -# TLS_APP_NAME, application_name=TLS_APP_NAME, channel="stable", config=tls_config |
49 |
| -# ), |
50 |
| -# ops_test.model.deploy( |
51 |
| -# TEST_APP_NAME, application_name=TEST_APP_NAME, channel="latest/edge" |
52 |
| -# ), |
53 |
| -# ) |
54 |
| -# |
55 |
| -# await ops_test.model.relate( |
56 |
| -# f"{MYSQL_ROUTER_APP_NAME}:backend-database", f"{MYSQL_APP_NAME}:database" |
57 |
| -# ) |
58 |
| -# await ops_test.model.relate( |
59 |
| -# f"{TEST_APP_NAME}:database", f"{MYSQL_ROUTER_APP_NAME}:database" |
60 |
| -# ) |
61 |
| -# |
62 |
| -# logger.info("Waiting for applications to become active") |
63 |
| -# # We can safely wait only for test application to be ready, given that it will |
64 |
| -# # only become active once all the other applications are ready. |
65 |
| -# await ops_test.model.wait_for_idle([TEST_APP_NAME], status="active", timeout=15 * 60) |
66 |
| -# |
67 |
| -# |
68 |
| -# @pytest.mark.group(1) |
69 |
| -# async def test_connected_encryption(ops_test: OpsTest) -> None: |
70 |
| -# """Test encryption when backend database is using TLS.""" |
71 |
| -# test_app_unit = ops_test.model.applications[TEST_APP_NAME].units[0] |
72 |
| -# |
73 |
| -# logger.info("Relating TLS with backend database") |
74 |
| -# await ops_test.model.relate(TLS_APP_NAME, MYSQL_APP_NAME) |
75 |
| -# |
76 |
| -# # Wait for hooks start reconfiguring app |
77 |
| -# await ops_test.model.block_until( |
78 |
| -# lambda: ops_test.model.applications[MYSQL_APP_NAME].status != "active", timeout=4 * 60 |
79 |
| -# ) |
80 |
| -# await ops_test.model.wait_for_idle(status="active", timeout=15 * 60) |
81 |
| -# |
82 |
| -# logger.info("Get cipher when TLS is enforced") |
83 |
| -# action = await test_app_unit.run_action("get-session-ssl-cipher") |
84 |
| -# result = await action.wait() |
85 |
| -# |
86 |
| -# cipher = result.results["cipher"] |
87 |
| -# # this assertion should be true even when TLS is not related to the backend database |
88 |
| -# # because by default mysqlrouter will use TLS, unless explicitly disabled, which we never do |
89 |
| -# assert cipher == "TLS_AES_256_GCM_SHA384", "Cipher not set" |
| 4 | +import asyncio |
| 5 | +import logging |
| 6 | +import time |
| 7 | + |
| 8 | +import pytest |
| 9 | +from pytest_operator.plugin import OpsTest |
| 10 | + |
| 11 | +from .helpers import get_tls_certificate_issuer |
| 12 | + |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | + |
| 15 | +MYSQL_APP_NAME = "mysql" |
| 16 | +MYSQL_ROUTER_APP_NAME = "mysqlrouter" |
| 17 | +TEST_APP_NAME = "mysql-test-app" |
| 18 | +TLS_APP_NAME = "tls-certificates-operator" |
| 19 | +SLOW_TIMEOUT = 15 * 60 |
| 20 | +MODEL_CONFIG = {"logging-config": "<root>=INFO;unit=DEBUG"} |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.group(1) |
| 24 | +@pytest.mark.abort_on_fail |
| 25 | +async def test_build_deploy_and_relate(ops_test: OpsTest, mysql_router_charm_series: str) -> None: |
| 26 | + """Test encryption when backend database is using TLS.""" |
| 27 | + await ops_test.model.set_config(MODEL_CONFIG) |
| 28 | + logger.info("Deploy and relate all applications") |
| 29 | + async with ops_test.fast_forward(): |
| 30 | + # deploy mysql first |
| 31 | + await ops_test.model.deploy( |
| 32 | + MYSQL_APP_NAME, channel="8.0/edge", config={"profile": "testing"}, num_units=1 |
| 33 | + ) |
| 34 | + tls_config = {"generate-self-signed-certificates": "true", "ca-common-name": "Test CA"} |
| 35 | + |
| 36 | + # ROUTER |
| 37 | + mysqlrouter_charm = await ops_test.build_charm(".") |
| 38 | + |
| 39 | + # tls, test app and router |
| 40 | + await asyncio.gather( |
| 41 | + ops_test.model.deploy( |
| 42 | + mysqlrouter_charm, |
| 43 | + application_name=MYSQL_ROUTER_APP_NAME, |
| 44 | + num_units=None, |
| 45 | + series=mysql_router_charm_series, |
| 46 | + ), |
| 47 | + ops_test.model.deploy( |
| 48 | + TLS_APP_NAME, application_name=TLS_APP_NAME, channel="stable", config=tls_config |
| 49 | + ), |
| 50 | + ops_test.model.deploy( |
| 51 | + TEST_APP_NAME, |
| 52 | + application_name=TEST_APP_NAME, |
| 53 | + channel="latest/edge", |
| 54 | + series=mysql_router_charm_series, |
| 55 | + ), |
| 56 | + ) |
| 57 | + |
| 58 | + await ops_test.model.relate( |
| 59 | + f"{MYSQL_ROUTER_APP_NAME}:backend-database", f"{MYSQL_APP_NAME}:database" |
| 60 | + ) |
| 61 | + await ops_test.model.relate( |
| 62 | + f"{TEST_APP_NAME}:database", f"{MYSQL_ROUTER_APP_NAME}:database" |
| 63 | + ) |
| 64 | + |
| 65 | + logger.info("Waiting for applications to become active") |
| 66 | + # We can safely wait only for test application to be ready, given that it will |
| 67 | + # only become active once all the other applications are ready. |
| 68 | + await ops_test.model.wait_for_idle([TEST_APP_NAME], status="active", timeout=SLOW_TIMEOUT) |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.group(1) |
| 72 | +@pytest.mark.abort_on_fail |
| 73 | +async def test_connected_encryption(ops_test: OpsTest) -> None: |
| 74 | + """Test encryption when backend database is using TLS.""" |
| 75 | + mysqlrouter_unit = ops_test.model.applications[MYSQL_ROUTER_APP_NAME].units[0] |
| 76 | + |
| 77 | + issuer = await get_tls_certificate_issuer( |
| 78 | + ops_test, |
| 79 | + mysqlrouter_unit.name, |
| 80 | + socket="/var/snap/charmed-mysql/common/run/mysqlrouter/mysql.sock", |
| 81 | + ) |
| 82 | + assert ( |
| 83 | + "Issuer: CN = MySQL_Router_Auto_Generated_CA_Certificate" in issuer |
| 84 | + ), "Expected mysqlrouter autogenerated CA certificate" |
| 85 | + |
| 86 | + logger.info("Relating TLS with mysqlrouter") |
| 87 | + await ops_test.model.relate(TLS_APP_NAME, MYSQL_ROUTER_APP_NAME) |
| 88 | + |
| 89 | + time.sleep(30) |
| 90 | + |
| 91 | + logger.info("Getting certificate issuer after relating with tls operator") |
| 92 | + issuer = await get_tls_certificate_issuer( |
| 93 | + ops_test, |
| 94 | + mysqlrouter_unit.name, |
| 95 | + socket="/var/snap/charmed-mysql/common/run/mysqlrouter/mysql.sock", |
| 96 | + ) |
| 97 | + assert "CN = Test CA" in issuer, f"Expected mysqlrouter certificate from {TLS_APP_NAME}" |
| 98 | + |
| 99 | + logger.info("Removing relation TLS with mysqlrouter") |
| 100 | + await ops_test.model.applications[MYSQL_ROUTER_APP_NAME].remove_relation( |
| 101 | + f"{TLS_APP_NAME}:certificates", f"{MYSQL_ROUTER_APP_NAME}:certificates" |
| 102 | + ) |
| 103 | + |
| 104 | + time.sleep(30) |
| 105 | + issuer = await get_tls_certificate_issuer( |
| 106 | + ops_test, |
| 107 | + mysqlrouter_unit.name, |
| 108 | + socket="/var/snap/charmed-mysql/common/run/mysqlrouter/mysql.sock", |
| 109 | + ) |
| 110 | + assert ( |
| 111 | + "Issuer: CN = MySQL_Router_Auto_Generated_CA_Certificate" in issuer |
| 112 | + ), "Expected mysqlrouter autogenerated CA certificate" |
0 commit comments