Skip to content

Commit 37820fd

Browse files
Merge pull request #1172 from GitGuardian/fix/docker-stdout-json
fix: keep docker stdout off JSON output
2 parents b0a6380 + 5f6dc32 commit 37820fd

File tree

3 files changed

+64
-10
lines changed

3 files changed

+64
-10
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<!--
2+
A new scriv changelog fragment.
3+
4+
Uncomment the section that is right (remove the HTML comment wrapper).
5+
For top level release notes, leave all the headers commented out.
6+
-->
7+
8+
<!--
9+
### Removed
10+
11+
- A bullet item for the Removed category.
12+
13+
-->
14+
<!--
15+
### Added
16+
17+
- A bullet item for the Added category.
18+
19+
-->
20+
<!--
21+
### Changed
22+
23+
- A bullet item for the Changed category.
24+
25+
-->
26+
<!--
27+
### Deprecated
28+
29+
- A bullet item for the Deprecated category.
30+
31+
-->
32+
33+
### Fixed
34+
35+
- Prevent docker scan stdout from leaking into JSON output.
36+
<!--
37+
38+
### Security
39+
40+
- A bullet item for the Security category.
41+
42+
-->

ggshield/verticals/secret/docker.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
import json
44
import re
55
import subprocess
6+
import sys
67
import tarfile
78
from contextlib import contextmanager
89
from dataclasses import dataclass
910
from pathlib import Path
10-
from typing import TYPE_CHECKING
11+
from typing import TYPE_CHECKING, BinaryIO, cast
1112

1213
from click import UsageError
1314

@@ -101,10 +102,9 @@ def is_longer_than(self, max_utf8_encoded_size: int) -> bool:
101102
self._content,
102103
self._utf8_encoded_size,
103104
) = Scannable._is_file_longer_than(
104-
fp, max_utf8_encoded_size # type:ignore
105+
cast(BinaryIO, fp),
106+
max_utf8_encoded_size,
105107
)
106-
# mypy complains that fp is IO[bytes] but _is_file_longer_than() expects
107-
# BinaryIO. They are compatible, ignore the error.
108108
return result
109109

110110
def _read_content(self) -> None:
@@ -323,6 +323,8 @@ def _run_docker_command(command: List[str], timeout: int) -> bool:
323323
subprocess.run(
324324
command,
325325
check=True,
326+
stdout=sys.stderr,
327+
stderr=sys.stderr,
326328
timeout=timeout,
327329
)
328330
return True
@@ -348,6 +350,7 @@ def docker_save_to_tmp(image_name: str, destination_path: Path, timeout: int) ->
348350
subprocess.run(
349351
command,
350352
check=True,
353+
stdout=sys.stderr,
351354
stderr=subprocess.PIPE,
352355
timeout=timeout,
353356
)

tests/unit/verticals/secret/test_scan_docker.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import re
22
import subprocess
3+
import sys
34
from pathlib import Path
45
from typing import Dict, List
56
from unittest.mock import patch
@@ -124,6 +125,8 @@ def test_docker_pull_image_success(self):
124125
call.assert_called_once_with(
125126
["docker", "pull", "ggshield-non-existant"],
126127
check=True,
128+
stdout=sys.stderr,
129+
stderr=sys.stderr,
127130
timeout=DOCKER_TIMEOUT,
128131
)
129132

@@ -149,16 +152,21 @@ def test_docker_pull_image_timeout(self):
149152
docker_pull_image("ggshield-non-existant", DOCKER_TIMEOUT)
150153

151154
def test_docker_pull_image_platform_fallback(self):
152-
with patch(
153-
"subprocess.run", side_effect=subprocess.CalledProcessError(1, cmd=[])
154-
) as call, pytest.raises(
155-
click.UsageError,
156-
match='Image "ggshield-non-existant" not found',
155+
with (
156+
patch(
157+
"subprocess.run", side_effect=subprocess.CalledProcessError(1, cmd=[])
158+
) as call,
159+
pytest.raises(
160+
click.UsageError,
161+
match='Image "ggshield-non-existant" not found',
162+
),
157163
):
158164
docker_pull_image("ggshield-non-existant", DOCKER_TIMEOUT)
159165
call.assert_called_once_with(
160166
["docker", "pull", "ggshield-non-existant", "--platform=linux/amd64"],
161167
check=True,
168+
stdout=sys.stderr,
169+
stderr=sys.stderr,
162170
timeout=DOCKER_TIMEOUT,
163171
)
164172

@@ -180,7 +188,8 @@ def test_docker_save_image_success(self):
180188
str(self.TMP_ARCHIVE),
181189
],
182190
check=True,
183-
stderr=-1,
191+
stderr=subprocess.PIPE,
192+
stdout=sys.stderr,
184193
timeout=DOCKER_TIMEOUT,
185194
)
186195

0 commit comments

Comments
 (0)