Blame SOURCES/bz1810466-aws-vpc-move-ip-1-add-routing_table_role.patch

02ddf4
From 4bc84bccec88abcd9bd0b840532b23ed0934c0d4 Mon Sep 17 00:00:00 2001
02ddf4
From: Jason McCloskey <jamscclo@amazon.com>
02ddf4
Date: Mon, 10 Feb 2020 15:18:30 -0800
02ddf4
Subject: [PATCH] Allow user to specify role with which to query/update route
02ddf4
 table
02ddf4
02ddf4
Allow user to specify role with which to query/update route table
02ddf4
02ddf4
Allow user to specify role with which to query/update route table
02ddf4
---
02ddf4
 heartbeat/aws-vpc-move-ip | 40 +++++++++++++++++++++++++++++++++------
02ddf4
 1 file changed, 34 insertions(+), 6 deletions(-)
02ddf4
02ddf4
diff --git a/heartbeat/aws-vpc-move-ip b/heartbeat/aws-vpc-move-ip
02ddf4
index b27d0bfcd..23eec18cc 100755
02ddf4
--- a/heartbeat/aws-vpc-move-ip
02ddf4
+++ b/heartbeat/aws-vpc-move-ip
02ddf4
@@ -145,6 +145,24 @@ Enable enhanced monitoring using AWS API calls to check route table entry
02ddf4
 END
02ddf4
 }
02ddf4
 
02ddf4
+
02ddf4
+execute_cmd_as_role(){
02ddf4
+	cmd=$1
02ddf4
+	role=$2
02ddf4
+	output="$(aws sts assume-role --role-arn $role --role-session-name AWSCLI-RouteTableUpdate --profile $OCF_RESKEY_profile --output=text)"
02ddf4
+	export AWS_ACCESS_KEY_ID="$(echo $output | awk -F" " '$4=="CREDENTIALS" {print $5}')"
02ddf4
+	export AWS_SECRET_ACCESS_KEY="$(echo $output | awk -F" " '$4=="CREDENTIALS" {print $7}')"
02ddf4
+	export AWS_SESSION_TOKEN="$(echo $output | awk -F" " '$4=="CREDENTIALS" {print $8}')"
02ddf4
+
02ddf4
+	#Execute command
02ddf4
+	ocf_log debug "Assumed Role ${role}"
02ddf4
+	ocf_log debug "$(aws sts get-caller-identity)"
02ddf4
+	ocf_log debug "executing command: $cmd"
02ddf4
+	response="$($cmd)"
02ddf4
+	unset output AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN
02ddf4
+	echo $response
02ddf4
+}
02ddf4
+
02ddf4
 ec2ip_set_address_param_compat(){
02ddf4
 	# Include backward compatibility for the deprecated address parameter
02ddf4
 	if [ -z  "$OCF_RESKEY_ip" ] && [ -n "$OCF_RESKEY_address" ]; then
02ddf4
@@ -177,9 +195,14 @@ ec2ip_monitor() {
02ddf4
 	if ocf_is_true ${OCF_RESKEY_monapi} || [ "$__OCF_ACTION" = "start" ] || ocf_is_probe; then
02ddf4
 		for rtb in $(echo $OCF_RESKEY_routing_table | sed -e 's/,/ /g'); do
02ddf4
 			ocf_log info "monitor: check routing table (API call) - $rtb"
02ddf4
-			cmd="$OCF_RESKEY_awscli --profile $OCF_RESKEY_profile --output text ec2 describe-route-tables --route-table-ids $rtb --query RouteTables[*].Routes[?DestinationCidrBlock=='$OCF_RESKEY_ip/32'].InstanceId"
02ddf4
-			ocf_log debug "executing command: $cmd"
02ddf4
-			ROUTE_TO_INSTANCE="$($cmd)"
02ddf4
+			if [[ -z "${OCF_RESKEY_routing_table_role}" ]]; then
02ddf4
+				cmd="$OCF_RESKEY_awscli --profile $OCF_RESKEY_profile --output text ec2 describe-route-tables --route-table-ids $rtb --query RouteTables[*].Routes[?DestinationCidrBlock=='$OCF_RESKEY_ip/32'].InstanceId"
02ddf4
+				ocf_log debug "executing command: $cmd"
02ddf4
+				ROUTE_TO_INSTANCE="$($cmd)"
02ddf4
+			else
02ddf4
+				cmd="$OCF_RESKEY_awscli --output text ec2 describe-route-tables --route-table-ids $rtb --query RouteTables[*].Routes[?DestinationCidrBlock=='$OCF_RESKEY_ip/32'].InstanceId"
02ddf4
+				ROUTE_TO_INSTANCE="$(execute_cmd_as_role "$cmd" $OCF_RESKEY_routing_table_role)"
02ddf4
+			fi
02ddf4
 			ocf_log debug "Overlay IP is currently routed to ${ROUTE_TO_INSTANCE}"
02ddf4
 			if [ -z "$ROUTE_TO_INSTANCE" ]; then
02ddf4
 				ROUTE_TO_INSTANCE="<unknown>"
02ddf4
@@ -253,9 +276,14 @@ ec2ip_get_and_configure() {
02ddf4
 	ocf_log debug "network interface id associated MAC address ${MAC_ADDR}: ${EC2_NETWORK_INTERFACE_ID}"
02ddf4
 
02ddf4
 	for rtb in $(echo $OCF_RESKEY_routing_table | sed -e 's/,/ /g'); do
02ddf4
-		cmd="$OCF_RESKEY_awscli --profile $OCF_RESKEY_profile --output text ec2 replace-route --route-table-id $rtb --destination-cidr-block ${OCF_RESKEY_ip}/32 --network-interface-id $EC2_NETWORK_INTERFACE_ID"
02ddf4
-		ocf_log debug "executing command: $cmd"
02ddf4
-		$cmd
02ddf4
+		if [[ -z "${OCF_RESKEY_routing_table_role}" ]]; then
02ddf4
+			cmd="$OCF_RESKEY_awscli --profile $OCF_RESKEY_profile --output text ec2 replace-route --route-table-id $rtb --destination-cidr-block ${OCF_RESKEY_ip}/32 --network-interface-id $EC2_NETWORK_INTERFACE_ID"
02ddf4
+			ocf_log debug "executing command: $cmd"
02ddf4
+			$cmd
02ddf4
+		else
02ddf4
+			cmd="$OCF_RESKEY_awscli --output text ec2 replace-route --route-table-id $rtb --destination-cidr-block ${OCF_RESKEY_ip}/32 --network-interface-id $EC2_NETWORK_INTERFACE_ID"
02ddf4
+			update_response="$(execute_cmd_as_role "$cmd" $OCF_RESKEY_routing_table_role)"
02ddf4
+		fi
02ddf4
 		rc=$?
02ddf4
 		if [ "$rc" != 0 ]; then
02ddf4
 			ocf_log warn "command failed, rc: $rc"