Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip creation of new hosted zone for ACM certificate requests #167

Merged
merged 2 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 2 additions & 88 deletions aladdin/lib/aws/dns_mapping.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
#!/usr/bin/env python3
import logging
from datetime import datetime

from aladdin.lib.cluster_rules import ClusterRules


def fill_hostedzone(
boto_session, hostnames_to_loadbalancers, cluster_domain_name=None, namespace_domain_name=None
boto_session, hostnames_to_loadbalancers, cluster_domain_name=None
):
cluster_domain_name = cluster_domain_name or ClusterRules().cluster_domain_name
namespace_domain_name = namespace_domain_name or ClusterRules().namespace_domain_name
namespace_hosted_zone = get_hostedzone(
boto_session, namespace_domain_name
) or create_hostedzone(boto_session, namespace_domain_name)

cluster_hosted_zone = get_hostedzone(boto_session, cluster_domain_name)
if cluster_hosted_zone is None:
raise KeyError("route 53 for [%s] not found" % cluster_domain_name)

dns_nameservers = get_ns_from_hostedzone(boto_session, namespace_hosted_zone)

check_ns_values(boto_session, cluster_hosted_zone, namespace_domain_name, dns_nameservers)

return fill_dns_dict(boto_session, namespace_hosted_zone, hostnames_to_loadbalancers)
return fill_dns_dict(boto_session, cluster_hosted_zone, hostnames_to_loadbalancers)


def get_hostedzone(boto_session, dns_name) -> str:
Expand All @@ -48,82 +38,6 @@ def get_hostedzone(boto_session, dns_name) -> str:
return hostedzone_id


def create_hostedzone(boto_session, dns_name) -> str:
log = logging.getLogger(__name__)
route53 = boto_session.client("route53")

ref = "aladdin_generated_{:%Y%m%d_%H%M%S}".format(datetime.now())

log.info("Hosted zone %s created", dns_name)

log.info("Creating hosted zone : %s", dns_name)
create_res = route53.create_hosted_zone(
Name=dns_name,
CallerReference=ref,
HostedZoneConfig=dict(Comment="Generated by aladdin", PrivateZone=False),
)

return create_res["HostedZone"]["Id"]


def get_ns_from_hostedzone(boto_session, hostedzone_id):
route53 = boto_session.client("route53")

hosted_info = route53.get_hosted_zone(Id=hostedzone_id)

dns_name = hosted_info["HostedZone"]["Name"].strip(".")

response = get_all_resource_record_sets(route53, hostedzone_id)
rrs_list = [rrs for rrs in response if rrs["Name"] == "%s." % dns_name and rrs["Type"] == "NS"]

if len(rrs_list) == 0:
raise Exception("Main NS in hosted zone not found")

if len(rrs_list) > 1:
raise Exception("Too much Main NS in hosted zone found")

res = [rr["Value"] for rr in rrs_list[0]["ResourceRecords"]]

return res


def check_ns_values(boto_session, main_hosted_id, sub_dns, ns_values):
log = logging.getLogger(__name__)
route53 = boto_session.client("route53")

response = get_all_resource_record_sets(route53, main_hosted_id)

rrs_list = [rrs for rrs in response if rrs["Name"] == "%s." % sub_dns and rrs["Type"] == "NS"]

values = None
if rrs_list:
values = [rr["Value"] for rr in rrs_list[0]["ResourceRecords"]]

if values == ns_values:
# Good case
log.info("NS value from %s to %s is good", main_hosted_id, sub_dns)
return

log.info("Setting NS value from %s to %s", main_hosted_id, sub_dns)
route53.change_resource_record_sets(
HostedZoneId=main_hosted_id,
ChangeBatch=dict(
Comment="generated by aladdin",
Changes=[
dict(
Action="UPSERT",
ResourceRecordSet=dict(
Name="%s." % sub_dns,
Type="NS",
TTL=5 * 60, # 5 min
ResourceRecords=[{"Value": v} for v in ns_values],
),
)
],
),
)


def extract_cname_mapping(boto_session, hostedzone_id):
route53 = boto_session.client("route53")

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "aladdin"
version = "1.27.13.0"
version = "1.27.13.1"
description = ""
authors = ["Fivestars <dev@fivestars.com>"]
include = [
Expand Down
Loading