Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

aws-sudo.sh 3.5 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  1. #!/usr/bin/env bash
  2. set -e
  3. # setup default values
  4. cfg_file="$HOME/.aws-sudo"
  5. duration=3600
  6. # parse command line
  7. while [ "$#" -gt 0 ]; do
  8. case "$1" in
  9. -n)
  10. session_name="$2"
  11. shift 2
  12. ;;
  13. -c)
  14. command="$2"
  15. shift 2
  16. ;;
  17. -x)
  18. clear=1
  19. shift 1
  20. ;;
  21. -f)
  22. cfg_file="$2"
  23. shift 2
  24. ;;
  25. -p)
  26. profile="$2"
  27. shift 2
  28. ;;
  29. -d)
  30. duration="$2"
  31. shift 2
  32. ;;
  33. -h)
  34. cat 1>&2 <<EOF
  35. $(basename "$0") [-n sess_name] [-c command] [-x] [-f cfg_file] [-p profile] argument
  36. Request credentials via STS and prepare environment variables for the
  37. AWS SDKs. By default, generates Bourne-shell code to be eval'ed.
  38. optional args:
  39. -n sess_name Session name for STS
  40. -c command Run a command as the role; passed to "sh -c"
  41. -x Generate command to clean modified environment vars
  42. -f cfg_file Override config file for defaults and aliases
  43. -p profile Use a non-default AWS profile when calling STS
  44. -d duration Session duration 12 hours default
  45. positional args:
  46. argument: Must be one of:
  47. full role ARN
  48. a configured alias name
  49. 12-digit AWS account number
  50. the literal "clear" (equivalent to -x)
  51. EOF
  52. exit
  53. ;;
  54. *)
  55. argument=$1
  56. shift 1
  57. ;;
  58. esac
  59. done
  60. # handle unset requests and exit
  61. if [[ "$argument" == "clear" || "$clear" == "1" ]]; then
  62. echo "unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SECURITY_TOKEN AWS_SESSION_TOKEN"
  63. exit
  64. fi
  65. # if the arg doesn't look like an arn, check for aliases
  66. if [[ "$argument" =~ arn:aws:iam::[0-9]{12}:role/ ]]; then
  67. role="$argument"
  68. else
  69. if [ -r $cfg_file ]; then
  70. alias=$(grep "^alias $argument" $cfg_file 2>/dev/null | head -n 1)
  71. role=$(echo "$alias" | awk '{print $3}')
  72. # if no session name was specified, look for one in the alias
  73. session_name=${session_name:-$(echo "$alias" | awk '{print $4}')}
  74. fi
  75. fi
  76. # if argument is an aws account number, look for a default role name
  77. # in the config. If found, build the role arn using that default
  78. if [[ -z "$role" && "$argument" =~ ^[0-9]{12}$ ]]; then
  79. def_role_name=$(grep "^default role " $cfg_file 2>/dev/null | awk '{print $3}' | head -n 1)
  80. if [ -n "$def_role_name" ]; then
  81. role="arn:aws:iam::${argument}:role/${def_role_name}"
  82. fi
  83. fi
  84. # if no session name was provided, try to find a default
  85. if [ -z "$session_name" ]; then
  86. def_session_name=$(grep "^default session_name" $cfg_file 2>/dev/null | awk '{print $3}')
  87. session_name=${def_session_name:-aws_sudo}
  88. fi
  89. # if no source profile was provided, try to find a default
  90. if [ -z "$profile" ]; then
  91. profile=$(grep "^default profile" $cfg_file 2>/dev/null | awk '{print $3}')
  92. fi
  93. # verify that a valid role arn was found or provided; awscli gives
  94. # terrible error messages if you try to assume some non-arn junk
  95. if ! [[ "$role" =~ arn:aws:iam::[0-9]{12}:role/ ]]; then
  96. echo "$argument is neither a role ARN nor a configured alias" 1>&2
  97. exit 1
  98. fi
  99. response=$(aws ${profile:+--profile $profile} \
  100. sts assume-role --output text \
  101. --role-arn "$role" \
  102. --role-session-name="$session_name" \
  103. --duration-seconds=$duration \
  104. --query Credentials)
  105. if [ -n "$command" ]; then
  106. env \
  107. AWS_ACCESS_KEY_ID=$(echo $response | awk '{print $1}') \
  108. AWS_SECRET_ACCESS_KEY=$(echo $response | awk '{print $3}') \
  109. AWS_SESSION_TOKEN=$(echo $response | awk '{print $4}') \
  110. bash -c "$command"
  111. else
  112. echo export \
  113. AWS_ACCESS_KEY_ID=$(echo $response | awk '{print $1}') \
  114. AWS_SECRET_ACCESS_KEY=$(echo $response | awk '{print $3}') \
  115. AWS_SESSION_TOKEN=$(echo $response | awk '{print $4}')
  116. fi
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...