Skip to content
Merged
Show file tree
Hide file tree
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
135 changes: 134 additions & 1 deletion openadapt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,38 @@ def __getattr__(self, name):
return getattr(self._stream, name)


def _is_login(argv: list[str]) -> bool:
"""``login`` or ``setup login``, not its help."""
if argv[:1] == ["login"]:
rest = argv[1:]
elif argv[:2] == ["setup", "login"]:
rest = argv[2:]
else:
return False
return not any(token in ("-h", "--help") for token in rest)


def _option_values(argv: list[str], name: str) -> Optional[list[str]]:
"""Every value of a repeatable ``--name VALUE`` option, or None."""
values = []
for index, token in enumerate(argv):
if token == name and index + 1 < len(argv):
values.append(argv[index + 1])
elif token.startswith(name + "="):
values.append(token[len(name) + 1 :])
return values or None


def _option_value(argv: list[str], name: str) -> Optional[str]:
"""The value of ``--name VALUE`` or ``--name=VALUE`` in ``argv``, if any."""
for index, token in enumerate(argv):
if token == name and index + 1 < len(argv):
return argv[index + 1]
if token.startswith(name + "="):
return token[len(name) + 1 :]
return None


def _invoke_flow(argv: list[str]) -> int:
"""Invoke the canonical engine once and return its exit code."""
if not _python_supported():
Expand All @@ -365,6 +397,17 @@ def _invoke_flow(argv: list[str]) -> int:
alias_context = suppress_alias_notices()
with alias_context:
_correct_released_flow_x11_sonames()
if _is_login(argv):
code = int(flow_main(argv))
if code == 0:
from openadapt.seal_claim import after_login

after_login(
_option_value(argv, "--host"),
destination_kind=_option_value(argv, "--destination-kind"),
trusted_hosts=_option_values(argv, "--trusted-host"),
)
return code
if argv[:1] != ["tutorial"]:
return int(flow_main(argv))
# The tutorial is the first-run path and ends by printing next steps.
Expand Down Expand Up @@ -1435,6 +1478,26 @@ def flow(command_ctx: click.Context) -> None:
)


@main.command(
"login",
context_settings={
"ignore_unknown_options": True,
"allow_extra_args": True,
"help_option_names": [],
},
)
@click.pass_context
def login(command_ctx: click.Context) -> None:
"""Sign in to OpenAdapt Cloud: `openadapt login` (same as `setup login`).

Every argument passes to `openadapt-flow login` unchanged. After a
successful sign-in, Seals this computer published are claimed into your
workspace when Cloud supports it.
"""
_require_supported_python()
_run_flow(["login", *command_ctx.args])


@main.command(
"pull",
context_settings={
Expand Down Expand Up @@ -1926,7 +1989,8 @@ def seal(command_ctx: click.Context) -> None:
[RUN_DIR]` uploads the run's public Seal (counts, outcomes and digests
only) after you say yes and prints its link; `openadapt seal unpublish
RUN_DIR` takes it down, and `openadapt seal verify <link>` checks a hosted
Seal's signature here. Also `openadapt seal keys pin` to pin Cloud's Seal
Seal's signature here. `openadapt seal claim` puts the Seals you published
in your Cloud workspace after `openadapt login` (login does it for you). Also `openadapt seal keys pin` to pin Cloud's Seal
keys after a person confirms them. Every other argument passes to
`openadapt-agent seal` unchanged.
"""
Expand All @@ -1938,6 +2002,8 @@ def seal(command_ctx: click.Context) -> None:
sys.exit(_publish_seal(args[1:]))
if args[:1] == ["unpublish"]:
sys.exit(_unpublish_seal(args[1:]))
if args[:1] == ["claim"]:
sys.exit(_claim_seals(args[1:]))
if len(args) >= 2 and args[0] == "verify" and _is_seal_link(args[1]):
sys.exit(_verify_seal_link(args[1], args[2:]))
if len(args) >= 2 and args[0] == "verify" and Path(args[1]).is_dir():
Expand Down Expand Up @@ -2156,6 +2222,73 @@ def _publish_seal(argv: list[str]) -> int:
return 0


def _claim_seals(argv: list[str]) -> int:
"""`openadapt seal claim [RUN_DIR...]`: claim published Seals into your workspace."""
from openadapt.seal_claim import claim_published, claims_live
from openadapt.seal_publish import host_of, urllib_transport

parser = argparse.ArgumentParser(
prog="openadapt seal claim",
description=(
"Claim the Seals this computer published into the Cloud workspace "
"you signed in to with `openadapt login`, so they show on the "
"dashboard. The proof is a signature by this computer's Seal key; "
"the delete token is never sent. `openadapt login` does this for "
"you. Name run folders (or a quickstart folder) to include Seals "
"published by an older openadapt."
),
allow_abbrev=False,
)
parser.add_argument(
"run_dirs", nargs="*", type=Path, help="published run folders to include"
)
parser.add_argument("--host", default=None, help="the Cloud you signed in to")
parser.add_argument(
"--destination-kind",
default=None,
help="as for `openadapt login`: the trust class of --host",
)
parser.add_argument(
"--trusted-host",
action="append",
default=None,
help="as for `openadapt login`: an allowlisted customer origin",
)
options = parser.parse_args(argv)
try:
from openadapt_flow.hosted import HostedError, resolve_host, resolve_token
except ImportError:
click.echo("This openadapt-flow cannot sign in to Cloud.", err=True)
return 1
from openadapt.seal_claim import check_destination

try:
origin = resolve_host(options.host)
check_destination(
origin,
destination_kind=options.destination_kind,
trusted_hosts=options.trusted_host,
)
token = resolve_token(None, host=origin)
except HostedError as exc:
click.echo(f"Not claimed: {exc}", err=True)
click.echo(f"Sign in first: {_hint('login')}", err=True)
return 1
if not claims_live(origin, transport=urllib_transport):
click.echo(f"{host_of(origin)} does not take Seal claims yet.", err=True)
return 1
try:
return claim_published(
origin,
token,
run_dirs=[p.expanduser() for p in options.run_dirs],
transport=urllib_transport,
)
except Exception as exc: # noqa: BLE001 - e.g. no Seal key on this computer
click.echo(f"Not claimed: {type(exc).__name__}: {exc}", err=True)
return 1


def _unpublish_seal(argv: list[str]) -> int:
"""`openadapt seal unpublish RUN_DIR`: delete the hosted Seal."""
from openadapt.seal_publish import (
Expand Down
Loading