<s> from __future__ import print_function <EOL> import json <EOL> import re <EOL> import hashlib <EOL> import boto3 <EOL> config_s3_region = '<STR_LIT>' <EOL> config_s3_bucket = '<STR_LIT>' <EOL> config_s3_key = '<STR_LIT>' <EOL> '''<STR_LIT>''' <EOL> def read_s3_config ( ) : <EOL> s3_client = boto3 . client ( <EOL> '<STR_LIT>' , <EOL> config_s3_region , <EOL> ) <EOL> s3_client . download_file ( <EOL> config_s3_bucket , <EOL> config_s3_key , <EOL> '<STR_LIT>' % config_s3_key <EOL> ) <EOL> full_config = ( open ( '<STR_LIT>' % config_s3_key ) . read ( ) ) <EOL> return json . loads ( full_config ) <EOL> '''<STR_LIT>''' <EOL> def route53_client ( execution_mode , aws_region , route_53_zone_id , <EOL> route_53_record_name , route_53_record_ttl , <EOL> route_53_record_type , public_ip ) : <EOL> route53_client = boto3 . client ( <EOL> '<STR_LIT>' , <EOL> region_name = aws_region <EOL> ) <EOL> if execution_mode == '<STR_LIT>' : <EOL> current_route53_record_set = route53_client . list_resource_record_sets ( <EOL> HostedZoneId = route_53_zone_id , <EOL> StartRecordName = route_53_record_name , <EOL> StartRecordType = route_53_record_type , <EOL> MaxItems = '<STR_LIT:2>' <EOL> ) <EOL> for eachRecord in current_route53_record_set [ '<STR_LIT>' ] : <EOL> if eachRecord [ '<STR_LIT:Name>' ] == route_53_record_name : <EOL> if len ( eachRecord [ '<STR_LIT>' ] ) == <NUM_LIT:1> : <EOL> for eachSubRecord in eachRecord [ '<STR_LIT>' ] : <EOL> currentroute53_ip = eachSubRecord [ '<STR_LIT>' ] <EOL> return_status = '<STR_LIT:success>' <EOL> return_message = currentroute53_ip <EOL> return { '<STR_LIT>' : return_status , <EOL> '<STR_LIT>' : return_message } <EOL> elif len ( eachRecord [ '<STR_LIT>' ] ) > <NUM_LIT:1> : <EOL> return_status = '<STR_LIT>' <EOL> return_message = '<STR_LIT>' '<STR_LIT>' <EOL> return { '<STR_LIT>' : return_status , <EOL> '<STR_LIT>' : return_message } <EOL> if execution_mode == '<STR_LIT>' : <EOL> change_route53_record_set = route53_client . change_resource_record_sets ( <EOL> HostedZoneId = route_53_zone_id , <EOL> ChangeBatch = { <EOL> '<STR_LIT>' : [ <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:Name>' : route_53_record_name , <EOL> '<STR_LIT>' : route_53_record_type , <EOL> '<STR_LIT>' : route_53_record_ttl , <EOL> '<STR_LIT>' : [ <EOL> { <EOL> '<STR_LIT>' : public_ip <EOL> } <EOL> ] <EOL> } <EOL> } <EOL> ] <EOL> } <EOL> ) <EOL> return <NUM_LIT:1> <EOL> '''<STR_LIT>''' <EOL> def run_set_mode ( set_hostname , validation_hash , source_ip ) : <EOL> try : <EOL> full_config = read_s3_config ( ) <EOL> except : <EOL> return_status = '<STR_LIT>' <EOL> return_message = '<STR_LIT>' '<STR_LIT>' <EOL> return { '<STR_LIT>' : return_status , <EOL> '<STR_LIT>' : return_message } <EOL> record_config_set = full_config [ set_hostname ] <EOL> aws_region = record_config_set [ '<STR_LIT>' ] <EOL> route_53_zone_id = record_config_set [ '<STR_LIT>' ] <EOL> route_53_record_ttl = record_config_set [ '<STR_LIT>' ] <EOL> route_53_record_type = record_config_set [ '<STR_LIT>' ] <EOL> shared_secret = record_config_set [ '<STR_LIT>' ] <EOL> if not re . match ( r'<STR_LIT>' , validation_hash ) : <EOL> return_status = '<STR_LIT>' <EOL> return_message = '<STR_LIT>' '<STR_LIT>' <EOL> return { '<STR_LIT>' : return_status , <EOL> '<STR_LIT>' : return_message } <EOL> calculated_hash = hashlib . sha256 ( <EOL> source_ip + set_hostname + shared_secret ) . hexdigest ( ) <EOL> if not calculated_hash == validation_hash : <EOL> return_status = '<STR_LIT>' <EOL> return_message = '<STR_LIT>' <EOL> return { '<STR_LIT>' : return_status , <EOL> '<STR_LIT>' : return_message } <EOL> else : <EOL> route53_get_response = route53_client ( <EOL> '<STR_LIT>' , <EOL> aws_region , <EOL> route_53_zone_id , <EOL> set_hostname , <EOL> route_53_record_ttl , <EOL> route_53_record_type , <EOL> '<STR_LIT>' ) <EOL> if not route53_get_response : <EOL> route53_ip = '<STR_LIT:0>' <EOL> elif route53_get_response [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> return_status = route53_get_response [ '<STR_LIT>' ] <EOL> return_message = route53_get_response [ '<STR_LIT>' ] <EOL> return { '<STR_LIT>' : return_status , <EOL> '<STR_LIT>' : return_message } <EOL> else : <EOL> route53_ip = route53_get_response [ '<STR_LIT>' ] <EOL> if route53_ip == source_ip : <EOL> return_status = '<STR_LIT:success>' <EOL> return_message = '<STR_LIT>' '<STR_LIT>' <EOL> return { '<STR_LIT>' : return_status , <EOL> '<STR_LIT>' : return_message } <EOL> else : <EOL> return_status = route53_client ( <EOL> '<STR_LIT>' , <EOL> aws_region , <EOL> route_53_zone_id , <EOL> set_hostname , <EOL> route_53_record_ttl , <EOL> route_53_record_type , <EOL> source_ip ) <EOL> return_status = '<STR_LIT:success>' <EOL> return_message = '<STR_LIT>' + set_hostname + '<STR_LIT>' + source_ip <EOL> return { '<STR_LIT>' : return_status , <EOL> '<STR_LIT>' : return_message } <EOL> '''<STR_LIT>''' <EOL> def lambda_handler ( event , context ) : <EOL> execution_mode = event [ '<STR_LIT>' ] <EOL> source_ip = event [ '<STR_LIT>' ] <EOL> query_string = event [ '<STR_LIT>' ] <EOL> validation_hash = event [ '<STR_LIT>' ] <EOL> set_hostname = event [ '<STR_LIT>' ] <EOL> execution_modes = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if execution_mode not in execution_modes : <EOL> return_status = '<STR_LIT>' <EOL> return_message = '<STR_LIT>' <EOL> return_dict = { '<STR_LIT>' : return_status , <EOL> '<STR_LIT>' : return_message } <EOL> if execution_mode == '<STR_LIT>' : <EOL> return_status = '<STR_LIT:success>' <EOL> return_message = source_ip <EOL> return_dict = { '<STR_LIT>' : return_status , <EOL> '<STR_LIT>' : return_message } <EOL> else : <EOL> return_dict = run_set_mode ( set_hostname , validation_hash , source_ip ) <EOL> return return_dict </s>
