-
Notifications
You must be signed in to change notification settings - Fork 14
/
AWSutils.sh
executable file
·566 lines (492 loc) · 17.7 KB
/
AWSutils.sh
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
#!/bin/bash
set -eu -o pipefail
#
# Install, configure and activate AWS utilities
#
#######################################################################
PROGNAME=$(basename "$0")
CHROOTMNT="${CHROOT:-/mnt/ec2-root}"
CLIV1SOURCE="${CLIV1SOURCE:-UNDEF}"
CLIV2SOURCE="${CLIV2SOURCE:-UNDEF}"
ICONNECTSRC="${ICONNECTSRC:-UNDEF}"
DEBUG="${DEBUG:-UNDEF}"
SSMAGENT="${SSMAGENT:-UNDEF}"
UTILSDIR="${UTILSDIR:-UNDEF}"
# Make interactive-execution more-verbose unless explicitly told not to
if [[ $( tty -s ) -eq 0 ]] && [[ ${DEBUG} == "UNDEF" ]]
then
DEBUG="true"
fi
# Error handler function
function err_exit {
local ERRSTR
local ISNUM
local SCRIPTEXIT
ERRSTR="${1}"
ISNUM='^[0-9]+$'
SCRIPTEXIT="${2:-1}"
if [[ ${DEBUG} == true ]]
then
# Our output channels
logger -i -t "${PROGNAME}" -p kern.crit -s -- "${ERRSTR}"
else
logger -i -t "${PROGNAME}" -p kern.crit -- "${ERRSTR}"
fi
# Only exit if requested exit is numerical
if [[ ${SCRIPTEXIT} =~ ${ISNUM} ]]
then
exit "${SCRIPTEXIT}"
fi
}
# Print out a basic usage message
function UsageMsg {
local SCRIPTEXIT
SCRIPTEXIT="${1:-1}"
(
echo "Usage: ${0} [GNU long option] [option] ..."
echo " Options:"
printf '\t%-4s%s\n' '-C' 'Where to get AWS CLIv1 (Installs to /usr/local/bin)'
printf '\t%-4s%s\n' '-c' 'Where to get AWS CLIv2 (Installs to /usr/bin)'
printf '\t%-4s%s\n' '-d' 'Directory containing installable utility-RPMs'
printf '\t%-4s%s\n' '-h' 'Print this message'
printf '\t%-4s%s\n' '-i' 'Where to get AWS InstanceConnect (RPM or git URL)'
printf '\t%-4s%s\n' '-m' 'Where chroot-dev is mounted (default: "/mnt/ec2-root")'
printf '\t%-4s%s\n' '-n' 'Where to get AWS CFN Bootstrap (Installs tar.gz via Python Pip)'
printf '\t%-4s%s\n' '-s' 'Where to get AWS SSM Agent (Installs via RPM)'
printf '\t%-4s%s\n' '-t' 'Systemd services to enable with systemctl'
echo " GNU long options:"
printf '\t%-20s%s\n' '--cfn-bootstrap' 'See "-n" short-option'
printf '\t%-20s%s\n' '--cli-v1' 'See "-C" short-option'
printf '\t%-20s%s\n' '--cli-v2' 'See "-c" short-option'
printf '\t%-20s%s\n' '--help' 'See "-h" short-option'
printf '\t%-20s%s\n' '--instance-connect' 'See "-i" short-option'
printf '\t%-20s%s\n' '--mountpoint' 'See "-m" short-option'
printf '\t%-20s%s\n' '--ssm-agent' 'See "-s" short-option'
printf '\t%-20s%s\n' '--systemd-services' 'See "-t" short-option'
printf '\t%-20s%s\n' '--utils-dir' 'See "-d" short-option'
)
exit "${SCRIPTEXIT}"
}
# Make sure Python3 is present when needed
function EnsurePy3 {
# Install python as necessary
if [[ -x ${CHROOTMNT}/bin/python3 ]]
then
err_exit "Python dependency met" NONE
else
err_exit "Installing python3..." NONE
yum --installroot="${CHROOTMNT}" install --quiet -y python3 || \
err_exit "Failed installing python3"
fi
}
# Make sure `fapolicyd` exemptions are pre-staged
function ExemptFapolicyd {
local RULE_DIR
local RULE_FILE
RULE_DIR="/etc/fapolicyd/rules.d"
RULE_FILE="${RULE_DIR}/30-aws.rules"
chroot "${CHROOTMNT}" install -dDm 0755 -o root -g root "${RULE_DIR}"
chroot "${CHROOTMNT}" install -bDm 0644 -o root -g root <(
printf "allow perm=any all : dir=/usr/local/aws-cli/v2/ "
printf "type=application/x-executable trust 1\n"
printf "allow perm=any all : dir=/usr/local/aws-cli/v2/ "
printf "type=application/x-sharedlib trust 1\n"
) "${RULE_FILE}"
}
# Install AWS CLI version 1.x
function InstallCLIv1 {
local INSTALLDIR
local BINDIR
local TMPDIR
INSTALLDIR="/usr/local/aws-cli/v1"
BINDIR="/usr/local/bin"
TMPDIR=$(chroot "${CHROOTMNT}" /bin/bash -c "mktemp -d")
if [[ ${CLIV1SOURCE} == "UNDEF" ]]
then
err_exit "AWS CLI v1 not requested for install. Skipping..." NONE
elif [[ ${CLIV1SOURCE} == http[s]://*zip ]]
then
# Make sure Python3 is present
EnsurePy3
err_exit "Fetching ${CLIV1SOURCE}..." NONE
curl -sL "${CLIV1SOURCE}" -o "${CHROOTMNT}${TMPDIR}/awscli-bundle.zip" || \
err_exit "Failed fetching ${CLIV1SOURCE}"
err_exit "Dearchiving awscli-bundle.zip..." NONE
(
cd "${CHROOTMNT}${TMPDIR}"
unzip -q awscli-bundle.zip
) || \
err_exit "Failed dearchiving awscli-bundle.zip"
err_exit "Installing AWS CLIv1..." NONE
chroot "${CHROOTMNT}" /bin/bash -c "python3 ${TMPDIR}/awscli-bundle/install -i '${INSTALLDIR}' -b '${BINDIR}/aws'" || \
err_exit "Failed installing AWS CLIv1"
err_exit "Creating AWS CLIv1 symlink ${BINDIR}/aws1..." NONE
chroot "${CHROOTMNT}" ln -sf "${INSTALLDIR}/bin/aws" "${BINDIR}/aws1" || \
err_exit "Failed creating ${BINDIR}/aws1"
err_exit "Cleaning up install files..." NONE
rm -rf "${CHROOTMNT}${TMPDIR}" || \
err_exit "Failed cleaning up install files"
elif [[ ${CLIV1SOURCE} == pip,* ]]
then
# Make sure Python3 is present
EnsurePy3
chroot "${CHROOTMNT}" /usr/bin/pip3 install --upgrade "${CLIV1SOURCE/pip*,}"
fi
}
# Install AWS CLI version 2.x
function InstallCLIv2 {
local INSTALLDIR
local BINDIR
local TMPDIR
INSTALLDIR="/usr/local/aws-cli" # installer appends v2/current
BINDIR="/usr/local/bin"
TMPDIR=$(chroot "${CHROOTMNT}" /bin/bash -c "mktemp -d")
if [[ ${CLIV2SOURCE} == "UNDEF" ]]
then
err_exit "AWS CLI v2 not requested for install. Skipping..." NONE
elif [[ ${CLIV2SOURCE} == http[s]://*zip ]]
then
# Make sure Python3 is present
EnsurePy3
err_exit "Fetching ${CLIV2SOURCE}..." NONE
curl -sL "${CLIV2SOURCE}" -o "${CHROOTMNT}${TMPDIR}/awscli-exe.zip" || \
err_exit "Failed fetching ${CLIV2SOURCE}"
err_exit "Dearchiving awscli-exe.zip..." NONE
(
cd "${CHROOTMNT}${TMPDIR}"
unzip -q awscli-exe.zip
) || \
err_exit "Failed dearchiving awscli-exe.zip"
err_exit "Installing AWS CLIv2..." NONE
chroot "${CHROOTMNT}" /bin/bash -c "${TMPDIR}/aws/install --update -i '${INSTALLDIR}' -b '${BINDIR}'" || \
err_exit "Failed installing AWS CLIv2"
err_exit "Creating AWS CLIv2 symlink ${BINDIR}/aws2..." NONE
chroot "${CHROOTMNT}" ln -sf "${INSTALLDIR}/v2/current/bin/aws" "${BINDIR}/aws2" || \
err_exit "Failed creating ${BINDIR}/aws2"
err_exit "Cleaning up install files..." NONE
rm -rf "${CHROOTMNT}${TMPDIR}" || \
err_exit "Failed cleaning up install files"
fi
}
# Install AWS utils from "directory"
function InstallFromDir {
true
}
# Install AWS InstanceConnect
function InstallInstanceConnect {
local BUILD_DIR
local ICRPM
local SELPOL
BUILD_DIR="/tmp/aws-ec2-instance-connect-config"
SELPOL="ec2-instance-connect"
if [[ ${ICONNECTSRC} == "UNDEF" ]]
then
err_exit "AWS Instance-Connect not requested for install. Skipping..." NONE
return 0
elif [[ ${ICONNECTSRC} == *.rpm ]]
then
err_exit "Installing v${ICONNECTSRC} via yum..." NONE
yum --installroot="${CHROOTMNT}" --quiet install -y "${ICONNECTSRC}" || \
err_exit "Failed installing v${ICONNECTSRC}"
elif [[ ${ICONNECTSRC} == *.git ]]
then
err_exit "Installing InstanceConnect from Git" NONE
# Build the RPM
if [[ $( command -v make )$? -ne 0 ]]
then
err_exit "No make-utility found in PATH"
fi
# Fetch via git
err_exit "Fetching ${ICONNECTSRC}..." NONE
git clone "${ICONNECTSRC}" "${BUILD_DIR}" || \
err_exit "Failed fetching ${ICONNECTSRC}"
err_exit "Making InstanceConnect RPM..." NONE
( cd "${BUILD_DIR}" && make rpm ) || \
err_exit "Failed to make InstanceConnect RPM"
# Install the RPM
ICRPM="$( stat -c '%n' "${BUILD_DIR}"/*noarch.rpm 2> /dev/null )"
if [[ -n ${ICRPM} ]]
then
err_exit "Installing ${ICRPM}..." NONE
yum --installroot="${CHROOTMNT}" install -y "${ICRPM}" || \
err_exit "Failed installing ${ICRPM}"
else
err_exit "Unable to find RPM in ${BUILD_DIR}"
fi
fi
# Ensure service is enabled
if [[ $( chroot "${CHROOTMNT}" bash -c "(
systemctl cat ec2-instance-connect > /dev/null 2>&1
)" )$? -eq 0 ]]
then
err_exit "Enabling ec2-instance-connect service..." NONE
chroot "${CHROOTMNT}" systemctl enable ec2-instance-connect || \
err_exit "Failed enabling ec2-instance-connect service"
else
err_exit "Could not find ec2-instance-connect in ${CHROOTMNT}"
fi
# Ensure SELinux is properly configured
# Necessary pending resolution of:
# - https://github.com/aws/aws-ec2-instance-connect-config/issues/2
# - https://github.com/aws/aws-ec2-instance-connect-config/issues/19
err_exit "Creating SELinux policy for InstanceConnect..." NONE
(
printf 'module ec2-instance-connect 1.0;\n\n'
printf 'require {\n'
printf '\ttype ssh_keygen_exec_t;\n'
printf '\ttype sshd_t;\n'
printf '\ttype http_port_t;\n'
printf '\tclass process setpgid;\n'
printf '\tclass tcp_socket name_connect;\n'
printf '\tclass file map;\n'
printf '\tclass file { execute execute_no_trans open read };\n'
printf '}\n\n'
printf '#============= sshd_t ==============\n\n'
printf 'allow sshd_t self:process setpgid;\n'
printf 'allow sshd_t ssh_keygen_exec_t:file map;\n'
printf 'allow sshd_t ssh_keygen_exec_t:file '
printf '{ execute execute_no_trans open read };\n'
printf 'allow sshd_t http_port_t:tcp_socket name_connect;\n'
) > "${CHROOTMNT}/tmp/${SELPOL}.te" || \
err_exit "Failed creating SELinux policy for InstanceConnect"
err_exit "Compiling/installing SELinux policy for InstanceConnect..." NONE
chroot "${CHROOTMNT}" /bin/bash -c "
cd /tmp
checkmodule -M -m -o ${SELPOL}.mod ${SELPOL}.te
semodule_package -o ${SELPOL}.pp -m ${SELPOL}.mod
semodule -i ${SELPOL}.pp && rm ${SELPOL}.*
" || \
err_exit "Failed compiling/installing SELinux policy for InstanceConnect"
}
# Install AWS utils from "directory"
function InstallSSMagent {
if [[ ${SSMAGENT} == "UNDEF" ]]
then
err_exit "AWS SSM-Agent not requested for install. Skipping..." NONE
elif [[ ${SSMAGENT} == *.rpm ]]
then
err_exit "Installing AWS SSM-Agent RPM..." NONE
yum --installroot="${CHROOTMNT}" install -y "${SSMAGENT}" || \
err_exit "Failed installing AWS SSM-Agent RPM"
err_exit "Ensuring AWS SSM-Agent is enabled..." NONE
chroot "${CHROOTMNT}" systemctl enable amazon-ssm-agent.service || \
err_exit "Failed ensuring AWS SSM-Agent is enabled"
fi
}
# Force systemd services to be enabled in resultant AMI
function EnableServices()
{
if [[ -z "${SYSTEMDSVCS:-}" ]]
then
err_exit "Systemd services not requested for enablement. Skipping..." NONE
return
fi
for SVC in "${SYSTEMDSVCS[@]}"
do
printf "Attempting to enable %s in %s... " "${SVC}.service" "${CHROOTMNT}"
chroot "${CHROOTMNT}" /usr/bin/systemctl enable "${SVC}.service" || err_exit "FAILED"
echo "SUCCESS"
done
}
# Install AWS CFN Bootstrap
function InstallCfnBootstrap {
if [[ -z ${CFNBOOTSTRAP:-} ]]
then
err_exit "AWS CFN Bootstrap not requested for install. Skipping..." NONE
elif [[ ${CFNBOOTSTRAP} == *.tar.gz ]]
then
local TMPDIR
TMPDIR=$(chroot "${CHROOTMNT}" mktemp -d)
err_exit "Installing rpm dependencies for AWS CFN Bootstrap install..." NONE
yum --installroot="${CHROOTMNT}" install -y tar || \
err_exit "Failed installing rpm dependencies"
err_exit "Fetching ${CFNBOOTSTRAP}..." NONE
curl -sL "${CFNBOOTSTRAP}" -o "${CHROOTMNT}${TMPDIR}/aws-cfn-bootstrap.tar.gz" || \
err_exit "Failed fetching ${CFNBOOTSTRAP}"
err_exit "Installing AWS CFN Bootstrap..." NONE
chroot "${CHROOTMNT}" python3 -m pip install "${TMPDIR}/aws-cfn-bootstrap.tar.gz" || \
err_exit "Failed installing AWS CFN Bootstrap"
err_exit "Setting up directory structure for cfn-hup service..." NONE
chroot "${CHROOTMNT}" install -Ddm 000755 /opt/aws/apitools/cfn-init/init/redhat/ /opt/aws/bin || \
err_exit "Failing setting up cfn-hup directories"
err_exit "Extracting cfn-hup service definition file..." NONE
chroot "${CHROOTMNT}" tar -C /opt/aws/apitools/cfn-init/ -xzv --wildcards --no-anchored --strip-components=1 -f "${TMPDIR}/aws-cfn-bootstrap.tar.gz" redhat/cfn-hup || \
err_exit "Failed to extract cfn-hup service definition"
err_exit "Ensure no invalid file-ownership on binary... " NONE
chroot "${CHROOTMNT}" chown root:root /opt/aws/apitools/cfn-init/init/redhat/cfn-hup || \
err_exit "Failed setting user/group on .../cfn-hup"
err_exit "Creating symlink for cfn-hup service..." NONE
chroot "${CHROOTMNT}" ln -sf /opt/aws/apitools/cfn-init/init/redhat/cfn-hup /etc/init.d/cfn-hup || \
err_exit "Failed creating symlink for cfn-hup service"
err_exit "Making sure cfn-hup service is executable..." NONE
chmod +x "${CHROOTMNT}/opt/aws/apitools/cfn-init/init/redhat/cfn-hup" || \
err_exit "Failed making cfn-hup service executable"
err_exit "Using alternatives to configure cfn-hup symlink and initscript..." NONE
chroot "${CHROOTMNT}" alternatives --verbose --install /opt/aws/bin/cfn-hup cfn-hup /usr/local/bin/cfn-hup 1 --initscript cfn-hup || \
err_exit "Failed configuring cfn-hup symlink and initscript"
err_exit "Cleaning up install files..." NONE
rm -rf "${CHROOTMNT}${TMPDIR}" || \
err_exit "Failed cleaning up install files"
fi
}
# shellcheck disable=SC2016,SC1003
function ProfileSetupAwsCli {
install -bDm 0644 -o root -g root <(
echo 'AWS_DEFAULT_REGION="$('
echo ' curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | \'
echo ' python3 -c '"'"'import sys, json; print(json.load(sys.stdin)["region"])'"'"''
echo ')"'
printf '\nexport AWS_DEFAULT_REGION\n'
) /etc/profile.d/aws_envs.sh
}
######################
## Main program-flow
######################
OPTIONBUFR=$( getopt \
-o C:c:d:hi:m:n:s:t:\
--long cfn-bootstrap:,cli-v1:,cli-v2:,help,instance-connect:,mountpoint:,ssm-agent:,systemd-services:,utils-dir: \
-n "${PROGNAME}" -- "$@")
eval set -- "${OPTIONBUFR}"
###################################
# Parse contents of ${OPTIONBUFR}
###################################
while true
do
case "$1" in
-C|--cli-v1)
case "$2" in
"")
err_exit "Error: option required but not specified"
shift 2;
exit 1
;;
*)
CLIV1SOURCE="${2}"
shift 2;
;;
esac
;;
-c|--cli-v2)
case "$2" in
"")
err_exit "Error: option required but not specified"
shift 2;
exit 1
;;
*)
CLIV2SOURCE="${2}"
shift 2;
;;
esac
;;
-d|--utils-dir)
case "$2" in
"")
err_exit "Error: option required but not specified"
shift 2;
exit 1
;;
*)
UTILSDIR="${2}"
shift 2;
;;
esac
;;
-h|--help)
UsageMsg 0
;;
-i|--instance-connect)
case "$2" in
"")
err_exit "Error: option required but not specified"
shift 2;
exit 1
;;
*)
ICONNECTSRC="${2}"
shift 2;
;;
esac
;;
-m|--mountpoint)
case "$2" in
"")
err_exit "Error: option required but not specified"
shift 2;
exit 1
;;
*)
CHROOTMNT="${2}"
shift 2;
;;
esac
;;
-n|--cfn-bootstrap)
case "$2" in
"")
err_exit "Error: option required but not specified"
shift 2;
exit 1
;;
*)
CFNBOOTSTRAP="${2}"
shift 2;
;;
esac
;;
-s|--ssm-agent)
case "$2" in
"")
err_exit "Error: option required but not specified"
shift 2;
exit 1
;;
*)
SSMAGENT="${2}"
shift 2;
;;
esac
;;
-t|--systemd-services)
case "$2" in
"")
echo "Error: option required but not specified" > /dev/stderr
shift 2;
exit 1
;;
*)
IFS=, read -ra SYSTEMDSVCS <<< "$2"
shift 2;
;;
esac
;;
--)
shift
break
;;
*)
err_exit "Internal error!"
exit 1
;;
esac
done
###############
# Do the work
# Install AWS CLIv1
InstallCLIv1
# Install AWS CLIv2
InstallCLIv2
# Install AWS SSM-Agent
InstallSSMagent
# Install AWS InstanceConnect
InstallInstanceConnect
# Install AWS utils from directory
InstallFromDir
# Install AWS CFN Bootstrap
InstallCfnBootstrap
# Set up fapolicyd Exemption
ExemptFapolicyd
# Enable services
EnableServices
# Set up /etc/profile.d file for AWS CLI
ProfileSetupAwsCli