-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·171 lines (132 loc) · 5.12 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python3
import logging
import os
import re
import socket
import time
from itertools import count
import CloudFlare
import heroku3
import sentry_sdk
from dotenv import load_dotenv
from heroku3.models.app import App
SUCCESS_ACM_STATUS = {
"cert issued",
"pending", # Assume this is ok. It'll be picked up on next iteration if it's not
}
ALLOWED_CNAME_TARGETS = [
re.compile(t) for t in os.environ.get("ALLOWED_CNAME_TARGETS", "").split(",")
]
def get_cloudflare_list(api, *args, params=None):
"""
Hack around Cloudflare's API to get all results in a nice way
"""
for page_num in count(start=1):
raw_results = api.get(
*args, params={"page": page_num, "per_page": 50, **(params or {})}
)
yield from raw_results["result"]
total_pages = raw_results["result_info"]["total_pages"]
if page_num == total_pages:
break
def enable_acm(app):
logging.info("Enabling ACM for %s", app.name)
app._h._http_resource(
method="POST", resource=("apps", app.id, "acm")
).raise_for_status()
def get_apps_for_teams(heroku, teams):
for team in teams:
yield from heroku._get_resources(("teams", team, "apps"), App)
def record_exists(record: str) -> bool:
"""
Determines whether a DNS record exists
"""
try:
socket.getaddrinfo(record, None)
except socket.gaierror:
return False
return True
def is_allowed_cname_target(record: str) -> bool:
"""
Is the record an allowed target
"""
return any(target.match(record) for target in ALLOWED_CNAME_TARGETS)
def main():
load_dotenv()
if sentry_dsn := os.environ.get("SENTRY_DSN"):
sentry_sdk.init(sentry_dsn)
cf = CloudFlare.CloudFlare(raw=True)
heroku = heroku3.from_key(os.getenv("HEROKU_API_KEY"))
interval = int(os.getenv("INTERVAL", 0))
matcher = re.compile(os.getenv("APP_NAME", r".*"))
heroku_teams = (
os.getenv("HEROKU_TEAMS").split(",") if "HEROKU_TEAMS" in os.environ else None
)
if interval:
while True:
do_create(cf, heroku, matcher, heroku_teams)
time.sleep(interval)
else:
do_create(cf, heroku, matcher, heroku_teams)
def do_create(cf, heroku, matcher, heroku_teams):
cf_zone = cf.zones.get(os.environ["CF_ZONE_ID"])["result"]
all_records = {
record["name"]: record
for record in get_cloudflare_list(
cf.zones.dns_records, cf_zone["id"], params={"type": "CNAME"}
)
}
heroku_apps = list(
heroku.apps()
if heroku_teams is None
else get_apps_for_teams(heroku, heroku_teams)
)
for app in heroku_apps:
if matcher.match(app.name) is None:
continue
app_domain = f"{app.name}.{cf_zone['name']}"
app_domains = {domain.hostname: domain for domain in app.domains()}
existing_record = all_records.get(app_domain)
# Add the domain to Heroku if it doesn't know about it
if app_domain not in app_domains:
logging.info("%s: domain not set in Heroku", app.name)
new_heroku_domain = app.add_domain(app_domain, sni_endpoint=None)
app_domains[new_heroku_domain.hostname] = new_heroku_domain
# This saves refreshing for the whole app, which can be noisy
if app_domains[app_domain].acm_status not in SUCCESS_ACM_STATUS:
logging.debug("%s: cycling domain to refresh ACM", app.name)
app.remove_domain(app_domain)
new_heroku_domain = app.add_domain(app_domain, sni_endpoint=None)
app_domains[new_heroku_domain.hostname] = new_heroku_domain
cname = getattr(app_domains.get(app_domain), "cname", None)
cf_record_data = {
"name": app.name,
"type": "CNAME",
"content": cname,
}
if existing_record is None:
logging.info("%s: domain not set", app.name)
cf.zones.dns_records.post(cf_zone["id"], data=cf_record_data)
elif existing_record["content"] != cname:
if is_allowed_cname_target(existing_record["content"]):
logging.info("%s: record is different, but an allowed value", app.name)
else:
logging.warning("%s: incorrect record value", app.name)
cf.zones.dns_records.patch(
cf_zone["id"], existing_record["id"], data=cf_record_data
)
# Enable ACM if not already, so certs can be issued
has_acm = any(d.acm_status for d in app_domains.values())
if not has_acm:
enable_acm(app)
# Delete heroku records which don't exist anymore
# This intentionally doesn't contain records we just created, so the records propagate
for existing_record in all_records.values():
existing_value = existing_record["content"]
if existing_value.endswith("herokudns.com") and not record_exists(
existing_value
):
logging.warning("%s: stale heroku domain", existing_value)
cf.zones.dns_records.delete(cf_zone["id"], existing_record["id"])
if __name__ == "__main__":
main()