Skip to content

Commit

Permalink
Decide based on state if authentication is required for restore
Browse files Browse the repository at this point in the history
  • Loading branch information
mmerklinger committed Jul 24, 2024
1 parent 174d462 commit c2c9e25
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pynitrokey/cli/nethsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import click
import nethsm as nethsm_sdk
from click import Context
from nethsm import Authentication, Base64, NetHSM
from nethsm import Authentication, Base64, NetHSM, State
from nethsm.backup import EncryptedBackup

from pynitrokey.cli.exceptions import CliException
Expand Down Expand Up @@ -1415,7 +1415,15 @@ def restore(
support_hint=False,
)

require_auth = None
with connect(ctx, require_auth=False) as nethsm:
state = nethsm.get_state()
if state == State.UNPROVISIONED:
require_auth = False
elif state == State.OPERATIONAL:
require_auth = True

with connect(ctx, require_auth=require_auth) as nethsm:
with open(filename, "rb") as f:
nethsm.restore(data, backup_passphrase, system_time)
print(f"Backup restored on NetHSM {nethsm.host}")
Expand Down

0 comments on commit c2c9e25

Please sign in to comment.