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

nethsm: Read host from NETHSM_HOST if not set #617

Merged
merged 1 commit into from
Jan 20, 2025
Merged
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
29 changes: 19 additions & 10 deletions pynitrokey/cli/nethsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import datetime
import json
import mimetypes
import os
import os.path
import sys
from dataclasses import dataclass
Expand Down Expand Up @@ -85,16 +86,14 @@ def print_table(headers: Sequence[str], data: Iterable[Sequence[Any]]) -> None:

@dataclass
class Config:
host: str
host: Optional[str]
username: Optional[str]
password: Optional[str]
verify_tls: bool


@click.group()
@click.option(
"-h", "--host", "host", required=True, help="Set the host of the NetHSM API"
)
@click.option("-h", "--host", "host", help="Set the host of the NetHSM API")
@click.option("-u", "--username", "username", help="The NetHSM user name")
@click.option("-p", "--password", "password", help="The NetHSM password")
@click.option(
Expand All @@ -105,12 +104,13 @@ class Config:
@click.pass_context
def nethsm(
ctx: Context,
host: str,
host: Optional[str],
username: Optional[str],
password: Optional[str],
verify_tls: bool,
) -> None:
"""Interact with NetHSM devices, see subcommands."""

ctx.obj = Config(
host=host, username=username, password=password, verify_tls=verify_tls
)
Expand All @@ -121,22 +121,31 @@ def connect(ctx: Context, require_auth: bool = True) -> Iterator[NetHSM]:
config = ctx.obj
assert isinstance(config, Config)

host = config.host
if host is None:
v = "NETHSM_HOST"
if v not in os.environ:
raise CliException(
"Missing NetHSM host: set the --host option or the "
f"{v} environment variable",
support_hint=False,
)
host = os.environ[v]

auth = None
if require_auth:
username = config.username
password = config.password
if not username:
username = prompt_str(f"[auth] User name for NetHSM {config.host}")
username = prompt_str(f"[auth] User name for NetHSM {host}")
if not password:
password = prompt_str(
f"[auth] Password for user {username} on NetHSM {config.host}",
f"[auth] Password for user {username} on NetHSM {host}",
hide_input=True,
)
auth = Authentication(username=username, password=password)

with nethsm_sdk.connect(
config.host, auth=auth, verify_tls=config.verify_tls
) as nethsm:
with nethsm_sdk.connect(host, auth=auth, verify_tls=config.verify_tls) as nethsm:
try:
yield nethsm
except nethsm_sdk.NetHSMError as e:
Expand Down
Loading