From 1d33350b48e3a33aef7ec3212766bbf32a3da5ef Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Wed, 17 Apr 2024 15:12:29 +0200 Subject: [PATCH] nethsm: Add support for namespaces --- pynitrokey/cli/nethsm.py | 51 ++++++++++++++++++++++++++++++++++++++-- pyproject.toml | 2 +- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/pynitrokey/cli/nethsm.py b/pynitrokey/cli/nethsm.py index 9d2ae5cb..a7d730b6 100644 --- a/pynitrokey/cli/nethsm.py +++ b/pynitrokey/cli/nethsm.py @@ -296,9 +296,15 @@ def get_user(ctx: Context, user_id: str) -> None: help="The passphrase of the new user", ) @click.option("-u", "--user-id", help="The user ID of the new user") +@click.option("-N", "--namespace", help="The namespace of the new user") @click.pass_context def add_user( - ctx: Context, real_name: str, role: str, passphrase: str, user_id: Optional[str] + ctx: Context, + real_name: str, + role: str, + passphrase: str, + user_id: Optional[str], + namespace: Optional[str], ) -> None: """Create a new user on the NetHSM. @@ -310,7 +316,7 @@ def add_user( role.""" with connect(ctx) as nethsm: user_id = nethsm.add_user( - real_name, nethsm_sdk.Role.from_string(role), passphrase, user_id + real_name, nethsm_sdk.Role.from_string(role), passphrase, user_id, namespace ) print(f"User {user_id} added to NetHSM {nethsm.host}") @@ -328,6 +334,47 @@ def delete_user(ctx: Context, user_id: str) -> None: print(f"User {user_id} deleted on NetHSM {nethsm.host}") +@nethsm.command() +@click.pass_context +def list_namespaces(ctx: Context) -> None: + """List all namespaces on the NetHSM. + + This command requires authentication as a user with the Administrator + role.""" + with connect(ctx) as nethsm: + namespaces = nethsm.list_namespaces() + + print(f"Namespaces on NetHSM {nethsm.host}:") + for namespace in namespaces: + print(f"- {namespace}") + + +@nethsm.command() +@click.argument("namespace") +@click.pass_context +def add_namespace(ctx: Context, namespace: str) -> None: + """Add a new namespace on the NetHSM. + + This command requires authentication as a user with the Administrator + role.""" + with connect(ctx) as nethsm: + nethsm.add_namespace(namespace) + print(f"Namespace {namespace} added to NetHSM {nethsm.host}") + + +@nethsm.command() +@click.argument("namespace") +@click.pass_context +def delete_namespace(ctx: Context, namespace: str) -> None: + """Delete a namespace on the NetHSM. + + This command requires authentication as a user with the Administrator + role.""" + with connect(ctx) as nethsm: + nethsm.delete_namespace(namespace) + print(f"Namespace {namespace} deleted on NetHSM {nethsm.host}") + + @nethsm.command() @click.option("-u", "--user-id", help="The user ID of the user") @click.option( diff --git a/pyproject.toml b/pyproject.toml index 67b09f5e..5cbc358f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -40,7 +40,7 @@ dependencies = [ "protobuf >=3.17.3, < 4.0.0", "click-aliases", "semver", - "nethsm >= 1.1.0,<2", + "nethsm >=1.2.0, <2", ] dynamic = ["version", "description"]