Skip to content

Commit

Permalink
change version to test heritage changes
Browse files Browse the repository at this point in the history
  • Loading branch information
badra001 committed Apr 26, 2022
1 parent 32f1351 commit b4f6edf
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 48 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,6 @@
1. MaxItem value changed to string.
1. Fixed def parse_heritage to add more logging and better input validation
1. Fixed def get_heritage_item to check input type and add more logging

* 0.3.1 - 2022-04-26
- change version purely to test heritage code handling a version change
96 changes: 49 additions & 47 deletions code/ddns-lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
LOGGER = logging.getLogger()
ACCOUNT = None
REGION = None
VERSION = '0.3.0'
VERSION = '0.3.1'

# Read Env variables
DEBUG_LOG_LEVEL = os.environ.get('DebugLogLevel', 'INFO')
Expand Down Expand Up @@ -715,7 +715,7 @@ def lambda_handler(
LOGGER.info("instance: %s, no custom tags - use default.", instance_id)
final_private_hostname = private_host_name
final_hosted_zone_name = private_hosted_zone_name
else:
else:
LOGGER.error(
"instance: %s, No DHCP Associated for VPC and no custom tags. Exiting Script", instance_id)
# nothing to do, exit out script
Expand Down Expand Up @@ -1618,22 +1618,23 @@ def new_get_resource_record(client, instance_id, zone_id, host_name, hosted_zone

while i < MAX_API_RETRY:
try:
LOGGER.debug("Getting %s record type for %s",
record_type, host_name + lineno())
LOGGER.debug("Getting %s record type for %s",
record_type, host_name + lineno())

if host_name[-1] != '.':
host_name = host_name + '.'

LOGGER.debug("list_resource_record_sets looking for record %s in zone %s",
str(host_name), str(hosted_zone_name) + lineno())
LOGGER.debug("list_resource_record_sets looking for record %s in zone %s",
str(host_name), str(hosted_zone_name) + lineno())

response = client.list_resource_record_sets(
HostedZoneId=zone_id,
StartRecordName=host_name + hosted_zone_name,
StartRecordType=record_type,
MaxItems='1')

LOGGER.debug("list_resource_record_sets response: %s", json.dumps(response) + lineno())
LOGGER.debug("list_resource_record_sets response: %s",
json.dumps(response) + lineno())

for rr_set in response['ResourceRecordSets']:
rr_name = rr_set['Name']
Expand All @@ -1642,11 +1643,11 @@ def new_get_resource_record(client, instance_id, zone_id, host_name, hosted_zone
if rr_name == (host_name + hosted_zone_name):
value = rr_set['ResourceRecords'][0]['Value']
LOGGER.debug("list_resource_record_sets returned value. %s",
str(value) + lineno())
else:
str(value) + lineno())
else:
LOGGER.debug("list_resource_record_sets returned different record ignoring. %s",
str(rr_name) + lineno())
str(rr_name) + lineno())

LOGGER.debug(
"list_resource_record_sets returned without error. %s", lineno())
break
Expand Down Expand Up @@ -2392,19 +2393,19 @@ def parse_heritage(info):
str(info) + lineno())

# return empty dictionary if non-string passed it
if not (isinstance(info,str)):
if not (isinstance(info, str)):
LOGGER.error("heritage parsing error value: non-string value passed in: %s",
str(info) + lineno())
str(info) + lineno())
return {}

# check if not empty string and then remove leading and trailing quotes
if len(info) > 1:
# remove beginning quote
if info[0] == '"':
if info[0] == '"':
info = info[1:]

# remove ending quote
if info[-1] == '"':
if info[-1] == '"':
info = info[:-1]

kv_results = {}
Expand All @@ -2414,13 +2415,13 @@ def parse_heritage(info):

if header[0] != 'heritage':
LOGGER.debug("heritage analysis: does not contain heritage header, returning: %s",
str(kv_results) + lineno())
str(kv_results) + lineno())
return kv_results
else:
appname = header[1]
kv_results['application_name'] = appname
LOGGER.debug("heritage analysis: assigning application_name: %s",
str(appname) + lineno())
str(appname) + lineno())

try:
for item in kv:
Expand All @@ -2437,9 +2438,9 @@ def parse_heritage(info):
# else:
version = 'null'
# return initialize_heritage(appname,version,kv_results)

LOGGER.debug("heritage parsed dictionary: %s",
str(kv_results) + lineno())
str(kv_results) + lineno())
return kv_results
except:
return {}
Expand Down Expand Up @@ -2472,15 +2473,16 @@ def get_heritage_item(data, key):
# result = v
# return result

if not isinstance(data,dict):
if not isinstance(data, dict):
LOGGER.debug("get_heritage_item: not valid dictionary: %s is a class of %s",
str(data), str(type(data)) + lineno())
str(data), str(type(data)) + lineno())
return None
else:
result = data.get(key, None)
LOGGER.debug("get_heritage_item: getting key %s value %s",
str(key), str(result) + lineno())
return result
str(key), str(result) + lineno())
return result


def compare_heritage(data, key, value):
"""
Expand Down Expand Up @@ -2532,9 +2534,9 @@ def publish_to_sns(client, message):
else:
LOGGER.debug("No SNS Topic specified, ignoring")

def process_delete_records(route53, instance_id, zone_id,
record_name, zone_name, record_type, record_value, heritage_value):

def process_delete_records(route53, instance_id, zone_id,
record_name, zone_name, record_type, record_value, heritage_value):
"""
Consolidate all of the logic to evaluate the process deletion for A/PTR/AAAA record and associated TXT record.
:param route53:
Expand All @@ -2545,15 +2547,15 @@ def process_delete_records(route53, instance_id, zone_id,
:param record_type:
:param record_value:
:return response: # dictionary of 'delete_success' and 'msg'
"""
"""

response = {}
response_delete_success = True
response_msg = []

LOGGER.info("instance: %s, Delete %s Record. Checking TXT record association for %s in zone %s",
instance_id, record_type, record_name, zone_name + lineno())

# if record type is CNAME, we need to add the TXT RR prefix
if record_type == 'CNAME':
txt_record_name = TXT_RR_PREFIX + '.' + record_name
Expand All @@ -2576,10 +2578,12 @@ def process_delete_records(route53, instance_id, zone_id,

# check if the TXT record was created by the Lambda as match instance-id
if verify_heritage_owner(heritage, HERITAGE_TAG):
LOGGER.debug("TXT record was created by Lambda DDNS %s", HERITAGE_TAG + lineno())
LOGGER.debug("TXT record was created by Lambda DDNS %s",
HERITAGE_TAG + lineno())
heritage_own = True
else:
LOGGER.info("TXT record was not created by Lambda DDNS %s", HERITAGE_TAG + lineno())
LOGGER.info("TXT record was not created by Lambda DDNS %s",
HERITAGE_TAG + lineno())
heritage_own = False

if compare_heritage(heritage, 'instance_id', instance_id):
Expand Down Expand Up @@ -2637,18 +2641,18 @@ def process_delete_records(route53, instance_id, zone_id,
LOGGER.info("instance: %s, Success: %s",
instance_id, response_text + lineno())
response_msg.append("Success: " + response_text)

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

else:
response_delete_success = False
response_msg.append("Failed, the TXT record for the " + record_type +
" record does not match expected value. Will not delete the " + record_type + " record.")
response_msg.append("Failed, the TXT record for the " + record_type +
" record does not match expected value. Will not delete the " + record_type + " record.")
LOGGER.error("instance: %s, the TXT record for the %s record does not match expected value. Will not delete the %s record. %s\n",
instance_id, record_type, record_type, lineno())
instance_id, record_type, record_type, lineno())
if SNS_ENABLE:
try:
sns_msg = {}
Expand All @@ -2662,21 +2666,20 @@ def process_delete_records(route53, instance_id, zone_id,
sns_heritage['zone_name'] = zone_name
sns_heritage['zone_id'] = zone_id
sns_heritage['heritage_value'] = heritage_value
sns_msg['heritage']=sns_heritage

sns_msg['heritage'] = sns_heritage
publish_to_sns(get_sns_client(), json.dumps(sns_msg))
LOGGER.info("instance: %s, sending sns message %s", instance_id,
json.dumps(sns_msg) + lineno())
except:
LOGGER.info("instance: %s, error: %s", instance_id,
str(sys.exc_info()[0]) + lineno())


# delete TXT record associated with A/PTR/CNAME/AAAA record
if heritage_own and heritage_instance_match:
try:
LOGGER.info("Deleting heritage TXT resource record %s, in the zone %s, with value of %s",
txt_record_name, zone_name, str(heritage_value) + lineno())
txt_record_name, zone_name, str(heritage_value) + lineno())

response_text = 'Delete ' + 'TXT' + \
' record in zone id: ' + zone_id + \
Expand Down Expand Up @@ -2721,29 +2724,29 @@ def process_delete_records(route53, instance_id, zone_id,
except BaseException as err:
response_delete_success = False
LOGGER.error("instance: %s, unexpected error. %s\n",
instance_id, str(err) + lineno())
instance_id, str(err) + lineno())
else:
response_delete_success = False
response_msg.append("Failed, the TXT record for " + record_type +
response_msg.append("Failed, the TXT record for " + record_type +
" does not match expected value. Will not delete the TXT record.")
LOGGER.error("instance: %s, the TXT record for the %s does not match expected value. Will not delete TXT record. %s",
instance_id, record_type, lineno())
instance_id, record_type, lineno())

if SNS_ENABLE:
try:
sns_msg = {}
sns_msg['instance_id'] = instance_id
sns_msg['account_id'] = get_caller_account_id()
sns_msg['message'] = 'TXT record does not match. Will not delete the TXT record.'

sns_heritage = {}
sns_heritage['record_type'] = 'TXT'
sns_heritage['record_name'] = txt_record_name
sns_heritage['zone_name'] = zone_name
sns_heritage['zone_id'] = zone_id
sns_heritage['heritage_value'] = heritage_value
sns_msg['heritage']=sns_heritage

sns_msg['heritage'] = sns_heritage
publish_to_sns(get_sns_client(), json.dumps(sns_msg))
LOGGER.info("instance: %s, sending sns message %s", instance_id,
json.dumps(sns_msg) + lineno())
Expand All @@ -2752,7 +2755,6 @@ def process_delete_records(route53, instance_id, zone_id,
LOGGER.info("instance: %s, error: %s", instance_id,
str(sys.exc_info()[0]) + lineno())


# create a dictionary to return
response['delete_success'] = response_delete_success
response['msg'] = response_msg
Expand Down
Binary file modified code/ddns-lambda.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion version.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
locals {
_module_version = "0.3.0"
_module_version = "0.3.1"
}

0 comments on commit b4f6edf

Please sign in to comment.