segmentstream-connections 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.
- segmentstream_connections/__init__.py +85 -0
- segmentstream_connections/__main__.py +18 -0
- segmentstream_connections/_api_key.py +245 -0
- segmentstream_connections/_api_key_presets.py +378 -0
- segmentstream_connections/_bigquery.py +35 -0
- segmentstream_connections/_client_credentials.py +200 -0
- segmentstream_connections/_evaluation.py +36 -0
- segmentstream_connections/_http_configuration.py +59 -0
- segmentstream_connections/_inspection.py +132 -0
- segmentstream_connections/_loader.py +71 -0
- segmentstream_connections/_managed.py +320 -0
- segmentstream_connections/_oauth2.py +463 -0
- segmentstream_connections/py.typed +0 -0
- segmentstream_connections-0.1.0.dist-info/METADATA +51 -0
- segmentstream_connections-0.1.0.dist-info/RECORD +18 -0
- segmentstream_connections-0.1.0.dist-info/WHEEL +4 -0
- segmentstream_connections-0.1.0.dist-info/licenses/LICENSE +201 -0
- segmentstream_connections-0.1.0.dist-info/licenses/NOTICE +2 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"""Workspace connection declarations.
|
|
2
|
+
|
|
3
|
+
`connections/connections.py` declares each provider a workspace reaches through the connection
|
|
4
|
+
proxy. Pipelines, the ontology and the agent refer to a declaration by its key; this package holds
|
|
5
|
+
only the declarations and their inspection, never credentials or a runtime client.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from segmentstream_connections._api_key import ApiKeyConnection, Bearer
|
|
9
|
+
from segmentstream_connections._api_key_presets import (
|
|
10
|
+
ApifyConnection,
|
|
11
|
+
ApolloConnection,
|
|
12
|
+
DeelConnection,
|
|
13
|
+
MercuryConnection,
|
|
14
|
+
ResendConnection,
|
|
15
|
+
StarlingConnection,
|
|
16
|
+
StripeConnection,
|
|
17
|
+
SupabaseConnection,
|
|
18
|
+
TelegramBotConnection,
|
|
19
|
+
WhatsAppBusinessConnection,
|
|
20
|
+
WiseConnection,
|
|
21
|
+
)
|
|
22
|
+
from segmentstream_connections._bigquery import BigQueryConnection
|
|
23
|
+
from segmentstream_connections._client_credentials import (
|
|
24
|
+
ClientCredentialsConnection,
|
|
25
|
+
XeroConnection,
|
|
26
|
+
)
|
|
27
|
+
from segmentstream_connections._evaluation import EvaluationConnection
|
|
28
|
+
from segmentstream_connections._managed import (
|
|
29
|
+
GmailConnection,
|
|
30
|
+
GoogleAdsConnection,
|
|
31
|
+
GoogleSheetsConnection,
|
|
32
|
+
HubSpotConnection,
|
|
33
|
+
LinkedInAdsConnection,
|
|
34
|
+
LinkedInCommunityConnection,
|
|
35
|
+
MetaAdsConnection,
|
|
36
|
+
MicrosoftAdsConnection,
|
|
37
|
+
TikTokAdsConnection,
|
|
38
|
+
WhatsAppConnection,
|
|
39
|
+
XAdsConnection,
|
|
40
|
+
)
|
|
41
|
+
from segmentstream_connections._oauth2 import (
|
|
42
|
+
OAuth2Connection,
|
|
43
|
+
OAuth2TokenSet,
|
|
44
|
+
Secret,
|
|
45
|
+
SecretReference,
|
|
46
|
+
SlackConnection,
|
|
47
|
+
secret_ref,
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
__all__ = [
|
|
51
|
+
"ApiKeyConnection",
|
|
52
|
+
"ApifyConnection",
|
|
53
|
+
"ApolloConnection",
|
|
54
|
+
"Bearer",
|
|
55
|
+
"BigQueryConnection",
|
|
56
|
+
"ClientCredentialsConnection",
|
|
57
|
+
"DeelConnection",
|
|
58
|
+
"EvaluationConnection",
|
|
59
|
+
"GmailConnection",
|
|
60
|
+
"GoogleAdsConnection",
|
|
61
|
+
"GoogleSheetsConnection",
|
|
62
|
+
"HubSpotConnection",
|
|
63
|
+
"LinkedInAdsConnection",
|
|
64
|
+
"LinkedInCommunityConnection",
|
|
65
|
+
"MercuryConnection",
|
|
66
|
+
"MetaAdsConnection",
|
|
67
|
+
"MicrosoftAdsConnection",
|
|
68
|
+
"OAuth2Connection",
|
|
69
|
+
"OAuth2TokenSet",
|
|
70
|
+
"ResendConnection",
|
|
71
|
+
"Secret",
|
|
72
|
+
"SecretReference",
|
|
73
|
+
"SlackConnection",
|
|
74
|
+
"StarlingConnection",
|
|
75
|
+
"StripeConnection",
|
|
76
|
+
"SupabaseConnection",
|
|
77
|
+
"TelegramBotConnection",
|
|
78
|
+
"TikTokAdsConnection",
|
|
79
|
+
"WhatsAppBusinessConnection",
|
|
80
|
+
"WhatsAppConnection",
|
|
81
|
+
"WiseConnection",
|
|
82
|
+
"XAdsConnection",
|
|
83
|
+
"XeroConnection",
|
|
84
|
+
"secret_ref",
|
|
85
|
+
]
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
from segmentstream_connections._inspection import inspect_connections
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def main() -> None:
|
|
8
|
+
parser = argparse.ArgumentParser(
|
|
9
|
+
description="Inspect connection declarations in an isolated build"
|
|
10
|
+
)
|
|
11
|
+
parser.add_argument("command", choices=["inspect"])
|
|
12
|
+
parser.add_argument("output", type=Path)
|
|
13
|
+
arguments = parser.parse_args()
|
|
14
|
+
inspect_connections(arguments.output)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
if __name__ == "__main__":
|
|
18
|
+
main()
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
"""Static API-key declarations; the backend attaches the configured values."""
|
|
2
|
+
|
|
3
|
+
import hashlib
|
|
4
|
+
import re
|
|
5
|
+
from collections.abc import Mapping
|
|
6
|
+
from typing import ClassVar, Literal, Self
|
|
7
|
+
|
|
8
|
+
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator
|
|
9
|
+
|
|
10
|
+
from segmentstream_connections._oauth2 import Secret, SecretReference
|
|
11
|
+
|
|
12
|
+
_CONNECTION_KEY = re.compile(r"^[a-z][a-z0-9_]{0,63}$")
|
|
13
|
+
_PROVIDER_KEY = re.compile(r"^[a-z][a-z0-9-]{0,63}$")
|
|
14
|
+
# Owned secret names embed the field key and must stay within 80 characters.
|
|
15
|
+
_FIELD_KEY = re.compile(r"^[a-z][a-z0-9_]{0,31}$")
|
|
16
|
+
_HEADER_NAME = re.compile(r"^[!#$%&'*+.^_`|~0-9A-Za-z-]{1,128}$")
|
|
17
|
+
_PARAMETER_NAME = re.compile(r"^[A-Za-z][A-Za-z0-9._~-]{0,127}$")
|
|
18
|
+
# The literal start of a path segment, such as Telegram's ``bot``; the value follows it.
|
|
19
|
+
_PATH_PREFIX = re.compile(r"^[A-Za-z][A-Za-z0-9._~-]{0,31}$")
|
|
20
|
+
_MAXIMUM_CREDENTIALS = 8
|
|
21
|
+
# Transport and proxy headers SegmentStream controls; `Authorization` stays declarable.
|
|
22
|
+
_RESERVED_HEADERS = {
|
|
23
|
+
"accept-encoding",
|
|
24
|
+
"connection",
|
|
25
|
+
"content-length",
|
|
26
|
+
"cookie",
|
|
27
|
+
"forwarded",
|
|
28
|
+
"host",
|
|
29
|
+
"keep-alive",
|
|
30
|
+
"proxy-authenticate",
|
|
31
|
+
"proxy-authorization",
|
|
32
|
+
"set-cookie",
|
|
33
|
+
"te",
|
|
34
|
+
"trailer",
|
|
35
|
+
"transfer-encoding",
|
|
36
|
+
"upgrade",
|
|
37
|
+
}
|
|
38
|
+
_RESERVED_HEADER_PREFIXES = ("proxy-", "x-forwarded-", "x-segmentstream-")
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class ApiKeyCredential(BaseModel):
|
|
42
|
+
"""One configured value the backend sends as a header, query parameter or path segment."""
|
|
43
|
+
|
|
44
|
+
model_config = ConfigDict(extra="forbid", frozen=True)
|
|
45
|
+
|
|
46
|
+
field: str
|
|
47
|
+
location: Literal["header", "query", "path"]
|
|
48
|
+
name: str
|
|
49
|
+
scheme: str | None = None
|
|
50
|
+
secret: SecretReference
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class Bearer(BaseModel):
|
|
54
|
+
"""A header value the backend sends as ``Bearer <value>``.
|
|
55
|
+
|
|
56
|
+
Wrap the `Secret(...)` or `secret_ref(...)` of an `Authorization` header so users
|
|
57
|
+
configure only the token, for example ``{"Authorization": Bearer(Secret())}``.
|
|
58
|
+
"""
|
|
59
|
+
|
|
60
|
+
model_config = ConfigDict(extra="forbid", frozen=True)
|
|
61
|
+
|
|
62
|
+
scheme: ClassVar[str] = "Bearer"
|
|
63
|
+
secret: SecretReference
|
|
64
|
+
|
|
65
|
+
def __init__(self, secret: SecretReference, /) -> None:
|
|
66
|
+
super().__init__(secret=secret)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class ApiKeyConnection(BaseModel):
|
|
70
|
+
"""HTTP access authenticated by static values, such as API keys or tokens.
|
|
71
|
+
|
|
72
|
+
Each entry in `headers` or `query` maps the name the provider expects to a
|
|
73
|
+
`Secret(...)` configured after deployment, or to `secret_ref(...)` for a shared
|
|
74
|
+
workspace secret. The value is sent unchanged; wrap a header's secret in
|
|
75
|
+
`Bearer(...)` to send ``Bearer <value>``. For providers that read the value from the
|
|
76
|
+
URL path, `path` maps one segment prefix to its secret: ``path={"bot": Secret()}``
|
|
77
|
+
sends ``<api>/bot<value>/<request path>``. The field key is the name in lowercase with
|
|
78
|
+
hyphens and dots replaced by underscores, for example `Client-Id` -> `client_id`.
|
|
79
|
+
|
|
80
|
+
Set `public=True` for an API with no authentication. Public connections must
|
|
81
|
+
have no header, query or path credentials and are ready immediately after deployment.
|
|
82
|
+
"""
|
|
83
|
+
|
|
84
|
+
model_config = ConfigDict(extra="forbid", frozen=True)
|
|
85
|
+
|
|
86
|
+
type: ClassVar[Literal["api_key"]] = "api_key"
|
|
87
|
+
key: str
|
|
88
|
+
name: str
|
|
89
|
+
provider: str
|
|
90
|
+
public: bool = Field(default=False, strict=True)
|
|
91
|
+
read_only: bool = Field(default=False, strict=True)
|
|
92
|
+
headers: Mapping[str, SecretReference | Bearer] = Field(default_factory=dict)
|
|
93
|
+
query: Mapping[str, SecretReference] = Field(default_factory=dict)
|
|
94
|
+
path: Mapping[str, SecretReference] = Field(default_factory=dict)
|
|
95
|
+
apis: dict[str, str]
|
|
96
|
+
default_api: str | None = None
|
|
97
|
+
|
|
98
|
+
@field_validator("key")
|
|
99
|
+
@classmethod
|
|
100
|
+
def validate_key(cls, value: str) -> str:
|
|
101
|
+
key = value.strip()
|
|
102
|
+
if _CONNECTION_KEY.fullmatch(key) is None:
|
|
103
|
+
raise ValueError(
|
|
104
|
+
"connection key must be a lowercase identifier containing only "
|
|
105
|
+
"letters, numbers, and underscores"
|
|
106
|
+
)
|
|
107
|
+
return key
|
|
108
|
+
|
|
109
|
+
@field_validator("name")
|
|
110
|
+
@classmethod
|
|
111
|
+
def validate_name(cls, value: str) -> str:
|
|
112
|
+
name = " ".join(value.split())
|
|
113
|
+
if not 2 <= len(name) <= 80:
|
|
114
|
+
raise ValueError("connection name must contain 2 to 80 characters")
|
|
115
|
+
return name
|
|
116
|
+
|
|
117
|
+
@field_validator("provider")
|
|
118
|
+
@classmethod
|
|
119
|
+
def validate_provider(cls, value: str) -> str:
|
|
120
|
+
provider = value.strip()
|
|
121
|
+
if _PROVIDER_KEY.fullmatch(provider) is None:
|
|
122
|
+
raise ValueError(
|
|
123
|
+
"provider must be a lowercase identifier containing only "
|
|
124
|
+
"letters, numbers, and hyphens"
|
|
125
|
+
)
|
|
126
|
+
return provider
|
|
127
|
+
|
|
128
|
+
@field_validator("headers")
|
|
129
|
+
@classmethod
|
|
130
|
+
def validate_headers(
|
|
131
|
+
cls, value: Mapping[str, SecretReference | Bearer]
|
|
132
|
+
) -> dict[str, SecretReference | Bearer]:
|
|
133
|
+
for name in value:
|
|
134
|
+
lower = name.lower()
|
|
135
|
+
if (
|
|
136
|
+
_HEADER_NAME.fullmatch(name) is None
|
|
137
|
+
or lower in _RESERVED_HEADERS
|
|
138
|
+
or lower.startswith(_RESERVED_HEADER_PREFIXES)
|
|
139
|
+
):
|
|
140
|
+
raise ValueError(f"header {name!r} cannot carry an API credential")
|
|
141
|
+
lowered = [name.lower() for name in value]
|
|
142
|
+
if len(set(lowered)) != len(lowered):
|
|
143
|
+
raise ValueError("credential header names must be unique regardless of case")
|
|
144
|
+
return dict(value)
|
|
145
|
+
|
|
146
|
+
@field_validator("query")
|
|
147
|
+
@classmethod
|
|
148
|
+
def validate_query(cls, value: Mapping[str, SecretReference]) -> dict[str, SecretReference]:
|
|
149
|
+
for name in value:
|
|
150
|
+
if _PARAMETER_NAME.fullmatch(name) is None:
|
|
151
|
+
raise ValueError(f"query parameter {name!r} is invalid")
|
|
152
|
+
return dict(value)
|
|
153
|
+
|
|
154
|
+
@field_validator("path")
|
|
155
|
+
@classmethod
|
|
156
|
+
def validate_path(cls, value: Mapping[str, SecretReference]) -> dict[str, SecretReference]:
|
|
157
|
+
if len(value) > 1:
|
|
158
|
+
raise ValueError("an API-key connection may send at most one path value")
|
|
159
|
+
for name in value:
|
|
160
|
+
if _PATH_PREFIX.fullmatch(name) is None:
|
|
161
|
+
raise ValueError(f"path prefix {name!r} is invalid")
|
|
162
|
+
return dict(value)
|
|
163
|
+
|
|
164
|
+
@field_validator("apis")
|
|
165
|
+
@classmethod
|
|
166
|
+
def validate_apis(cls, value: dict[str, str]) -> dict[str, str]:
|
|
167
|
+
from segmentstream_connections._http_configuration import validate_http_apis
|
|
168
|
+
|
|
169
|
+
if not value:
|
|
170
|
+
raise ValueError("API-key connections must declare at least one HTTP API")
|
|
171
|
+
return validate_http_apis(value)
|
|
172
|
+
|
|
173
|
+
@model_validator(mode="after")
|
|
174
|
+
def validate_connection(self) -> Self:
|
|
175
|
+
if self.default_api is not None and self.default_api not in self.apis:
|
|
176
|
+
raise ValueError("default_api must name a declared HTTP API")
|
|
177
|
+
credentials = self.credentials
|
|
178
|
+
if self.public and credentials:
|
|
179
|
+
raise ValueError("public connections cannot declare credentials")
|
|
180
|
+
if not self.public and not credentials:
|
|
181
|
+
raise ValueError(
|
|
182
|
+
"API-key connections must declare at least one header, query or path value"
|
|
183
|
+
)
|
|
184
|
+
if len(credentials) > _MAXIMUM_CREDENTIALS:
|
|
185
|
+
raise ValueError(
|
|
186
|
+
f"API-key connections may declare at most {_MAXIMUM_CREDENTIALS} values"
|
|
187
|
+
)
|
|
188
|
+
fields = [credential.field for credential in credentials]
|
|
189
|
+
duplicates = sorted({field for field in fields if fields.count(field) > 1})
|
|
190
|
+
if duplicates:
|
|
191
|
+
raise ValueError(
|
|
192
|
+
"credential names must map to distinct field keys: " + ", ".join(duplicates)
|
|
193
|
+
)
|
|
194
|
+
references = [(item.secret.type, item.secret.name) for item in credentials]
|
|
195
|
+
if len(set(references)) != len(references):
|
|
196
|
+
raise ValueError("credential values must use different secret references")
|
|
197
|
+
return self
|
|
198
|
+
|
|
199
|
+
@property
|
|
200
|
+
def credentials(self) -> tuple[ApiKeyCredential, ...]:
|
|
201
|
+
"""Declared values in manifest order, with owned secrets bound to their fields."""
|
|
202
|
+
|
|
203
|
+
identity = hashlib.sha256(self.key.encode()).hexdigest()[:32]
|
|
204
|
+
entries: list[
|
|
205
|
+
tuple[Literal["header", "query", "path"], str, SecretReference, str | None]
|
|
206
|
+
] = [
|
|
207
|
+
*(
|
|
208
|
+
("header", name, value.secret, value.scheme)
|
|
209
|
+
if isinstance(value, Bearer)
|
|
210
|
+
else ("header", name, value, None)
|
|
211
|
+
for name, value in self.headers.items()
|
|
212
|
+
),
|
|
213
|
+
*(("query", name, reference, None) for name, reference in self.query.items()),
|
|
214
|
+
*(("path", name, reference, None) for name, reference in self.path.items()),
|
|
215
|
+
]
|
|
216
|
+
result: list[ApiKeyCredential] = []
|
|
217
|
+
for location, name, reference, scheme in entries:
|
|
218
|
+
field = _field_key(name)
|
|
219
|
+
secret = (
|
|
220
|
+
SecretReference(
|
|
221
|
+
type="owned_secret",
|
|
222
|
+
name=f"connection-{identity}-{field.replace('_', '-')}",
|
|
223
|
+
label=reference.label or name,
|
|
224
|
+
description=reference.description,
|
|
225
|
+
)
|
|
226
|
+
if isinstance(reference, Secret)
|
|
227
|
+
else reference
|
|
228
|
+
)
|
|
229
|
+
result.append(
|
|
230
|
+
ApiKeyCredential(
|
|
231
|
+
field=field,
|
|
232
|
+
location=location,
|
|
233
|
+
name=name,
|
|
234
|
+
scheme=scheme,
|
|
235
|
+
secret=secret,
|
|
236
|
+
)
|
|
237
|
+
)
|
|
238
|
+
return tuple(result)
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
def _field_key(name: str) -> str:
|
|
242
|
+
field = re.sub(r"[^a-z0-9]+", "_", name.lower()).strip("_")
|
|
243
|
+
if _FIELD_KEY.fullmatch(field) is None:
|
|
244
|
+
raise ValueError(f"credential name {name!r} does not form a valid field key")
|
|
245
|
+
return field
|