Skip to content

Commit f3e44dd

Browse files
committed
check that rabbitmq settings are passed with password clear
1 parent 22b97c2 commit f3e44dd

File tree

3 files changed

+62
-10
lines changed

3 files changed

+62
-10
lines changed

services/clusters-keeper/src/simcore_service_clusters_keeper/core/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ class PrimaryEC2InstancesSettings(BaseCustomSettings):
253253
] = "172.20.0.0/14" # nosec
254254

255255
PRIMARY_EC2_INSTANCES_RABBIT_SETTINGS: Annotated[
256-
RabbitSettings | None,
256+
RabbitSettings,
257257
Field(
258258
description="defines the Rabbit settings for the primary instance (may be disabled)",
259259
json_schema_extra={"auto_default_from_env": True},

services/clusters-keeper/src/simcore_service_clusters_keeper/utils/clusters.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import yaml
1010
from aws_library.ec2 import EC2InstanceBootSpecific, EC2InstanceData, EC2Tags
1111
from aws_library.ec2._models import CommandStr
12+
from common_library.json_serialization import json_dumps
13+
from common_library.serialization import model_dump_with_secrets
1214
from fastapi.encoders import jsonable_encoder
1315
from models_library.api_schemas_clusters_keeper.clusters import (
1416
ClusterState,
@@ -102,7 +104,7 @@ def _convert_to_env_dict(entries: dict[str, Any]) -> str:
102104
f"WORKERS_EC2_INSTANCES_SUBNET_ID={app_settings.CLUSTERS_KEEPER_WORKERS_EC2_INSTANCES.WORKERS_EC2_INSTANCES_SUBNET_ID}",
103105
f"WORKERS_EC2_INSTANCES_TIME_BEFORE_DRAINING={app_settings.CLUSTERS_KEEPER_WORKERS_EC2_INSTANCES.WORKERS_EC2_INSTANCES_TIME_BEFORE_DRAINING}",
104106
f"WORKERS_EC2_INSTANCES_TIME_BEFORE_TERMINATION={app_settings.CLUSTERS_KEEPER_WORKERS_EC2_INSTANCES.WORKERS_EC2_INSTANCES_TIME_BEFORE_TERMINATION}",
105-
f"AUTOSCALING_RABBITMQ={app_settings.CLUSTERS_KEEPER_RABBITMQ.model_dump_json() if app_settings.CLUSTERS_KEEPER_RABBITMQ else 'null'}",
107+
f"AUTOSCALING_RABBITMQ={json_dumps(model_dump_with_secrets(app_settings.CLUSTERS_KEEPER_RABBITMQ, show_secrets=True)) if app_settings.CLUSTERS_KEEPER_RABBITMQ else 'null'}",
106108
]
107109

108110

services/clusters-keeper/tests/unit/test_utils_clusters.py

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
)
2626
from pydantic import ByteSize, TypeAdapter
2727
from pytest_simcore.helpers.monkeypatch_envs import EnvVarsDict, setenvs_from_dict
28+
from settings_library.rabbit import RabbitSettings
2829
from simcore_service_clusters_keeper.core.settings import ApplicationSettings
2930
from simcore_service_clusters_keeper.utils.clusters import (
3031
_prepare_environment_variables,
@@ -34,6 +35,12 @@
3435
)
3536
from types_aiobotocore_ec2.literals import InstanceStateNameType
3637

38+
pytest_simcore_core_services_selection = [
39+
"rabbit",
40+
]
41+
42+
pytest_simcore_ops_services_selection = []
43+
3744

3845
@pytest.fixture
3946
def cluster_machines_name_prefix(faker: Faker) -> str:
@@ -69,9 +76,9 @@ def app_environment(
6976
monkeypatch,
7077
{
7178
"CLUSTERS_KEEPER_COMPUTATIONAL_BACKEND_DEFAULT_CLUSTER_AUTH": json_dumps(
72-
TLSAuthentication.model_config["json_schema_extra"]["examples"][0]
79+
TLSAuthentication.model_json_schema()["examples"][0]
7380
if isinstance(backend_cluster_auth, TLSAuthentication)
74-
else NoAuthentication.model_config["json_schema_extra"]["examples"][0]
81+
else NoAuthentication.model_json_schema()["examples"][0]
7582
)
7683
},
7784
)
@@ -105,7 +112,9 @@ def test_create_deploy_cluster_stack_script(
105112
clusters_keeper_docker_compose: dict[str, Any],
106113
):
107114
additional_custom_tags = {
108-
AWSTagKey("pytest-tag-key"): AWSTagValue("pytest-tag-value")
115+
TypeAdapter(AWSTagKey)
116+
.validate_python("pytest-tag-key"): TypeAdapter(AWSTagValue)
117+
.validate_python("pytest-tag-value")
109118
}
110119
deploy_script = create_deploy_cluster_stack_script(
111120
app_settings,
@@ -175,13 +184,52 @@ def test_create_deploy_cluster_stack_script(
175184
for i in dict_settings
176185
)
177186

187+
# check that the RabbitMQ settings are null since rabbit is disabled
188+
assert re.search(r"AUTOSCALING_RABBITMQ=null", deploy_script)
189+
178190
# check the additional tags are in
179191
assert all(
180192
f'"{key}": "{value}"' in deploy_script
181193
for key, value in additional_custom_tags.items()
182194
)
183195

184196

197+
def test_rabbitmq_settings_are_passed_with_pasword_clear(
198+
docker_swarm: None,
199+
enabled_rabbitmq: None,
200+
mocked_ec2_server_envs: EnvVarsDict,
201+
mocked_ssm_server_envs: EnvVarsDict,
202+
mocked_redis_server: None,
203+
app_settings: ApplicationSettings,
204+
cluster_machines_name_prefix: str,
205+
clusters_keeper_docker_compose: dict[str, Any],
206+
):
207+
assert app_settings.CLUSTERS_KEEPER_RABBITMQ
208+
assert app_settings.CLUSTERS_KEEPER_RABBITMQ.RABBIT_HOST
209+
assert app_settings.CLUSTERS_KEEPER_RABBITMQ.RABBIT_PORT
210+
assert app_settings.CLUSTERS_KEEPER_RABBITMQ.RABBIT_SECURE is False
211+
assert app_settings.CLUSTERS_KEEPER_RABBITMQ.RABBIT_USER
212+
assert app_settings.CLUSTERS_KEEPER_RABBITMQ.RABBIT_PASSWORD.get_secret_value()
213+
214+
additional_custom_tags = {
215+
TypeAdapter(AWSTagKey)
216+
.validate_python("pytest-tag-key"): TypeAdapter(AWSTagValue)
217+
.validate_python("pytest-tag-value")
218+
}
219+
deploy_script = create_deploy_cluster_stack_script(
220+
app_settings,
221+
cluster_machines_name_prefix=cluster_machines_name_prefix,
222+
additional_custom_tags=additional_custom_tags,
223+
)
224+
assert isinstance(deploy_script, str)
225+
226+
match = re.search(r"AUTOSCALING_RABBITMQ=({.*?})", deploy_script)
227+
assert match, "AUTOSCALING_RABBITMQ is not present in the deploy script!"
228+
autoscaling_rabbitmq = match.group(1)
229+
passed_settings = RabbitSettings.model_validate_json(autoscaling_rabbitmq)
230+
assert passed_settings == app_settings.CLUSTERS_KEEPER_RABBITMQ
231+
232+
185233
def test_create_deploy_cluster_stack_script_below_64kb(
186234
disabled_rabbitmq: None,
187235
mocked_ec2_server_envs: EnvVarsDict,
@@ -192,7 +240,9 @@ def test_create_deploy_cluster_stack_script_below_64kb(
192240
clusters_keeper_docker_compose: dict[str, Any],
193241
):
194242
additional_custom_tags = {
195-
AWSTagKey("pytest-tag-key"): AWSTagValue("pytest-tag-value")
243+
TypeAdapter(AWSTagKey)
244+
.validate_python("pytest-tag-key"): TypeAdapter(AWSTagValue)
245+
.validate_python("pytest-tag-value")
196246
}
197247
deploy_script = create_deploy_cluster_stack_script(
198248
app_settings,
@@ -239,7 +289,9 @@ def test__prepare_environment_variables_defines_all_envs_for_docker_compose(
239289
clusters_keeper_docker_compose_file: Path,
240290
):
241291
additional_custom_tags = {
242-
AWSTagKey("pytest-tag-key"): AWSTagValue("pytest-tag-value")
292+
TypeAdapter(AWSTagKey)
293+
.validate_python("pytest-tag-key"): TypeAdapter(AWSTagValue)
294+
.validate_python("pytest-tag-value")
243295
}
244296
environment_variables = _prepare_environment_variables(
245297
app_settings,
@@ -285,9 +337,7 @@ def test__prepare_environment_variables_defines_all_envs_for_docker_compose(
285337
"authentication",
286338
[
287339
NoAuthentication(),
288-
TLSAuthentication(
289-
**TLSAuthentication.model_config["json_schema_extra"]["examples"][0]
290-
),
340+
TLSAuthentication(**TLSAuthentication.model_json_schema()["examples"][0]),
291341
],
292342
)
293343
def test_create_cluster_from_ec2_instance(

0 commit comments

Comments
 (0)