From 4b89090e76207f10326909d9db1b96bda77562c9 Mon Sep 17 00:00:00 2001 From: don2112e Date: Thu, 20 Nov 2025 14:27:15 +0000 Subject: [PATCH 1/3] Include metadata.namespace as field in logging payload Update NAMESPACE in deployment.yaml to be more explicit as APP_NAMESPACE Signed-off-by: don2112e --- .../templates/deployment/deployment.yaml | 2 +- eric-oss-hello-world-python-app/config.py | 2 + .../mtls_logging.py | 1 + .../tests/test_mtls_logging.py | 40 +++++++++++++------ 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/charts/eric-oss-hello-world-python-app/templates/deployment/deployment.yaml b/charts/eric-oss-hello-world-python-app/templates/deployment/deployment.yaml index 6dc21cd..11cf591 100644 --- a/charts/eric-oss-hello-world-python-app/templates/deployment/deployment.yaml +++ b/charts/eric-oss-hello-world-python-app/templates/deployment/deployment.yaml @@ -150,7 +150,7 @@ spec: valueFrom: fieldRef: fieldPath: metadata.uid - - name: NAMESPACE + - name: APP_NAMESPACE valueFrom: fieldRef: fieldPath: metadata.namespace diff --git a/eric-oss-hello-world-python-app/config.py b/eric-oss-hello-world-python-app/config.py index 28266c4..3f3dd36 100644 --- a/eric-oss-hello-world-python-app/config.py +++ b/eric-oss-hello-world-python-app/config.py @@ -17,6 +17,7 @@ def get_config(): app_cert_file_path = get_os_env_string("APP_CERT_FILE_PATH", "") client_creds_file_path = get_os_env_string("CLIENT_CREDS_FILE_PATH", "") client_id_file_name = get_os_env_string("CLIENT_ID_FILE_NAME", "") + app_namespace = get_os_env_string("APP_NAMESPACE", "") config = { "iam_client_id": iam_client_id, @@ -32,6 +33,7 @@ def get_config(): "client_creds_file_path": client_creds_file_path, "client_id_file_name": client_id_file_name, "chosen_unique_name": "eric-oss-hello-world-python-app", + "app_namespace": app_namespace, } return config diff --git a/eric-oss-hello-world-python-app/mtls_logging.py b/eric-oss-hello-world-python-app/mtls_logging.py index 36cc3a6..cdd7755 100644 --- a/eric-oss-hello-world-python-app/mtls_logging.py +++ b/eric-oss-hello-world-python-app/mtls_logging.py @@ -76,6 +76,7 @@ def log(self, message, severity): "message": message, "service_id": "rapp-" + self.config.get("chosen_unique_name"), "severity": severity.name.lower(), + "metadata": {"namespace": self.config.get("app_namespace")}, } # print to console diff --git a/eric-oss-hello-world-python-app/tests/test_mtls_logging.py b/eric-oss-hello-world-python-app/tests/test_mtls_logging.py index 4b9b623..75ad1b5 100644 --- a/eric-oss-hello-world-python-app/tests/test_mtls_logging.py +++ b/eric-oss-hello-world-python-app/tests/test_mtls_logging.py @@ -5,6 +5,18 @@ import requests from mtls_logging import MtlsLogging, Severity +MOCK_CONFIG = { + "log_ctrl_file": "/dummy/path/logcontrol.json", + "ca_cert_file_name": "ca.pem", + "ca_cert_file_path": "certs", + "app_cert": "appcert.pem", + "app_key": "appkey.pem", + "app_cert_file_path": "certs", + "log_endpoint": "log.endpoint", + "chosen_unique_name": "eric-oss-hello-world-python-app", + "app_namespace": "test-namespace" +} + def test_log_stdout_and_mtls(caplog): """Ensure any log is sent both to STDOUT and through HTTPS""" @@ -88,22 +100,24 @@ def test_init_sets_log_level_from_log_ctrl_file(): ] log_ctrl_json = json.dumps(log_ctrl_data) - # Mocked config dict including the log_ctrl_file path - mock_config = { - "log_ctrl_file": "/dummy/path/logcontrol.json", - "ca_cert_file_name": "ca.pem", - "ca_cert_file_path": "certs", - "app_cert": "appcert.pem", - "app_key": "appkey.pem", - "app_cert_file_path": "certs", - "log_endpoint": "log.endpoint", - "chosen_unique_name": "eric-oss-hello-world-python-app" - } - # Patch config and environment variable - with mock.patch("mtls_logging.get_config", return_value=mock_config), \ + with mock.patch("mtls_logging.get_config", return_value=MOCK_CONFIG), \ mock.patch("mtls_logging.get_os_env_string", return_value="test-container"), \ mock.patch("builtins.open", mock.mock_open(read_data=log_ctrl_json)): logger = MtlsLogging(level=None) assert logger.logger.level == Severity.CRITICAL + +def test_namespace_is_set_in_mtls_log(): + """Ensure the namespace is set in the mTLS log""" + message = "Message that should be included in a request payload that includes the namespace" + + # Patch config and environment variable + with mock.patch("mtls_logging.get_config", return_value=MOCK_CONFIG): + + mock_post = with_mocked_post(send_log, message,Severity.INFO, Severity.INFO) + mock_post.assert_called() + + request_body_object = mock_post.call_args.kwargs.get('json') + + assert request_body_object['metadata']['namespace'] == 'test-namespace' From 1a3b4b46afb07157ea4f8b51d5a7eae9a4505e5d Mon Sep 17 00:00:00 2001 From: don2112e Date: Thu, 20 Nov 2025 14:34:06 +0000 Subject: [PATCH 2/3] removed unnecessary comment Signed-off-by: don2112e --- eric-oss-hello-world-python-app/tests/test_mtls_logging.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/eric-oss-hello-world-python-app/tests/test_mtls_logging.py b/eric-oss-hello-world-python-app/tests/test_mtls_logging.py index 75ad1b5..89ab498 100644 --- a/eric-oss-hello-world-python-app/tests/test_mtls_logging.py +++ b/eric-oss-hello-world-python-app/tests/test_mtls_logging.py @@ -112,9 +112,7 @@ def test_namespace_is_set_in_mtls_log(): """Ensure the namespace is set in the mTLS log""" message = "Message that should be included in a request payload that includes the namespace" - # Patch config and environment variable with mock.patch("mtls_logging.get_config", return_value=MOCK_CONFIG): - mock_post = with_mocked_post(send_log, message,Severity.INFO, Severity.INFO) mock_post.assert_called() From 25710e53f4fb093ca285ea351c2425cb21ce02e3 Mon Sep 17 00:00:00 2001 From: don2112e Date: Thu, 20 Nov 2025 17:29:57 +0000 Subject: [PATCH 3/3] consolidated mock config into conftest.py --- .../tests/conftest.py | 7 ++++++ .../tests/test_mtls_logging.py | 22 +++++-------------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/eric-oss-hello-world-python-app/tests/conftest.py b/eric-oss-hello-world-python-app/tests/conftest.py index 28eb8fb..6204eed 100644 --- a/eric-oss-hello-world-python-app/tests/conftest.py +++ b/eric-oss-hello-world-python-app/tests/conftest.py @@ -107,6 +107,12 @@ def no_log_certs(): populate_environment_variables() +@pytest.fixture(name="with_log_ctrl_file") +def with_log_ctrl_file(): + log_ctrl_config = get_config() + log_ctrl_config["log_ctrl_file"] = "/dummy/path/logcontrol.json" + return log_ctrl_config + def populate_environment_variables(): os.environ["IAM_CLIENT_ID"] = "IAM_CLIENT_ID" @@ -120,3 +126,4 @@ def populate_environment_variables(): os.environ["APP_CERT_FILE_PATH"] = "APP_CERT_FILE_PATH" os.environ["CLIENT_CREDS_FILE_PATH"] = os.path.relpath(os.path.dirname(__file__), "/") os.environ["CLIENT_ID_FILE_NAME"] = "client_id_example" + os.environ["APP_NAMESPACE"] = "test-namespace" \ No newline at end of file diff --git a/eric-oss-hello-world-python-app/tests/test_mtls_logging.py b/eric-oss-hello-world-python-app/tests/test_mtls_logging.py index 89ab498..fcb5e2d 100644 --- a/eric-oss-hello-world-python-app/tests/test_mtls_logging.py +++ b/eric-oss-hello-world-python-app/tests/test_mtls_logging.py @@ -5,17 +5,6 @@ import requests from mtls_logging import MtlsLogging, Severity -MOCK_CONFIG = { - "log_ctrl_file": "/dummy/path/logcontrol.json", - "ca_cert_file_name": "ca.pem", - "ca_cert_file_path": "certs", - "app_cert": "appcert.pem", - "app_key": "appkey.pem", - "app_cert_file_path": "certs", - "log_endpoint": "log.endpoint", - "chosen_unique_name": "eric-oss-hello-world-python-app", - "app_namespace": "test-namespace" -} def test_log_stdout_and_mtls(caplog): @@ -92,7 +81,7 @@ def test_log_handles_missing_schema(caplog): assert "Request failed for mTLS logging: Missing schema" in caplog.text -def test_init_sets_log_level_from_log_ctrl_file(): +def test_init_sets_log_level_from_log_ctrl_file(with_log_ctrl_file): # Sample log control file contents with container mapping log_ctrl_data = [ {"container": "test-container", "severity": "critical"}, @@ -101,19 +90,20 @@ def test_init_sets_log_level_from_log_ctrl_file(): log_ctrl_json = json.dumps(log_ctrl_data) # Patch config and environment variable - with mock.patch("mtls_logging.get_config", return_value=MOCK_CONFIG), \ + with mock.patch("mtls_logging.get_config", return_value=with_log_ctrl_file), \ mock.patch("mtls_logging.get_os_env_string", return_value="test-container"), \ mock.patch("builtins.open", mock.mock_open(read_data=log_ctrl_json)): logger = MtlsLogging(level=None) assert logger.logger.level == Severity.CRITICAL -def test_namespace_is_set_in_mtls_log(): + +def test_namespace_is_set_in_mtls_log(config): """Ensure the namespace is set in the mTLS log""" message = "Message that should be included in a request payload that includes the namespace" - with mock.patch("mtls_logging.get_config", return_value=MOCK_CONFIG): - mock_post = with_mocked_post(send_log, message,Severity.INFO, Severity.INFO) + with mock.patch("mtls_logging.get_config", return_value=config): + mock_post = with_mocked_post(send_log, message, Severity.INFO, Severity.INFO) mock_post.assert_called() request_body_object = mock_post.call_args.kwargs.get('json')