pubrules 0.48.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.
- pubrules/__init__.py +14 -0
- pubrules/audit.py +116 -0
- pubrules/audit_draft.py +295 -0
- pubrules/audit_prepare.py +1128 -0
- pubrules/cache.py +319 -0
- pubrules/cbor.py +45 -0
- pubrules/ci.py +910 -0
- pubrules/cli.py +1069 -0
- pubrules/conform.py +295 -0
- pubrules/doc_coverage.py +235 -0
- pubrules/freshness.py +75 -0
- pubrules/gherkin.py +348 -0
- pubrules/hooks.py +59 -0
- pubrules/hosting.py +372 -0
- pubrules/ignores.py +86 -0
- pubrules/new.py +242 -0
- pubrules/nightly.py +1002 -0
- pubrules/paths.py +32 -0
- pubrules/protect.py +596 -0
- pubrules/providers.py +129 -0
- pubrules/refs.py +213 -0
- pubrules/release-commit +1 -0
- pubrules/releases.py +107 -0
- pubrules/repos.py +67 -0
- pubrules/ruleset.json +43 -0
- pubrules/secrets_config.py +165 -0
- pubrules/security.py +134 -0
- pubrules/service_needs.py +467 -0
- pubrules/standard.json +635 -0
- pubrules/standard.py +266 -0
- pubrules/supply_chain.py +188 -0
- pubrules/tag-ruleset.json +14 -0
- pubrules/templates/common/AGENTS.md +32 -0
- pubrules/templates/common/pubrules +26 -0
- pubrules/templates/common/tests/features/repository.feature +11 -0
- pubrules/templates/common/tests/install-lock +65 -0
- pubrules/templates/common/tests/pytest.ini +4 -0
- pubrules/templates/common/tests/requirements-pubrules.txt +17 -0
- pubrules/templates/common/tests/requirements.txt +3 -0
- pubrules/templates/common/tests/steps/test_repository.py +14 -0
- pubrules/templates/deploy/pubdeploy +28 -0
- pubrules/templates/new-rust/CHANGELOG.md +3 -0
- pubrules/templates/new-rust/Cargo.toml +46 -0
- pubrules/templates/new-rust/LICENSE +202 -0
- pubrules/templates/new-rust/Makefile +18 -0
- pubrules/templates/new-rust/README.md +34 -0
- pubrules/templates/new-rust/SECURITY.md +26 -0
- pubrules/templates/new-rust/corpus/corpus.json +11 -0
- pubrules/templates/new-rust/deny.toml +32 -0
- pubrules/templates/new-rust/fuzz/Cargo.toml +23 -0
- pubrules/templates/new-rust/fuzz/fuzz_targets/greet.rs +8 -0
- pubrules/templates/new-rust/fuzz/gitignore +4 -0
- pubrules/templates/new-rust/fuzz/regressions/README.md +4 -0
- pubrules/templates/new-rust/gitignore +10 -0
- pubrules/templates/new-rust/src/lib.rs +91 -0
- pubrules/templates/new-rust/src/main.rs +19 -0
- pubrules/templates/new-rust/supply-chain/audits.toml +4 -0
- pubrules/templates/new-rust/supply-chain/config.toml +23 -0
- pubrules/templates/new-rust/supply-chain/imports.lock +2 -0
- pubrules/templates/new-rust/tests/conftest.py +9 -0
- pubrules/templates/new-rust/tests/features/cli.feature +14 -0
- pubrules/templates/new-rust/tests/steps/test_cli.py +15 -0
- pubrules/templates/new-rust/tests/vectors.rs +30 -0
- pubrules/templates/new-rust/tools/vectors/generate.py +24 -0
- pubrules/templates/python/Makefile +5 -0
- pubrules/templates/python/pubrules-python.mk +47 -0
- pubrules/templates/rust/Makefile +9 -0
- pubrules/templates/rust/pubrules.mk +138 -0
- pubrules/templates/rust/rust-toolchain.toml +8 -0
- pubrules/templates/service/pubsec +27 -0
- pubrules/templates/service/with-secrets +20 -0
- pubrules/templates/service/with-secrets.json +8 -0
- pubrules/variables.py +45 -0
- pubrules-0.48.0.dist-info/METADATA +1241 -0
- pubrules-0.48.0.dist-info/RECORD +79 -0
- pubrules-0.48.0.dist-info/WHEEL +5 -0
- pubrules-0.48.0.dist-info/entry_points.txt +2 -0
- pubrules-0.48.0.dist-info/licenses/LICENSE +202 -0
- pubrules-0.48.0.dist-info/top_level.txt +1 -0
pubrules/hosting.py
ADDED
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
"""The `hosting` block of pubrules.json: the hosting providers a repository
|
|
2
|
+
uses and what each of its zones has at them, which the hosting modules read.
|
|
3
|
+
Everything provider-specific is here, in the repository, not in pubrules:
|
|
4
|
+
|
|
5
|
+
"hosting": {
|
|
6
|
+
"ttl": {"email": 1800},
|
|
7
|
+
"providers": {
|
|
8
|
+
"hetzner": {"type": "hetzner",
|
|
9
|
+
"dns": {"external_nameservers": ["helium.ns.hetzner.de.", ...]}},
|
|
10
|
+
"cloudflare": {"type": "cloudflare", "account_id": "0123...cdef"},
|
|
11
|
+
"desec": {"type": "desec",
|
|
12
|
+
"dns": {"external_nameservers": ["ns1.desec.io.", "ns2.desec.org."]}},
|
|
13
|
+
"gcp": {"type": "google-cloud", "project": "example", "region": "europe-north1",
|
|
14
|
+
"cloud_run": {"domain_target": "ghs.googlehosted.com."}},
|
|
15
|
+
"mailbox": {"type": "mailbox-org", "mx": ["10 mxext1.mailbox.org.", ...],
|
|
16
|
+
"spf": "include:mailbox.org",
|
|
17
|
+
"dkim": {"mbo0001": "mbo0001._domainkey.mailbox.org.", ...}}
|
|
18
|
+
},
|
|
19
|
+
"zones": {
|
|
20
|
+
"example.eu": {
|
|
21
|
+
"dns": ["hetzner", "cloudflare"],
|
|
22
|
+
"email": {"provider": "mailbox", "verification": {"f67e...": "fa0e..."},
|
|
23
|
+
"dmarc": {"policy": "none", "rua": "alerts@example.eu"}},
|
|
24
|
+
"apex_txt": ["google-site-verification=..."],
|
|
25
|
+
"dnssec": {"keys": {"cloudflare": ["257 3 13 mdss...", "256 3 13 oJMR..."],
|
|
26
|
+
"desec": ["257 3 13 quFX..."]}},
|
|
27
|
+
"sites": {
|
|
28
|
+
"@": {"provider": "hetzner", "kind": "server", "server": "web"},
|
|
29
|
+
"blog": {"provider": "cloudflare", "kind": "pages", "project": "example-blog",
|
|
30
|
+
"served_by": "pages", "standby": "web"},
|
|
31
|
+
"pub": {"provider": "gcp", "kind": "cloud-run", "service": "pub-site"}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
A provider is any name the repository chooses, with one of the types in
|
|
38
|
+
TYPES. A zone's `dnssec` pins each of its DNS providers' public keys, as
|
|
39
|
+
DNSKEY values: every provider signs the zone with its own, and publishes
|
|
40
|
+
the others' (multi-signer), so a provider that cannot sign cannot serve it. `ttl`, in the hosting block, a zone or a site, sets TTLs over the
|
|
41
|
+
defaults (TTL_KINDS: a day for the apex NS rrset, an hour for email, five
|
|
42
|
+
minutes for sites); the nearest wins. A type is what pubrules supports: its settings, whether it serves
|
|
43
|
+
DNS, mail, and which kind of site it hosts. A new type needs pubrules to
|
|
44
|
+
learn it, here and in the hosting modules; a new provider of a known type
|
|
45
|
+
needs only pubrules.json.
|
|
46
|
+
|
|
47
|
+
Terraform drops a setting its types do not name without a word, so
|
|
48
|
+
`pubrules sync` (and `sync --check`) reads the block first and stops on
|
|
49
|
+
anything it does not know or cannot use.
|
|
50
|
+
"""
|
|
51
|
+
|
|
52
|
+
from __future__ import annotations
|
|
53
|
+
|
|
54
|
+
import base64
|
|
55
|
+
import binascii
|
|
56
|
+
import sys
|
|
57
|
+
from collections.abc import Callable
|
|
58
|
+
from typing import Any, NoReturn
|
|
59
|
+
|
|
60
|
+
LABEL = set("abcdefghijklmnopqrstuvwxyz0123456789-_")
|
|
61
|
+
HEX = set("0123456789abcdef")
|
|
62
|
+
POLICIES = ("none", "quarantine", "reject")
|
|
63
|
+
SERVED_BY = ("pages", "server")
|
|
64
|
+
MAX_PRIORITY = 65535
|
|
65
|
+
ACCOUNT_ID_LENGTH = 32
|
|
66
|
+
TTL_KINDS = ("nameservers", "email", "sites")
|
|
67
|
+
TTL_RANGE = (60, 604800)
|
|
68
|
+
KEY_FLAGS = (256, 257)
|
|
69
|
+
SEP = 257
|
|
70
|
+
DNSSEC_PROTOCOL = 3
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def stop(message: str) -> NoReturn:
|
|
74
|
+
sys.exit(f"pubrules: pubrules.json's hosting.{message}")
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def is_name(text: Any) -> bool:
|
|
78
|
+
"""A relative DNS name in lowercase, like mbo0001 or a.b."""
|
|
79
|
+
return isinstance(text, str) and all(label and set(label) <= LABEL for label in text.split("."))
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def is_host(text: Any) -> bool:
|
|
83
|
+
"""A fully qualified host name, with its trailing dot."""
|
|
84
|
+
return isinstance(text, str) and text.endswith(".") and is_name(text[:-1])
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def is_mx(text: Any) -> bool:
|
|
88
|
+
"""An MX value, "priority host.", as in a zone file."""
|
|
89
|
+
if not isinstance(text, str) or text.count(" ") != 1:
|
|
90
|
+
return False
|
|
91
|
+
priority, host = text.split(" ")
|
|
92
|
+
return priority.isdigit() and int(priority) <= MAX_PRIORITY and is_host(host)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def is_email(text: Any) -> bool:
|
|
96
|
+
if not isinstance(text, str) or text.count("@") != 1:
|
|
97
|
+
return False
|
|
98
|
+
local, domain = text.split("@")
|
|
99
|
+
return bool(local) and not set(local) & set(' "') and "." in domain and is_name(domain)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def is_slug(text: Any, longest: int) -> bool:
|
|
103
|
+
"""A lowercase name of letters, digits and hyphens, as Cloudflare Pages
|
|
104
|
+
projects, Cloud Run services and Google Cloud projects are."""
|
|
105
|
+
return (isinstance(text, str) and 0 < len(text) <= longest and text[0].isalnum()
|
|
106
|
+
and set(text) <= LABEL - {"_"})
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def is_unquoted(text: Any) -> bool:
|
|
110
|
+
return isinstance(text, str) and bool(text) and '"' not in text
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def known(given: Any, where: str, settings: tuple[str, ...]) -> dict[str, Any]:
|
|
114
|
+
if not isinstance(given, dict):
|
|
115
|
+
stop(f"{where} must be an object")
|
|
116
|
+
unknown = sorted(set(given) - set(settings))
|
|
117
|
+
if unknown:
|
|
118
|
+
stop(f"{where} has no setting {', '.join(unknown)}; it takes {', '.join(settings)}")
|
|
119
|
+
return given
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def require(given: dict[str, Any], where: str, setting: str, ok: Callable[[Any], bool],
|
|
123
|
+
what: str) -> None:
|
|
124
|
+
if setting in given and not ok(given[setting]):
|
|
125
|
+
stop(f"{where}.{setting} must be {what}")
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def is_ttl(value: Any) -> bool:
|
|
129
|
+
return (isinstance(value, int) and not isinstance(value, bool)
|
|
130
|
+
and TTL_RANGE[0] <= value <= TTL_RANGE[1])
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def check_ttl(given: Any, where: str) -> None:
|
|
134
|
+
"""A TTL block: seconds for the apex NS rrset, the email records and the
|
|
135
|
+
sites' records, over the defaults (a day, an hour, five minutes)."""
|
|
136
|
+
for kind, value in known(given, where, TTL_KINDS).items():
|
|
137
|
+
if not is_ttl(value):
|
|
138
|
+
stop(f"{where}.{kind} must be a number of seconds from {TTL_RANGE[0]} to {TTL_RANGE[1]}")
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
# --- Provider types ---
|
|
142
|
+
|
|
143
|
+
def check_hetzner(given: dict[str, Any], where: str) -> None:
|
|
144
|
+
dns = known(given.get("dns", {}), f"{where}.dns", ("external_nameservers",))
|
|
145
|
+
require(dns, f"{where}.dns", "external_nameservers",
|
|
146
|
+
lambda v: isinstance(v, list) and all(is_host(n) for n in v),
|
|
147
|
+
'a list of host names with their trailing dots, such as "helium.ns.hetzner.de."')
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def check_desec(given: dict[str, Any], where: str) -> None:
|
|
151
|
+
dns = known(given.get("dns", {}), f"{where}.dns", ("external_nameservers",))
|
|
152
|
+
names = dns.get("external_nameservers")
|
|
153
|
+
if not isinstance(names, list) or not names or not all(is_host(n) for n in names):
|
|
154
|
+
stop(f"{where}.dns.external_nameservers must list the names deSEC serves zones from, "
|
|
155
|
+
'with their trailing dots, such as ["ns1.desec.io.", "ns2.desec.org."]; '
|
|
156
|
+
"its API does not report them")
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def check_cloudflare(given: dict[str, Any], where: str) -> None:
|
|
160
|
+
account = given.get("account_id")
|
|
161
|
+
if not isinstance(account, str) or len(account) != ACCOUNT_ID_LENGTH or not set(account) <= HEX:
|
|
162
|
+
stop(f"{where}.account_id must be the account's id: 32 lowercase hexadecimal digits")
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def check_google_cloud(given: dict[str, Any], where: str) -> None:
|
|
166
|
+
if not is_slug(given.get("project"), 30):
|
|
167
|
+
stop(f"{where}.project must be the Google Cloud project's id, such as \"example-prod\"")
|
|
168
|
+
if not is_slug(given.get("region"), 63):
|
|
169
|
+
stop(f"{where}.region must be a Google Cloud region, such as \"europe-north1\"")
|
|
170
|
+
if "cloud_run" in given:
|
|
171
|
+
run = known(given["cloud_run"], f"{where}.cloud_run", ("domain_target",))
|
|
172
|
+
if not is_host(run.get("domain_target")):
|
|
173
|
+
stop(f"{where}.cloud_run.domain_target must be the host a custom domain's CNAME "
|
|
174
|
+
'names, with its trailing dot, such as "ghs.googlehosted.com."')
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def check_mailbox_org(given: dict[str, Any], where: str) -> None:
|
|
178
|
+
require(given, where, "mx", lambda v: isinstance(v, list) and all(is_mx(m) for m in v),
|
|
179
|
+
'a list of MX values as "priority host.", such as "10 mxext1.mailbox.org."')
|
|
180
|
+
require(given, where, "spf", lambda v: is_unquoted(v) and not v.startswith("v=spf1"),
|
|
181
|
+
'the SPF mechanisms that allow the provider to send, unquoted, such as '
|
|
182
|
+
'"include:mailbox.org" (the zone adds "v=spf1" and "~all")')
|
|
183
|
+
require(given, where, "dkim",
|
|
184
|
+
lambda v: isinstance(v, dict) and all(is_name(k) and is_host(t) for k, t in v.items()),
|
|
185
|
+
"an object mapping each selector, in lowercase (Hetzner refuses capitals), to the "
|
|
186
|
+
'host name its key is at, such as {"mbo0001": "mbo0001._domainkey.mailbox.org."}')
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
TYPES: dict[str, dict[str, Any]] = {
|
|
190
|
+
"hetzner": {"settings": ("dns",), "check": check_hetzner, "dns": True, "site": "server"},
|
|
191
|
+
"cloudflare": {"settings": ("account_id",), "check": check_cloudflare, "dns": True,
|
|
192
|
+
"signs": True, "site": "pages"},
|
|
193
|
+
"desec": {"settings": ("dns",), "check": check_desec, "dns": True, "signs": True},
|
|
194
|
+
"google-cloud": {"settings": ("project", "region", "cloud_run"), "check": check_google_cloud,
|
|
195
|
+
"dns": False, "site": "cloud-run"},
|
|
196
|
+
"mailbox-org": {"settings": ("mx", "spf", "dkim"), "check": check_mailbox_org, "dns": False,
|
|
197
|
+
"mail": True},
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def check_provider(name: str, given: Any) -> str:
|
|
202
|
+
where = f"providers.{name}"
|
|
203
|
+
kind = given.get("type") if isinstance(given, dict) else None
|
|
204
|
+
if kind not in TYPES:
|
|
205
|
+
stop(f"{where}.type must be one of {', '.join(TYPES)}; pubrules has to learn a new type "
|
|
206
|
+
"before a repository can use it")
|
|
207
|
+
TYPES[kind]["check"](known(given, where, ("type", *TYPES[kind]["settings"])), where)
|
|
208
|
+
return kind
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
# --- Zones ---
|
|
212
|
+
|
|
213
|
+
def check_email(email: Any, where: str, types: dict[str, str]) -> None:
|
|
214
|
+
email = known(email, where, ("provider", "verification", "dmarc"))
|
|
215
|
+
if not TYPES.get(types.get(email.get("provider"), ""), {}).get("mail"):
|
|
216
|
+
stop(f"{where}.provider must name a mail provider in hosting.providers")
|
|
217
|
+
require(email, where, "verification",
|
|
218
|
+
lambda v: isinstance(v, dict) and all(
|
|
219
|
+
is_name(k) and k != "_dmarc" and is_unquoted(t) for k, t in v.items()),
|
|
220
|
+
"an object mapping each name, in lowercase, to its value, unquoted")
|
|
221
|
+
if "dmarc" in email:
|
|
222
|
+
dmarc = known(email["dmarc"], f"{where}.dmarc", ("policy", "rua"))
|
|
223
|
+
if dmarc.get("policy", "none") not in POLICIES:
|
|
224
|
+
stop(f"{where}.dmarc.policy must be one of {', '.join(POLICIES)}")
|
|
225
|
+
if not is_email(dmarc.get("rua")):
|
|
226
|
+
stop(f"{where}.dmarc.rua must be the address reports go to, such as alerts@example.eu")
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def dnskey(text: Any) -> tuple[int, int, int, bytes] | None:
|
|
230
|
+
"""A DNSKEY value, "flags 3 algorithm public-key" with the key in base64
|
|
231
|
+
and no spaces, as flags, protocol, algorithm and key; None if it is not
|
|
232
|
+
one, or its flags are neither a zone key (256) nor a key-signing one
|
|
233
|
+
(257)."""
|
|
234
|
+
parts = text.split(" ") if isinstance(text, str) else []
|
|
235
|
+
if len(parts) != 4 or not all(p.isdigit() for p in parts[:3]):
|
|
236
|
+
return None
|
|
237
|
+
flags, protocol, algorithm = (int(p) for p in parts[:3])
|
|
238
|
+
try:
|
|
239
|
+
key = base64.b64decode(parts[3], validate=True)
|
|
240
|
+
except (binascii.Error, ValueError):
|
|
241
|
+
return None
|
|
242
|
+
if flags not in KEY_FLAGS or protocol != DNSSEC_PROTOCOL or not key:
|
|
243
|
+
return None
|
|
244
|
+
return flags, protocol, algorithm, key
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def check_dnssec(dnssec: Any, where: str, dns: list[str], types: dict[str, str]) -> None:
|
|
248
|
+
dnssec = known(dnssec, where, ("keys",))
|
|
249
|
+
for name in dns:
|
|
250
|
+
if not TYPES[types[name]].get("signs"):
|
|
251
|
+
stop(f"{where} cannot be set while {name} serves the zone: a {types[name]} provider "
|
|
252
|
+
"cannot sign, and resolvers reject its unsigned answers once the registrar has "
|
|
253
|
+
"a DS record")
|
|
254
|
+
keys = dnssec.get("keys")
|
|
255
|
+
if not isinstance(keys, dict) or sorted(keys) != sorted(dns):
|
|
256
|
+
stop(f"{where}.keys must map each provider in the zone's dns ({', '.join(dns)}) to its "
|
|
257
|
+
"public keys")
|
|
258
|
+
algorithms = set()
|
|
259
|
+
for name, values in keys.items():
|
|
260
|
+
parsed = [dnskey(v) for v in values] if isinstance(values, list) else [None]
|
|
261
|
+
if not parsed or None in parsed:
|
|
262
|
+
stop(f'{where}.keys.{name} must be a list of DNSKEY values, "flags 3 algorithm key", '
|
|
263
|
+
'such as "257 3 13 mdss...GQ==": flags 256 or 257, the key in base64 without '
|
|
264
|
+
"spaces")
|
|
265
|
+
if not any(key[0] == SEP for key in parsed):
|
|
266
|
+
stop(f"{where}.keys.{name} must have a key-signing key (flags 257), which a DS "
|
|
267
|
+
"record names")
|
|
268
|
+
algorithms |= {key[2] for key in parsed}
|
|
269
|
+
if len(algorithms) > 1:
|
|
270
|
+
stop(f"{where}.keys must all use one algorithm: every provider signs the same zone "
|
|
271
|
+
"(RFC 8901), and these use " + ", ".join(map(str, sorted(algorithms))))
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
def check_server(site: dict[str, Any], where: str, setting: str) -> None:
|
|
275
|
+
if not is_slug(site.get(setting), 63):
|
|
276
|
+
stop(f"{where}.{setting} must name a server, whose addresses the root module gives "
|
|
277
|
+
"in servers")
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
def check_server_site(site: dict[str, Any], where: str, _provider: dict[str, Any]) -> None:
|
|
281
|
+
check_server(site, where, "server")
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
def check_pages_site(site: dict[str, Any], where: str, _provider: dict[str, Any]) -> None:
|
|
285
|
+
if not is_slug(site.get("project"), 58):
|
|
286
|
+
stop(f"{where}.project must be the Pages project's name, such as \"example-docs\"")
|
|
287
|
+
subdomain = site.get("subdomain", "x.pages.dev")
|
|
288
|
+
if not (isinstance(subdomain, str) and subdomain.endswith(".pages.dev")
|
|
289
|
+
and is_slug(subdomain.removesuffix(".pages.dev"), 63)):
|
|
290
|
+
stop(f"{where}.subdomain must be the project's pages.dev name, such as \"example-docs-2ab.pages.dev\"")
|
|
291
|
+
if site.get("served_by", "pages") not in SERVED_BY:
|
|
292
|
+
stop(f"{where}.served_by must be one of {', '.join(SERVED_BY)}")
|
|
293
|
+
if site.get("served_by") == "server" or "standby" in site:
|
|
294
|
+
check_server(site, where, "standby")
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
def check_cloud_run_site(site: dict[str, Any], where: str, provider: dict[str, Any]) -> None:
|
|
298
|
+
if not is_slug(site.get("service"), 49):
|
|
299
|
+
stop(f"{where}.service must be the Cloud Run service's name")
|
|
300
|
+
if "cloud_run" not in provider:
|
|
301
|
+
stop(f"{where} needs cloud_run.domain_target in its provider")
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
SITES: dict[str, tuple[tuple[str, ...], Callable[[dict[str, Any], str, dict[str, Any]], None]]] = {
|
|
305
|
+
"server": (("server",), check_server_site),
|
|
306
|
+
"pages": (("project", "subdomain", "served_by", "standby"), check_pages_site),
|
|
307
|
+
"cloud-run": (("service",), check_cloud_run_site),
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
def check_site(name: str, site: Any, where: str, providers: dict[str, Any]) -> None:
|
|
312
|
+
if name != "@" and (not is_name(name) or name.startswith("_")):
|
|
313
|
+
stop(f"{where} must be named by the name relative to the zone, in lowercase, or \"@\"")
|
|
314
|
+
provider = providers.get(site.get("provider") if isinstance(site, dict) else None, {})
|
|
315
|
+
kind = TYPES.get(provider.get("type", ""), {}).get("site")
|
|
316
|
+
if kind is None:
|
|
317
|
+
stop(f"{where}.provider must name a provider in hosting.providers that hosts sites")
|
|
318
|
+
settings_of, check = SITES[kind]
|
|
319
|
+
site = known(site, where, ("provider", "kind", *settings_of, "ttl"))
|
|
320
|
+
require(site, where, "ttl", is_ttl, f"a number of seconds from {TTL_RANGE[0]} to {TTL_RANGE[1]}")
|
|
321
|
+
if site.get("kind") != kind:
|
|
322
|
+
stop(f"{where}.kind must be {kind}, the kind of site a {provider['type']} provider hosts")
|
|
323
|
+
if kind != "server" and name == "@":
|
|
324
|
+
stop(f"{where} cannot be at the apex: its name is a CNAME, which the apex cannot have")
|
|
325
|
+
check(site, where, provider)
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
def check_zone(name: str, zone: Any, providers: dict[str, Any], types: dict[str, str]) -> None:
|
|
329
|
+
where = f"zones.{name}"
|
|
330
|
+
if not is_name(name) or "." not in name:
|
|
331
|
+
stop(f"{where} must be named by the domain, in lowercase, such as example.eu")
|
|
332
|
+
zone = known(zone, where, ("dns", "ttl", "dnssec", "email", "apex_txt", "sites"))
|
|
333
|
+
if "ttl" in zone:
|
|
334
|
+
check_ttl(zone["ttl"], f"{where}.ttl")
|
|
335
|
+
dns = zone.get("dns")
|
|
336
|
+
if (not isinstance(dns, list) or not dns
|
|
337
|
+
or not all(TYPES.get(types.get(p, ""), {}).get("dns") for p in dns)
|
|
338
|
+
or len({types[p] for p in dns}) != len(dns)):
|
|
339
|
+
stop(f"{where}.dns must list the providers in hosting.providers that serve the zone, "
|
|
340
|
+
"at most one of each type: " + ", ".join(t for t in TYPES if TYPES[t]["dns"]))
|
|
341
|
+
if "dnssec" in zone:
|
|
342
|
+
check_dnssec(zone["dnssec"], f"{where}.dnssec", dns, types)
|
|
343
|
+
if "email" in zone:
|
|
344
|
+
check_email(zone["email"], f"{where}.email", types)
|
|
345
|
+
require(zone, where, "apex_txt", lambda v: isinstance(v, list) and all(map(is_unquoted, v)),
|
|
346
|
+
"a list of TXT values, unquoted")
|
|
347
|
+
sites = zone.get("sites", {})
|
|
348
|
+
if not isinstance(sites, dict):
|
|
349
|
+
stop(f"{where}.sites must be an object of sites by name")
|
|
350
|
+
for site_name, site in sites.items():
|
|
351
|
+
check_site(site_name, site, f"{where}.sites.{site_name}", providers)
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
def settings(config: dict[str, Any]) -> dict[str, Any]:
|
|
355
|
+
"""pubrules.json's hosting block, checked; {} when there is none."""
|
|
356
|
+
given = config.get("hosting", {})
|
|
357
|
+
if not isinstance(given, dict):
|
|
358
|
+
sys.exit("pubrules: pubrules.json's hosting must be an object of providers and zones")
|
|
359
|
+
unknown = sorted(set(given) - {"ttl", "providers", "zones"})
|
|
360
|
+
if unknown:
|
|
361
|
+
sys.exit(f"pubrules: pubrules.json's hosting has no setting {', '.join(unknown)}; "
|
|
362
|
+
"it takes ttl, providers and zones")
|
|
363
|
+
if "ttl" in given:
|
|
364
|
+
check_ttl(given["ttl"], "ttl")
|
|
365
|
+
providers = given.get("providers", {})
|
|
366
|
+
zones = given.get("zones", {})
|
|
367
|
+
if not isinstance(providers, dict) or not isinstance(zones, dict):
|
|
368
|
+
sys.exit("pubrules: pubrules.json's hosting.providers and hosting.zones must be objects, by name")
|
|
369
|
+
types = {name: check_provider(name, provider) for name, provider in providers.items()}
|
|
370
|
+
for name, zone in zones.items():
|
|
371
|
+
check_zone(name, zone, providers, types)
|
|
372
|
+
return given
|
pubrules/ignores.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"""What every repository's .gitignore must ignore, and the healing that adds it.
|
|
2
|
+
|
|
3
|
+
Local tools read secrets from `.env`: with-secrets reads its Bitwarden token
|
|
4
|
+
there. A repository that doesn't ignore it is one `git add -A` from committing
|
|
5
|
+
that token. `.venv` is the local environment, and pubrel's prepare links the
|
|
6
|
+
checkout's into its worktree: `.venv/` matches only a directory, so a link
|
|
7
|
+
went into a release commit. The rest of .gitignore is the repository's own,
|
|
8
|
+
so `sync` only mends what is missing, and `sync --check` fails while anything
|
|
9
|
+
is: a pattern that matches the name only as a directory (`/.venv/`) loses its
|
|
10
|
+
slash where it stands, and a name no line covers is appended.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
from fnmatch import fnmatchcase
|
|
16
|
+
from pathlib import Path
|
|
17
|
+
|
|
18
|
+
GITIGNORE = ".gitignore"
|
|
19
|
+
# Each name, and the comment written above it when sync adds it.
|
|
20
|
+
REQUIRED = {
|
|
21
|
+
".env": "Local secrets, such as the Bitwarden token ./with-secrets reads: never committed.",
|
|
22
|
+
".venv": "The local environment, a directory or a link (pubrel prepare links it into its worktree).",
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def ignores(text: str, name: str) -> bool:
|
|
27
|
+
"""Whether `text`, a .gitignore, ignores the file `name` at the repository's
|
|
28
|
+
root: the last pattern that matches it decides, and `!` takes it back."""
|
|
29
|
+
out = False
|
|
30
|
+
for line in text.splitlines():
|
|
31
|
+
line = line.strip()
|
|
32
|
+
if not line or line.startswith("#"):
|
|
33
|
+
continue
|
|
34
|
+
negated = line.startswith("!")
|
|
35
|
+
pattern = line[1:] if negated else line
|
|
36
|
+
if pattern.endswith("/"): # only directories
|
|
37
|
+
continue
|
|
38
|
+
pattern = pattern.removeprefix("**/").removeprefix("/")
|
|
39
|
+
if fnmatchcase(name, pattern):
|
|
40
|
+
out = not negated
|
|
41
|
+
return out
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def missing(root: Path) -> list[str]:
|
|
45
|
+
path = root / GITIGNORE
|
|
46
|
+
text = path.read_text(encoding="utf-8") if path.exists() else ""
|
|
47
|
+
return [name for name in REQUIRED if not ignores(text, name)]
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _undirected(line: str, name: str) -> str | None:
|
|
51
|
+
"""`line` without its trailing slash, when it names `name` only as a
|
|
52
|
+
directory (`.venv/`, `/.venv/`, `**/.venv/`); None for any other line,
|
|
53
|
+
a wildcard's included."""
|
|
54
|
+
stripped = line.strip()
|
|
55
|
+
if stripped.startswith(("!", "#")) or not stripped.endswith("/"):
|
|
56
|
+
return None
|
|
57
|
+
pattern = stripped.rstrip("/")
|
|
58
|
+
# Only a pattern that names the file: dropping the slash of a wildcard
|
|
59
|
+
# (`*/`, `.*/`, `*env/`) would ignore far more than .venv, and undo a
|
|
60
|
+
# later `!` that brings a directory back (`!.github/`).
|
|
61
|
+
if any(c in pattern.removeprefix("**/") for c in "*?["):
|
|
62
|
+
return None
|
|
63
|
+
return pattern if pattern and ignores(pattern, name) else None
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def heal(root: Path) -> list[str]:
|
|
67
|
+
"""Mend .gitignore for each missing name, leaving the rest as it is: a
|
|
68
|
+
line that ignores it only as a directory loses its slash where it stands,
|
|
69
|
+
and a name still not ignored is appended under its comment. Returns what
|
|
70
|
+
it mended."""
|
|
71
|
+
added = missing(root)
|
|
72
|
+
if added:
|
|
73
|
+
path = root / GITIGNORE
|
|
74
|
+
text = path.read_text(encoding="utf-8") if path.exists() else ""
|
|
75
|
+
lines = text.splitlines()
|
|
76
|
+
for name in added:
|
|
77
|
+
for n, line in enumerate(lines):
|
|
78
|
+
if (pattern := _undirected(line, name)) is not None:
|
|
79
|
+
lines[n] = pattern
|
|
80
|
+
text = "".join(f"{line}\n" for line in lines)
|
|
81
|
+
appended = [name for name in added if not ignores(text, name)]
|
|
82
|
+
if appended and text:
|
|
83
|
+
text += "\n"
|
|
84
|
+
text += "".join(f"# {REQUIRED[name]}\n{name}\n" for name in appended)
|
|
85
|
+
path.write_text(text, encoding="utf-8")
|
|
86
|
+
return added
|