Skip to content

Commit 555fd87

Browse files
committed
Allow specifying some CNAMEs which don't need to be overridden
1 parent d533391 commit 555fd87

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Optionally:
2525

2626
- `APP_NAME`: A regex of app names to act on. Any not matching this will be skipped.
2727
- `HEROKU_TEAMS`: A comma separated list of Heroku teams to operate on. By default will use all apps the account has access to.
28+
- `ALLOWED_CNAME_TARGETS`: A comma-separated list of regexes which match CNAMEs. If these CNAMEs are found in place of the correct Heroku CNAME, they won't be overridden.
2829

2930
These can also be set in a `.env` file.
3031

main.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
"pending", # Assume this is ok. It'll be picked up on next iteration if it's not
1919
}
2020

21+
ALLOWED_CNAME_TARGETS = [
22+
re.compile(t) for t in os.environ.get("ALLOWED_CNAME_TARGETS", "").split(",")
23+
]
24+
2125

2226
def get_cloudflare_list(api, *args, params=None):
2327
"""
@@ -59,6 +63,13 @@ def record_exists(record: str) -> bool:
5963
return True
6064

6165

66+
def is_allowed_cname_target(record: str) -> bool:
67+
"""
68+
Is the record an allowed target
69+
"""
70+
return any(target.match(record) for target in ALLOWED_CNAME_TARGETS)
71+
72+
6273
def main():
6374
load_dotenv()
6475

@@ -132,10 +143,13 @@ def do_create(cf, heroku, matcher, heroku_teams):
132143
logging.info("%s: domain not set", app.name)
133144
cf.zones.dns_records.post(cf_zone["id"], data=cf_record_data)
134145
elif existing_record["content"] != cname:
135-
logging.warning("%s: incorrect record value", app.name)
136-
cf.zones.dns_records.patch(
137-
cf_zone["id"], existing_record["id"], data=cf_record_data
138-
)
146+
if is_allowed_cname_target(existing_record["content"]):
147+
logging.info("%s: record is different, but an allowed value", app.name)
148+
else:
149+
logging.warning("%s: incorrect record value", app.name)
150+
cf.zones.dns_records.patch(
151+
cf_zone["id"], existing_record["id"], data=cf_record_data
152+
)
139153

140154
# Enable ACM if not already, so certs can be issued
141155
has_acm = any(d.acm_status for d in app_domains.values())

0 commit comments

Comments
 (0)