simple-module-remote 0.0.50__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.
- simple_module_remote-0.0.50/.gitignore +75 -0
- simple_module_remote-0.0.50/LICENSE +21 -0
- simple_module_remote-0.0.50/PKG-INFO +73 -0
- simple_module_remote-0.0.50/README.md +44 -0
- simple_module_remote-0.0.50/pyproject.toml +41 -0
- simple_module_remote-0.0.50/simple_module_remote/__init__.py +12 -0
- simple_module_remote-0.0.50/simple_module_remote/commands/__init__.py +1 -0
- simple_module_remote-0.0.50/simple_module_remote/commands/api_cmd.py +157 -0
- simple_module_remote-0.0.50/simple_module_remote/commands/auth_cmd.py +166 -0
- simple_module_remote-0.0.50/simple_module_remote/commands/doctor_cmd.py +157 -0
- simple_module_remote-0.0.50/simple_module_remote/commands/env_cmd.py +198 -0
- simple_module_remote-0.0.50/simple_module_remote/commands/generated.py +50 -0
- simple_module_remote-0.0.50/simple_module_remote/commands/root.py +186 -0
- simple_module_remote-0.0.50/simple_module_remote/commands/runtime.py +263 -0
- simple_module_remote-0.0.50/simple_module_remote/commands/schema_cmd.py +155 -0
- simple_module_remote-0.0.50/simple_module_remote/commands/schema_fetch.py +129 -0
- simple_module_remote-0.0.50/simple_module_remote/commands/schema_source.py +181 -0
- simple_module_remote-0.0.50/simple_module_remote/config/__init__.py +1 -0
- simple_module_remote-0.0.50/simple_module_remote/config/_file_store.py +182 -0
- simple_module_remote-0.0.50/simple_module_remote/config/_private_dir.py +191 -0
- simple_module_remote-0.0.50/simple_module_remote/config/credentials.py +296 -0
- simple_module_remote-0.0.50/simple_module_remote/config/environments.py +186 -0
- simple_module_remote-0.0.50/simple_module_remote/config/paths.py +32 -0
- simple_module_remote-0.0.50/simple_module_remote/config/resolve.py +96 -0
- simple_module_remote-0.0.50/simple_module_remote/curated/__init__.py +1 -0
- simple_module_remote-0.0.50/simple_module_remote/generate/__init__.py +1 -0
- simple_module_remote-0.0.50/simple_module_remote/generate/body.py +198 -0
- simple_module_remote-0.0.50/simple_module_remote/generate/confirm.py +38 -0
- simple_module_remote-0.0.50/simple_module_remote/generate/context.py +38 -0
- simple_module_remote-0.0.50/simple_module_remote/generate/emit.py +278 -0
- simple_module_remote-0.0.50/simple_module_remote/generate/execute.py +220 -0
- simple_module_remote-0.0.50/simple_module_remote/generate/naming.py +69 -0
- simple_module_remote-0.0.50/simple_module_remote/generate/operations.py +181 -0
- simple_module_remote-0.0.50/simple_module_remote/generate/pagination.py +80 -0
- simple_module_remote-0.0.50/simple_module_remote/generate/pairs.py +22 -0
- simple_module_remote-0.0.50/simple_module_remote/generate/params.py +210 -0
- simple_module_remote-0.0.50/simple_module_remote/generate/prepare.py +134 -0
- simple_module_remote-0.0.50/simple_module_remote/generate/tree.py +59 -0
- simple_module_remote-0.0.50/simple_module_remote/generate/trust.py +42 -0
- simple_module_remote-0.0.50/simple_module_remote/generate/validate.py +136 -0
- simple_module_remote-0.0.50/simple_module_remote/http/__init__.py +1 -0
- simple_module_remote-0.0.50/simple_module_remote/http/client.py +292 -0
- simple_module_remote-0.0.50/simple_module_remote/http/errors.py +59 -0
- simple_module_remote-0.0.50/simple_module_remote/http/safety.py +124 -0
- simple_module_remote-0.0.50/simple_module_remote/output/__init__.py +1 -0
- simple_module_remote-0.0.50/simple_module_remote/output/reasons.py +17 -0
- simple_module_remote-0.0.50/simple_module_remote/output/render.py +149 -0
- simple_module_remote-0.0.50/simple_module_remote/output/sanitize.py +32 -0
- simple_module_remote-0.0.50/simple_module_remote/py.typed +0 -0
- simple_module_remote-0.0.50/simple_module_remote/schema/__init__.py +1 -0
- simple_module_remote-0.0.50/simple_module_remote/schema/cache.py +187 -0
- simple_module_remote-0.0.50/simple_module_remote/schema/loader.py +56 -0
- simple_module_remote-0.0.50/tests/commands/test_api_cmd.py +185 -0
- simple_module_remote-0.0.50/tests/commands/test_auth_cmd.py +280 -0
- simple_module_remote-0.0.50/tests/commands/test_auth_login.py +34 -0
- simple_module_remote-0.0.50/tests/commands/test_auth_tenant.py +38 -0
- simple_module_remote-0.0.50/tests/commands/test_doctor_cmd.py +117 -0
- simple_module_remote-0.0.50/tests/commands/test_env_cmd.py +222 -0
- simple_module_remote-0.0.50/tests/commands/test_env_normalise.py +41 -0
- simple_module_remote-0.0.50/tests/commands/test_schema_etag.py +69 -0
- simple_module_remote-0.0.50/tests/commands/test_schema_live_shapes.py +144 -0
- simple_module_remote-0.0.50/tests/commands/test_schema_live_trust.py +182 -0
- simple_module_remote-0.0.50/tests/commands/test_schema_sync.py +184 -0
- simple_module_remote-0.0.50/tests/commands/test_whoami_validation.py +34 -0
- simple_module_remote-0.0.50/tests/config/test_credential_index.py +124 -0
- simple_module_remote-0.0.50/tests/config/test_credentials.py +283 -0
- simple_module_remote-0.0.50/tests/config/test_credentials_read_errors.py +45 -0
- simple_module_remote-0.0.50/tests/config/test_environments.py +178 -0
- simple_module_remote-0.0.50/tests/config/test_import_isolation.py +25 -0
- simple_module_remote-0.0.50/tests/config/test_private_dir_fifo.py +26 -0
- simple_module_remote-0.0.50/tests/conftest.py +261 -0
- simple_module_remote-0.0.50/tests/generate/fixtures/mini_openapi.json +49 -0
- simple_module_remote-0.0.50/tests/generate/test_body.py +254 -0
- simple_module_remote-0.0.50/tests/generate/test_execute.py +282 -0
- simple_module_remote-0.0.50/tests/generate/test_execute_failures.py +296 -0
- simple_module_remote-0.0.50/tests/generate/test_execute_flags.py +227 -0
- simple_module_remote-0.0.50/tests/generate/test_execute_output.py +81 -0
- simple_module_remote-0.0.50/tests/generate/test_execute_request.py +63 -0
- simple_module_remote-0.0.50/tests/generate/test_naming.py +40 -0
- simple_module_remote-0.0.50/tests/generate/test_operations.py +108 -0
- simple_module_remote-0.0.50/tests/generate/test_pagination.py +98 -0
- simple_module_remote-0.0.50/tests/generate/test_params.py +163 -0
- simple_module_remote-0.0.50/tests/generate/test_server_text_safety.py +32 -0
- simple_module_remote-0.0.50/tests/generate/test_tree.py +112 -0
- simple_module_remote-0.0.50/tests/http/test_client.py +277 -0
- simple_module_remote-0.0.50/tests/http/test_client_redirects.py +168 -0
- simple_module_remote-0.0.50/tests/http/test_safety.py +96 -0
- simple_module_remote-0.0.50/tests/output/test_render.py +119 -0
- simple_module_remote-0.0.50/tests/output/test_sanitize.py +14 -0
- simple_module_remote-0.0.50/tests/schema/test_cache.py +156 -0
- simple_module_remote-0.0.50/tests/schema/test_loader.py +34 -0
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*.egg-info/
|
|
5
|
+
dist/
|
|
6
|
+
build/
|
|
7
|
+
.venv/
|
|
8
|
+
*.egg
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
# Node
|
|
12
|
+
node_modules/
|
|
13
|
+
|
|
14
|
+
# IDE
|
|
15
|
+
.idea/
|
|
16
|
+
.vscode/
|
|
17
|
+
*.swp
|
|
18
|
+
*.swo
|
|
19
|
+
|
|
20
|
+
# Environment
|
|
21
|
+
.env
|
|
22
|
+
|
|
23
|
+
# Database
|
|
24
|
+
*.db
|
|
25
|
+
*.db-wal
|
|
26
|
+
*.db-shm
|
|
27
|
+
*.db-journal
|
|
28
|
+
*.sqlite3
|
|
29
|
+
|
|
30
|
+
# Module-managed runtime state (e.g. uploaded dataset files,
|
|
31
|
+
# default storage_dir for SM_DATASETS_STORAGE_DIR).
|
|
32
|
+
var/
|
|
33
|
+
|
|
34
|
+
# file_storage filesystem backend default root (override via SM_FILE_STORAGE_FS_ROOT_PATH).
|
|
35
|
+
uploads/
|
|
36
|
+
|
|
37
|
+
# Vite
|
|
38
|
+
host/static/dist/
|
|
39
|
+
|
|
40
|
+
# VitePress (docs)
|
|
41
|
+
docs/.vitepress/cache/
|
|
42
|
+
docs/.vitepress/dist/
|
|
43
|
+
|
|
44
|
+
# Auto-generated frontend module manifest (regenerated by the host at boot
|
|
45
|
+
# or via `make gen-pages`).
|
|
46
|
+
host/client_app/modules.manifest.json
|
|
47
|
+
host/client_app/modules.generated.ts
|
|
48
|
+
host/client_app/modules.generated.css
|
|
49
|
+
host/client_app/modules.assets.json
|
|
50
|
+
|
|
51
|
+
# Worktrees
|
|
52
|
+
.worktrees/
|
|
53
|
+
|
|
54
|
+
# Performance profiles
|
|
55
|
+
.memray/
|
|
56
|
+
.benchmarks/
|
|
57
|
+
|
|
58
|
+
# OS
|
|
59
|
+
.DS_Store
|
|
60
|
+
Thumbs.db
|
|
61
|
+
|
|
62
|
+
.playwright-cli/*
|
|
63
|
+
.playwright-mcp/*
|
|
64
|
+
host/client_app/.playwright-cli/*
|
|
65
|
+
.superpowers/
|
|
66
|
+
.qa/
|
|
67
|
+
.verify/
|
|
68
|
+
/openapi.json
|
|
69
|
+
qa-shots/
|
|
70
|
+
|
|
71
|
+
host/client_app/.module-tsconfig
|
|
72
|
+
|
|
73
|
+
# Incremental generation cache
|
|
74
|
+
.smpy/gen.json
|
|
75
|
+
.smpy/gen.*.tmp
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Anto Subash
|
|
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,73 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: simple_module_remote
|
|
3
|
+
Version: 0.0.50
|
|
4
|
+
Summary: Client-only engine for a per-project CLI over a simple_module app's REST API
|
|
5
|
+
Project-URL: Homepage, https://github.com/antosubash/simple_module_python
|
|
6
|
+
Project-URL: Repository, https://github.com/antosubash/simple_module_python
|
|
7
|
+
Project-URL: Issues, https://github.com/antosubash/simple_module_python/issues
|
|
8
|
+
Project-URL: Changelog, https://github.com/antosubash/simple_module_python/blob/main/CHANGELOG.md
|
|
9
|
+
Author-email: Anto Subash <antosubash@live.com>
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: cli,openapi,rest,simple-module
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Environment :: Console
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
17
|
+
Classifier: Operating System :: OS Independent
|
|
18
|
+
Classifier: Programming Language :: Python :: 3
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Typing :: Typed
|
|
21
|
+
Requires-Python: >=3.12
|
|
22
|
+
Requires-Dist: click<9,>=8.1
|
|
23
|
+
Requires-Dist: httpx<1,>=0.27
|
|
24
|
+
Requires-Dist: keyring<26,>=25
|
|
25
|
+
Requires-Dist: pyyaml<7,>=6.0
|
|
26
|
+
Requires-Dist: rich<16,>=13
|
|
27
|
+
Requires-Dist: tomli-w<2,>=1.0
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
|
|
30
|
+
# simple_module_remote
|
|
31
|
+
|
|
32
|
+
Client-only engine for a per-project command line over a simple_module app's
|
|
33
|
+
REST API. It reads the app's OpenAPI schema, generates one command per
|
|
34
|
+
operation, and handles environments, credentials, HTTP and output formatting.
|
|
35
|
+
It never imports the server framework: it talks to a running app over HTTP only.
|
|
36
|
+
|
|
37
|
+
## What it provides
|
|
38
|
+
|
|
39
|
+
- Named environments (`url`, optional `tenant`, `protected`) stored in a
|
|
40
|
+
per-CLI user config file, merged over an optional defaults file shipped with
|
|
41
|
+
the project.
|
|
42
|
+
- Environment selection by flag, `<CLI>_ENV` or the saved current environment.
|
|
43
|
+
- Request safety rules: only `/api/` paths, no credentials in URLs, plain
|
|
44
|
+
`http://` refused except for loopback hosts.
|
|
45
|
+
|
|
46
|
+
- `build_cli(name, schema_path, defaults_path)`: a `click` group with `env`,
|
|
47
|
+
`login`, `logout`, `whoami`, `doctor`, `schema`, `api`, `raw` and one group per
|
|
48
|
+
OpenAPI tag, generated from operations marked `x-sm-token`.
|
|
49
|
+
|
|
50
|
+
See `docs/framework/remote-cli.md` and `docs/guide/project-cli.md`.
|
|
51
|
+
|
|
52
|
+
## Install
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
uv add simple_module_remote
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Usage
|
|
59
|
+
|
|
60
|
+
The package is an engine, not a command. A project wraps it in its own CLI
|
|
61
|
+
entry point, naming the CLI so that config lives in `~/.config/<cli>/` and
|
|
62
|
+
environment variables use the `<CLI>_` prefix.
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
from pathlib import Path
|
|
66
|
+
|
|
67
|
+
from simple_module_remote import build_cli
|
|
68
|
+
|
|
69
|
+
main = build_cli("my-app", Path("openapi.json"), None, version="1.0")
|
|
70
|
+
|
|
71
|
+
if __name__ == "__main__":
|
|
72
|
+
main()
|
|
73
|
+
```
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# simple_module_remote
|
|
2
|
+
|
|
3
|
+
Client-only engine for a per-project command line over a simple_module app's
|
|
4
|
+
REST API. It reads the app's OpenAPI schema, generates one command per
|
|
5
|
+
operation, and handles environments, credentials, HTTP and output formatting.
|
|
6
|
+
It never imports the server framework: it talks to a running app over HTTP only.
|
|
7
|
+
|
|
8
|
+
## What it provides
|
|
9
|
+
|
|
10
|
+
- Named environments (`url`, optional `tenant`, `protected`) stored in a
|
|
11
|
+
per-CLI user config file, merged over an optional defaults file shipped with
|
|
12
|
+
the project.
|
|
13
|
+
- Environment selection by flag, `<CLI>_ENV` or the saved current environment.
|
|
14
|
+
- Request safety rules: only `/api/` paths, no credentials in URLs, plain
|
|
15
|
+
`http://` refused except for loopback hosts.
|
|
16
|
+
|
|
17
|
+
- `build_cli(name, schema_path, defaults_path)`: a `click` group with `env`,
|
|
18
|
+
`login`, `logout`, `whoami`, `doctor`, `schema`, `api`, `raw` and one group per
|
|
19
|
+
OpenAPI tag, generated from operations marked `x-sm-token`.
|
|
20
|
+
|
|
21
|
+
See `docs/framework/remote-cli.md` and `docs/guide/project-cli.md`.
|
|
22
|
+
|
|
23
|
+
## Install
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
uv add simple_module_remote
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
The package is an engine, not a command. A project wraps it in its own CLI
|
|
32
|
+
entry point, naming the CLI so that config lives in `~/.config/<cli>/` and
|
|
33
|
+
environment variables use the `<CLI>_` prefix.
|
|
34
|
+
|
|
35
|
+
```python
|
|
36
|
+
from pathlib import Path
|
|
37
|
+
|
|
38
|
+
from simple_module_remote import build_cli
|
|
39
|
+
|
|
40
|
+
main = build_cli("my-app", Path("openapi.json"), None, version="1.0")
|
|
41
|
+
|
|
42
|
+
if __name__ == "__main__":
|
|
43
|
+
main()
|
|
44
|
+
```
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "simple_module_remote"
|
|
3
|
+
version = "0.0.50"
|
|
4
|
+
description = "Client-only engine for a per-project CLI over a simple_module app's REST API"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
requires-python = ">=3.12"
|
|
9
|
+
authors = [{ name = "Anto Subash", email = "antosubash@live.com" }]
|
|
10
|
+
keywords = ["simple-module", "cli", "openapi", "rest"]
|
|
11
|
+
classifiers = [
|
|
12
|
+
"Development Status :: 3 - Alpha",
|
|
13
|
+
"Environment :: Console",
|
|
14
|
+
"Intended Audience :: Developers",
|
|
15
|
+
"License :: OSI Approved :: MIT License",
|
|
16
|
+
"Operating System :: OS Independent",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Programming Language :: Python :: 3.12",
|
|
19
|
+
"Typing :: Typed",
|
|
20
|
+
]
|
|
21
|
+
dependencies = [
|
|
22
|
+
"click>=8.1,<9",
|
|
23
|
+
"httpx>=0.27,<1",
|
|
24
|
+
"keyring>=25,<26",
|
|
25
|
+
"pyyaml>=6.0,<7",
|
|
26
|
+
"rich>=13,<16",
|
|
27
|
+
"tomli-w>=1.0,<2",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[project.urls]
|
|
31
|
+
Homepage = "https://github.com/antosubash/simple_module_python"
|
|
32
|
+
Repository = "https://github.com/antosubash/simple_module_python"
|
|
33
|
+
Issues = "https://github.com/antosubash/simple_module_python/issues"
|
|
34
|
+
Changelog = "https://github.com/antosubash/simple_module_python/blob/main/CHANGELOG.md"
|
|
35
|
+
|
|
36
|
+
[build-system]
|
|
37
|
+
requires = ["hatchling"]
|
|
38
|
+
build-backend = "hatchling.build"
|
|
39
|
+
|
|
40
|
+
[tool.hatch.build.targets.wheel]
|
|
41
|
+
packages = ["simple_module_remote"]
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"""Client-only engine for a per-project CLI over a simple_module app's REST API."""
|
|
2
|
+
|
|
3
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
4
|
+
|
|
5
|
+
try:
|
|
6
|
+
__version__ = version("simple_module_remote")
|
|
7
|
+
except PackageNotFoundError: # pragma: no cover - source checkout without install
|
|
8
|
+
__version__ = "0.0.0"
|
|
9
|
+
|
|
10
|
+
from simple_module_remote.commands.root import build_cli
|
|
11
|
+
|
|
12
|
+
__all__ = ["__version__", "build_cli"]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""commands area of the remote CLI engine."""
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"""``api METHOD PATH``: call any /api/ endpoint with the same safety rules."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
from typing import Any
|
|
7
|
+
from urllib.parse import unquote
|
|
8
|
+
|
|
9
|
+
import click
|
|
10
|
+
|
|
11
|
+
from simple_module_remote.commands.runtime import state_of
|
|
12
|
+
from simple_module_remote.generate.operations import Operation, Param, parse_operations
|
|
13
|
+
from simple_module_remote.generate.pairs import split_pairs
|
|
14
|
+
from simple_module_remote.generate.params import PATH_VAR, click_params_for, param_map
|
|
15
|
+
from simple_module_remote.generate.tree import session_only_operations
|
|
16
|
+
from simple_module_remote.generate.trust import route_key
|
|
17
|
+
from simple_module_remote.http.errors import UnsafePathError
|
|
18
|
+
from simple_module_remote.http.safety import validate_api_path
|
|
19
|
+
from simple_module_remote.output.sanitize import sanitize
|
|
20
|
+
|
|
21
|
+
__all__ = ["api"]
|
|
22
|
+
|
|
23
|
+
METHODS = ("GET", "POST", "PUT", "PATCH", "DELETE")
|
|
24
|
+
BODY_METHODS = ("POST", "PUT", "PATCH", "DELETE")
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _matching(root: dict, method: str, path: str) -> list[Operation]:
|
|
28
|
+
"""Schema operations whose path template matches ``path``: they define its risk."""
|
|
29
|
+
found = []
|
|
30
|
+
for op in parse_operations(root):
|
|
31
|
+
pattern = "[^/]+".join(re.escape(part) for part in PATH_VAR.split(op.path)[::2])
|
|
32
|
+
if op.method == method and re.fullmatch(pattern, path):
|
|
33
|
+
found.append(op)
|
|
34
|
+
return found
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _synthetic(
|
|
38
|
+
method: str,
|
|
39
|
+
path: str,
|
|
40
|
+
queries: list[tuple[str, str]],
|
|
41
|
+
root: dict | None = None,
|
|
42
|
+
*,
|
|
43
|
+
trusted: dict | None,
|
|
44
|
+
) -> Operation:
|
|
45
|
+
"""``trusted`` is the bundled schema when ``root`` is a live one: only it may waive."""
|
|
46
|
+
# Risk comes from the endpoint the server will route to: match the decoded path.
|
|
47
|
+
matched = _matching(root, method, unquote(path)) if root else []
|
|
48
|
+
vouched = _matching(trusted, method, unquote(path)) if trusted is not None else matched
|
|
49
|
+
templates = {route_key(op.method, op.path) for op in matched}
|
|
50
|
+
# A waiver needs one unambiguous route that both schemas agree on.
|
|
51
|
+
waivable = len(templates) == 1 and templates == {
|
|
52
|
+
route_key(op.method, op.path) for op in vouched
|
|
53
|
+
}
|
|
54
|
+
names: dict[str, int] = {}
|
|
55
|
+
for key, _ in queries:
|
|
56
|
+
names[key] = names.get(key, 0) + 1
|
|
57
|
+
params = tuple(
|
|
58
|
+
Param(
|
|
59
|
+
name=key,
|
|
60
|
+
location="query",
|
|
61
|
+
required=False,
|
|
62
|
+
schema={"type": "array"} if count > 1 else {},
|
|
63
|
+
description="",
|
|
64
|
+
)
|
|
65
|
+
for key, count in names.items()
|
|
66
|
+
)
|
|
67
|
+
return Operation(
|
|
68
|
+
operation_id="api",
|
|
69
|
+
method=method,
|
|
70
|
+
path=path,
|
|
71
|
+
group="api",
|
|
72
|
+
name="api",
|
|
73
|
+
params=params,
|
|
74
|
+
body_kind="json" if method in BODY_METHODS else "none",
|
|
75
|
+
body_schema=None,
|
|
76
|
+
summary="",
|
|
77
|
+
description="",
|
|
78
|
+
safe=waivable and all(op.safe for op in vouched),
|
|
79
|
+
confirm=any(op.confirm for op in (*matched, *vouched)),
|
|
80
|
+
# A path that only session-only schema operations match gets the session-only hint.
|
|
81
|
+
token_capable=not (matched and not any(op.token_capable for op in matched)),
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _list_session_only(schema: dict) -> None:
|
|
86
|
+
for op in session_only_operations(schema):
|
|
87
|
+
click.echo(sanitize(f"{op.method} {op.path} {op.operation_id}"))
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def _options() -> list[click.Parameter]:
|
|
91
|
+
template = _synthetic("POST", "/api/x", [], trusted={})
|
|
92
|
+
shared = [p for p in click_params_for(template) if p.name in _SHARED]
|
|
93
|
+
return [
|
|
94
|
+
click.Argument(["method"], required=False, metavar="METHOD"),
|
|
95
|
+
click.Argument(["path"], required=False, metavar="PATH"),
|
|
96
|
+
click.Option(["--query", "queries"], multiple=True, help="Query parameter: key=value."),
|
|
97
|
+
click.Option(
|
|
98
|
+
["--session-only", "session_only"],
|
|
99
|
+
is_flag=True,
|
|
100
|
+
help="List operations a token cannot call, then exit.",
|
|
101
|
+
),
|
|
102
|
+
*shared,
|
|
103
|
+
]
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
_SHARED = frozenset(
|
|
107
|
+
{
|
|
108
|
+
"json_arg",
|
|
109
|
+
"file_arg",
|
|
110
|
+
"sets",
|
|
111
|
+
"set_jsons",
|
|
112
|
+
"dry_run",
|
|
113
|
+
"output",
|
|
114
|
+
"columns",
|
|
115
|
+
"quiet",
|
|
116
|
+
"out",
|
|
117
|
+
"yes",
|
|
118
|
+
}
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _callback(**values: Any) -> None:
|
|
123
|
+
ctx = click.get_current_context()
|
|
124
|
+
state = state_of(ctx)
|
|
125
|
+
if values.pop("session_only"):
|
|
126
|
+
_list_session_only(state.root)
|
|
127
|
+
return
|
|
128
|
+
method, path = values.pop("method"), values.pop("path")
|
|
129
|
+
if not method or not path:
|
|
130
|
+
raise click.UsageError("usage: api METHOD PATH (or --session-only)")
|
|
131
|
+
method = method.upper()
|
|
132
|
+
if method not in METHODS:
|
|
133
|
+
raise click.UsageError(f"METHOD must be one of: {', '.join(METHODS)}")
|
|
134
|
+
try:
|
|
135
|
+
validate_api_path(path)
|
|
136
|
+
except UnsafePathError as exc:
|
|
137
|
+
raise click.UsageError(str(exc)) from None
|
|
138
|
+
if method not in BODY_METHODS and any(
|
|
139
|
+
values.get(k) for k in ("json_arg", "file_arg", "sets", "set_jsons")
|
|
140
|
+
):
|
|
141
|
+
raise click.UsageError(f"{method} takes no request body: drop --json/--file/--set")
|
|
142
|
+
queries = split_pairs(values.pop("queries"), "--query")
|
|
143
|
+
op = _synthetic(method, path, queries, state.root, trusted=state.bundled_root)
|
|
144
|
+
for dest, param in param_map(op).items():
|
|
145
|
+
found = [v for k, v in queries if k == param.name]
|
|
146
|
+
values[dest] = tuple(found) if len(found) > 1 else found[0]
|
|
147
|
+
code = state.run(op, values, generated=False)
|
|
148
|
+
if code:
|
|
149
|
+
ctx.exit(code)
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
api = click.Command(
|
|
153
|
+
"api",
|
|
154
|
+
params=_options(),
|
|
155
|
+
callback=_callback,
|
|
156
|
+
help="Call any /api/ endpoint: api METHOD PATH [--json ... | --set k=v] [--query k=v].",
|
|
157
|
+
)
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"""``login``, ``logout`` and ``whoami``."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import sys
|
|
6
|
+
|
|
7
|
+
import click
|
|
8
|
+
|
|
9
|
+
from simple_module_remote.commands.runtime import WHOAMI_PATH, CliError, fetch_whoami, state_of
|
|
10
|
+
from simple_module_remote.config.credentials import (
|
|
11
|
+
CredentialError,
|
|
12
|
+
StoredCredential,
|
|
13
|
+
credential_key,
|
|
14
|
+
describe_tenant,
|
|
15
|
+
is_single_tenant,
|
|
16
|
+
login_hint,
|
|
17
|
+
pin_kwargs,
|
|
18
|
+
tenant_matches,
|
|
19
|
+
validate_token,
|
|
20
|
+
)
|
|
21
|
+
from simple_module_remote.config.resolve import find_credential
|
|
22
|
+
from simple_module_remote.http.errors import AUTH_STATUSES, ApiError, exit_code_for
|
|
23
|
+
from simple_module_remote.output.sanitize import sanitize
|
|
24
|
+
|
|
25
|
+
__all__ = ["login", "logout", "whoami"]
|
|
26
|
+
|
|
27
|
+
MAX_TOKEN_CHARS = 4096
|
|
28
|
+
NOT_FOUND = 404
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _stdin_is_tty() -> bool:
|
|
32
|
+
return sys.stdin.isatty()
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _api_failure(exc: ApiError, what: str) -> click.ClickException:
|
|
36
|
+
return CliError(f"{what}: {exc.message}", exit_code_for(exc.status))
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class _LoginCommand(click.Command):
|
|
40
|
+
"""Never echoes extra arguments: they may be a token typed on the command line."""
|
|
41
|
+
|
|
42
|
+
def parse_args(self, ctx: click.Context, args: list[str]) -> list[str]:
|
|
43
|
+
try:
|
|
44
|
+
return super().parse_args(ctx, args)
|
|
45
|
+
except click.UsageError:
|
|
46
|
+
raise CliError(
|
|
47
|
+
"login takes no arguments and no token value: use --token (hidden prompt) "
|
|
48
|
+
"or --token-stdin (--env and --tenant go before the word login)"
|
|
49
|
+
) from None
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@click.command(
|
|
53
|
+
"login", cls=_LoginCommand, help="Store a personal access token for the selected environment."
|
|
54
|
+
)
|
|
55
|
+
@click.option("--token", "prompt_token", is_flag=True, help="Prompt for the token (hidden input).")
|
|
56
|
+
@click.option("--token-stdin", is_flag=True, help="Read the token from one line of stdin.")
|
|
57
|
+
@click.pass_context
|
|
58
|
+
def login(ctx: click.Context, prompt_token: bool, token_stdin: bool) -> None:
|
|
59
|
+
state = state_of(ctx)
|
|
60
|
+
if prompt_token and token_stdin:
|
|
61
|
+
raise CliError("choose one of --token and --token-stdin")
|
|
62
|
+
if not (prompt_token or token_stdin):
|
|
63
|
+
raise CliError("browser login arrives in a later release: use --token or --token-stdin")
|
|
64
|
+
env, _ = state.select()
|
|
65
|
+
if token_stdin:
|
|
66
|
+
candidate = sys.stdin.readline(MAX_TOKEN_CHARS + 2).strip()
|
|
67
|
+
if len(candidate) > MAX_TOKEN_CHARS:
|
|
68
|
+
raise CliError(f"the token is longer than {MAX_TOKEN_CHARS} characters")
|
|
69
|
+
else:
|
|
70
|
+
if not _stdin_is_tty():
|
|
71
|
+
raise CliError("stdin is not a terminal: pipe the token to --token-stdin instead")
|
|
72
|
+
candidate = click.prompt("Token", hide_input=True, err=True).strip()
|
|
73
|
+
if not candidate:
|
|
74
|
+
raise CliError("no token provided")
|
|
75
|
+
with state.guard():
|
|
76
|
+
validate_token(candidate, source="the token you entered")
|
|
77
|
+
try:
|
|
78
|
+
with state.client(env, candidate) as client:
|
|
79
|
+
who = fetch_whoami(client)
|
|
80
|
+
except ApiError as exc:
|
|
81
|
+
if exc.status == 0:
|
|
82
|
+
raise _api_failure(
|
|
83
|
+
exc, f"cannot reach {env.name} ({env.url}); nothing was stored"
|
|
84
|
+
) from None
|
|
85
|
+
if exc.status == NOT_FOUND:
|
|
86
|
+
raise _api_failure(
|
|
87
|
+
exc,
|
|
88
|
+
f"{env.url} has no {WHOAMI_PATH}: check the URL and any path prefix; "
|
|
89
|
+
"nothing was stored",
|
|
90
|
+
) from None
|
|
91
|
+
raise _api_failure(exc, "the token was not accepted; nothing was stored") from None
|
|
92
|
+
wanted = state.tenant(env)
|
|
93
|
+
single = bool(wanted) and is_single_tenant(who)
|
|
94
|
+
if single:
|
|
95
|
+
click.echo(
|
|
96
|
+
f"note: {env.name} is a single-tenant server; --tenant {sanitize(wanted)} is ignored",
|
|
97
|
+
err=True,
|
|
98
|
+
)
|
|
99
|
+
elif wanted and not tenant_matches(wanted, who):
|
|
100
|
+
raise CliError(
|
|
101
|
+
f"tenant {wanted!r} matches neither the slug nor the id of the token's tenant "
|
|
102
|
+
f"{describe_tenant(who.get('tenant_slug'), who.get('tenant_id'))}; "
|
|
103
|
+
"nothing was stored"
|
|
104
|
+
)
|
|
105
|
+
store = state.store()
|
|
106
|
+
where = credential_key(env.name, None if single else wanted) # the plain slot when ignored
|
|
107
|
+
with state.guard():
|
|
108
|
+
store.put(
|
|
109
|
+
where,
|
|
110
|
+
StoredCredential(candidate, env.origin, **pin_kwargs(who)),
|
|
111
|
+
)
|
|
112
|
+
if store.using_fallback:
|
|
113
|
+
click.echo("warning: no OS keyring; token stored in a 0600 file", err=True)
|
|
114
|
+
click.echo(f"logged in to {where} as {sanitize(str(who.get('email', 'unknown')))}")
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
@click.command("logout", help="Remove the stored token for the selected environment.")
|
|
118
|
+
@click.pass_context
|
|
119
|
+
def logout(ctx: click.Context) -> None:
|
|
120
|
+
state = state_of(ctx)
|
|
121
|
+
env, _ = state.select()
|
|
122
|
+
tenant = state.tenant(env)
|
|
123
|
+
where = credential_key(env.name, tenant)
|
|
124
|
+
try:
|
|
125
|
+
found = find_credential(state.store(), env, tenant)
|
|
126
|
+
where = found[0] if found else where
|
|
127
|
+
removed = state.store().delete(where)
|
|
128
|
+
except CredentialError as exc:
|
|
129
|
+
raise CliError(str(exc)) from None
|
|
130
|
+
if not removed:
|
|
131
|
+
raise CliError(f"no stored token for {where}")
|
|
132
|
+
click.echo(f"logged out of {where}")
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
@click.command("whoami", help="Show the identity the selected environment sees.")
|
|
136
|
+
@click.pass_context
|
|
137
|
+
def whoami(ctx: click.Context) -> None:
|
|
138
|
+
state = state_of(ctx)
|
|
139
|
+
env, explicit = state.select()
|
|
140
|
+
token = state.token(env, explicit)
|
|
141
|
+
try:
|
|
142
|
+
with state.client(env, token) as client:
|
|
143
|
+
who = fetch_whoami(client)
|
|
144
|
+
except ApiError as exc:
|
|
145
|
+
hint = ""
|
|
146
|
+
if exc.status in AUTH_STATUSES:
|
|
147
|
+
hint = f"\nhint: run `{login_hint(state.cli, env, state.tenant(env))}`"
|
|
148
|
+
raise CliError(f"whoami failed: {exc.message}{hint}", exit_code_for(exc.status)) from None
|
|
149
|
+
if env.protected:
|
|
150
|
+
click.echo("*** PROTECTED ENVIRONMENT ***")
|
|
151
|
+
scopes = who.get("scopes")
|
|
152
|
+
rows = [
|
|
153
|
+
("user", who.get("email")),
|
|
154
|
+
("auth", who.get("auth_kind")),
|
|
155
|
+
("scopes", ", ".join(scopes) if isinstance(scopes, list) else "all"),
|
|
156
|
+
(
|
|
157
|
+
"tenant",
|
|
158
|
+
describe_tenant(who.get("tenant_slug"), who.get("tenant_id"))
|
|
159
|
+
if who.get("tenant_id")
|
|
160
|
+
else "-",
|
|
161
|
+
),
|
|
162
|
+
("env", env.name),
|
|
163
|
+
("url", env.url),
|
|
164
|
+
]
|
|
165
|
+
for label, value in rows:
|
|
166
|
+
click.echo(f"{label + ':':8}{sanitize(str(value))}")
|