custom-domain-sdk 0.1.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.
- custom_domain_sdk-0.1.0/.gitignore +16 -0
- custom_domain_sdk-0.1.0/LICENSE +201 -0
- custom_domain_sdk-0.1.0/PKG-INFO +189 -0
- custom_domain_sdk-0.1.0/README.md +164 -0
- custom_domain_sdk-0.1.0/custom_domain/__init__.py +62 -0
- custom_domain_sdk-0.1.0/custom_domain/assertion.py +102 -0
- custom_domain_sdk-0.1.0/custom_domain/client.py +244 -0
- custom_domain_sdk-0.1.0/custom_domain/errors.py +64 -0
- custom_domain_sdk-0.1.0/custom_domain/middleware.py +138 -0
- custom_domain_sdk-0.1.0/custom_domain/models.py +164 -0
- custom_domain_sdk-0.1.0/custom_domain/py.typed +0 -0
- custom_domain_sdk-0.1.0/custom_domain/webhooks.py +79 -0
- custom_domain_sdk-0.1.0/pyproject.toml +38 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: custom-domain-sdk
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Client, assertion verification and middleware for the Custom Domain API.
|
|
5
|
+
Project-URL: Homepage, https://github.com/sireto/custom-domain
|
|
6
|
+
Project-URL: Documentation, https://github.com/sireto/custom-domain/blob/main/sdk/README.md
|
|
7
|
+
Project-URL: Source, https://github.com/sireto/custom-domain/tree/main/sdk
|
|
8
|
+
Project-URL: Issues, https://github.com/sireto/custom-domain/issues
|
|
9
|
+
Author-email: Sireto <info@sireto.com>
|
|
10
|
+
License-Expression: Apache-2.0
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: asgi,caddy,custom-domain,multi-tenant,saas
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
21
|
+
Classifier: Typing :: Typed
|
|
22
|
+
Requires-Python: >=3.10
|
|
23
|
+
Requires-Dist: httpx>=0.27
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
|
|
26
|
+
# custom-domain-sdk
|
|
27
|
+
|
|
28
|
+
Python client and origin helpers for the Custom Domain API. Pure Python 3.10+
|
|
29
|
+
with `httpx`; no dependency on the service itself.
|
|
30
|
+
|
|
31
|
+
```
|
|
32
|
+
pip install custom-domain-sdk # from PyPI; from a checkout: pip install ./sdk
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Quick start: register a workspace domain
|
|
36
|
+
|
|
37
|
+
```python
|
|
38
|
+
from custom_domain import Client
|
|
39
|
+
|
|
40
|
+
client = Client("https://domains.example.net", credential="cd_...") # credential from the operator
|
|
41
|
+
|
|
42
|
+
domain = client.create_domain(
|
|
43
|
+
"forms.customer.example", # the customer's exact subdomain
|
|
44
|
+
reference="ws_8f3a1c", # your workspace id, returned to you on every request
|
|
45
|
+
idempotency_key="signup-ws_8f3a1c", # makes the call safe to retry
|
|
46
|
+
)
|
|
47
|
+
print(domain.status) # pending_dns
|
|
48
|
+
print(domain.render_dns_instructions())
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
prints something like
|
|
52
|
+
|
|
53
|
+
```
|
|
54
|
+
Create these DNS records for forms.customer.example:
|
|
55
|
+
|
|
56
|
+
TXT _custom-domain-challenge.forms.customer.example -> custom-domain-verify=...
|
|
57
|
+
purpose: ownership. Create a TXT record with exactly this name and value. ...
|
|
58
|
+
CNAME forms.customer.example -> acme.edge.example.net
|
|
59
|
+
purpose: routing. Point the hostname at the target with a CNAME record. ...
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Show both records to the customer as returned; each carries `help` text for
|
|
63
|
+
the usual DNS console mistakes. Nothing here requires knowing anything about
|
|
64
|
+
the edge or Caddy.
|
|
65
|
+
|
|
66
|
+
Later:
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
domain = client.get_domain(domain.id)
|
|
70
|
+
for check in domain.checks:
|
|
71
|
+
print(check.type, check.status, check.error_code)
|
|
72
|
+
if not domain.is_ready:
|
|
73
|
+
client.request_recheck(domain.id) # after the customer fixed DNS; rate limited
|
|
74
|
+
for d in client.iter_domains(reference="ws_8f3a1c"):
|
|
75
|
+
...
|
|
76
|
+
client.delete_domain(domain.id)
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Errors
|
|
80
|
+
|
|
81
|
+
Every API error raises a subclass of `custom_domain.ApiError` with `status`,
|
|
82
|
+
the stable `code` from the API, `message` and `details`:
|
|
83
|
+
`AuthenticationError` (401), `NotFoundError` (404), `ConflictError` (409, for
|
|
84
|
+
example `hostname_already_claimed`), `ValidationError` (422, for example
|
|
85
|
+
`apex_not_supported`), `RateLimitedError` (429, with `retry_after`),
|
|
86
|
+
`ServerError` (5xx). `TransportError` means no HTTP response was received.
|
|
87
|
+
|
|
88
|
+
## Timeouts and retries
|
|
89
|
+
|
|
90
|
+
Each request has a 10 second timeout (`Client(timeout=...)`). Requests that
|
|
91
|
+
are safe to repeat, that is `GET`, `DELETE`, and `create_domain` **when an
|
|
92
|
+
idempotency key is given**, are retried up to `max_retries` (default 2) on
|
|
93
|
+
connection errors, timeouts, 429 (waiting `Retry-After`) and 5xx, with
|
|
94
|
+
exponential backoff. A create without an idempotency key and a recheck are
|
|
95
|
+
never retried by the client: retrying a create could register twice and a
|
|
96
|
+
recheck is rate limited, so the caller decides.
|
|
97
|
+
|
|
98
|
+
## Verifying requests at your origin
|
|
99
|
+
|
|
100
|
+
The edge adds `X-Custom-Domain-Assertion` to every proxied request. Select
|
|
101
|
+
the workspace from it and from nothing else.
|
|
102
|
+
|
|
103
|
+
ASGI (FastAPI, Starlette, and others):
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
from custom_domain import CustomDomainMiddleware
|
|
107
|
+
|
|
108
|
+
app.add_middleware(
|
|
109
|
+
CustomDomainMiddleware,
|
|
110
|
+
keys={
|
|
111
|
+
"1": "<EDGE_ASSERTION_KEYS secret from the operator>"
|
|
112
|
+
}, # current and previous key during rotation
|
|
113
|
+
application_id="<your application id>",
|
|
114
|
+
workspace_lookup=load_workspace, # returns the workspace for a reference, or None
|
|
115
|
+
on_missing="reject", # "passthrough" if this app also serves its own domain
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
@app.get("/")
|
|
120
|
+
def home(request: Request):
|
|
121
|
+
assertion = request.state.custom_domain # None only with on_missing="passthrough"
|
|
122
|
+
workspace = load_workspace(assertion.reference)
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
The middleware also answers `GET /.well-known/custom-domain-workspace`: it
|
|
126
|
+
verifies the assertion, calls your `workspace_lookup` with the reference, and
|
|
127
|
+
returns the reference only when the lookup confirms the workspace exists
|
|
128
|
+
(404 otherwise). The service's lifecycle worker uses that answer to prove
|
|
129
|
+
that routing and tenant selection really work before it marks a domain
|
|
130
|
+
ready, so `workspace_lookup` must consult the same source your request
|
|
131
|
+
handlers use.
|
|
132
|
+
|
|
133
|
+
Any framework:
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
from custom_domain import WorkspaceResolver, AssertionInvalid
|
|
137
|
+
|
|
138
|
+
resolver = WorkspaceResolver(keys={"1": "..."}, application_id="...")
|
|
139
|
+
try:
|
|
140
|
+
assertion = resolver.resolve(request.headers, request.host)
|
|
141
|
+
except AssertionInvalid as exc:
|
|
142
|
+
return forbidden(exc.code) # missing, expired, bad_signature, wrong_application, ...
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Origins should only be reachable from the edge (network rules), or treat any
|
|
146
|
+
request without a valid assertion as a direct call and refuse it.
|
|
147
|
+
|
|
148
|
+
## Webhooks
|
|
149
|
+
|
|
150
|
+
```python
|
|
151
|
+
from custom_domain import verify_webhook, parse_event, SignatureInvalid
|
|
152
|
+
|
|
153
|
+
hook = client.create_webhook(
|
|
154
|
+
"https://app.acme.example/hooks/custom-domain", ["domain.ready", "domain.deleted"]
|
|
155
|
+
)
|
|
156
|
+
store(hook.secret) # shown once
|
|
157
|
+
|
|
158
|
+
# in your handler
|
|
159
|
+
try:
|
|
160
|
+
verify_webhook(
|
|
161
|
+
request.headers.get("X-Custom-Domain-Signature"), body, secrets=[current, previous]
|
|
162
|
+
)
|
|
163
|
+
except SignatureInvalid:
|
|
164
|
+
return 400
|
|
165
|
+
event = parse_event(body) # event.id, event.type, event.created_at, event.domain
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
Deliveries are at least once and may arrive out of order: deduplicate by
|
|
169
|
+
`event.id` and compare `event.created_at` before moving state. A complete
|
|
170
|
+
consumer is in `examples/webhook_consumer.py`.
|
|
171
|
+
|
|
172
|
+
## Local development versus production
|
|
173
|
+
|
|
174
|
+
Against the local Compose environment (`DISABLE_HTTPS=true`,
|
|
175
|
+
`ORIGIN_ALLOW_PRIVATE=true`), the edge serves plain HTTP, the certificate
|
|
176
|
+
check is marked not applicable, and origins on private addresses are
|
|
177
|
+
accepted. In production the edge obtains real certificates on demand at the
|
|
178
|
+
first handshake after DNS points to it, verifies origin TLS, refuses private
|
|
179
|
+
origin and webhook addresses, and a domain becomes `ready` only after the
|
|
180
|
+
HTTPS probe through the edge returns the right workspace. Customer DNS
|
|
181
|
+
changes take up to the record TTL to be observed; the API's `checks` show the
|
|
182
|
+
last observation and the next attempt.
|
|
183
|
+
|
|
184
|
+
## Compatibility
|
|
185
|
+
|
|
186
|
+
The SDK follows the API's `v1` contract. Minor SDK releases add fields and
|
|
187
|
+
methods; a field the API stops sending is never removed within a major
|
|
188
|
+
version. Unknown fields in API responses are ignored, so newer servers work
|
|
189
|
+
with older SDKs. Python 3.10 and newer are supported.
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
# custom-domain-sdk
|
|
2
|
+
|
|
3
|
+
Python client and origin helpers for the Custom Domain API. Pure Python 3.10+
|
|
4
|
+
with `httpx`; no dependency on the service itself.
|
|
5
|
+
|
|
6
|
+
```
|
|
7
|
+
pip install custom-domain-sdk # from PyPI; from a checkout: pip install ./sdk
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
## Quick start: register a workspace domain
|
|
11
|
+
|
|
12
|
+
```python
|
|
13
|
+
from custom_domain import Client
|
|
14
|
+
|
|
15
|
+
client = Client("https://domains.example.net", credential="cd_...") # credential from the operator
|
|
16
|
+
|
|
17
|
+
domain = client.create_domain(
|
|
18
|
+
"forms.customer.example", # the customer's exact subdomain
|
|
19
|
+
reference="ws_8f3a1c", # your workspace id, returned to you on every request
|
|
20
|
+
idempotency_key="signup-ws_8f3a1c", # makes the call safe to retry
|
|
21
|
+
)
|
|
22
|
+
print(domain.status) # pending_dns
|
|
23
|
+
print(domain.render_dns_instructions())
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
prints something like
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
Create these DNS records for forms.customer.example:
|
|
30
|
+
|
|
31
|
+
TXT _custom-domain-challenge.forms.customer.example -> custom-domain-verify=...
|
|
32
|
+
purpose: ownership. Create a TXT record with exactly this name and value. ...
|
|
33
|
+
CNAME forms.customer.example -> acme.edge.example.net
|
|
34
|
+
purpose: routing. Point the hostname at the target with a CNAME record. ...
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Show both records to the customer as returned; each carries `help` text for
|
|
38
|
+
the usual DNS console mistakes. Nothing here requires knowing anything about
|
|
39
|
+
the edge or Caddy.
|
|
40
|
+
|
|
41
|
+
Later:
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
domain = client.get_domain(domain.id)
|
|
45
|
+
for check in domain.checks:
|
|
46
|
+
print(check.type, check.status, check.error_code)
|
|
47
|
+
if not domain.is_ready:
|
|
48
|
+
client.request_recheck(domain.id) # after the customer fixed DNS; rate limited
|
|
49
|
+
for d in client.iter_domains(reference="ws_8f3a1c"):
|
|
50
|
+
...
|
|
51
|
+
client.delete_domain(domain.id)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Errors
|
|
55
|
+
|
|
56
|
+
Every API error raises a subclass of `custom_domain.ApiError` with `status`,
|
|
57
|
+
the stable `code` from the API, `message` and `details`:
|
|
58
|
+
`AuthenticationError` (401), `NotFoundError` (404), `ConflictError` (409, for
|
|
59
|
+
example `hostname_already_claimed`), `ValidationError` (422, for example
|
|
60
|
+
`apex_not_supported`), `RateLimitedError` (429, with `retry_after`),
|
|
61
|
+
`ServerError` (5xx). `TransportError` means no HTTP response was received.
|
|
62
|
+
|
|
63
|
+
## Timeouts and retries
|
|
64
|
+
|
|
65
|
+
Each request has a 10 second timeout (`Client(timeout=...)`). Requests that
|
|
66
|
+
are safe to repeat, that is `GET`, `DELETE`, and `create_domain` **when an
|
|
67
|
+
idempotency key is given**, are retried up to `max_retries` (default 2) on
|
|
68
|
+
connection errors, timeouts, 429 (waiting `Retry-After`) and 5xx, with
|
|
69
|
+
exponential backoff. A create without an idempotency key and a recheck are
|
|
70
|
+
never retried by the client: retrying a create could register twice and a
|
|
71
|
+
recheck is rate limited, so the caller decides.
|
|
72
|
+
|
|
73
|
+
## Verifying requests at your origin
|
|
74
|
+
|
|
75
|
+
The edge adds `X-Custom-Domain-Assertion` to every proxied request. Select
|
|
76
|
+
the workspace from it and from nothing else.
|
|
77
|
+
|
|
78
|
+
ASGI (FastAPI, Starlette, and others):
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
from custom_domain import CustomDomainMiddleware
|
|
82
|
+
|
|
83
|
+
app.add_middleware(
|
|
84
|
+
CustomDomainMiddleware,
|
|
85
|
+
keys={
|
|
86
|
+
"1": "<EDGE_ASSERTION_KEYS secret from the operator>"
|
|
87
|
+
}, # current and previous key during rotation
|
|
88
|
+
application_id="<your application id>",
|
|
89
|
+
workspace_lookup=load_workspace, # returns the workspace for a reference, or None
|
|
90
|
+
on_missing="reject", # "passthrough" if this app also serves its own domain
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
@app.get("/")
|
|
95
|
+
def home(request: Request):
|
|
96
|
+
assertion = request.state.custom_domain # None only with on_missing="passthrough"
|
|
97
|
+
workspace = load_workspace(assertion.reference)
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
The middleware also answers `GET /.well-known/custom-domain-workspace`: it
|
|
101
|
+
verifies the assertion, calls your `workspace_lookup` with the reference, and
|
|
102
|
+
returns the reference only when the lookup confirms the workspace exists
|
|
103
|
+
(404 otherwise). The service's lifecycle worker uses that answer to prove
|
|
104
|
+
that routing and tenant selection really work before it marks a domain
|
|
105
|
+
ready, so `workspace_lookup` must consult the same source your request
|
|
106
|
+
handlers use.
|
|
107
|
+
|
|
108
|
+
Any framework:
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
from custom_domain import WorkspaceResolver, AssertionInvalid
|
|
112
|
+
|
|
113
|
+
resolver = WorkspaceResolver(keys={"1": "..."}, application_id="...")
|
|
114
|
+
try:
|
|
115
|
+
assertion = resolver.resolve(request.headers, request.host)
|
|
116
|
+
except AssertionInvalid as exc:
|
|
117
|
+
return forbidden(exc.code) # missing, expired, bad_signature, wrong_application, ...
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Origins should only be reachable from the edge (network rules), or treat any
|
|
121
|
+
request without a valid assertion as a direct call and refuse it.
|
|
122
|
+
|
|
123
|
+
## Webhooks
|
|
124
|
+
|
|
125
|
+
```python
|
|
126
|
+
from custom_domain import verify_webhook, parse_event, SignatureInvalid
|
|
127
|
+
|
|
128
|
+
hook = client.create_webhook(
|
|
129
|
+
"https://app.acme.example/hooks/custom-domain", ["domain.ready", "domain.deleted"]
|
|
130
|
+
)
|
|
131
|
+
store(hook.secret) # shown once
|
|
132
|
+
|
|
133
|
+
# in your handler
|
|
134
|
+
try:
|
|
135
|
+
verify_webhook(
|
|
136
|
+
request.headers.get("X-Custom-Domain-Signature"), body, secrets=[current, previous]
|
|
137
|
+
)
|
|
138
|
+
except SignatureInvalid:
|
|
139
|
+
return 400
|
|
140
|
+
event = parse_event(body) # event.id, event.type, event.created_at, event.domain
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Deliveries are at least once and may arrive out of order: deduplicate by
|
|
144
|
+
`event.id` and compare `event.created_at` before moving state. A complete
|
|
145
|
+
consumer is in `examples/webhook_consumer.py`.
|
|
146
|
+
|
|
147
|
+
## Local development versus production
|
|
148
|
+
|
|
149
|
+
Against the local Compose environment (`DISABLE_HTTPS=true`,
|
|
150
|
+
`ORIGIN_ALLOW_PRIVATE=true`), the edge serves plain HTTP, the certificate
|
|
151
|
+
check is marked not applicable, and origins on private addresses are
|
|
152
|
+
accepted. In production the edge obtains real certificates on demand at the
|
|
153
|
+
first handshake after DNS points to it, verifies origin TLS, refuses private
|
|
154
|
+
origin and webhook addresses, and a domain becomes `ready` only after the
|
|
155
|
+
HTTPS probe through the edge returns the right workspace. Customer DNS
|
|
156
|
+
changes take up to the record TTL to be observed; the API's `checks` show the
|
|
157
|
+
last observation and the next attempt.
|
|
158
|
+
|
|
159
|
+
## Compatibility
|
|
160
|
+
|
|
161
|
+
The SDK follows the API's `v1` contract. Minor SDK releases add fields and
|
|
162
|
+
methods; a field the API stops sending is never removed within a major
|
|
163
|
+
version. Unknown fields in API responses are ignored, so newer servers work
|
|
164
|
+
with older SDKs. Python 3.10 and newer are supported.
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"""Custom Domain SDK.
|
|
2
|
+
|
|
3
|
+
* :class:`Client` talks to the v1 API.
|
|
4
|
+
* :func:`verify_assertion` and :class:`WorkspaceResolver` verify the signed
|
|
5
|
+
workspace assertion the edge adds to every proxied request.
|
|
6
|
+
* :func:`verify_webhook` and :func:`parse_event` handle webhook deliveries.
|
|
7
|
+
* :class:`CustomDomainMiddleware` does both for ASGI applications and serves
|
|
8
|
+
the workspace probe.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from custom_domain.assertion import Assertion, AssertionInvalid, verify_assertion
|
|
12
|
+
from custom_domain.client import Client
|
|
13
|
+
from custom_domain.errors import (
|
|
14
|
+
ApiError,
|
|
15
|
+
AuthenticationError,
|
|
16
|
+
ConflictError,
|
|
17
|
+
NotFoundError,
|
|
18
|
+
RateLimitedError,
|
|
19
|
+
ServerError,
|
|
20
|
+
TransportError,
|
|
21
|
+
ValidationError,
|
|
22
|
+
)
|
|
23
|
+
from custom_domain.middleware import CustomDomainMiddleware, WorkspaceResolver
|
|
24
|
+
from custom_domain.models import (
|
|
25
|
+
Check,
|
|
26
|
+
Delivery,
|
|
27
|
+
DnsRecord,
|
|
28
|
+
Domain,
|
|
29
|
+
Page,
|
|
30
|
+
Webhook,
|
|
31
|
+
WebhookEvent,
|
|
32
|
+
)
|
|
33
|
+
from custom_domain.webhooks import SignatureInvalid, parse_event, verify_webhook
|
|
34
|
+
|
|
35
|
+
__all__ = [
|
|
36
|
+
"ApiError",
|
|
37
|
+
"Assertion",
|
|
38
|
+
"AssertionInvalid",
|
|
39
|
+
"AuthenticationError",
|
|
40
|
+
"Check",
|
|
41
|
+
"Client",
|
|
42
|
+
"ConflictError",
|
|
43
|
+
"CustomDomainMiddleware",
|
|
44
|
+
"Delivery",
|
|
45
|
+
"DnsRecord",
|
|
46
|
+
"Domain",
|
|
47
|
+
"NotFoundError",
|
|
48
|
+
"Page",
|
|
49
|
+
"RateLimitedError",
|
|
50
|
+
"ServerError",
|
|
51
|
+
"SignatureInvalid",
|
|
52
|
+
"TransportError",
|
|
53
|
+
"ValidationError",
|
|
54
|
+
"Webhook",
|
|
55
|
+
"WebhookEvent",
|
|
56
|
+
"WorkspaceResolver",
|
|
57
|
+
"parse_event",
|
|
58
|
+
"verify_assertion",
|
|
59
|
+
"verify_webhook",
|
|
60
|
+
]
|
|
61
|
+
|
|
62
|
+
__version__ = "0.1.0"
|