|
| 1 | +#!/usr/bin/env bash |
| 2 | +#. .env |
| 3 | + |
| 4 | +set -e |
| 5 | + |
| 6 | +mkdir -p /eks-workshop/logs |
| 7 | +log_file=/eks-workshop/logs/action-$(date +%s).log |
| 8 | + |
| 9 | +exec 2>&1 |
| 10 | + |
| 11 | +logmessage() { |
| 12 | + echo "$@" >&7 |
| 13 | + echo "$@" >&1 |
| 14 | +} |
| 15 | +export -f logmessage |
| 16 | + |
| 17 | +# Function to get the role name from a role ARN |
| 18 | +get_role_name_from_arn() { |
| 19 | + local role_arn=$1 |
| 20 | + |
| 21 | + # Extract the role name from the ARN |
| 22 | + role_name=$(logmessage "$role_arn" | awk -F'/' '{print $NF}') |
| 23 | + |
| 24 | + if [ -n "$role_name" ]; then |
| 25 | + logmessage "$role_name" |
| 26 | + else |
| 27 | + logmessage "Failed to retrieve role name from ARN: $role_arn" |
| 28 | + return 1 |
| 29 | + fi |
| 30 | +} |
| 31 | + |
| 32 | +# Function to get the Kubernetes role attached to a service account |
| 33 | +get_service_account_role() { |
| 34 | + local namespace=$1 |
| 35 | + local service_account=$2 |
| 36 | + |
| 37 | + # Get the role ARN associated with the service account |
| 38 | + role_arn=$(kubectl get serviceaccount "$service_account" -n "$namespace" -o jsonpath="{.metadata.annotations['eks\.amazonaws\.com\/role-arn']}") |
| 39 | + |
| 40 | + if [ -n "$role_arn" ]; then |
| 41 | + logmessage "Service Account: $service_account" |
| 42 | + logmessage "Namespace: $namespace" |
| 43 | + logmessage "Role ARN: $role_arn" |
| 44 | + get_role_name_from_arn "$role_arn" |
| 45 | + return 0 |
| 46 | + else |
| 47 | + logmessage "Failed to retrieve role for service account '$service_account' in namespace '$namespace'" |
| 48 | + return 1 |
| 49 | + fi |
| 50 | + |
| 51 | +} |
| 52 | + |
| 53 | +# Function to get the first policy ARN attached to a role ARN |
| 54 | +get_first_policy_arn_from_role_arn() { |
| 55 | + local role_arn=$1 |
| 56 | + |
| 57 | + # Get the list of policies attached to the role |
| 58 | + policy_arn=$(aws iam list-attached-role-policies --role-name "$role_arn" --query 'AttachedPolicies[0].PolicyArn' --output text) |
| 59 | + |
| 60 | + if [ -n "$policy_arn" ]; then |
| 61 | + logmessage "First Policy ARN attached to role '$role_arn':" |
| 62 | + logmessage "Policy: $policy_arn" |
| 63 | + return 0 |
| 64 | + else |
| 65 | + logmessage "Failed to retrieve policy ARN for role '$role_arn'" |
| 66 | + return 1 |
| 67 | + fi |
| 68 | +} |
| 69 | + |
| 70 | +# Function to update the policy with new statement |
| 71 | +update_policy_with_new_statement() { |
| 72 | + local policy_arn=$1 |
| 73 | + local new_statement=$2 |
| 74 | + |
| 75 | + logmessage "PolicyARN: $policy_arn" |
| 76 | + logmessage "Statement: $new_statement" |
| 77 | + aws iam create-policy-version --policy-arn $policy_arn --policy-document $new_statement --set-as-default |
| 78 | + |
| 79 | +} |
| 80 | + |
| 81 | +# Function to remove an action from a policy statement |
| 82 | +remove_action_from_policy_statement() { |
| 83 | + local policy_name=$1 |
| 84 | + local action_to_remove=$2 |
| 85 | + |
| 86 | + # Get the current policy document |
| 87 | + policy_document=$(aws iam get-policy-version --policy-arn "$policy_arn" --query 'PolicyVersion.Document' --version-id v1 --output json) |
| 88 | + |
| 89 | + # Remove the specified action from the statements |
| 90 | + new_statements=$(logmessage "$policy_document" | jq ".Statement[] | select(.Action[] | contains('$action_to_remove')) | .Action = [.Action[] | select(. != '$action_to_remove')]") |
| 91 | + new_policy_document=$(logmessage '{"Version": "2012-10-17", "Statement": '"$new_statements"'}') |
| 92 | ++ |
| 93 | + # Update the policy with the modified document |
| 94 | + logmessage "Policy Document" |
| 95 | + logmessage $new_policy_document |
| 96 | + #aws iam create-policy-version --policy-arn "$policy_arn" --policy-document "$new_policy_document" --set-as-default |
| 97 | + |
| 98 | + if [ $? -eq 0 ]; then |
| 99 | + logmessage "Action removed from policy statement successfully." |
| 100 | + return 0 |
| 101 | + else |
| 102 | + logmessage "Failed to remove action from policy statement." |
| 103 | + return 1 |
| 104 | + fi |
| 105 | +} |
| 106 | + |
| 107 | +# Function to remove tags from subnets ids |
| 108 | +remove_tags_from_subnets() { |
| 109 | + local tag_key="Key=kubernetes.io/role/elb,Value=1" |
| 110 | + |
| 111 | + logmessage "retrive subnets ids with tag key assigned to specific vpc_id via aws cli" |
| 112 | + logmessage "getting public subnets from VPC: $vpc_id " |
| 113 | + |
| 114 | + |
| 115 | + subnets_vpc=$(aws ec2 describe-subnets --filters "Name=vpc-id,Values=$vpc_id" --query 'Subnets[*].SubnetId' --output text) |
| 116 | + logmessage "subnets_vpc: $subnets_vpc" |
| 117 | + |
| 118 | + |
| 119 | +#remove tag from subnets with AWS cli |
| 120 | + for subnet_id in $subnets_vpc; do |
| 121 | + logmessage "public subnets: $subnet_id" |
| 122 | + aws ec2 delete-tags --resources "$subnet_id" --tags "Key=$tag_key" || logmessage "Failed to remove tag from subnet $subnet_id" |
| 123 | + done |
| 124 | + return 0 |
| 125 | +} |
| 126 | + |
| 127 | +# Getting the service role |
| 128 | +path_tofile=$1 |
| 129 | +mode=$2 |
| 130 | +vpc_id=$3 |
| 131 | +public_subnets=$4 |
| 132 | +namespace="kube-system" |
| 133 | +service_account="aws-load-balancer-controller-sa" |
| 134 | +#new_statement="file://$path_tofile/template/iam_policy_incorrect.json" |
| 135 | +new_statement="file://$path_tofile/template/other_issue.json" |
| 136 | + |
| 137 | +logmessage "path_sent: $path_tofile" |
| 138 | + |
| 139 | + |
| 140 | +# validate if mode is equal to mod1 |
| 141 | +logmessage "mode: $mode" |
| 142 | +if [ "$mode" == "mod1" ]; then |
| 143 | + logmessage "Removing subnet tags" |
| 144 | + remove_tags_from_subnets |
| 145 | +else |
| 146 | + logmessage "Removing permissions" |
| 147 | + get_service_account_role "$namespace" "$service_account" |
| 148 | + get_first_policy_arn_from_role_arn "$role_name" |
| 149 | + update_policy_with_new_statement "$policy_arn" "$new_statement" |
| 150 | + |
| 151 | +fi |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | + |
0 commit comments