Skip to content

Commit 5f34673

Browse files
refactor: implement review feedback - simplify SSL verification config
Implement all review feedback from @agateau-gg: - Remove allow_self_signed field from UserConfig, keep only insecure - Convert allow_self_signed to insecure during config loading - Use ui.display_warning() directly instead of deprecation_messages - Shorten all deprecation warning messages - Reorder --insecure help text (functionality before warning) - Update v1 config converter to map allow_self_signed to insecure - Simplify code by eliminating dual field state This makes the code cleaner and prevents inconsistent state where allow_self_signed and insecure could have different values. Issue: SCRT-5952
1 parent 8b5bcd3 commit 5f34673

File tree

8 files changed

+24
-57
lines changed

8 files changed

+24
-57
lines changed

ggshield/__main__.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ def exit_code(ctx: click.Context, exit_code: int, **kwargs: Any) -> int:
8181
def cli(
8282
ctx: click.Context,
8383
*,
84-
allow_self_signed: Optional[bool],
8584
insecure: Optional[bool],
8685
config_path: Optional[Path],
8786
**kwargs: Any,
@@ -99,14 +98,10 @@ def cli(
9998
ensure_level(ui.Level.VERBOSE)
10099

101100
# Update SSL verification settings in the config
102-
# Both --allow-self-signed and --insecure disable SSL verification
103-
# If either is set, we enable both for backward compatibility
104101
# TODO: this should be reworked: if a command which writes the config is called with
105-
# --allow-self-signed or --insecure, the config will contain the enabled flag.
106-
if allow_self_signed or insecure:
107-
# Set both flags to maintain consistency
108-
user_config.allow_self_signed = True
109-
user_config.insecure = True
102+
# --insecure, the config will contain `insecure: true`.
103+
if insecure:
104+
user_config.insecure = insecure
110105

111106
ctx_obj.config._dotenv_vars = load_dot_env()
112107

ggshield/cmd/utils/common_options.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,26 +141,24 @@ def allow_self_signed_callback(
141141
) -> Optional[bool]:
142142
if value:
143143
ui.display_warning(
144-
"The --allow-self-signed option is deprecated and will be removed in a future version. "
145-
"Use --insecure instead, which more accurately describes what this option does "
146-
"(it disables all certificate validation, not just allows self-signed certificates)."
144+
"The --allow-self-signed option is deprecated. Use --insecure instead."
147145
)
148-
return create_config_callback("allow_self_signed")(ctx, param, value)
146+
return create_config_callback("insecure")(ctx, param, value)
149147

150148

151149
_allow_self_signed_option = click.option(
152150
"--allow-self-signed",
153151
is_flag=True,
154152
default=None,
155-
help="(Deprecated: use --insecure) Ignore ssl verification.",
153+
help="Deprecated: use --insecure.",
156154
callback=allow_self_signed_callback,
157155
)
158156

159157
_insecure_option = click.option(
160158
"--insecure",
161159
is_flag=True,
162160
default=None,
163-
help="WARNING: using this option makes the transfer insecure, by skipping all certificate's validation checks.",
161+
help="Skip all certificate verification checks. WARNING: this option makes the transfer insecure.",
164162
callback=create_config_callback("insecure"),
165163
)
166164

ggshield/core/client.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ def create_client_from_config(config: Config) -> GGClient:
5151
return create_client(
5252
api_key,
5353
api_url,
54-
allow_self_signed=config.user_config.allow_self_signed
55-
or config.user_config.insecure,
54+
allow_self_signed=config.user_config.insecure,
5655
callbacks=callbacks,
5756
)
5857

ggshield/core/config/user_config.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ class UserConfig(FilteredConfig):
8787
instance: Optional[str] = None
8888
exit_zero: bool = False
8989
verbose: bool = False
90-
allow_self_signed: bool = False
9190
insecure: bool = False
9291
max_commits_for_hook: int = 50
9392
secret: SecretConfig = field(default_factory=SecretConfig)
@@ -199,7 +198,7 @@ def _load_config_dict(
199198
if dash_keys:
200199
_warn_about_dash_keys(config_path, dash_keys)
201200
_fix_ignore_known_secrets(dct)
202-
_check_allow_self_signed_deprecation(dct, config_path, deprecation_messages)
201+
_fix_allow_self_signed(dct, config_path)
203202
elif config_version == 1:
204203
deprecation_messages.append(
205204
f"{config_path} uses a deprecated configuration file format."
@@ -228,16 +227,14 @@ def _fix_ignore_known_secrets(data: Dict[str, Any]) -> None:
228227
secret_dct[_IGNORE_KNOWN_SECRETS_KEY] = value
229228

230229

231-
def _check_allow_self_signed_deprecation(
232-
data: Dict[str, Any], config_path: Path, deprecation_messages: List[str]
233-
) -> None:
234-
"""Check if allow_self_signed is used and add a deprecation warning."""
235-
if data.get("allow_self_signed"):
236-
deprecation_messages.append(
237-
f"{config_path}: The 'allow_self_signed' option is deprecated and will be removed in a future version. "
238-
"Use 'insecure: true' instead, which more accurately describes what this option does "
239-
"(it disables all certificate validation, not just allows self-signed certificates)."
230+
def _fix_allow_self_signed(data: Dict[str, Any], config_path: Path) -> None:
231+
"""Convert allow_self_signed to insecure and display a deprecation warning."""
232+
if insecure := data.pop("allow_self_signed", None):
233+
ui.display_warning(
234+
f"{config_path}: The 'allow_self_signed' option is deprecated. "
235+
"Use 'insecure: true' instead."
240236
)
237+
data["insecure"] = insecure
241238

242239

243240
def _warn_about_dash_keys(config_path: Path, dash_keys: Set[str]) -> None:

ggshield/core/config/v1_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def copy_if_set(dct: Dict[str, Any], dst_key: str, src_key: Optional[str] = None
6060
copy_if_set(dct, "instance")
6161
copy_if_set(dct, "exit_zero")
6262
copy_if_set(dct, "verbose")
63-
copy_if_set(dct, "allow_self_signed")
63+
copy_if_set(dct, "insecure", "allow_self_signed")
6464
copy_if_set(dct, "max_commits_for_hook")
6565

6666
return dct

tests/unit/cmd/scan/test_path.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -236,18 +236,6 @@ def test_instance_option(self, cli_fs_runner):
236236

237237
@pytest.mark.parametrize("position", [0, 1, 2, 3, 4])
238238
def test_ssl_verify(self, cli_fs_runner, position):
239-
self.create_files()
240-
241-
cmd = ["secret", "scan", "path", "file1"]
242-
cmd.insert(position, "--allow-self-signed")
243-
244-
with patch("ggshield.core.client.GGClient") as client_mock:
245-
cli_fs_runner.invoke(cli, cmd)
246-
_, kwargs = client_mock.call_args
247-
assert kwargs["session"].verify is False
248-
249-
@pytest.mark.parametrize("position", [0, 1, 2, 3, 4])
250-
def test_ssl_verify_insecure(self, cli_fs_runner, position):
251239
"""
252240
GIVEN the --insecure flag
253241
WHEN running the path scan command

tests/unit/cmd/test_status.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,6 @@ def get_api_status(env, instance=None):
122122

123123
@pytest.mark.parametrize("verify", [True, False])
124124
def test_ssl_verify(cli_fs_runner, verify):
125-
cmd = ["api-status"] if verify else ["--allow-self-signed", "api-status"]
126-
127-
with mock.patch("ggshield.core.client.GGClient") as client_mock:
128-
cli_fs_runner.invoke(cli, cmd)
129-
_, kwargs = client_mock.call_args
130-
assert kwargs["session"].verify == verify
131-
132-
133-
@pytest.mark.parametrize("verify", [True, False])
134-
def test_ssl_verify_insecure(cli_fs_runner, verify):
135125
"""
136126
GIVEN the --insecure flag
137127
WHEN running the api-status command

tests/unit/core/config/test_user_config.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,11 @@ def test_can_load_ignored_known_secrets_from_secret(self, local_config_path):
294294
config, _ = UserConfig.load(local_config_path)
295295
assert config.secret.ignore_known_secrets
296296

297-
def test_allow_self_signed_deprecation_warning(self, local_config_path):
297+
def test_allow_self_signed_deprecation_warning(self, local_config_path, capsys):
298298
"""
299299
GIVEN a config file containing allow_self_signed: true
300300
WHEN loading the config
301-
THEN a deprecation warning is added to deprecation_messages
301+
THEN it is converted to insecure and a deprecation warning is displayed
302302
"""
303303
write_yaml(
304304
local_config_path,
@@ -308,11 +308,11 @@ def test_allow_self_signed_deprecation_warning(self, local_config_path):
308308
},
309309
)
310310
config, _ = UserConfig.load(local_config_path)
311-
assert config.allow_self_signed is True
312-
assert len(config.deprecation_messages) == 1
313-
assert "allow_self_signed" in config.deprecation_messages[0]
314-
assert "deprecated" in config.deprecation_messages[0]
315-
assert "insecure" in config.deprecation_messages[0]
311+
assert config.insecure is True
312+
captured = capsys.readouterr()
313+
assert "allow_self_signed" in captured.err
314+
assert "deprecated" in captured.err
315+
assert "insecure" in captured.err
316316

317317
def test_bad_local_config(self, local_config_path, global_config_path):
318318
"""

0 commit comments

Comments
 (0)