Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ spec:
valueFrom:
fieldRef:
fieldPath: metadata.uid
- name: NAMESPACE
- name: APP_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
Expand Down
2 changes: 2 additions & 0 deletions eric-oss-hello-world-python-app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand Down
1 change: 1 addition & 0 deletions eric-oss-hello-world-python-app/mtls_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions eric-oss-hello-world-python-app/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
30 changes: 16 additions & 14 deletions eric-oss-hello-world-python-app/tests/test_mtls_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -80,30 +81,31 @@ 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"},
{"container": "other-container", "severity": "warning"},
]
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'