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/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 4b9b623..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 @@ -6,6 +6,7 @@ from mtls_logging import MtlsLogging, Severity + def test_log_stdout_and_mtls(caplog): """Ensure any log is sent both to STDOUT and through HTTPS""" message = "Message which should appear in both STDOUT and sent as a POST request" @@ -80,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"}, @@ -88,22 +89,23 @@ 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=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(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=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'