Skip to content

Commit

Permalink
Update nethsm dependency
Browse files Browse the repository at this point in the history
This update pulls in two relevant changes:
- The auto-generated client is only loaded if required.  This improves
  execution times significantly, especially for simple commands.
- Key certificates are now always stored as raw bytes, not as strings,
  so the get-certificate and set-certificate commands had to be updated.

On my machine, this reduces the execution time for simple commands like
version or --help from 0.9 s to 0.3 s.
  • Loading branch information
robin-nitrokey committed Nov 14, 2023
1 parent c50a59c commit 623960e
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 22 deletions.
23 changes: 2 additions & 21 deletions pynitrokey/cli/nethsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,12 +919,6 @@ def get_api_or_key_id(api, key_id):
"-a", "--api", is_flag=True, help="Set the certificate for the NetHSM TLS interface"
)
@click.option("-k", "--key-id", help="The ID of the key to set the certificate for")
@click.option(
"-m",
"--mime-type",
type=click.Choice(KEY_CERTIFICATE_MIME_TYPES),
help="The MIME type of the certificate (only with --key-id)",
)
@click.argument("filename")
@click.pass_context
def set_certificate(ctx, api, key_id, mime_type, filename):
Expand All @@ -940,25 +934,11 @@ def set_certificate(ctx, api, key_id, mime_type, filename):
with connect(ctx) as nethsm:
with open(filename, "rb") as f:
if key_id:
if not mime_type:
(mime_type, _) = mimetypes.guess_type(filename)
if not mime_type:
raise click.ClickException(
f"Failed to detect MIME type for {filename}. Use --mime-type to "
"explicitly set the MIME type."
)
if mime_type not in KEY_CERTIFICATE_MIME_TYPES:
raise click.ClickException(
f"Unsupported certificate mime type {mime_type} detected for "
f"{filename}"
)
nethsm.set_key_certificate(key_id, f, mime_type)
print(
f"Updated the certificate for key {key_id} on NetHSM {nethsm.host}"
)
else:
if mime_type:
raise click.ClickException("--mime-type cannot be used with --api")
nethsm.set_certificate(f)
print(f"Updated the API certificate for NetHSM {nethsm.host}")

Expand All @@ -983,9 +963,10 @@ def get_certificate(ctx, api, key_id):
with connect(ctx) as nethsm:
if key_id:
cert = nethsm.get_key_certificate(key_id)
print(cert)
else:
cert = nethsm.get_certificate()
print(cert)
print(cert)


@nethsm.command()
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ dependencies = [
"protobuf >=3.17.3, < 4.0.0",
"click-aliases",
"semver",
"nethsm >= 0.4,<0.5",
"nethsm @ git+https://github.com/Nitrokey/nethsm-sdk-py@a662beb596a2402724e15f15e09b7c57837441fe",
]
dynamic = ["version", "description"]

Expand Down

0 comments on commit 623960e

Please sign in to comment.