Repository navigation
Expand file tree
/
Copy pathtest_cli_exit_contract.py
More file actions
293 lines (237 loc) · 11.8 KB
/
Copy pathtest_cli_exit_contract.py
File metadata and controls
293 lines (237 loc) · 11.8 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
"""Exercise the public exit contract through argument parsing and the CLI flow."""
import json
import os
import signal
import subprocess
import sys
from contextlib import nullcontext
from types import SimpleNamespace
from unittest.mock import MagicMock, Mock
import pytest
from socketsecurity import socketcli
from socketsecurity.config import CliConfig, create_argument_parser, parse_files
from socketsecurity.core import Core
from socketsecurity.core.classes import Diff, Issue, Package
from socketsecurity.exit_codes import InfrastructureError
POLICIES = [
pytest.param([], 3, id="default"),
pytest.param(["--exit-code-on-api-error", "100"], 100, id="remap"),
pytest.param(["--exit-code-on-api-error", "0"], 0, id="remap-zero"),
pytest.param(["--disable-blocking"], 0, id="nonblocking"),
pytest.param(["--disable-blocking", "--exit-code-on-api-error", "100"], 0, id="both"),
]
TOKEN_VARS = (
"SOCKET_SECURITY_API_KEY", "SOCKET_SECURITY_API_TOKEN", "SOCKET_API_KEY", "SOCKET_API_TOKEN",
"GH_API_TOKEN", "GITLAB_TOKEN",
)
@pytest.fixture
def cli_runtime(monkeypatch):
for name in TOKEN_VARS:
monkeypatch.delenv(name, raising=False)
sdk = MagicMock()
sdk.org.get.return_value = {"organizations": {"org-id": {"slug": "acme", "plan": "enterprise"}}}
monkeypatch.setattr(socketcli, "build_socket_sdk", Mock(return_value=sdk))
monkeypatch.setattr(socketcli, "CliClient", Mock())
monkeypatch.setattr(socketcli, "StreamingLogs", lambda **kwargs: nullcontext(Mock()))
monkeypatch.setattr(socketcli, "apply_git_context", lambda config: (False, None))
monkeypatch.setattr(Core, "has_manifest_files", Mock(return_value=True))
monkeypatch.setattr(Core, "find_files", Mock(return_value=[]))
diff = Diff(id="scan")
scan = Mock(return_value=diff)
monkeypatch.setattr(Core, "create_new_diff", scan)
monkeypatch.setattr(socketcli.OutputHandler, "handle_output", Mock())
monkeypatch.setattr(socketcli.shutil, "which", lambda dep: f"/bin/{dep}")
def run(flags=(), *, token=True):
argv = ["socketcli", "--enable-diff", "--repo", "repo", "--branch", "feature"]
if token:
argv += ["--api-token", "test"]
monkeypatch.setattr(sys, "argv", [*argv, *flags])
with pytest.raises(SystemExit) as result:
socketcli.cli()
return result.value.code
return SimpleNamespace(sdk=sdk, scan=scan, diff=diff, run=run)
@pytest.mark.parametrize("policy, infrastructure_code", POLICIES)
@pytest.mark.parametrize("stage", [
"socket-token", "github-token", "gitlab-token", "reach-dependency", "reach-plan", "reach-org",
])
def test_setup_failures_follow_exit_policy(cli_runtime, monkeypatch, policy, infrastructure_code, stage):
flags = list(policy)
if stage.endswith("-token") and stage != "socket-token":
flags += ["--scm", stage.removesuffix("-token")]
elif stage.startswith("reach-"):
flags += ["--reach"]
if stage == "reach-dependency":
monkeypatch.setattr(socketcli.shutil, "which", lambda dep: None)
elif stage == "reach-plan":
cli_runtime.sdk.org.get.return_value["organizations"]["org-id"]["plan"] = "free"
elif stage == "reach-org":
cli_runtime.sdk.org.get.return_value = {"organizations": {}}
assert cli_runtime.run(flags, token=stage != "socket-token") == infrastructure_code
cli_runtime.scan.assert_not_called()
@pytest.mark.parametrize("policy, infrastructure_code", POLICIES)
@pytest.mark.parametrize("outcome, expected", [
("clean", 0), ("warning", 0), ("new-blocker", 1), ("existing-blocker", 0),
("strict-existing-blocker", 1), ("infrastructure", 3), ("interrupt", 130),
])
def test_scan_outcomes_follow_exit_policy(cli_runtime, policy, infrastructure_code, outcome, expected):
flags = list(policy)
if outcome in ("new-blocker", "warning"):
cli_runtime.diff.new_alerts = [Issue(error=outcome == "new-blocker", warn=outcome == "warning")]
elif outcome.endswith("existing-blocker"):
cli_runtime.diff.unchanged_alerts = [Issue(error=True)]
if outcome.startswith("strict-"):
flags.append("--strict-blocking")
elif outcome == "infrastructure":
cli_runtime.scan.side_effect = RuntimeError("unexpected scan failure")
expected = infrastructure_code
elif outcome == "interrupt":
cli_runtime.scan.side_effect = KeyboardInterrupt()
if "--disable-blocking" in policy:
expected = 0
assert cli_runtime.run(flags) == expected
cli_runtime.scan.assert_called_once()
@pytest.mark.parametrize("policy, infrastructure_code", POLICIES)
@pytest.mark.parametrize("flags", [
["--files", "not-json"], ["--files", "null"], ["--files", "{}"],
["--files", "[1]"], ["--timeout", "0"], ["--timeout", "-1"],
])
def test_usage_errors_fail_before_setup(cli_runtime, policy, infrastructure_code, flags):
# Missing credentials must not hide invalid input, even in nonblocking mode.
assert cli_runtime.run([*policy, *flags], token=False) == 2
socketcli.build_socket_sdk.assert_not_called()
@pytest.mark.parametrize("setting", [
{"strict_blocking": "false"}, {"timeout": 0}, {"timeout": 1.5},
{"sarif_scope": "invalid"}, {"target_path": 123}, {"socketcli": []},
])
def test_invalid_config_file_is_a_usage_error(tmp_path, setting):
path = tmp_path / "socketcli.json"
path.write_text(json.dumps(setting))
with pytest.raises(SystemExit) as result:
CliConfig.from_args(["--config", str(path), "--disable-blocking"])
assert result.value.code == 2
def test_config_cannot_enable_nonblocking_mode_with_a_string(tmp_path):
path = tmp_path / "socketcli.json"
path.write_text(json.dumps({"disable_blocking": "false"}))
with pytest.raises(SystemExit) as result:
CliConfig.from_args(["--config", str(path)])
assert result.value.code == 2
def test_cli_overrides_invalid_config_defaults_before_validation(tmp_path):
path = tmp_path / "socketcli.json"
path.write_text(json.dumps({"strict_blocking": "false", "timeout": 0, "sarif_scope": "invalid"}))
config = CliConfig.from_args([
"--config", str(path), "--strict-blocking", "--timeout", "30", "--sarif-scope", "diff",
])
assert config.strict_blocking is True
assert config.timeout == 30
assert config.sarif_scope == "diff"
@pytest.mark.parametrize("config_kind", ["directory", "invalid-utf8"])
def test_unreadable_config_is_a_usage_error(tmp_path, config_kind):
path = tmp_path / "socketcli.json"
if config_kind == "directory":
path.mkdir()
else:
path.write_bytes(b"\xff")
with pytest.raises(SystemExit) as result:
CliConfig.from_args(["--config", str(path), "--disable-blocking"])
assert result.value.code == 2
@pytest.mark.parametrize("files", [
'["package.json"]', "['package.json']", "'[\"package.json\"]'", ["package.json"],
])
def test_supported_file_list_formats_are_preserved(files):
assert parse_files(files) == ["package.json"]
def test_help_describes_the_current_exit_contract():
help_text = " ".join(create_argument_parser().format_help().split())
assert "warnings alone exit 0" in help_text
assert "Invalid flags or configuration still exit 2" in help_text
assert "Ctrl+C (130) are unaffected" in help_text
assert "5 for warnings" not in help_text
@pytest.mark.parametrize("policy, infrastructure_code", POLICIES)
def test_exit_policy_survives_config_file_removal(
cli_runtime, tmp_path, policy, infrastructure_code
):
path = tmp_path / "socketcli.json"
path.write_text("{}")
def fail_after_config_cleanup(*args, **kwargs):
path.unlink()
raise InfrastructureError("scan comparison failed")
cli_runtime.scan.side_effect = fail_after_config_cleanup
assert cli_runtime.run([*policy, "--config", str(path)]) == infrastructure_code
@pytest.mark.parametrize("policy, infrastructure_code", POLICIES)
def test_temporary_findings_override_does_not_change_error_policy(
cli_runtime, monkeypatch, policy, infrastructure_code
):
def fail_after_findings_override(handler, diff):
handler.config.disable_blocking = True
raise InfrastructureError("commit status failed")
monkeypatch.setattr(socketcli.OutputHandler, "handle_output", fail_after_findings_override)
assert cli_runtime.run(policy) == infrastructure_code
@pytest.mark.parametrize("policy, infrastructure_code", POLICIES)
def test_missing_existing_alert_data_reaches_cli_exit_policy(cli_runtime, policy, infrastructure_code):
package = Package(id="pkg", type="npm", name="pkg", version="1", score={}, alerts=[])
cli_runtime.sdk.fullscans.stream.return_value = SimpleNamespace(success=True, artifacts={})
# Run the real hydration step from the scan boundary without making any network request.
def compare(*args, **kwargs):
core = Core.__new__(Core)
core.sdk = cli_runtime.sdk
core.config = SimpleNamespace(org_slug="acme")
core.add_existing_alerts_from_full_scan({package.id: package}, "new")
cli_runtime.scan.side_effect = compare
assert cli_runtime.run([*policy, "--strict-blocking"]) == infrastructure_code
@pytest.mark.parametrize("policy, infrastructure_code", POLICIES)
def test_real_sigint_uses_documented_exit_status(policy, infrastructure_code):
script = """
import time
from socketsecurity import socketcli
def wait_for_interrupt(config=None):
print("READY", flush=True)
time.sleep(30)
socketcli.main_code = wait_for_interrupt
socketcli.cli()
"""
env = {key: value for key, value in os.environ.items() if key not in TOKEN_VARS}
with subprocess.Popen(
[sys.executable, "-c", script, "--api-token", "test", *policy],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, env=env,
) as process:
try:
assert process.stdout.readline().strip() == "READY"
process.send_signal(signal.SIGINT)
_, stderr = process.communicate(timeout=10)
assert process.returncode == (0 if "--disable-blocking" in policy else 130), stderr
finally:
if process.poll() is None:
process.kill()
process.communicate()
@pytest.mark.parametrize("output_flag", [[], ["--enable-json"], ["--enable-sarif"], ["--enable-gitlab-security"]])
@pytest.mark.parametrize("strict", [False, True])
def test_branch_full_scan_does_not_gate_on_findings(cli_runtime, monkeypatch, output_flag, strict):
monkeypatch.setenv("GH_API_TOKEN", "test")
monkeypatch.setenv("GITHUB_EVENT_NAME", "push")
monkeypatch.setenv("GITHUB_REPOSITORY", "acme/repo")
monkeypatch.setenv("BUILDKITE", "false")
monkeypatch.delenv("PR_NUMBER", raising=False)
full_scan = Mock(return_value=Diff(id="full", new_alerts=[Issue(error=True)]))
monkeypatch.setattr(Core, "create_full_scan_with_report_url", full_scan)
flags = ["--scm", "github", "--pr-number", "0", *output_flag]
if strict:
flags.append("--strict-blocking")
assert cli_runtime.run(flags) == 0
full_scan.assert_called_once()
cli_runtime.scan.assert_not_called()
@pytest.mark.parametrize("scm, event, action", [
("github", "pull_request", "closed"), ("github", "unsupported", ""),
("github", "pull_request_target", "opened"), ("gitlab", "unsupported", ""),
])
def test_scm_event_filters_skip_without_scanning(cli_runtime, monkeypatch, caplog, scm, event, action):
monkeypatch.setenv("GH_API_TOKEN", "test")
monkeypatch.setenv("GITLAB_TOKEN", "test")
monkeypatch.setenv("GITHUB_EVENT_NAME", event)
monkeypatch.setenv("EVENT_ACTION", action)
monkeypatch.setenv("CI_PIPELINE_SOURCE", event)
with caplog.at_level("INFO", logger="socketdev"):
assert cli_runtime.run(["--scm", scm]) == 0
cli_runtime.scan.assert_not_called()
assert any(
record.levelname == "WARNING" and "No scan ran" in record.getMessage()
for record in caplog.records
)