canyonos 0.2.3.dev4__tar.gz → 0.2.4.dev1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/PKG-INFO +2 -1
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/api.py +25 -15
- canyonos-0.2.4.dev1/canyonos/auth.py +237 -0
- canyonos-0.2.4.dev1/canyonos/aws_account.py +349 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/aws_provision.py +187 -162
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/chat.py +30 -8
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/clean.py +34 -24
- canyonos-0.2.4.dev1/canyonos/cluster_provision.py +692 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/constants.py +2 -2
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/context.py +71 -36
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/convert.py +9 -1
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/curl.py +7 -6
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/dashboard.compose.yml +7 -7
- canyonos-0.2.4.dev1/canyonos/dashboard.py +92 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/dashboard_registration.py +10 -4
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/dashboard_stack.py +51 -31
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/deploy.py +44 -21
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/deployment.py +68 -29
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/init.py +3 -3
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/internal_ca.py +1 -1
- canyonos-0.2.4.dev1/canyonos/keychain.py +197 -0
- canyonos-0.2.4.dev1/canyonos/kubernetes_local.py +688 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/kubernetes_target.py +108 -15
- canyonos-0.2.4.dev1/canyonos/login.py +472 -0
- canyonos-0.2.4.dev1/canyonos/logout.py +54 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/new_app.py +1 -4
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/os_support/_windows.py +6 -2
- canyonos-0.2.4.dev1/canyonos/platform.py +165 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/ports.py +1 -1
- canyonos-0.2.4.dev1/canyonos/provision.py +221 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/quickstart.py +7 -7
- canyonos-0.2.4.dev1/canyonos/records.py +438 -0
- canyonos-0.2.4.dev1/canyonos/rosa_provision.py +932 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/sdk/models.py +66 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/selection.py +22 -23
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/serve.py +28 -50
- canyonos-0.2.4.dev1/canyonos/session.py +396 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/starters/helloworld/README.md +12 -9
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/status.py +28 -6
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/stop.py +22 -9
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/sync.py +51 -13
- canyonos-0.2.4.dev1/canyonos/terraform/rosa/.terraform.lock.hcl +124 -0
- canyonos-0.2.4.dev1/canyonos/terraform/rosa/main.tf +65 -0
- canyonos-0.2.4.dev1/canyonos/terraform/rosa/outputs.tf +29 -0
- canyonos-0.2.4.dev1/canyonos/terraform/rosa/variables.tf +80 -0
- canyonos-0.2.4.dev1/canyonos/terraform/rosa/versions.tf +17 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/test.py +10 -11
- canyonos-0.2.4.dev1/canyonos/tools.py +131 -0
- canyonos-0.2.4.dev1/canyonos/whoami.py +92 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos.egg-info/PKG-INFO +2 -1
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos.egg-info/SOURCES.txt +16 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos.egg-info/requires.txt +1 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/cli.py +195 -56
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/pyproject.toml +6 -1
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/utils/help_screen.py +23 -7
- canyonos-0.2.3.dev4/canyonos/auth.py +0 -196
- canyonos-0.2.3.dev4/canyonos/dashboard.py +0 -84
- canyonos-0.2.3.dev4/canyonos/kubernetes_local.py +0 -424
- canyonos-0.2.3.dev4/canyonos/provision.py +0 -44
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/README.md +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/__init__.py +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/config.py +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/contact.py +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/dev_machine.py +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/docker_cmd.py +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/doctor.py +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/env.py +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/feedback.py +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/forwards.py +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/logs.py +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/new_app_template/.env.example +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/new_app_template/.gitignore +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/new_app_template/agents/my_agent.py.tmpl +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/new_app_template/agents/workflow.py.tmpl +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/new_app_template/config/llms.yaml +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/new_app_template/config/policy.yaml +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/new_app_template/config/prompts.yaml +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/new_app_template/config/scaling.yaml +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/new_app_template/config/workflow_config.yaml +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/os_support/__init__.py +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/os_support/_posix.py +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/port_utils.py +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/resources.py +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/sdk/__init__.py +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/starters/helloworld/helloworld/agents/hello_agent.py.tmpl +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/starters/helloworld/helloworld/agents/workflow.py.tmpl +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/starters/helloworld/helloworld/config/policy.yaml +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/starters/helloworld/helloworld/config/test_query.txt +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/starters/helloworld/helloworld/config/workflow_config.yaml +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/starters/helloworld/workflow_group.yaml +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/starters/portfolio/portfolio/.env +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/starters/portfolio/portfolio/.env.example +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/starters/portfolio/portfolio/agents/advisor_agent.py.tmpl +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/starters/portfolio/portfolio/agents/intent_agent.py.tmpl +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/starters/portfolio/portfolio/agents/metrics_agent.py.tmpl +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/starters/portfolio/portfolio/agents/price_agent.py.tmpl +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/starters/portfolio/portfolio/agents/risk_agent.py.tmpl +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/starters/portfolio/portfolio/agents/workflow.py.tmpl +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/starters/portfolio/portfolio/config/llms.yaml +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/starters/portfolio/portfolio/config/policy.yaml +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/starters/portfolio/portfolio/config/prompts.yaml +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/starters/portfolio/portfolio/config/scaling.yaml +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/starters/portfolio/portfolio/config/test_query.txt +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/starters/portfolio/portfolio/config/workflow_config.yaml +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/starters/portfolio/workflow_group.yaml +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/theme.py +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/ui.py +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/upgrade.py +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/usage/__init__.py +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/usage/consent.py +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/usage/delivery.py +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/usage/outbox.py +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/usage/settings.py +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/validate.py +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos/workflow_group.yaml +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos.egg-info/dependency_links.txt +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos.egg-info/entry_points.txt +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/canyonos.egg-info/top_level.txt +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/setup.cfg +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/utils/__init__.py +0 -0
- {canyonos-0.2.3.dev4 → canyonos-0.2.4.dev1}/utils/tui.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: canyonos
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.4.dev1
|
|
4
4
|
Summary: Open-source platform for multi-agent apps: per-agent scaling, tracing and cost, in plain Python
|
|
5
5
|
License-Expression: AGPL-3.0-only
|
|
6
6
|
Project-URL: Homepage, https://canyonos.ai
|
|
@@ -17,6 +17,7 @@ Description-Content-Type: text/markdown
|
|
|
17
17
|
Requires-Dist: boto3[crt]>=1.43.109
|
|
18
18
|
Requires-Dist: cryptography>=50.0.2
|
|
19
19
|
Requires-Dist: pyfiglet
|
|
20
|
+
Requires-Dist: pyjwt[crypto]>=2.10.1
|
|
20
21
|
Requires-Dist: python-dotenv
|
|
21
22
|
Requires-Dist: pyyaml
|
|
22
23
|
Requires-Dist: rich
|
|
@@ -6,7 +6,8 @@ The CanyonOS API's `/v1/workflow-groups` routes, so the commands that deploy, st
|
|
|
6
6
|
read a workflow group don't each restate the same routes, payloads and failure modes.
|
|
7
7
|
|
|
8
8
|
On Docker the API is the local stack's (`dashboard_stack`), on 127.0.0.1 at its host
|
|
9
|
-
port, and every call sends this machine's
|
|
9
|
+
port, and every call sends this machine's deploy key, which opens every workflow
|
|
10
|
+
group's routes here and is the only key that creates one; the API runs each workflow
|
|
10
11
|
group's Global Controller container through Docker. On Kubernetes it is the cluster's,
|
|
11
12
|
the one the operator's chart installs, reached through a port-forward with the user's
|
|
12
13
|
kubectl login (`kubernetes_target.kubernetes_state`); it runs each workflow group as a
|
|
@@ -22,7 +23,7 @@ import urllib.parse
|
|
|
22
23
|
import urllib.request
|
|
23
24
|
|
|
24
25
|
from canyonos import deployment, env
|
|
25
|
-
from canyonos.auth import
|
|
26
|
+
from canyonos.auth import api_key_path, bearer_header
|
|
26
27
|
from canyonos.init import load_state
|
|
27
28
|
|
|
28
29
|
REQUEST_TIMEOUT_SECONDS = 60
|
|
@@ -46,19 +47,18 @@ ERROR_PREFIX = "workflow_groups."
|
|
|
46
47
|
UNKNOWN_WORKFLOW_GROUP = "unknown_workflow_group"
|
|
47
48
|
# What an older chart's API answers on Kubernetes: no route there (a release before
|
|
48
49
|
# the /v1 routes), or the /v1 routes of an API that drives only Docker, without Docker
|
|
49
|
-
# or the control key.
|
|
50
|
+
# or the control key those releases asked for.
|
|
50
51
|
OLDER_CHART_ERRORS = {
|
|
51
52
|
404: (None, "not_found"),
|
|
52
53
|
503: ("docker.disabled",),
|
|
53
54
|
401: ("auth.control_key_unset",),
|
|
54
55
|
}
|
|
55
56
|
|
|
56
|
-
# What the API answers a /v1/workflow-groups call
|
|
57
|
-
#
|
|
58
|
-
|
|
57
|
+
# What the API answers a /v1/workflow-groups call that sends neither its deploy key nor
|
|
58
|
+
# the workflow group's own key.
|
|
59
|
+
API_KEY_REFUSALS = (
|
|
59
60
|
"auth.missing_key",
|
|
60
61
|
"auth.invalid_key",
|
|
61
|
-
"auth.control_key_unset",
|
|
62
62
|
)
|
|
63
63
|
DATABASE_UNAVAILABLE = "database.unavailable"
|
|
64
64
|
|
|
@@ -116,13 +116,21 @@ def _on_kubernetes(state):
|
|
|
116
116
|
return bool(state and "api_url" in state)
|
|
117
117
|
|
|
118
118
|
|
|
119
|
+
def docker_api_headers():
|
|
120
|
+
"""The headers every call to this machine's CanyonOS API sends: its deploy key."""
|
|
121
|
+
# Imported here: the stack's module reads ports this one has no need of.
|
|
122
|
+
from canyonos.dashboard_stack import deploy_key
|
|
123
|
+
|
|
124
|
+
return bearer_header(deploy_key())
|
|
125
|
+
|
|
126
|
+
|
|
119
127
|
def _connection(state):
|
|
120
128
|
"""The base URL and headers a call about `state`'s workflow group goes to: the
|
|
121
|
-
cluster's API on Kubernetes, else this machine's with
|
|
129
|
+
cluster's API on Kubernetes, else this machine's with its deploy key; this
|
|
122
130
|
machine's when `state` is None."""
|
|
123
131
|
if _on_kubernetes(state):
|
|
124
132
|
return state["api_url"], state["api_headers"]
|
|
125
|
-
return docker_api_url(),
|
|
133
|
+
return docker_api_url(), docker_api_headers()
|
|
126
134
|
|
|
127
135
|
|
|
128
136
|
def error_detail(body, status):
|
|
@@ -171,11 +179,13 @@ def _next_step(state, code):
|
|
|
171
179
|
its message; "" when there is nothing to add."""
|
|
172
180
|
if _on_kubernetes(state):
|
|
173
181
|
return ""
|
|
174
|
-
if code in
|
|
175
|
-
# deploy, sync and stop restart
|
|
182
|
+
if code in API_KEY_REFUSALS:
|
|
183
|
+
# deploy, sync and stop restart an API on another deploy key themselves
|
|
184
|
+
# (`serve.ensure_api`).
|
|
176
185
|
return (
|
|
177
|
-
"
|
|
178
|
-
|
|
186
|
+
f" This workflow group's API key is in {api_key_path()}; "
|
|
187
|
+
"`canyonos deploy --api-key <key>` sets it. `canyonos serve` restarts the "
|
|
188
|
+
"CanyonOS API with this machine's deploy key."
|
|
179
189
|
)
|
|
180
190
|
if code == DATABASE_UNAVAILABLE:
|
|
181
191
|
return _database_hint(state)
|
|
@@ -383,8 +393,8 @@ def put_workflow_group(state, config, env_text, workflows=None, api_key_hash=Non
|
|
|
383
393
|
and the text `env_text` of the `.env` beside it (None when there is none); with
|
|
384
394
|
`workflows`, a list of workflow_ids, the API removes the uploaded workflows not
|
|
385
395
|
among them; with
|
|
386
|
-
`api_key_hash`, the SHA-256 of
|
|
387
|
-
|
|
396
|
+
`api_key_hash`, the SHA-256 of this workflow group's API key ("" for none), it keeps
|
|
397
|
+
that as its key, which only this workflow group's routes and endpoint take. Return the new generation and the upload's warnings. Raises
|
|
388
398
|
ApiError on failure."""
|
|
389
399
|
body = {
|
|
390
400
|
"name": deployment.folder_name(),
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Canyon Code, Inc.
|
|
2
|
+
# SPDX-License-Identifier: AGPL-3.0-only
|
|
3
|
+
|
|
4
|
+
"""Each workflow group's API key: one string the user picks with `canyonos deploy
|
|
5
|
+
--api-key`, kept in the workflow group's record (`~/.canyonos/workflows/<workflow
|
|
6
|
+
group>/api_key`), that opens that workflow group's endpoint and its routes on the
|
|
7
|
+
CanyonOS API, never another workflow group's. The containers get only its SHA-256 hash.
|
|
8
|
+
A workflow group without a key checks nothing. `CANYONOS_API_KEY` stands in for the
|
|
9
|
+
record's key for one command."""
|
|
10
|
+
|
|
11
|
+
import hashlib
|
|
12
|
+
import json
|
|
13
|
+
import os
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
|
|
16
|
+
from canyonos import deployment, records, ui
|
|
17
|
+
from canyonos.os_support import make_private_dir, write_private_file
|
|
18
|
+
|
|
19
|
+
# packages/core/canyonos_core/auth.py repeats this on purpose: the CLI cannot import
|
|
20
|
+
# canyonos_core, and both sides must agree on it.
|
|
21
|
+
API_KEY_HASH_ENV = "CANYONOS_API_KEY_HASH"
|
|
22
|
+
API_KEY_ENV = "CANYONOS_API_KEY"
|
|
23
|
+
API_KEY_FILE = "api_key"
|
|
24
|
+
|
|
25
|
+
# The header every printed `curl` sends; the key itself is never printed.
|
|
26
|
+
BEARER_PLACEHOLDER = '-H "Authorization: Bearer <your api key>"'
|
|
27
|
+
|
|
28
|
+
NO_KEY_WARNING = (
|
|
29
|
+
"This workflow group has no API key: its endpoint and its dashboard routes answer "
|
|
30
|
+
"anyone who reaches them. Set one with --api-key <key>."
|
|
31
|
+
)
|
|
32
|
+
KEY_SET = "This workflow group now uses an API key for secure connections."
|
|
33
|
+
ONLY_THIS_GROUP = (
|
|
34
|
+
"Only this workflow group changes: other workflow groups keep their own keys."
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def api_key_path(name: str | None = None) -> Path:
|
|
39
|
+
"""Where the record of workflow group `name`, by default the one this command acts
|
|
40
|
+
on, keeps its API key."""
|
|
41
|
+
return Path(deployment.state_dir(name)) / API_KEY_FILE
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _machine_key_path() -> Path:
|
|
45
|
+
"""Where a CLI before per-workflow-group keys kept one key for every workflow group
|
|
46
|
+
on this machine; read once, by `_adopt_machine_key`."""
|
|
47
|
+
return Path.home() / ".canyonos" / API_KEY_FILE
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _read_key(path: Path) -> str | None:
|
|
51
|
+
"""The key in the file at `path`, or None when there is no file or it is empty;
|
|
52
|
+
raise RuntimeError when it can't be read."""
|
|
53
|
+
try:
|
|
54
|
+
key = path.read_text(encoding="utf-8").strip()
|
|
55
|
+
except FileNotFoundError:
|
|
56
|
+
return None
|
|
57
|
+
except OSError as error:
|
|
58
|
+
raise RuntimeError(
|
|
59
|
+
f"Could not read {path}: {error.strerror or error}"
|
|
60
|
+
) from error
|
|
61
|
+
return key or None
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _write_key(path: Path, key: str) -> None:
|
|
65
|
+
try:
|
|
66
|
+
make_private_dir(path.parent)
|
|
67
|
+
write_private_file(path, key + "\n")
|
|
68
|
+
except OSError as error:
|
|
69
|
+
raise RuntimeError(
|
|
70
|
+
f"Could not write {path}: {error.strerror or error}"
|
|
71
|
+
) from error
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _deployed_without_key(name: str) -> bool:
|
|
75
|
+
"""Whether the Docker deploy of workflow group `name` that still runs was made with
|
|
76
|
+
no API key, as its `state.json` records it. False when there is no `state.json`,
|
|
77
|
+
which a stop removes and a Kubernetes deploy never writes, or it can't be read:
|
|
78
|
+
such a workflow group takes the machine key, and its next deploy sets it."""
|
|
79
|
+
try:
|
|
80
|
+
with open(deployment.state_path(name), encoding="utf-8") as f:
|
|
81
|
+
state = json.load(f)
|
|
82
|
+
except (OSError, ValueError):
|
|
83
|
+
return False
|
|
84
|
+
return isinstance(state, dict) and state.get("auth_disabled") is True
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def _adopt_machine_key(deploying: str) -> None:
|
|
88
|
+
"""Upgrade, once, as a deploy starts, from the CLI that kept one key for the whole
|
|
89
|
+
machine in `~/.canyonos/api_key`: every workflow group recorded here without a key
|
|
90
|
+
of its own, and `deploying`, the one the deploy is about to set up, take that key,
|
|
91
|
+
which their deploys were made with, then the file is removed, as no record needs it
|
|
92
|
+
anymore. A Docker workflow group still deployed with no key stays without one."""
|
|
93
|
+
legacy = _machine_key_path()
|
|
94
|
+
key = _read_key(legacy)
|
|
95
|
+
if key is None and not legacy.exists():
|
|
96
|
+
return
|
|
97
|
+
if key is not None:
|
|
98
|
+
for name in {*records.names(), deploying}:
|
|
99
|
+
path = api_key_path(name)
|
|
100
|
+
if not path.exists() and not _deployed_without_key(name):
|
|
101
|
+
_write_key(path, key)
|
|
102
|
+
try:
|
|
103
|
+
legacy.unlink(missing_ok=True)
|
|
104
|
+
except OSError as error:
|
|
105
|
+
raise RuntimeError(
|
|
106
|
+
f"Could not remove {legacy}: {error.strerror or error}"
|
|
107
|
+
) from error
|
|
108
|
+
if key is not None:
|
|
109
|
+
ui.note(
|
|
110
|
+
f"Each workflow group now has its own API key. The key from {legacy} "
|
|
111
|
+
"was copied into each group's api_key file."
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def group_api_key(name: str | None = None) -> str | None:
|
|
116
|
+
"""The API key of workflow group `name`, by default the one this command acts on:
|
|
117
|
+
`CANYONOS_API_KEY` when it is set, else the one its record keeps; None when there
|
|
118
|
+
is none. Reads only: a deploy is what moves an older CLI's machine key into the
|
|
119
|
+
records (`resolve_api_key`)."""
|
|
120
|
+
env_key = os.environ.get(API_KEY_ENV, "").strip()
|
|
121
|
+
if env_key:
|
|
122
|
+
return env_key
|
|
123
|
+
return _read_key(api_key_path(name))
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def workflow_group_api_key(workflow_group_id: str) -> str | None:
|
|
127
|
+
"""The API key of the workflow group whose ID is `workflow_group_id`, as
|
|
128
|
+
`group_api_key` finds it; None when no record has that ID, so a request about a
|
|
129
|
+
workflow group this machine does not know carries no other workflow group's key."""
|
|
130
|
+
name = records.resolve(workflow_group_id, missing_ok=True)
|
|
131
|
+
if name is None:
|
|
132
|
+
return None
|
|
133
|
+
return group_api_key(name)
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def hash_api_key(key: str | None) -> str:
|
|
137
|
+
"""The SHA-256 of `key` in lowercase hex, as the verifiers hold it; "" for no key."""
|
|
138
|
+
if not key:
|
|
139
|
+
return ""
|
|
140
|
+
return hashlib.sha256(key.encode("utf-8")).hexdigest()
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def _save(key: str) -> None:
|
|
144
|
+
"""Keep `key` in the record of the workflow group this deploy acts on, saying so in
|
|
145
|
+
the terminal, and that only this workflow group changes when the key replaces one
|
|
146
|
+
it ran with: another key, or none on a workflow group deployed before."""
|
|
147
|
+
path = api_key_path()
|
|
148
|
+
previous = _read_key(path)
|
|
149
|
+
if previous is None:
|
|
150
|
+
replaces_a_deploys_key = os.path.isfile(deployment.state_path())
|
|
151
|
+
else:
|
|
152
|
+
replaces_a_deploys_key = previous != key
|
|
153
|
+
_write_key(path, key)
|
|
154
|
+
ui.ok(KEY_SET)
|
|
155
|
+
if replaces_a_deploys_key:
|
|
156
|
+
ui.hint(ONLY_THIS_GROUP)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def _remove() -> None:
|
|
160
|
+
path = api_key_path()
|
|
161
|
+
try:
|
|
162
|
+
path.unlink()
|
|
163
|
+
except FileNotFoundError:
|
|
164
|
+
return
|
|
165
|
+
except OSError as error:
|
|
166
|
+
raise RuntimeError(
|
|
167
|
+
f"Could not remove {path}: {error.strerror or error}"
|
|
168
|
+
) from error
|
|
169
|
+
ui.ok("Removed this workflow group's API key.")
|
|
170
|
+
ui.hint(ONLY_THIS_GROUP)
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def check_api_key_options(api_key: str | None, no_api_key: bool) -> None:
|
|
174
|
+
"""Raise RuntimeError for an empty `--api-key`, or when CANYONOS_API_KEY holds
|
|
175
|
+
another key than `--api-key` or `--no-api-key` set, which later commands would send
|
|
176
|
+
instead; a deploy checks this before it changes anything."""
|
|
177
|
+
if api_key is not None and not api_key.strip():
|
|
178
|
+
raise RuntimeError(
|
|
179
|
+
"--api-key needs a key. To run without one, pass --no-api-key instead"
|
|
180
|
+
)
|
|
181
|
+
env_key = os.environ.get(API_KEY_ENV, "").strip()
|
|
182
|
+
if env_key and no_api_key:
|
|
183
|
+
raise RuntimeError(
|
|
184
|
+
f"{API_KEY_ENV} holds a key, which later commands would send to this "
|
|
185
|
+
f"workflow group. Unset {API_KEY_ENV}, then run this again"
|
|
186
|
+
)
|
|
187
|
+
if env_key and api_key is not None and env_key != api_key.strip():
|
|
188
|
+
raise RuntimeError(
|
|
189
|
+
f"{API_KEY_ENV} holds another key, which later commands would send "
|
|
190
|
+
f"instead of this one. Unset {API_KEY_ENV} or set it to the new key, "
|
|
191
|
+
"then run this again"
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def resolve_api_key(api_key: str | None, no_api_key: bool) -> str | None:
|
|
196
|
+
"""Apply `--api-key` (save it in the record of the workflow group this deploy acts
|
|
197
|
+
on, replacing its key) or `--no-api-key` (remove its key) and return the key this
|
|
198
|
+
deploy sets, warning when there is none; raise RuntimeError as
|
|
199
|
+
`check_api_key_options` does."""
|
|
200
|
+
check_api_key_options(api_key, no_api_key)
|
|
201
|
+
_adopt_machine_key(deployment.deployment_name())
|
|
202
|
+
if no_api_key:
|
|
203
|
+
_remove()
|
|
204
|
+
key = None
|
|
205
|
+
elif api_key is not None:
|
|
206
|
+
key = api_key.strip()
|
|
207
|
+
_save(key)
|
|
208
|
+
else:
|
|
209
|
+
key = group_api_key()
|
|
210
|
+
if key is None:
|
|
211
|
+
ui.warn(NO_KEY_WARNING)
|
|
212
|
+
return key
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def bearer_header(key: str | None) -> dict[str, str]:
|
|
216
|
+
"""The header that sends `key`; none when there is no key."""
|
|
217
|
+
if not key:
|
|
218
|
+
return {}
|
|
219
|
+
return {"Authorization": f"Bearer {key}"}
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def auth_header() -> dict[str, str]:
|
|
223
|
+
"""The header that sends the API key of the workflow group this command acts on;
|
|
224
|
+
none when it has no key."""
|
|
225
|
+
return bearer_header(group_api_key())
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def curl_auth(keyed: bool) -> str:
|
|
229
|
+
"""The placeholder header a printed `curl` shows when the deploy is `keyed`, else
|
|
230
|
+
""."""
|
|
231
|
+
return f" {BEARER_PLACEHOLDER}" if keyed else ""
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def gc_env(key: str | None) -> list[str]:
|
|
235
|
+
"""The `docker run` options that hand the Global Controller the hash of `key`; an
|
|
236
|
+
empty hash when there is no key, so nothing is checked."""
|
|
237
|
+
return ["-e", f"{API_KEY_HASH_ENV}={hash_api_key(key)}"]
|