-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtest_command_redaction.py
More file actions
49 lines (39 loc) · 1.71 KB
/
Copy pathtest_command_redaction.py
File metadata and controls
49 lines (39 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""Unit tests for redaction of secrets in command logging"""
from unittest import mock
import pytest
from restic_compose_backup import commands
from .conftest import BaseTestCase
pytestmark = pytest.mark.unit
REPO_WITH_PASSWORD = "rest:http://user:s3cr3t@host:8000/repo"
class CommandRedactionTests(BaseTestCase):
"""restic invocations must never log the repository password, even at DEBUG."""
def _fake_process(self):
proc = mock.MagicMock()
proc.communicate.return_value = (b"", b"")
proc.returncode = 0
return proc
def test_run_does_not_log_repository_password(self):
"""commands.run() must redact the repository URL in its debug log."""
cmd = ["restic", "-r", REPO_WITH_PASSWORD, "snapshots"]
with mock.patch(
"restic_compose_backup.commands.Popen", return_value=self._fake_process()
):
with self.assertLogs(
"restic_compose_backup.commands", level="DEBUG"
) as log:
commands.run(cmd)
output = "\n".join(log.output)
self.assertNotIn("s3cr3t", output)
self.assertIn("***", output)
def test_run_capture_std_does_not_log_repository_password(self):
"""commands.run_capture_std() must redact the repository URL in its debug log."""
cmd = ["restic", "-r", REPO_WITH_PASSWORD, "snapshots"]
with mock.patch(
"restic_compose_backup.commands.Popen", return_value=self._fake_process()
):
with self.assertLogs(
"restic_compose_backup.commands", level="DEBUG"
) as log:
commands.run_capture_std(cmd)
output = "\n".join(log.output)
self.assertNotIn("s3cr3t", output)