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

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