11#! /bin/bash -e
22
3+ show_help () {
4+ cat << EOF
5+ Usage: ${0##*/ } [-hv] [-a ARN] [-i GROUP,GROUP,...] [-l GROUP,GROUP,...] [-s GROUP]
6+ Install import_users.sh and authorized_key_commands.
7+
8+ -h display this help and exit
9+ -v verbose mode.
10+
11+ -a arn AssumeRole ARN to get users from another AWS account
12+ -i group,group Comma seperated list of IAM groups that should have ssh access
13+ -l group,group Comma seperated list of UNIX groups to add the users to
14+ -s group IAM group that should have sudo access. use ##ALL## if all users need it
15+
16+
17+ EOF
18+ }
19+
20+ IAM_GROUPS=" "
21+ SUDO_GROUP=" "
22+ LOCAL_GROUPS=" "
23+ ASSUME_ROLE=" "
24+
25+ while getopts :hva:i:l:s: opt
26+ do
27+ case $opt in
28+ h)
29+ show_help
30+ exit 0
31+ ;;
32+ i)
33+ IAM_GROUPS=" $OPTARG "
34+ ;;
35+ s)
36+ SUDO_GROUP=" $OPTARG "
37+ ;;
38+ l)
39+ LOCAL_GROUPS=" $OPTARG "
40+ ;;
41+ v)
42+ set -x
43+ ;;
44+ a)
45+ ASSUME_ROLE=" $OPTARG "
46+ ;;
47+ \? )
48+ echo " Invalid option: -$OPTARG " >&2
49+ show_help
50+ exit 1
51+ ;;
52+ :)
53+ echo " Option -$OPTARG requires an argument." >&2
54+ show_help
55+ exit 1
56+ esac
57+ done
58+
359tmpdir=$( mktemp -d)
460
561cd " $tmpdir "
@@ -15,24 +71,36 @@ cp import_users.sh /opt/import_users.sh
1571# changing GROUPNAMES to a comma seperated list of IAM groups you want to sync.
1672# You can specify 1 or more groups, comma seperated, without spaces.
1773# If you leave it blank, all IAM users will be synced.
18- # sed -i 's/IAM_AUTHORIZED_GROUPS=""/IAM_AUTHORIZED_GROUPS="GROUPNAMES"/' /opt/import_users.sh
74+ if [ " ${IAM_GROUPS} " != " " ]
75+ then
76+ sed -i " s/IAM_AUTHORIZED_GROUPS=\"\" /IAM_AUTHORIZED_GROUPS=\" ${IAM_GROUPS} \" /" /opt/import_users.sh
77+ fi
1978
2079# To control which users are given sudo privileges, uncomment the line below
2180# changing GROUPNAME to either the name of the IAM group for sudo users, or
2281# to ##ALL## to give all users sudo access. If you leave it blank, no users will
2382# be given sudo access.
24- # sed -i 's/SUDOERSGROUP=""/SUDOERSGROUP="GROUPNAME"/' /opt/import_users.sh
83+ if [ " ${SUDO_GROUP} " != " " ]
84+ then
85+ sed -i " s/SUDOERSGROUP=\"\" /SUDOERSGROUP=\" ${SUDO_GROUP} \" /" /opt/import_users.sh
86+ fi
2587
2688# To control which local groups a user will get, uncomment the line belong
2789# changing GROUPNAMES to a comma seperated list of local UNIX groups.
2890# If you live it blank, this setting will be ignored
29- # sed -i 's/LOCAL_GROUPS=""/LOCAL_GROUPS="GROUPNAMES"/' /opt/import_users.sh
91+ if [ " ${LOCAL_GROUPS} " != " " ]
92+ then
93+ sed -i " s/LOCAL_GROUPS=\"\" /LOCAL_GROUPS=\" ${LOCAL_GROUPS} \" /" /opt/import_users.sh
94+ fi
3095
3196# If your IAM users are in another AWS account, put the AssumeRole ARN here.
3297# replace the word ASSUMEROLEARN with the full arn. eg 'arn:aws:iam::$accountid:role/$role'
3398# See docs/multiawsaccount.md on how to make this work
34- # sed -i 's/ASSUMEROLE=""/ASSUMEROLE="ASSUMEROLEARN"/' /opt/import_users.sh
35- # sed -i 's/ASSUMEROLE=""/ASSUMEROLE="ASSUMEROLEARN"/' /opt/authorized_keys_command.sh
99+ if [ " ${ASSUME_ROLE} " != " " ]
100+ then
101+ sed -i " s/ASSUMEROLE=\"\" /ASSUMEROLE=\" ${ASSUME_ROLE} \" /" /opt/import_users.sh
102+ sed -i " s/ASSUMEROLE=\"\" /ASSUMEROLE=\" ${ASSUME_ROLE} \" /" /opt/authorized_keys_command.sh
103+ fi
36104
37105sed -i ' s:#AuthorizedKeysCommand none:AuthorizedKeysCommand /opt/authorized_keys_command.sh:g' /etc/ssh/sshd_config
38106sed -i ' s:#AuthorizedKeysCommandUser nobody:AuthorizedKeysCommandUser nobody:g' /etc/ssh/sshd_config
0 commit comments