Skip to content

Commit 52b4e15

Browse files
authored
Include metadata.namespace as field in logging payload (#63)
* Include metadata.namespace as field in logging payload Update NAMESPACE in deployment.yaml to be more explicit as APP_NAMESPACE Signed-off-by: don2112e <darren.oneill@ericsson.com> * removed unnecessary comment Signed-off-by: don2112e <darren.oneill@ericsson.com> * consolidated mock config into conftest.py --------- Signed-off-by: don2112e <darren.oneill@ericsson.com>
1 parent ba8ee95 commit 52b4e15

5 files changed

Lines changed: 27 additions & 15 deletions

File tree

charts/eric-oss-hello-world-python-app/templates/deployment/deployment.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ spec:
146146
valueFrom:
147147
fieldRef:
148148
fieldPath: metadata.uid
149-
- name: NAMESPACE
149+
- name: APP_NAMESPACE
150150
valueFrom:
151151
fieldRef:
152152
fieldPath: metadata.namespace

eric-oss-hello-world-python-app/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def get_config():
1515
app_cert_file_path = get_os_env_string("APP_CERT_FILE_PATH", "")
1616
client_creds_file_path = get_os_env_string("CLIENT_CREDS_FILE_PATH", "")
1717
client_id_file_name = get_os_env_string("CLIENT_ID_FILE_NAME", "")
18+
app_namespace = get_os_env_string("APP_NAMESPACE", "")
1819

1920
config = {
2021
"eic_host_url": eic_host_url,
@@ -28,6 +29,7 @@ def get_config():
2829
"client_creds_file_path": client_creds_file_path,
2930
"client_id_file_name": client_id_file_name,
3031
"chosen_unique_name": "eric-oss-hello-world-python-app",
32+
"app_namespace": app_namespace,
3133
}
3234
return config
3335

eric-oss-hello-world-python-app/mtls_logging.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def log(self, message, severity):
7676
"message": message,
7777
"service_id": "rapp-" + self.config.get("chosen_unique_name"),
7878
"severity": severity.name.lower(),
79+
"metadata": {"namespace": self.config.get("app_namespace")},
7980
}
8081

8182
# print to console

eric-oss-hello-world-python-app/tests/conftest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ def no_log_certs():
9696
populate_environment_variables()
9797

9898

99+
@pytest.fixture(name="with_log_ctrl_file")
100+
def with_log_ctrl_file():
101+
log_ctrl_config = get_config()
102+
log_ctrl_config["log_ctrl_file"] = "/dummy/path/logcontrol.json"
103+
return log_ctrl_config
104+
99105

100106
def populate_environment_variables():
101107
os.environ["EIC_HOST_URL"] = "https://www.eic-host-url.com"
@@ -107,3 +113,4 @@ def populate_environment_variables():
107113
os.environ["APP_CERT_FILE_PATH"] = "APP_CERT_FILE_PATH"
108114
os.environ["CLIENT_CREDS_FILE_PATH"] = os.path.relpath(os.path.dirname(__file__), "/")
109115
os.environ["CLIENT_ID_FILE_NAME"] = "client_id_example"
116+
os.environ["APP_NAMESPACE"] = "test-namespace"

eric-oss-hello-world-python-app/tests/test_mtls_logging.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from mtls_logging import MtlsLogging, Severity
77

88

9+
910
def test_log_stdout_and_mtls(caplog):
1011
"""Ensure any log is sent both to STDOUT and through HTTPS"""
1112
message = "Message which should appear in both STDOUT and sent as a POST request"
@@ -80,30 +81,31 @@ def test_log_handles_missing_schema(caplog):
8081
assert "Request failed for mTLS logging: Missing schema" in caplog.text
8182

8283

83-
def test_init_sets_log_level_from_log_ctrl_file():
84+
def test_init_sets_log_level_from_log_ctrl_file(with_log_ctrl_file):
8485
# Sample log control file contents with container mapping
8586
log_ctrl_data = [
8687
{"container": "test-container", "severity": "critical"},
8788
{"container": "other-container", "severity": "warning"},
8889
]
8990
log_ctrl_json = json.dumps(log_ctrl_data)
9091

91-
# Mocked config dict including the log_ctrl_file path
92-
mock_config = {
93-
"log_ctrl_file": "/dummy/path/logcontrol.json",
94-
"ca_cert_file_name": "ca.pem",
95-
"ca_cert_file_path": "certs",
96-
"app_cert": "appcert.pem",
97-
"app_key": "appkey.pem",
98-
"app_cert_file_path": "certs",
99-
"log_endpoint": "log.endpoint",
100-
"chosen_unique_name": "eric-oss-hello-world-python-app"
101-
}
102-
10392
# Patch config and environment variable
104-
with mock.patch("mtls_logging.get_config", return_value=mock_config), \
93+
with mock.patch("mtls_logging.get_config", return_value=with_log_ctrl_file), \
10594
mock.patch("mtls_logging.get_os_env_string", return_value="test-container"), \
10695
mock.patch("builtins.open", mock.mock_open(read_data=log_ctrl_json)):
10796

10897
logger = MtlsLogging(level=None)
10998
assert logger.logger.level == Severity.CRITICAL
99+
100+
101+
def test_namespace_is_set_in_mtls_log(config):
102+
"""Ensure the namespace is set in the mTLS log"""
103+
message = "Message that should be included in a request payload that includes the namespace"
104+
105+
with mock.patch("mtls_logging.get_config", return_value=config):
106+
mock_post = with_mocked_post(send_log, message, Severity.INFO, Severity.INFO)
107+
mock_post.assert_called()
108+
109+
request_body_object = mock_post.call_args.kwargs.get('json')
110+
111+
assert request_body_object['metadata']['namespace'] == 'test-namespace'

0 commit comments

Comments
 (0)