onecli-sdk 0.1.0__py3-none-any.whl
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.
- onecli/__init__.py +97 -0
- onecli/_agent_env.py +238 -0
- onecli/_async/__init__.py +1 -0
- onecli/_async/_client.py +431 -0
- onecli/_async/_connected_agent.py +184 -0
- onecli/_async/_http.py +330 -0
- onecli/_async/_io.py +118 -0
- onecli/_async/_resources.py +1293 -0
- onecli/_errors.py +106 -0
- onecli/_js.py +122 -0
- onecli/_keys.py +101 -0
- onecli/_lifetime.py +110 -0
- onecli/_retry.py +116 -0
- onecli/_sync/__init__.py +1 -0
- onecli/_sync/_client.py +426 -0
- onecli/_sync/_connected_agent.py +185 -0
- onecli/_sync/_http.py +329 -0
- onecli/_sync/_io.py +110 -0
- onecli/_sync/_resources.py +1286 -0
- onecli/_url.py +264 -0
- onecli/_version.py +19 -0
- onecli/_wire.py +469 -0
- onecli/gateway.py +454 -0
- onecli/py.typed +0 -0
- onecli/types/__init__.py +229 -0
- onecli/types/_base.py +42 -0
- onecli/types/_openapi.py +1590 -0
- onecli_sdk-0.1.0.dist-info/METADATA +354 -0
- onecli_sdk-0.1.0.dist-info/RECORD +31 -0
- onecli_sdk-0.1.0.dist-info/WHEEL +4 -0
- onecli_sdk-0.1.0.dist-info/licenses/LICENSE +21 -0
onecli/__init__.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"""The OneCLI SDK for Python.
|
|
2
|
+
|
|
3
|
+
``OneCLI`` (and ``AsyncOneCLI``) is the owner part, made with a managing key:
|
|
4
|
+
it manages vaults, their items, agents and approvals. ``OneCLI.agent()`` is
|
|
5
|
+
the agent part, made with an agent's credential: it sees the items of its own
|
|
6
|
+
vault, never a value. ``OneCLI.connect()`` connects an agent with a vault's
|
|
7
|
+
connect token.
|
|
8
|
+
|
|
9
|
+
The API's shapes are in :mod:`onecli.types`; the gateway's error bodies are in
|
|
10
|
+
:mod:`onecli.gateway`.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from onecli import gateway, types
|
|
14
|
+
from onecli._async._client import AsyncOneCLI, AsyncOneCLIAgent
|
|
15
|
+
from onecli._async._connected_agent import AsyncConnectedAgent
|
|
16
|
+
from onecli._async._resources import (
|
|
17
|
+
AsyncActivities,
|
|
18
|
+
AsyncAgents,
|
|
19
|
+
AsyncApprovals,
|
|
20
|
+
AsyncConnectTokens,
|
|
21
|
+
AsyncItems,
|
|
22
|
+
AsyncKeys,
|
|
23
|
+
AsyncOneUseConnectTokens,
|
|
24
|
+
AsyncOrgRules,
|
|
25
|
+
AsyncVaults,
|
|
26
|
+
)
|
|
27
|
+
from onecli._errors import OneCLIApiError, OneCLIError, OneCLIRequestError
|
|
28
|
+
from onecli._keys import AGENT_PREFIX, CONNECT_PREFIX, MANAGING_PREFIX
|
|
29
|
+
from onecli._sync._client import OneCLI, OneCLIAgent
|
|
30
|
+
from onecli._sync._connected_agent import ConnectedAgent
|
|
31
|
+
from onecli._sync._resources import (
|
|
32
|
+
Activities,
|
|
33
|
+
Agents,
|
|
34
|
+
Approvals,
|
|
35
|
+
ConnectTokens,
|
|
36
|
+
Items,
|
|
37
|
+
Keys,
|
|
38
|
+
OneUseConnectTokens,
|
|
39
|
+
OrgRules,
|
|
40
|
+
Vaults,
|
|
41
|
+
)
|
|
42
|
+
from onecli._version import SDK_VERSION
|
|
43
|
+
from onecli._wire import CLIENT_HEADER, NOT_GIVEN, NotGiven, ResponseHook, ResponseInfo
|
|
44
|
+
from onecli.gateway import (
|
|
45
|
+
CONNECTION_ID_HEADER,
|
|
46
|
+
CONNECTIONS_HEADER,
|
|
47
|
+
GatewayError,
|
|
48
|
+
parse_gateway_error,
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
__version__ = SDK_VERSION
|
|
52
|
+
|
|
53
|
+
__all__ = [
|
|
54
|
+
"AGENT_PREFIX",
|
|
55
|
+
"CLIENT_HEADER",
|
|
56
|
+
"CONNECTIONS_HEADER",
|
|
57
|
+
"CONNECTION_ID_HEADER",
|
|
58
|
+
"CONNECT_PREFIX",
|
|
59
|
+
"MANAGING_PREFIX",
|
|
60
|
+
"NOT_GIVEN",
|
|
61
|
+
"SDK_VERSION",
|
|
62
|
+
"Activities",
|
|
63
|
+
"Agents",
|
|
64
|
+
"Approvals",
|
|
65
|
+
"AsyncActivities",
|
|
66
|
+
"AsyncAgents",
|
|
67
|
+
"AsyncApprovals",
|
|
68
|
+
"AsyncConnectTokens",
|
|
69
|
+
"AsyncConnectedAgent",
|
|
70
|
+
"AsyncItems",
|
|
71
|
+
"AsyncKeys",
|
|
72
|
+
"AsyncOneCLI",
|
|
73
|
+
"AsyncOneCLIAgent",
|
|
74
|
+
"AsyncOneUseConnectTokens",
|
|
75
|
+
"AsyncOrgRules",
|
|
76
|
+
"AsyncVaults",
|
|
77
|
+
"ConnectTokens",
|
|
78
|
+
"ConnectedAgent",
|
|
79
|
+
"GatewayError",
|
|
80
|
+
"Items",
|
|
81
|
+
"Keys",
|
|
82
|
+
"NotGiven",
|
|
83
|
+
"OneCLI",
|
|
84
|
+
"OneCLIAgent",
|
|
85
|
+
"OneCLIApiError",
|
|
86
|
+
"OneCLIError",
|
|
87
|
+
"OneCLIRequestError",
|
|
88
|
+
"OneUseConnectTokens",
|
|
89
|
+
"OrgRules",
|
|
90
|
+
"ResponseHook",
|
|
91
|
+
"ResponseInfo",
|
|
92
|
+
"Vaults",
|
|
93
|
+
"__version__",
|
|
94
|
+
"gateway",
|
|
95
|
+
"parse_gateway_error",
|
|
96
|
+
"types",
|
|
97
|
+
]
|
onecli/_agent_env.py
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
"""What a sandbox needs to send its calls through the gateway as one agent.
|
|
2
|
+
|
|
3
|
+
The environment, the one setup command, the hosts to allow, and the address
|
|
4
|
+
form: pure functions of the server's answer.
|
|
5
|
+
|
|
6
|
+
The environment has the names the connect script writes into ``agent.env``,
|
|
7
|
+
with the same values, so a sandbox started with it and a sandbox that ran the
|
|
8
|
+
script behave the same. One value says more: ``NO_PROXY`` always names the API
|
|
9
|
+
and the gateway. Two more names (``ONECLI_HOME``, ``ONECLI_PROFILE``) tell the
|
|
10
|
+
script where to put its files, so the paths below are known before it runs.
|
|
11
|
+
|
|
12
|
+
Every value is checked against a strict set of characters first: they come
|
|
13
|
+
from a server and end up in a shell's environment.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import re
|
|
19
|
+
from collections.abc import Mapping
|
|
20
|
+
from dataclasses import dataclass
|
|
21
|
+
from typing import Final, Literal, cast
|
|
22
|
+
|
|
23
|
+
from onecli._errors import OneCLIError
|
|
24
|
+
from onecli._js import js_trim
|
|
25
|
+
from onecli._url import HttpUrl, parse_http_url
|
|
26
|
+
|
|
27
|
+
__all__ = [
|
|
28
|
+
"DEFAULT_AGENT_HOME",
|
|
29
|
+
"SETUP_COMMAND",
|
|
30
|
+
"allow_hosts_for",
|
|
31
|
+
"build_agent_env",
|
|
32
|
+
"check_home",
|
|
33
|
+
"gateway_base_url",
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
DEFAULT_AGENT_HOME: Final = "/tmp/onecli" # noqa: S108 - a folder in the sandbox, not here
|
|
37
|
+
"""Where the setup command puts its files in the sandbox, unless ``home`` says otherwise."""
|
|
38
|
+
|
|
39
|
+
# The connect script's own rules for the same values.
|
|
40
|
+
_CREDENTIAL_RE: Final = re.compile(r"oc_agent_[A-Za-z0-9_]+")
|
|
41
|
+
_ID_RE: Final = re.compile(r"[A-Za-z0-9_-]+")
|
|
42
|
+
_ADDRESS_RE: Final = re.compile(r"[A-Za-z0-9.-]+:[0-9]{1,5}")
|
|
43
|
+
# The API's address: http or https, a plain host name or an IPv6 address in brackets, a plain path.
|
|
44
|
+
_API_URL_RE: Final = re.compile(
|
|
45
|
+
r"https?://(?:[A-Za-z0-9._-]+|\[[0-9A-Fa-f:.]+\])(:[0-9]+)?(/[A-Za-z0-9._/-]*)?"
|
|
46
|
+
)
|
|
47
|
+
_HOME_RE: Final = re.compile(r"/[A-Za-z0-9._/-]+")
|
|
48
|
+
# A host for the address form: a name or an IPv4 address, with a port if it has one.
|
|
49
|
+
_UPSTREAM_RE: Final = re.compile(r"[a-z0-9](?:[a-z0-9.-]*[a-z0-9])?(?::([0-9]{1,5}))?")
|
|
50
|
+
_PORT_MAX: Final = 65535
|
|
51
|
+
|
|
52
|
+
SETUP_COMMAND: Final = (
|
|
53
|
+
"s=$(env -u CURL_CA_BUNDLE -u SSL_CERT_FILE curl --noproxy '*' -fsSL "
|
|
54
|
+
'"$ONECLI_API_HOST/v1/install/connect")'
|
|
55
|
+
' && printf %s "$s" | sh -s -- --credential "$ONECLI_AGENT_CREDENTIAL"'
|
|
56
|
+
)
|
|
57
|
+
"""The one line to run once in the sandbox, in a shell.
|
|
58
|
+
|
|
59
|
+
It fetches the connect script from the API and runs it with the credential
|
|
60
|
+
that is already in the environment, so the line itself holds no secret. The
|
|
61
|
+
script writes the two certificate files the environment points at. Running it
|
|
62
|
+
again is safe.
|
|
63
|
+
|
|
64
|
+
The fetch must not go through the gateway and must not use the certificate
|
|
65
|
+
files, which do not exist yet. The script is read whole before it runs, so a
|
|
66
|
+
download that was cut off runs nothing.
|
|
67
|
+
"""
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
@dataclass(frozen=True, slots=True)
|
|
71
|
+
class _Gateway:
|
|
72
|
+
scheme: Literal["http", "https"]
|
|
73
|
+
address: str
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _part(value: object, name: str) -> object:
|
|
77
|
+
"""A field of the server's gateway: a model's attribute or a mapping's key."""
|
|
78
|
+
if isinstance(value, Mapping):
|
|
79
|
+
return cast("Mapping[str, object]", value).get(name)
|
|
80
|
+
return getattr(value, name, None)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def check_home(home: object) -> str:
|
|
84
|
+
"""Refuse a folder that is not a plain absolute path. Returns it with no slash at the end."""
|
|
85
|
+
if not isinstance(home, str) or not _HOME_RE.fullmatch(home) or ".." in home.split("/"):
|
|
86
|
+
msg = (
|
|
87
|
+
"home must be an absolute path in the sandbox made of letters, digits, dots, dashes, "
|
|
88
|
+
"underscores and slashes, like /tmp/onecli."
|
|
89
|
+
)
|
|
90
|
+
raise OneCLIError(msg)
|
|
91
|
+
trimmed = home.rstrip("/")
|
|
92
|
+
if trimmed == "":
|
|
93
|
+
msg = "home cannot be the root folder."
|
|
94
|
+
raise OneCLIError(msg)
|
|
95
|
+
return trimmed
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def _host_of_address(address: str) -> str:
|
|
99
|
+
return address[: address.rindex(":")]
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def _check_gateway(gateway: object) -> _Gateway:
|
|
103
|
+
if gateway is None or isinstance(gateway, str | int | float):
|
|
104
|
+
msg = "The server's answer had no gateway."
|
|
105
|
+
raise OneCLIError(msg)
|
|
106
|
+
scheme = _part(gateway, "scheme")
|
|
107
|
+
address = _part(gateway, "address")
|
|
108
|
+
if scheme not in {"http", "https"}:
|
|
109
|
+
msg = "The server's gateway scheme is not http or https."
|
|
110
|
+
raise OneCLIError(msg)
|
|
111
|
+
if not isinstance(address, str) or not _ADDRESS_RE.fullmatch(address):
|
|
112
|
+
msg = "The server's gateway address is not a plain host and port."
|
|
113
|
+
raise OneCLIError(msg)
|
|
114
|
+
return _Gateway(scheme="https" if scheme == "https" else "http", address=address)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def _check_api_url(api_url: object) -> HttpUrl:
|
|
118
|
+
if not isinstance(api_url, str) or not _API_URL_RE.fullmatch(api_url):
|
|
119
|
+
msg = (
|
|
120
|
+
"The API address cannot go into a sandbox's environment: it must be http or https, "
|
|
121
|
+
"a plain host name, and a plain path."
|
|
122
|
+
)
|
|
123
|
+
raise OneCLIError(msg)
|
|
124
|
+
parsed = parse_http_url(api_url)
|
|
125
|
+
if parsed is None:
|
|
126
|
+
msg = "The API address is not a valid URL."
|
|
127
|
+
raise OneCLIError(msg)
|
|
128
|
+
return parsed
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def _no_proxy_for(api: HttpUrl, gateway: _Gateway) -> str:
|
|
132
|
+
"""NO_PROXY: calls to the API and to the gateway's own address go straight there.
|
|
133
|
+
|
|
134
|
+
Both hosts are in ``allow_hosts``, so the direct call is always open. The
|
|
135
|
+
gateway is named for the address form (``base_url``): that call already
|
|
136
|
+
goes to the gateway, and sent through the proxy as well it fails from
|
|
137
|
+
Node's http and https modules in a sandbox that allows hosts by name,
|
|
138
|
+
because they give an HTTPS proxy no server name (nodejs/node#66718).
|
|
139
|
+
|
|
140
|
+
An IPv6 address is left out, as the connect script does: clients do not
|
|
141
|
+
agree on how to spell one here.
|
|
142
|
+
"""
|
|
143
|
+
hosts = ["localhost", "127.0.0.1"]
|
|
144
|
+
for host in (api.host, _host_of_address(gateway.address)):
|
|
145
|
+
if host and ":" not in host and not host.startswith("[") and host not in hosts:
|
|
146
|
+
hosts.append(host)
|
|
147
|
+
return ",".join(hosts)
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def build_agent_env(
|
|
151
|
+
*,
|
|
152
|
+
credential: object,
|
|
153
|
+
agent_id: object,
|
|
154
|
+
vault_id: object,
|
|
155
|
+
gateway: object,
|
|
156
|
+
api_url: object,
|
|
157
|
+
home: object,
|
|
158
|
+
) -> dict[str, str]:
|
|
159
|
+
"""The environment for the sandbox. It holds the agent's credential.
|
|
160
|
+
|
|
161
|
+
``gateway`` is the server's answer; ``api_url`` is the API's address as the
|
|
162
|
+
sandbox reaches it; ``home`` is the folder in the sandbox for the
|
|
163
|
+
certificate files. Every value is checked first.
|
|
164
|
+
"""
|
|
165
|
+
if not isinstance(credential, str) or not _CREDENTIAL_RE.fullmatch(credential):
|
|
166
|
+
msg = "The server's answer had no agent credential that is safe to use."
|
|
167
|
+
raise OneCLIError(msg)
|
|
168
|
+
if not (
|
|
169
|
+
isinstance(agent_id, str)
|
|
170
|
+
and isinstance(vault_id, str)
|
|
171
|
+
and _ID_RE.fullmatch(agent_id)
|
|
172
|
+
and _ID_RE.fullmatch(vault_id)
|
|
173
|
+
):
|
|
174
|
+
msg = "The server's agent id or vault id is not safe to use."
|
|
175
|
+
raise OneCLIError(msg)
|
|
176
|
+
checked = _check_gateway(gateway)
|
|
177
|
+
api = _check_api_url(api_url)
|
|
178
|
+
folder = check_home(home)
|
|
179
|
+
|
|
180
|
+
proxy = f"{checked.scheme}://x:{credential}@{checked.address}"
|
|
181
|
+
no_proxy = _no_proxy_for(api, checked)
|
|
182
|
+
# The gateway's certificate alone, for a setting that ADDS to a trust store.
|
|
183
|
+
ca_file = f"{folder}/gateway-ca.pem"
|
|
184
|
+
# The system's roots and the gateway's certificate, for a setting that REPLACES one.
|
|
185
|
+
bundle_file = f"{folder}/ca-bundle.pem"
|
|
186
|
+
|
|
187
|
+
return {
|
|
188
|
+
"ONECLI_AGENT_ID": agent_id,
|
|
189
|
+
"ONECLI_VAULT_ID": vault_id,
|
|
190
|
+
"ONECLI_AGENT_CREDENTIAL": credential,
|
|
191
|
+
"ONECLI_API_HOST": api.raw,
|
|
192
|
+
"ONECLI_HOME": folder,
|
|
193
|
+
# The script adds one line to a shell profile. A sandbox whose
|
|
194
|
+
# environment is already set needs none.
|
|
195
|
+
"ONECLI_PROFILE": "/dev/null",
|
|
196
|
+
"HTTPS_PROXY": proxy,
|
|
197
|
+
"HTTP_PROXY": proxy,
|
|
198
|
+
"https_proxy": proxy,
|
|
199
|
+
"http_proxy": proxy,
|
|
200
|
+
"NO_PROXY": no_proxy,
|
|
201
|
+
"no_proxy": no_proxy,
|
|
202
|
+
"NODE_EXTRA_CA_CERTS": ca_file,
|
|
203
|
+
"NODE_USE_ENV_PROXY": "1",
|
|
204
|
+
"SSL_CERT_FILE": bundle_file,
|
|
205
|
+
"CURL_CA_BUNDLE": bundle_file,
|
|
206
|
+
"REQUESTS_CA_BUNDLE": bundle_file,
|
|
207
|
+
"GIT_SSL_CAINFO": bundle_file,
|
|
208
|
+
"PGSSLROOTCERT": bundle_file,
|
|
209
|
+
"AWS_CA_BUNDLE": bundle_file,
|
|
210
|
+
"DENO_CERT": bundle_file,
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def allow_hosts_for(api_url: object, gateway: object) -> list[str]:
|
|
215
|
+
"""The hosts a sandbox must reach, for an egress allow list: the gateway and the API."""
|
|
216
|
+
hosts = [
|
|
217
|
+
_host_of_address(_check_gateway(gateway).address),
|
|
218
|
+
_check_api_url(api_url).bare_host,
|
|
219
|
+
]
|
|
220
|
+
return list(dict.fromkeys(hosts))
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def gateway_base_url(gateway: object, host: object) -> str:
|
|
224
|
+
"""The address form of one API's host: a base URL on the gateway.
|
|
225
|
+
|
|
226
|
+
For a client that cannot use a proxy. The client must send the agent's
|
|
227
|
+
credential as ``Authorization: Bearer``.
|
|
228
|
+
"""
|
|
229
|
+
upstream = js_trim(host).lower() if isinstance(host, str) else ""
|
|
230
|
+
match = _UPSTREAM_RE.fullmatch(upstream)
|
|
231
|
+
if not match or (match.group(1) is not None and int(match.group(1)) > _PORT_MAX):
|
|
232
|
+
msg = "base_url: give the API's host, like api.example.com, with no scheme and no path."
|
|
233
|
+
raise OneCLIError(msg)
|
|
234
|
+
checked = _check_gateway(gateway)
|
|
235
|
+
standard_port = ":443" if checked.scheme == "https" else ":80"
|
|
236
|
+
address = checked.address
|
|
237
|
+
authority = address.removesuffix(standard_port)
|
|
238
|
+
return f"{checked.scheme}://{authority}/r/{upstream}"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""The async client: the hand-written source of truth. ``_sync`` is made from it."""
|