Skip to content

Commit

Permalink
nethsm: Add support for namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
robin-nitrokey committed Jul 17, 2024
1 parent 50b60ed commit 1d33350
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
51 changes: 49 additions & 2 deletions pynitrokey/cli/nethsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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}")

Expand All @@ -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(
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 >= 1.1.0,<2",
"nethsm >=1.2.0, <2",
]
dynamic = ["version", "description"]

Expand Down

0 comments on commit 1d33350

Please sign in to comment.