Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
badra001 committed Apr 27, 2023
1 parent 7db1f88 commit 8e763c9
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 115 deletions.
246 changes: 131 additions & 115 deletions code/ddns-lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
LOGGER = logging.getLogger()
account_id = None
region = None
VERSION = '1.2.0b64'
VERSION = '1.2.0b65'

# Read Env variables
DEBUG_LOG_LEVEL = os.environ.get('DebugLogLevel', 'INFO')
Expand Down Expand Up @@ -650,52 +650,63 @@ def lambda_handler(
default_hostname = '-'.join(['ip'] + private_ip.split('.'))
if tag_data['option_zone'].valid:
if tag_data['option_name'].valid:
LOGGER.info("1.1 instance: %s, using tag_option.zone hostname %s and tag_option.zone zone %s.",
instance_id, tag_data['option_name'].hostname, tag_data['option_zone'].zonename)
LOGGER.info(f"1.1 instance: {instance_id}, using tag_option.zone hostname {tag_data['option_name'].hostname} and tag_option.zone zone {tag_data['option_zone'].zonename)}")
f_hostname = tag_data['option_name'].hostname
f_zonename = tag_data['option_zone'].zonename
elif tag_data['name'].valid:
LOGGER.info("1.2 instance: %s, using tag_key.Name hostname %s and tag_option.zone zone %s.",
instance_id, tag_data['name'].hostname, tag_data['option_zone'].zonename)
LOGGER.info(
f"1.2 instance: {instance_id}, using tag_key.Name hostname {tag_data['name'].hostname} and tag_option.zone zone {tag_data['option_zone'].zonename}")
f_hostname = tag_data['name'].hostname
f_zonename = tag_data['option_zone'].zonename
else:
LOGGER.info("1.3 instance: %s, using default ip-address %s for hostname and tag_option.zone zone %s.",
instance_id, default_hostname, tag_data['option_zone'].zonename)
LOGGER.info(
f"1.3 instance: {instance_id}, using default ip-address {default_hostname} for hostname and tag_option.zone zone {tag_data['option_zone'].zonename}")
f_hostname = default_hostname
f_zonename = tag_data['option_zone'].zonename

else:
if tag_data['option_name'].valid:
LOGGER.info("2.1 instance: %s, using tag_option.name hostname %s and and tag_option.name zone %s.",
instance_id, tag_data['option_name'].hostname, tag_data['option_name'].zonename)
LOGGER.info(f"2.1 instance: {instance_id}, using tag_option.name hostname {tag_data['option_name'].hostname} and and tag_option.name zone {tag_data['option_name'].zonename)}")
f_hostname = tag_data['option_name'].hostname
f_zonename = tag_data['option_name'].zonename
elif not tag_data['option_name'].valid and tag_data['option_name'].hostname and tag_data['dhcp_options'].valid:
LOGGER.info("2.2 instance: %s, using tag_option.name hostname %s and and dhcp_options zone %s.",
instance_id, tag_data['option_name'].hostname, tag_data['dhcp_options'].zonename)
f_hostname = tag_data['option_name'].hostname
f_zonename = tag_data['dhcp_options'].zonename
elif tag_data['name'].valid:
LOGGER.info("2.3 instance: %s, using tag_key.Name hostname %s and tag_key.Name zone %s.",
instance_id, tag_data['name'].hostname, tag_data['name'].zonename)
LOGGER.info(
f"2.3 instance: {instance_id}, using tag_key.Name hostname {tag_data['name'].hostname} and tag_key.Name zone {tag_data['name'].zonename}")
f_hostname = tag_data['name'].hostname
f_zonename = tag_data['name'].zonename
elif not tag_data['name'].valid and tag_data['dhcp_options'].valid:
LOGGER.info("2.4 instance: %s, using default ip-addresss hostname %s and dhcp_options zone %s.",
instance_id, default_hostname, tag_data['dhcp_options'].zonename)
LOGGER.info(
f"2.4 instance: {instance_id}, using default ip-addresss hostname {default_hostname} and dhcp_options zone {tag_data['dhcp_options'].zonename}")
f_hostname = default_hostname
f_zonename = tag_data['dhcp_options'].zonename
# elif not tag_data['name'].valid and not tag_data['dhcp_options'].valid:
else:
LOGGER.info("3.1 instance: %s, no valid hostname or zone found", instance_id)
LOGGER.info(f"3.1 instance: {instance_id}, no valid hostname or zone found")
f_hostname = None
f_zonename = None
LOGGER.error(
"instance: %s, No DHCP Associated for VPC and no custom tags. Exiting Script", instance_id)
f"instance: {instance_id}, No DHCP Associated for VPC and no custom tags. Exiting Script")
caller_response.append(
'No DHCP Associated for VPC and no custom tags. Exiting Script')
f"No DHCP Associated for VPC and no custom tags. Exiting Script")
return caller_response

if tag_data['option_cname'].valid:
LOGGER.info(
f"4.1 instance: {instance_id}, CNAME using tag_option.cname hostname {tag_data['option_cname'].hostname} and tag_option.cname {tag_data['option_cname'].zonename}")
cf_hostname = tag_data['option_cname'].hostname
cf_zonename = tag_data['option_cname'].zonename
elif not tag_data['option_nname'].valid and tag_data['option_name'].hostname and f_zonename:
LOGGER.info(f"4.2 instance: {instance_id}, CNAME using tag_option.cname hostname {tag_data['option_cname'].hostname} and current zone {f_zonename}")
cf_hostname = tag_data['option_cname'].hostname
cf_zonename = f_zonename
else:
LOGGER.info(f"4.3 instance: {instance_id}, CNAME no valid hostname or zone found, skipping CNAME")
cf_hostname = None
cf_zonename = None
cf_fqdn=create_fqdn(cf_hostname, cf_zonename)

# note this will not continue and set a cname

final_private_hostname = f_hostname if len(f_hostname) > 0 else default_hostname
Expand Down Expand Up @@ -889,7 +900,6 @@ def lambda_handler(
# so using existing tag data will not be valid

if state != 'running':
if not flags['noforward']:
heritage_records = {}
for entry in dns_data:
if entry.rr_type == 'TXT' and "heritage=" in entry.rr_value:
Expand All @@ -898,81 +908,88 @@ def lambda_handler(
if not (entry.rr_type == 'TXT' and "heritage=" in entry.rr_value):
process_response = new_process_delete_records(
instance_id, entry.zone_id, entry.rr_name, entry.zone_name, entry.rr_type, entry.rr_value, heritage_records.get(entry.rr_name, ''))

# Process and delete A record and associated TXT record
process_response = process_delete_records(
route53,
instance_id,
zone_data_forward.zone_id,
final_private_hostname,
zone_data_forward.name,
'A',
private_ip,
heritage_value
)

# only true if existing delete_records and the delete_success from the subroutine is true
delete_records = delete_records and process_response['delete_success']
# append to the lsit
caller_response = caller_response + process_response['msg']
count[f"delete_success.{process_response.get('delete_success')}"] += 1

if not flags['noreverse']:
# Process and delete PTR record and associated TXT record
process_response = process_delete_records(
route53,
instance_id,
zone_data_reverse.zone_id,
tag_data['ptr_entry'].hostname,
tag_data['ptr_entry'].zonename,
'PTR',
final_private_dns_name,
heritage_value
)
# only true if existing delete_records and the delete_success from the subroutine is true
delete_records = delete_records and process_response['delete_success']
# append to the lsit
caller_response = caller_response + process_response['msg']
count[f"delete_success.{process_response.get('delete_success')}"] += 1
delete_records = delete_records and process_response['delete_success']
caller_response = caller_response + process_response['msg']
count[f"delete_success.{process_response.get('delete_success')}"] += 1

# # Process and delete A record and associated TXT record
# process_response = process_delete_records(
# route53,
# instance_id,
# zone_data_forward.zone_id,
# final_private_hostname,
# zone_data_forward.name,
# 'A',
# private_ip,
# heritage_value
# )
#
# # only true if existing delete_records and the delete_success from the subroutine is true
# delete_records = delete_records and process_response['delete_success']
# # append to the lsit
# caller_response = caller_response + process_response['msg']
# count[f"delete_success.{process_response.get('delete_success')}"] += 1
#
# if not flags['noreverse']:
# # Process and delete PTR record and associated TXT record
# process_response = process_delete_records(
# route53,
# instance_id,
# zone_data_reverse.zone_id,
# tag_data['ptr_entry'].hostname,
# tag_data['ptr_entry'].zonename,
# 'PTR',
# final_private_dns_name,
# heritage_value
# )
# # only true if existing delete_records and the delete_success from the subroutine is true
# delete_records = delete_records and process_response['delete_success']
# # append to the lsit
# caller_response = caller_response + process_response['msg']
# count[f"delete_success.{process_response.get('delete_success')}"] += 1

# Process the CNAME record only if it has passed the check
if tag_data['option_cname'].valid:
cname_host_name = tag_data['option_cname'].hostname
cname_domain_suffix = tag_data['option_cname'].zonename
LOGGER.debug("cname record is valid - creating CNAME record:"
" %s", str(cname_host_name) + "." + str(cname_domain_suffix) + lineno())

cname_domain_suffix_item = phz_collection_by_vpc[cname_domain_suffix]
cname_domain_suffix_id = cname_domain_suffix_item['zone_id']
LOGGER.debug("cname_domain_suffix_id: %s", str(cname_domain_suffix_id))
# if tag_data['option_cname'].valid:
if cf_hostname:
LOGGER.debug(f"cname record is valid - creating CNAME record host {cf_hostname} zone {cf_zonename}: {lineno()}")
# cname_host_name = tag_data['option_cname'].hostname
# cname_domain_suffix = tag_data['option_cname'].zonename
# cname_domain_suffix_item = phz_collection_by_vpc[cname_domain_suffix]
# cname_domain_suffix_id = cname_domain_suffix_item['zone_id']
# LOGGER.debug("cname_domain_suffix_id: %s", str(cname_domain_suffix_id))

cf_zonename_id = phz_collection_by_vpc[cf_zonename]['zone_id']
LOGGER.debug(f"cname_domain_suffix_id: {cf_zonename_id}")

# create CNAME record in private zone
if state == 'running':
try:
LOGGER.debug("cname_host_name:"
" %s", str(cname_host_name) + lineno())
LOGGER.debug("cname_domain_suffix:"
" %s", str(cname_domain_suffix) + lineno())
LOGGER.debug("cname_domain_suffix_id:"
" %s", str(cname_domain_suffix_id) + lineno())

# LOGGER.debug("cname_host_name:"
# " %s", str(cname_host_name) + lineno())
# LOGGER.debug("cname_domain_suffix:"
# " %s", str(cname_domain_suffix) + lineno())
# LOGGER.debug("cname_domain_suffix_id:"
# " %s", str(cname_domain_suffix_id) + lineno())
#
create_response = create_resource_record(
route53,
instance_id,
cname_domain_suffix_id,
cname_host_name,
cname_domain_suffix,
# cname_domain_suffix_id,
# cname_host_name,
# cname_domain_suffix,
cf_zonename_id,
cf_hostname,
cf_zonename,
'CNAME',
final_private_dns_name
final_private_dns_name,
)
append_msg = f"CNAME record in zone id: {cname_domain_suffix_id} owner {phz_collection_by_vpc[cname_domain_suffix]['owner_account']} " + \
f"hostname {cname_host_name} in zone {cname_domain_suffix} with value {final_private_dns_name}"
append_msg = f"CNAME record in zone id: {cf_zonename_id} owner {phz_collection_by_vpc[cf_zonename_id]['owner_account']} " + \
f"hostname {cf_hostname} in zone {cf_zonename} with value {final_private_dns_name}"

if create_response == 'success':
dns_data.append(dns_data_tuple(
cname_domain_suffix_id, cname_host_name, cname_domain_suffix, 'CNAME', final_private_dns_name))
LOGGER.info("instance: %s, Created %s",
instance_id, append_msg + lineno())
cf_zonename_id, cf_hostname, cf_zonename, 'CNAME', cf_fqdn))
LOGGER.info(f"instance: {instance_id}, Created {append_msg} {lineno()}")
caller_response.append('Created ' + append_msg)
else:
caller_response.append(create_response)
Expand All @@ -985,56 +1002,55 @@ def lambda_handler(
try:
if not flags['noheritage']:
if len(heritage) > 0:
LOGGER.debug("Creating heritage TXT resource records %s, with value of %s",
TXT_RR_PREFIX + '.' + cname_host_name, str(heritage_value) + lineno())
cname_host_name_txt = TXT_RR_PREFIX + '.' + cname_host_name
cf_hostname_txt=TXT_RR_PREFIX+'.'+cf_hostname
LOGGER.debug(f"Creating heritage TXT resource records host {cf_hostname_txt} zone {cf_zonename} value {heritage_value} {lineno()}")
create_response = create_resource_record(
route53,
instance_id,
cname_domain_suffix_id,
cname_host_name_txt,
cname_domain_suffix,
# cname_domain_suffix_id,
# cname_host_name_txt,
# cname_domain_suffix,
cf_zonename_id,
cf_hostname_txt,
cf_zonename,
'TXT',
heritage_value
)
append_msg = f"TXT for CNAME record in zone id: {cname_domain_suffix_id} owner {phz_collection_by_vpc[cname_domain_suffix]['owner_account']} " + \
f"hostname {cname_host_name} in zone {cname_domain_suffix} with value {heritage_value}"
append_msg = f"TXT for CNAME record in zone id: {cf_zonename_id} owner {phz_collection_by_vpc[cf_zonename]['owner_account']} " + \
f"hostname {cf_hostname} in zone {cf_zonename} with value {heritage_value}"

if create_response == 'success':
dns_data.append(dns_data_tuple(
cname_domain_suffix_id, cname_host_name_txt, cname_domain_suffix, 'TXT', heritage_value))
LOGGER.info("instance: %s, Created %s",
instance_id, append_msg + lineno())
cf_zonename_id, cf_hostname_txt, cf_zonename, 'TXT', heritage_value))
LOGGER.info(f"instance: {instance_id}, Created {append_msg} {lineno()}")
caller_response.append('Created ' + append_msg)
else:
caller_response.append(create_response)
caller_response.append('Failed to create ' + append_msg)
LOGGER.error(
'Failed to create TXT fpr CNAME record: %s', create_response)
LOGGER.error(f"Failed to create TXT for CNAME record: {create_response}")

except BaseException as err:
LOGGER.error("instance: %s, unexpected error. %s\n",
instance_id, str(err) + lineno())

# not running, so process delete CNAME and associated TXT record
else:
# Process and delete CNAME record and associated TXT record
process_response = process_delete_records(
route53,
instance_id,
cname_domain_suffix_id,
cname_host_name,
cname_domain_suffix,
'CNAME',
final_private_dns_name,
heritage_value
)

# only true if existing delete_records and the delete_success from the subroutine is true
delete_records = delete_records and process_response['delete_success']
# append to the lsit
caller_response = caller_response + process_response['msg']
LOGGER.error(f"instance: {instance_id}, unexpected error: {err} {lineno()}")

# # not running, so process delete CNAME and associated TXT record
# else:
# # Process and delete CNAME record and associated TXT record
# process_response = process_delete_records(
# route53,
# instance_id,
# cname_domain_suffix_id,
# cname_host_name,
# cname_domain_suffix,
# 'CNAME',
# final_private_dns_name,
# heritage_value
# )
#
# # only true if existing delete_records and the delete_success from the subroutine is true
# delete_records = delete_records and process_response['delete_success']
# # append to the lsit
# caller_response = caller_response + process_response['msg']
#
# update ddb entry to include dns entries written to be able to delete them properly
if state == 'running':
try:
Expand Down
Binary file modified code/ddns-lambda.zip
Binary file not shown.

0 comments on commit 8e763c9

Please sign in to comment.