Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use config field availability information from the sdk #577

Merged
merged 5 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 33 additions & 28 deletions pynitrokey/cli/nk3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,23 +172,39 @@ def set_config(ctx: Context, key: str, value: str, force: bool, dry_run: bool) -
"""

with ctx.connect_device() as device:
# before the confirmation prompt, check if the config value is supported
if not device.admin.has_config(key):
config_fields = device.admin.list_available_fields()

field_metadata = None
for field in config_fields:
if field.name == key:
field_metadata = field

if field_metadata is None:
print(
"Changing configuration values can have unexpected side effects, including data loss.",
file=sys.stderr,
)
print(
"This should only be used for development and testing.",
file=sys.stderr,
)
if not force:
raise CliException(
"Unknown config values can only be set if the --force/-f flag is set. Aborting.",
support_hint=False,
)

if (
not force
and field_metadata is not None
and not field_metadata.ty.is_valid(value)
):
raise CliException(
f"The configuration option '{key}' is not supported by the device.",
f"Invalid config value for {field}: expected {field_metadata.ty}, got `{value}`. Unknown config values can only be set if the --force/-f flag is set. Aborting.",
support_hint=False,
)

# config fields that don’t have side effects
whitelist = [
"fido.disable_skip_up_timeout",
]
requires_touch = False
requires_reboot = False

if key == "opcard.use_se050_backend":
requires_touch = True
requires_reboot = True
print(
"This configuration values determines whether the OpenPGP Card "
"application uses a software implementation or the secure element.",
Expand All @@ -200,39 +216,28 @@ def set_config(ctx: Context, key: str, value: str, force: bool, dry_run: bool) -
"user data currently stored on the device.",
file=sys.stderr,
)
elif key not in whitelist:
pass
print(
"Changing configuration values can have unexpected side effects, including data loss.",
file=sys.stderr,
)
elif field_metadata is not None and field_metadata.destructive:
print(
"This should only be used for development and testing.",
"This configuration value may delete data on your device",
file=sys.stderr,
)

if not force:
raise CliException(
"Unknown config values can only be set if the --force/-f flag is set. Aborting.",
support_hint=False,
)

if key not in whitelist:
if field_metadata is not None and field_metadata.destructive:
click.confirm("Do you want to continue anyway?", abort=True)

if dry_run:
print("Stopping dry run.", file=sys.stderr)
raise click.Abort()

if requires_touch:
if field_metadata is not None and field_metadata.requires_touch_confirmation:
print(
"Press the touch button to confirm the configuration change.",
file=sys.stderr,
)

device.admin.set_config(key, value)

if requires_reboot:
if field_metadata is not None and field_metadata.requires_reboot:
print("Rebooting device to apply config change.")
device.reboot()

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ dependencies = [
"fido2 >=1.1.2,<2",
"intelhex",
"nkdfu",
"nitrokey ~= 0.2.0rc1",
"nitrokey ~=0.2.1",
"python-dateutil ~= 2.7.0",
"pyusb",
"requests",
Expand Down