Fix check of empty service name for `derivepassphrase vault`
Marco Ricci

Marco Ricci commited on 2025-01-01 02:02:43
Zeige 1 geänderte Dateien mit 13 Einfügungen und 12 Löschungen.


We asserted in c1bf00eadd1bf733ac25a25eafbe110d61936c54 that an empty
service name is both diagnosed, then treated as if it were missing later
on.  This wasn't actually strictly true: the checks for a missing or
superfluous service name tested for truthy service names instead of
non-missing service names, and the key selection and passphrase entry
dialogs happen before the check for a non-empty service name.  Fix these
two issues by checking for non-missing service names in the former case,
and moving the non-empty service name check further to the front in the
latter case.
... ...
@@ -2911,7 +2911,7 @@ def derivepassphrase_vault(  # noqa: C901,PLR0912,PLR0913,PLR0914,PLR0915
2911 2911
     sv_or_global_options = options_in_group[PassphraseGenerationOption]
2912 2912
     for param in sv_or_global_options:
2913 2913
         if is_param_set(param) and not (
2914
-            service or is_param_set(params_by_str['--config'])
2914
+            service is not None or is_param_set(params_by_str['--config'])
2915 2915
         ):
2916 2916
             err_msg = _msg.TranslatedString(
2917 2917
                 _msg.ErrMsgTemplate.PARAMS_NEEDS_SERVICE_OR_CONFIG,
... ...
@@ -2921,7 +2921,7 @@ def derivepassphrase_vault(  # noqa: C901,PLR0912,PLR0913,PLR0914,PLR0915
2921 2921
             raise click.UsageError(str(err_msg))  # noqa: DOC501
2922 2922
     sv_options = [params_by_str['--notes'], params_by_str['--delete']]
2923 2923
     for param in sv_options:
2924
-        if is_param_set(param) and not service:
2924
+        if is_param_set(param) and not service is not None:
2925 2925
             err_msg = _msg.TranslatedString(
2926 2926
                 _msg.ErrMsgTemplate.PARAMS_NEEDS_SERVICE,
2927 2927
                 param=param.opts[0],
... ...
@@ -2934,7 +2934,7 @@ def derivepassphrase_vault(  # noqa: C901,PLR0912,PLR0913,PLR0914,PLR0915
2934 2934
         *options_in_group[StorageManagementOption],
2935 2935
     ]
2936 2936
     for param in no_sv_options:
2937
-        if is_param_set(param) and service:
2937
+        if is_param_set(param) and service is not None:
2938 2938
             err_msg = _msg.TranslatedString(
2939 2939
                 _msg.ErrMsgTemplate.PARAMS_NO_SERVICE,
2940 2940
                 param=param.opts[0],
... ...
@@ -3215,10 +3215,18 @@ def derivepassphrase_vault(  # noqa: C901,PLR0912,PLR0913,PLR0914,PLR0915
3215 3215
             },
3216 3216
             cast(
3217 3217
                 dict[str, Any],
3218
-                configuration['services'].get(service or '', {}),
3218
+                configuration['services'].get(service, {}) if service else {},
3219 3219
             ),
3220 3220
             cast(dict[str, Any], configuration.get('global', {})),
3221 3221
         )
3222
+        if not store_config_only and not service:
3223
+            err_msg = _msg.TranslatedString(
3224
+                _msg.ErrMsgTemplate.SERVICE_REQUIRED,
3225
+                service_metavar=_msg.TranslatedString(
3226
+                    _msg.Label.VAULT_METAVAR_SERVICE
3227
+                ),
3228
+            )
3229
+            raise click.UsageError(str(err_msg))
3222 3230
         if use_key:
3223 3231
             try:
3224 3232
                 key = base64.standard_b64encode(
... ...
@@ -3350,14 +3358,7 @@ def derivepassphrase_vault(  # noqa: C901,PLR0912,PLR0913,PLR0914,PLR0915
3350 3358
             ), f'Invalid vault configuration: {configuration!r}'
3351 3359
             put_config(configuration)
3352 3360
         else:
3353
-            if not service:
3354
-                err_msg = _msg.TranslatedString(
3355
-                    _msg.ErrMsgTemplate.SERVICE_REQUIRED,
3356
-                    service_metavar=_msg.TranslatedString(
3357
-                        _msg.Label.VAULT_METAVAR_SERVICE
3358
-                    ),
3359
-                )
3360
-                raise click.UsageError(str(err_msg))
3361
+            assert service is not None
3361 3362
             kwargs: dict[str, Any] = {
3362 3363
                 k: v
3363 3364
                 for k, v in settings.items()
3364 3365