Skip to content

Commit 19a5e5c

Browse files
fix: keep docker stdout off JSON output
1 parent 1b8b4fa commit 19a5e5c

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

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: 23 additions & 14 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
@@ -121,11 +122,13 @@ class TestDockerPull:
121122
def test_docker_pull_image_success(self):
122123
with patch("subprocess.run") as call:
123124
docker_pull_image("ggshield-non-existant", DOCKER_TIMEOUT)
124-
call.assert_called_once_with(
125-
["docker", "pull", "ggshield-non-existant"],
126-
check=True,
127-
timeout=DOCKER_TIMEOUT,
128-
)
125+
call.assert_called_once()
126+
args, kwargs = call.call_args
127+
assert args == (["docker", "pull", "ggshield-non-existant"],)
128+
assert kwargs["check"] is True
129+
assert kwargs["timeout"] == DOCKER_TIMEOUT
130+
assert kwargs["stdout"] is sys.stderr
131+
assert kwargs["stderr"] is sys.stderr
129132

130133
def test_docker_pull_image_non_exist(self):
131134
with patch(
@@ -149,11 +152,14 @@ 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(
@@ -171,18 +177,21 @@ def test_docker_save_image_success(self):
171177
docker_save_to_tmp(
172178
"ggshield-non-existant", self.TMP_ARCHIVE, DOCKER_TIMEOUT
173179
)
174-
call.assert_called_once_with(
180+
call.assert_called_once()
181+
args, kwargs = call.call_args
182+
assert args == (
175183
[
176184
"docker",
177185
"save",
178186
"ggshield-non-existant:latest",
179187
"-o",
180188
str(self.TMP_ARCHIVE),
181189
],
182-
check=True,
183-
stderr=-1,
184-
timeout=DOCKER_TIMEOUT,
185190
)
191+
assert kwargs["check"] is True
192+
assert kwargs["stderr"] == subprocess.PIPE
193+
assert kwargs["stdout"] is sys.stderr
194+
assert kwargs["timeout"] == DOCKER_TIMEOUT
186195

187196
def test_docker_save_image_does_not_exist(self):
188197
with patch(

0 commit comments

Comments
 (0)