knuckles-cli 0.2.0__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.
- knuckles_cli-0.2.0/LICENSE +21 -0
- knuckles_cli-0.2.0/PKG-INFO +140 -0
- knuckles_cli-0.2.0/README.md +103 -0
- knuckles_cli-0.2.0/pyproject.toml +89 -0
- knuckles_cli-0.2.0/setup.cfg +4 -0
- knuckles_cli-0.2.0/src/knuckles_cli/__init__.py +3 -0
- knuckles_cli-0.2.0/src/knuckles_cli/__main__.py +7 -0
- knuckles_cli-0.2.0/src/knuckles_cli/api.py +252 -0
- knuckles_cli-0.2.0/src/knuckles_cli/cli.py +684 -0
- knuckles_cli-0.2.0/src/knuckles_cli/config.py +201 -0
- knuckles_cli-0.2.0/src/knuckles_cli/doctor.py +250 -0
- knuckles_cli-0.2.0/src/knuckles_cli/envfile.py +103 -0
- knuckles_cli-0.2.0/src/knuckles_cli/errors.py +43 -0
- knuckles_cli-0.2.0/src/knuckles_cli/ops.py +451 -0
- knuckles_cli-0.2.0/src/knuckles_cli/providers.py +365 -0
- knuckles_cli-0.2.0/src/knuckles_cli/py.typed +0 -0
- knuckles_cli-0.2.0/src/knuckles_cli.egg-info/PKG-INFO +140 -0
- knuckles_cli-0.2.0/src/knuckles_cli.egg-info/SOURCES.txt +24 -0
- knuckles_cli-0.2.0/src/knuckles_cli.egg-info/dependency_links.txt +1 -0
- knuckles_cli-0.2.0/src/knuckles_cli.egg-info/entry_points.txt +2 -0
- knuckles_cli-0.2.0/src/knuckles_cli.egg-info/requires.txt +11 -0
- knuckles_cli-0.2.0/src/knuckles_cli.egg-info/top_level.txt +1 -0
- knuckles_cli-0.2.0/tests/test_cli.py +515 -0
- knuckles_cli-0.2.0/tests/test_config_and_env.py +118 -0
- knuckles_cli-0.2.0/tests/test_doctor.py +161 -0
- knuckles_cli-0.2.0/tests/test_providers.py +184 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Garrett Sooter
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: knuckles-cli
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Operator CLI for the Knuckles identity service
|
|
5
|
+
Author-email: Garrett Sooter <garrett.sooter@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/gsooter/knuckles
|
|
8
|
+
Project-URL: Documentation, https://gsooter.github.io/knuckles/cli
|
|
9
|
+
Project-URL: Source, https://github.com/gsooter/knuckles/tree/main/packages/knuckles-cli
|
|
10
|
+
Project-URL: Issues, https://github.com/gsooter/knuckles/issues
|
|
11
|
+
Keywords: authentication,identity,cli,knuckles,oauth,passkey
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Environment :: Console
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Topic :: Security
|
|
22
|
+
Classifier: Typing :: Typed
|
|
23
|
+
Requires-Python: >=3.11
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
License-File: LICENSE
|
|
26
|
+
Requires-Dist: requests<3,>=2.31
|
|
27
|
+
Requires-Dist: cryptography<46,>=42.0
|
|
28
|
+
Provides-Extra: dev
|
|
29
|
+
Requires-Dist: pytest<9,>=8.0; extra == "dev"
|
|
30
|
+
Requires-Dist: pytest-cov<6,>=5.0; extra == "dev"
|
|
31
|
+
Requires-Dist: responses<1,>=0.25; extra == "dev"
|
|
32
|
+
Requires-Dist: mypy<2,>=1.9; extra == "dev"
|
|
33
|
+
Requires-Dist: ruff==0.15.11; extra == "dev"
|
|
34
|
+
Requires-Dist: black<25,>=24.0; extra == "dev"
|
|
35
|
+
Requires-Dist: types-requests<3,>=2.31; extra == "dev"
|
|
36
|
+
Dynamic: license-file
|
|
37
|
+
|
|
38
|
+
# knuckles-cli
|
|
39
|
+
|
|
40
|
+
The operator CLI for [Knuckles](https://github.com/gsooter/knuckles).
|
|
41
|
+
It lets you add a new site, configure its sign-in methods, and check
|
|
42
|
+
that everything is wired correctly, using just your Knuckles URL and
|
|
43
|
+
admin token. You don't need a checkout, database access, or `curl`.
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
pip install knuckles-cli
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
It talks to a running Knuckles over HTTP only: the public discovery
|
|
50
|
+
endpoints plus the `/v1/admin/*` API, so the server needs
|
|
51
|
+
`KNUCKLES_ADMIN_TOKEN` set (Decisions #021 and #025).
|
|
52
|
+
|
|
53
|
+
## Five-minute setup for a new site
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
# 1. Point the CLI at your server. The token is read from
|
|
57
|
+
# $KNUCKLES_ADMIN_TOKEN, a file (--token-file, '-' = stdin), or a
|
|
58
|
+
# hidden prompt. It never goes on the command line.
|
|
59
|
+
knuckles login --url https://auth.example.com
|
|
60
|
+
|
|
61
|
+
# 2. Create the tenant and drop its credentials into the app's .env
|
|
62
|
+
knuckles init my-site --name "My Site" \
|
|
63
|
+
--origin https://my-site.com --origin http://localhost:3000 \
|
|
64
|
+
--env-file ../my-site/.env
|
|
65
|
+
|
|
66
|
+
# 3. Turn on sign-in methods (any subset)
|
|
67
|
+
knuckles providers email my-site --from "My Site <signin@mail.my-site.com>"
|
|
68
|
+
knuckles providers google my-site --client-secret-json ~/Downloads/client_secret_*.json
|
|
69
|
+
knuckles providers apple my-site --services-id com.my-site.web \
|
|
70
|
+
--team-id AB12CD34EF --p8 ~/Downloads/AuthKey_ABCDE12345.p8
|
|
71
|
+
knuckles providers passkey my-site --origin https://my-site.com
|
|
72
|
+
|
|
73
|
+
# 4. Check it
|
|
74
|
+
knuckles doctor my-site
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Commands
|
|
78
|
+
|
|
79
|
+
| Command | What it does |
|
|
80
|
+
|---|---|
|
|
81
|
+
| `login --url URL` | Verifies the admin token against the server, then stores the profile. |
|
|
82
|
+
| `logout` | Forgets the stored profile. |
|
|
83
|
+
| `init ID --name N --origin O [--env-file F]` | Creates a tenant and prints the next setup steps. |
|
|
84
|
+
| `apps list` / `apps show ID` | Lists tenants, or shows one. Secrets appear only as `*_configured` booleans. |
|
|
85
|
+
| `apps create ID --name N --origin O [--env-file F]` | Creates a tenant without the next-steps output. |
|
|
86
|
+
| `apps rotate-secret ID [--env-file F --force]` | Mints a new client secret. The old one stops working immediately. |
|
|
87
|
+
| `apps set-origins ID ORIGIN...` | Replaces the tenant's allowed origins. |
|
|
88
|
+
| `providers google ID --client-secret-json F` | Configures Google from the JSON file Google Cloud Console downloads. It warns if a redirect URI's origin isn't allowed. |
|
|
89
|
+
| `providers apple ID --services-id S --team-id T --p8 F` | Checks the key is P-256 and reads the key id from `AuthKey_<KEYID>.p8`. |
|
|
90
|
+
| `providers email ID --from ADDR` | Configures Resend. It checks that the sending domain is verified before storing the key. |
|
|
91
|
+
| `providers passkey ID --origin O [--rp-id R]` | Derives the WebAuthn relying party and checks that the origin fits under the `rp_id`. |
|
|
92
|
+
| `providers clear ID GROUP` | Resets `email`, `google`, `apple` or `passkey` to the operator env fallback. |
|
|
93
|
+
| `doctor [ID]` | Checks the server (health, `issuer`, JWKS, HTTPS) and each tenant (origins, config groups, passkey fit). |
|
|
94
|
+
|
|
95
|
+
### Where secrets come from
|
|
96
|
+
|
|
97
|
+
The CLI never takes a secret as a command-line argument, because
|
|
98
|
+
those end up in shell history and `ps`. Each secret is read from the
|
|
99
|
+
first source available:
|
|
100
|
+
|
|
101
|
+
| Secret | `--*-file` option (`-` = stdin) | Env var | Otherwise |
|
|
102
|
+
|---|---|---|---|
|
|
103
|
+
| Admin token | `login --token-file` | `KNUCKLES_ADMIN_TOKEN` | hidden prompt |
|
|
104
|
+
| Resend API key | `--api-key-file` | `KNUCKLES_RESEND_API_KEY` | hidden prompt |
|
|
105
|
+
| Google client secret (manual entry) | `--client-secret-file` | `KNUCKLES_GOOGLE_CLIENT_SECRET` | hidden prompt |
|
|
106
|
+
|
|
107
|
+
## For scripts and coding agents
|
|
108
|
+
|
|
109
|
+
- Pass `--json` (before the subcommand) to get the Knuckles API
|
|
110
|
+
envelope: `{"data": ...}` on success, `{"error": {"code", "message"}}`
|
|
111
|
+
on failure. Server error codes such as `APP_CLIENT_NOT_FOUND` are
|
|
112
|
+
passed through verbatim.
|
|
113
|
+
- Exit codes: `0` success, `1` failure (including any failing
|
|
114
|
+
`doctor` check), `2` usage error.
|
|
115
|
+
- No config file needed: `KNUCKLES_URL` and `KNUCKLES_ADMIN_TOKEN`
|
|
116
|
+
override the stored profile.
|
|
117
|
+
- Profiles are stored in `$XDG_CONFIG_HOME/knuckles/config.json`
|
|
118
|
+
(mode 0600), or at `KNUCKLES_CONFIG` if that's set. Select one with
|
|
119
|
+
`--profile NAME` or `KNUCKLES_PROFILE`.
|
|
120
|
+
|
|
121
|
+
## What `doctor` can't see
|
|
122
|
+
|
|
123
|
+
`doctor` sees only what the admin API returns. Encrypted values show
|
|
124
|
+
up as booleans, and whether an empty group inherits the operator's
|
|
125
|
+
env vars depends on server-side state
|
|
126
|
+
(`KNUCKLES_ALLOW_ENV_TENANT_FALLBACK`). That's why empty groups are
|
|
127
|
+
warnings, not failures. Resend domain verification is checked once,
|
|
128
|
+
by `providers email`, while the key is in hand.
|
|
129
|
+
|
|
130
|
+
## Releasing
|
|
131
|
+
|
|
132
|
+
Bump the version in `pyproject.toml` and `__init__.py`, add a
|
|
133
|
+
CHANGELOG entry, merge, then push a tag:
|
|
134
|
+
|
|
135
|
+
```bash
|
|
136
|
+
git tag knuckles-cli-vX.Y.Z && git push origin knuckles-cli-vX.Y.Z
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
`.github/workflows/release-cli.yml` tests, builds and publishes to PyPI
|
|
140
|
+
via Trusted Publishing.
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# knuckles-cli
|
|
2
|
+
|
|
3
|
+
The operator CLI for [Knuckles](https://github.com/gsooter/knuckles).
|
|
4
|
+
It lets you add a new site, configure its sign-in methods, and check
|
|
5
|
+
that everything is wired correctly, using just your Knuckles URL and
|
|
6
|
+
admin token. You don't need a checkout, database access, or `curl`.
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
pip install knuckles-cli
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
It talks to a running Knuckles over HTTP only: the public discovery
|
|
13
|
+
endpoints plus the `/v1/admin/*` API, so the server needs
|
|
14
|
+
`KNUCKLES_ADMIN_TOKEN` set (Decisions #021 and #025).
|
|
15
|
+
|
|
16
|
+
## Five-minute setup for a new site
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
# 1. Point the CLI at your server. The token is read from
|
|
20
|
+
# $KNUCKLES_ADMIN_TOKEN, a file (--token-file, '-' = stdin), or a
|
|
21
|
+
# hidden prompt. It never goes on the command line.
|
|
22
|
+
knuckles login --url https://auth.example.com
|
|
23
|
+
|
|
24
|
+
# 2. Create the tenant and drop its credentials into the app's .env
|
|
25
|
+
knuckles init my-site --name "My Site" \
|
|
26
|
+
--origin https://my-site.com --origin http://localhost:3000 \
|
|
27
|
+
--env-file ../my-site/.env
|
|
28
|
+
|
|
29
|
+
# 3. Turn on sign-in methods (any subset)
|
|
30
|
+
knuckles providers email my-site --from "My Site <signin@mail.my-site.com>"
|
|
31
|
+
knuckles providers google my-site --client-secret-json ~/Downloads/client_secret_*.json
|
|
32
|
+
knuckles providers apple my-site --services-id com.my-site.web \
|
|
33
|
+
--team-id AB12CD34EF --p8 ~/Downloads/AuthKey_ABCDE12345.p8
|
|
34
|
+
knuckles providers passkey my-site --origin https://my-site.com
|
|
35
|
+
|
|
36
|
+
# 4. Check it
|
|
37
|
+
knuckles doctor my-site
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Commands
|
|
41
|
+
|
|
42
|
+
| Command | What it does |
|
|
43
|
+
|---|---|
|
|
44
|
+
| `login --url URL` | Verifies the admin token against the server, then stores the profile. |
|
|
45
|
+
| `logout` | Forgets the stored profile. |
|
|
46
|
+
| `init ID --name N --origin O [--env-file F]` | Creates a tenant and prints the next setup steps. |
|
|
47
|
+
| `apps list` / `apps show ID` | Lists tenants, or shows one. Secrets appear only as `*_configured` booleans. |
|
|
48
|
+
| `apps create ID --name N --origin O [--env-file F]` | Creates a tenant without the next-steps output. |
|
|
49
|
+
| `apps rotate-secret ID [--env-file F --force]` | Mints a new client secret. The old one stops working immediately. |
|
|
50
|
+
| `apps set-origins ID ORIGIN...` | Replaces the tenant's allowed origins. |
|
|
51
|
+
| `providers google ID --client-secret-json F` | Configures Google from the JSON file Google Cloud Console downloads. It warns if a redirect URI's origin isn't allowed. |
|
|
52
|
+
| `providers apple ID --services-id S --team-id T --p8 F` | Checks the key is P-256 and reads the key id from `AuthKey_<KEYID>.p8`. |
|
|
53
|
+
| `providers email ID --from ADDR` | Configures Resend. It checks that the sending domain is verified before storing the key. |
|
|
54
|
+
| `providers passkey ID --origin O [--rp-id R]` | Derives the WebAuthn relying party and checks that the origin fits under the `rp_id`. |
|
|
55
|
+
| `providers clear ID GROUP` | Resets `email`, `google`, `apple` or `passkey` to the operator env fallback. |
|
|
56
|
+
| `doctor [ID]` | Checks the server (health, `issuer`, JWKS, HTTPS) and each tenant (origins, config groups, passkey fit). |
|
|
57
|
+
|
|
58
|
+
### Where secrets come from
|
|
59
|
+
|
|
60
|
+
The CLI never takes a secret as a command-line argument, because
|
|
61
|
+
those end up in shell history and `ps`. Each secret is read from the
|
|
62
|
+
first source available:
|
|
63
|
+
|
|
64
|
+
| Secret | `--*-file` option (`-` = stdin) | Env var | Otherwise |
|
|
65
|
+
|---|---|---|---|
|
|
66
|
+
| Admin token | `login --token-file` | `KNUCKLES_ADMIN_TOKEN` | hidden prompt |
|
|
67
|
+
| Resend API key | `--api-key-file` | `KNUCKLES_RESEND_API_KEY` | hidden prompt |
|
|
68
|
+
| Google client secret (manual entry) | `--client-secret-file` | `KNUCKLES_GOOGLE_CLIENT_SECRET` | hidden prompt |
|
|
69
|
+
|
|
70
|
+
## For scripts and coding agents
|
|
71
|
+
|
|
72
|
+
- Pass `--json` (before the subcommand) to get the Knuckles API
|
|
73
|
+
envelope: `{"data": ...}` on success, `{"error": {"code", "message"}}`
|
|
74
|
+
on failure. Server error codes such as `APP_CLIENT_NOT_FOUND` are
|
|
75
|
+
passed through verbatim.
|
|
76
|
+
- Exit codes: `0` success, `1` failure (including any failing
|
|
77
|
+
`doctor` check), `2` usage error.
|
|
78
|
+
- No config file needed: `KNUCKLES_URL` and `KNUCKLES_ADMIN_TOKEN`
|
|
79
|
+
override the stored profile.
|
|
80
|
+
- Profiles are stored in `$XDG_CONFIG_HOME/knuckles/config.json`
|
|
81
|
+
(mode 0600), or at `KNUCKLES_CONFIG` if that's set. Select one with
|
|
82
|
+
`--profile NAME` or `KNUCKLES_PROFILE`.
|
|
83
|
+
|
|
84
|
+
## What `doctor` can't see
|
|
85
|
+
|
|
86
|
+
`doctor` sees only what the admin API returns. Encrypted values show
|
|
87
|
+
up as booleans, and whether an empty group inherits the operator's
|
|
88
|
+
env vars depends on server-side state
|
|
89
|
+
(`KNUCKLES_ALLOW_ENV_TENANT_FALLBACK`). That's why empty groups are
|
|
90
|
+
warnings, not failures. Resend domain verification is checked once,
|
|
91
|
+
by `providers email`, while the key is in hand.
|
|
92
|
+
|
|
93
|
+
## Releasing
|
|
94
|
+
|
|
95
|
+
Bump the version in `pyproject.toml` and `__init__.py`, add a
|
|
96
|
+
CHANGELOG entry, merge, then push a tag:
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
git tag knuckles-cli-vX.Y.Z && git push origin knuckles-cli-vX.Y.Z
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
`.github/workflows/release-cli.yml` tests, builds and publishes to PyPI
|
|
103
|
+
via Trusted Publishing.
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "knuckles-cli"
|
|
7
|
+
version = "0.2.0"
|
|
8
|
+
description = "Operator CLI for the Knuckles identity service"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.11"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Garrett Sooter", email = "garrett.sooter@gmail.com" },
|
|
14
|
+
]
|
|
15
|
+
keywords = ["authentication", "identity", "cli", "knuckles", "oauth", "passkey"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 3 - Alpha",
|
|
18
|
+
"Environment :: Console",
|
|
19
|
+
"Intended Audience :: Developers",
|
|
20
|
+
"License :: OSI Approved :: MIT License",
|
|
21
|
+
"Programming Language :: Python :: 3",
|
|
22
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
23
|
+
"Programming Language :: Python :: 3.11",
|
|
24
|
+
"Programming Language :: Python :: 3.12",
|
|
25
|
+
"Programming Language :: Python :: 3.13",
|
|
26
|
+
"Topic :: Security",
|
|
27
|
+
"Typing :: Typed",
|
|
28
|
+
]
|
|
29
|
+
dependencies = [
|
|
30
|
+
"requests>=2.31,<3",
|
|
31
|
+
"cryptography>=42.0,<46",
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
[project.scripts]
|
|
35
|
+
knuckles = "knuckles_cli.cli:main"
|
|
36
|
+
|
|
37
|
+
[project.urls]
|
|
38
|
+
Homepage = "https://github.com/gsooter/knuckles"
|
|
39
|
+
Documentation = "https://gsooter.github.io/knuckles/cli"
|
|
40
|
+
Source = "https://github.com/gsooter/knuckles/tree/main/packages/knuckles-cli"
|
|
41
|
+
Issues = "https://github.com/gsooter/knuckles/issues"
|
|
42
|
+
|
|
43
|
+
[project.optional-dependencies]
|
|
44
|
+
dev = [
|
|
45
|
+
"pytest>=8.0,<9",
|
|
46
|
+
"pytest-cov>=5.0,<6",
|
|
47
|
+
"responses>=0.25,<1",
|
|
48
|
+
"mypy>=1.9,<2",
|
|
49
|
+
"ruff==0.15.11",
|
|
50
|
+
"black>=24.0,<25",
|
|
51
|
+
"types-requests>=2.31,<3",
|
|
52
|
+
]
|
|
53
|
+
|
|
54
|
+
[tool.setuptools.packages.find]
|
|
55
|
+
where = ["src"]
|
|
56
|
+
|
|
57
|
+
[tool.setuptools.package-data]
|
|
58
|
+
knuckles_cli = ["py.typed"]
|
|
59
|
+
|
|
60
|
+
[tool.black]
|
|
61
|
+
line-length = 88
|
|
62
|
+
target-version = ["py311"]
|
|
63
|
+
|
|
64
|
+
[tool.ruff]
|
|
65
|
+
line-length = 88
|
|
66
|
+
target-version = "py311"
|
|
67
|
+
|
|
68
|
+
[tool.ruff.lint]
|
|
69
|
+
select = ["E", "W", "F", "I", "N", "UP", "B", "SIM", "TCH", "RUF"]
|
|
70
|
+
|
|
71
|
+
[tool.ruff.lint.per-file-ignores]
|
|
72
|
+
"tests/**/*.py" = ["TC001", "TC002", "TC003"]
|
|
73
|
+
|
|
74
|
+
[tool.mypy]
|
|
75
|
+
python_version = "3.11"
|
|
76
|
+
strict = true
|
|
77
|
+
warn_return_any = true
|
|
78
|
+
disallow_untyped_defs = true
|
|
79
|
+
|
|
80
|
+
[tool.pytest.ini_options]
|
|
81
|
+
testpaths = ["tests"]
|
|
82
|
+
addopts = "-v"
|
|
83
|
+
|
|
84
|
+
[tool.coverage.run]
|
|
85
|
+
source = ["knuckles_cli"]
|
|
86
|
+
|
|
87
|
+
[tool.coverage.report]
|
|
88
|
+
fail_under = 80
|
|
89
|
+
show_missing = true
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
"""Thin HTTP client for the Knuckles public and admin endpoints.
|
|
2
|
+
|
|
3
|
+
Deliberately independent of the ``knuckles-client`` SDK (Decision
|
|
4
|
+
#025): the operator has an admin token, not app-client credentials,
|
|
5
|
+
and ``doctor`` needs raw status codes from the discovery endpoints.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
import requests
|
|
13
|
+
|
|
14
|
+
from knuckles_cli import __version__
|
|
15
|
+
from knuckles_cli.errors import (
|
|
16
|
+
ADMIN_API_DISABLED,
|
|
17
|
+
HTTP_ERROR,
|
|
18
|
+
NETWORK_ERROR,
|
|
19
|
+
NOT_LOGGED_IN,
|
|
20
|
+
CliError,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
_ADMIN_BASE = "/v1/admin/app-clients"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class KnucklesAPI:
|
|
27
|
+
"""Calls one Knuckles deployment.
|
|
28
|
+
|
|
29
|
+
Attributes:
|
|
30
|
+
base_url: Knuckles base URL, no trailing slash.
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
def __init__(
|
|
34
|
+
self,
|
|
35
|
+
base_url: str,
|
|
36
|
+
*,
|
|
37
|
+
admin_token: str | None = None,
|
|
38
|
+
timeout: float = 10.0,
|
|
39
|
+
session: requests.Session | None = None,
|
|
40
|
+
) -> None:
|
|
41
|
+
"""Initialize the client.
|
|
42
|
+
|
|
43
|
+
Args:
|
|
44
|
+
base_url: Knuckles base URL.
|
|
45
|
+
admin_token: Operator admin token for ``/v1/admin/*``.
|
|
46
|
+
timeout: Per-request timeout in seconds.
|
|
47
|
+
session: Optional pre-built session.
|
|
48
|
+
"""
|
|
49
|
+
self.base_url = base_url.rstrip("/")
|
|
50
|
+
self._admin_token = admin_token
|
|
51
|
+
self._timeout = timeout
|
|
52
|
+
self._session = session or requests.Session()
|
|
53
|
+
self._session.headers["User-Agent"] = f"knuckles-cli/{__version__}"
|
|
54
|
+
|
|
55
|
+
@property
|
|
56
|
+
def has_admin_token(self) -> bool:
|
|
57
|
+
"""Whether admin calls can be made.
|
|
58
|
+
|
|
59
|
+
Returns:
|
|
60
|
+
``True`` when an admin token is configured.
|
|
61
|
+
"""
|
|
62
|
+
return self._admin_token is not None
|
|
63
|
+
|
|
64
|
+
def _send(
|
|
65
|
+
self,
|
|
66
|
+
method: str,
|
|
67
|
+
path: str,
|
|
68
|
+
*,
|
|
69
|
+
json: dict[str, Any] | None = None,
|
|
70
|
+
headers: dict[str, str] | None = None,
|
|
71
|
+
) -> requests.Response:
|
|
72
|
+
"""Send one request, converting transport failures.
|
|
73
|
+
|
|
74
|
+
Args:
|
|
75
|
+
method: HTTP verb.
|
|
76
|
+
path: Path under the base URL, leading slash included.
|
|
77
|
+
json: Optional JSON body.
|
|
78
|
+
headers: Optional extra headers.
|
|
79
|
+
|
|
80
|
+
Returns:
|
|
81
|
+
The raw response.
|
|
82
|
+
|
|
83
|
+
Raises:
|
|
84
|
+
CliError: ``NETWORK_ERROR`` if the server is unreachable.
|
|
85
|
+
"""
|
|
86
|
+
try:
|
|
87
|
+
return self._session.request(
|
|
88
|
+
method,
|
|
89
|
+
f"{self.base_url}{path}",
|
|
90
|
+
json=json,
|
|
91
|
+
headers=headers,
|
|
92
|
+
timeout=self._timeout,
|
|
93
|
+
)
|
|
94
|
+
except requests.RequestException as exc:
|
|
95
|
+
raise CliError(
|
|
96
|
+
NETWORK_ERROR, f"Could not reach {self.base_url}: {exc}"
|
|
97
|
+
) from exc
|
|
98
|
+
|
|
99
|
+
def get_public(self, path: str) -> tuple[int, Any]:
|
|
100
|
+
"""GET an unauthenticated endpoint, returning status and body.
|
|
101
|
+
|
|
102
|
+
Args:
|
|
103
|
+
path: Path under the base URL.
|
|
104
|
+
|
|
105
|
+
Returns:
|
|
106
|
+
Tuple of HTTP status and parsed JSON (``None`` when the body
|
|
107
|
+
is not JSON).
|
|
108
|
+
|
|
109
|
+
Raises:
|
|
110
|
+
CliError: ``NETWORK_ERROR`` if the server is unreachable.
|
|
111
|
+
"""
|
|
112
|
+
response = self._send("GET", path)
|
|
113
|
+
try:
|
|
114
|
+
return response.status_code, response.json()
|
|
115
|
+
except ValueError:
|
|
116
|
+
return response.status_code, None
|
|
117
|
+
|
|
118
|
+
def admin(
|
|
119
|
+
self, method: str, path: str = "", *, json: dict[str, Any] | None = None
|
|
120
|
+
) -> Any:
|
|
121
|
+
"""Call an admin route and return its ``data`` payload.
|
|
122
|
+
|
|
123
|
+
Args:
|
|
124
|
+
method: HTTP verb.
|
|
125
|
+
path: Path under ``/v1/admin/app-clients``.
|
|
126
|
+
json: Optional JSON body.
|
|
127
|
+
|
|
128
|
+
Returns:
|
|
129
|
+
The response's ``data`` value.
|
|
130
|
+
|
|
131
|
+
Raises:
|
|
132
|
+
CliError: ``NOT_LOGGED_IN`` without a token,
|
|
133
|
+
``ADMIN_API_DISABLED`` when the server's admin surface
|
|
134
|
+
is off, or the server's own error code otherwise.
|
|
135
|
+
"""
|
|
136
|
+
if self._admin_token is None:
|
|
137
|
+
raise CliError(
|
|
138
|
+
NOT_LOGGED_IN,
|
|
139
|
+
"No admin token. Run `knuckles login` or set KNUCKLES_ADMIN_TOKEN.",
|
|
140
|
+
)
|
|
141
|
+
response = self._send(
|
|
142
|
+
method,
|
|
143
|
+
f"{_ADMIN_BASE}{path}",
|
|
144
|
+
json=json,
|
|
145
|
+
headers={"X-Admin-Token": self._admin_token},
|
|
146
|
+
)
|
|
147
|
+
try:
|
|
148
|
+
body = response.json()
|
|
149
|
+
except ValueError:
|
|
150
|
+
body = None
|
|
151
|
+
if response.ok and isinstance(body, dict) and "data" in body:
|
|
152
|
+
return body["data"]
|
|
153
|
+
|
|
154
|
+
error = body.get("error") if isinstance(body, dict) else None
|
|
155
|
+
if isinstance(error, dict) and isinstance(error.get("code"), str):
|
|
156
|
+
code = error["code"]
|
|
157
|
+
message = str(error.get("message", ""))
|
|
158
|
+
else:
|
|
159
|
+
code = HTTP_ERROR
|
|
160
|
+
message = f"Unexpected HTTP {response.status_code} from {self.base_url}."
|
|
161
|
+
# Every admin route exists when the surface is on, so a bare
|
|
162
|
+
# NOT_FOUND (not APP_CLIENT_NOT_FOUND) means it is switched off.
|
|
163
|
+
if response.status_code == 404 and code == "NOT_FOUND":
|
|
164
|
+
code = ADMIN_API_DISABLED
|
|
165
|
+
message = (
|
|
166
|
+
"The admin API is disabled on this server. Set "
|
|
167
|
+
"KNUCKLES_ADMIN_TOKEN in the Knuckles environment and redeploy."
|
|
168
|
+
)
|
|
169
|
+
raise CliError(code, message, status_code=response.status_code)
|
|
170
|
+
|
|
171
|
+
def list_apps(self) -> list[dict[str, Any]]:
|
|
172
|
+
"""List every tenant.
|
|
173
|
+
|
|
174
|
+
Returns:
|
|
175
|
+
Redacted app-client rows.
|
|
176
|
+
"""
|
|
177
|
+
data = self.admin("GET")
|
|
178
|
+
return list(data)
|
|
179
|
+
|
|
180
|
+
def get_app(self, client_id: str) -> dict[str, Any]:
|
|
181
|
+
"""Fetch one tenant.
|
|
182
|
+
|
|
183
|
+
Args:
|
|
184
|
+
client_id: The tenant's ``client_id``.
|
|
185
|
+
|
|
186
|
+
Returns:
|
|
187
|
+
The redacted app-client row.
|
|
188
|
+
"""
|
|
189
|
+
return dict(self.admin("GET", f"/{client_id}"))
|
|
190
|
+
|
|
191
|
+
def create_app(
|
|
192
|
+
self, *, client_id: str, app_name: str, allowed_origins: list[str]
|
|
193
|
+
) -> dict[str, Any]:
|
|
194
|
+
"""Register a tenant.
|
|
195
|
+
|
|
196
|
+
Args:
|
|
197
|
+
client_id: New ``client_id`` slug.
|
|
198
|
+
app_name: Human-facing name.
|
|
199
|
+
allowed_origins: Origins permitted in redirect URLs.
|
|
200
|
+
|
|
201
|
+
Returns:
|
|
202
|
+
The row plus the one-time ``client_secret``.
|
|
203
|
+
"""
|
|
204
|
+
return dict(
|
|
205
|
+
self.admin(
|
|
206
|
+
"POST",
|
|
207
|
+
json={
|
|
208
|
+
"client_id": client_id,
|
|
209
|
+
"app_name": app_name,
|
|
210
|
+
"allowed_origins": allowed_origins,
|
|
211
|
+
},
|
|
212
|
+
)
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
def update_app(self, client_id: str, fields: dict[str, Any]) -> dict[str, Any]:
|
|
216
|
+
"""Patch tenant-config columns.
|
|
217
|
+
|
|
218
|
+
Args:
|
|
219
|
+
client_id: The tenant's ``client_id``.
|
|
220
|
+
fields: Column values; ``None`` clears to env fallback.
|
|
221
|
+
|
|
222
|
+
Returns:
|
|
223
|
+
The updated redacted row.
|
|
224
|
+
"""
|
|
225
|
+
return dict(self.admin("PATCH", f"/{client_id}", json=fields))
|
|
226
|
+
|
|
227
|
+
def rotate_secret(self, client_id: str) -> dict[str, Any]:
|
|
228
|
+
"""Mint a new client secret.
|
|
229
|
+
|
|
230
|
+
Args:
|
|
231
|
+
client_id: The tenant's ``client_id``.
|
|
232
|
+
|
|
233
|
+
Returns:
|
|
234
|
+
``{"client_id", "client_secret"}``.
|
|
235
|
+
"""
|
|
236
|
+
return dict(self.admin("POST", f"/{client_id}/rotate-secret"))
|
|
237
|
+
|
|
238
|
+
def set_origins(self, client_id: str, origins: list[str]) -> dict[str, Any]:
|
|
239
|
+
"""Replace a tenant's allowed origins.
|
|
240
|
+
|
|
241
|
+
Args:
|
|
242
|
+
client_id: The tenant's ``client_id``.
|
|
243
|
+
origins: The full new origin list.
|
|
244
|
+
|
|
245
|
+
Returns:
|
|
246
|
+
The updated redacted row.
|
|
247
|
+
"""
|
|
248
|
+
return dict(
|
|
249
|
+
self.admin(
|
|
250
|
+
"PUT", f"/{client_id}/origins", json={"allowed_origins": origins}
|
|
251
|
+
)
|
|
252
|
+
)
|