Skip to content
This repository was archived by the owner on Nov 8, 2021. It is now read-only.

Commit f2c832a

Browse files
mvanbaakmichaelwittig
authored andcommitted
Make useradd program and arguments configurable (#45)
* Make both the useradd program and the arguments for useradd configurable. Closes #39 * fix documentation
1 parent 8d09401 commit f2c832a

File tree

3 files changed

+51
-19
lines changed

3 files changed

+51
-19
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,14 @@ Linux user names may only be up to 32 characters long.
7373
There are a couple of things you can configure by editing/creating the file `/etc/aws-ec2-ssh.conf` and adding
7474
one or more of the following lines:
7575

76-
ASSUMEROLE="IAM-role-arn" # IAM Role ARN for multi account. See below for more info
77-
IAM_AUTHORIZED_GROUPS="GROUPNAMES" # Comma seperated list of IAM groups to import
78-
SUDOERSGROUP="GROUPNAME" # IAM group that should have sudo access
79-
LOCAL_GROUPS="GROUPNAMES" # Comma seperated list of UNIX groups to add the users in
76+
```
77+
ASSUMEROLE="IAM-role-arn" # IAM Role ARN for multi account. See below for more info
78+
IAM_AUTHORIZED_GROUPS="GROUPNAMES" # Comma seperated list of IAM groups to import
79+
SUDOERSGROUP="GROUPNAME" # IAM group that should have sudo access
80+
LOCAL_GROUPS="GROUPNAMES" # Comma seperated list of UNIX groups to add the users in
81+
USERADD_PROGRAM="/usr/sbin/useradd" # The useradd program to use. defaults to `/usr/sbin/useradd`
82+
USERADD_ARGS="--create-home --shell /bin/bash" # Arguments for the useradd program. defaults to `--create-home --shell /bin/bash`
83+
```
8084

8185
## Using a multi account strategy with a central IAM user account
8286

import_users.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
# instance you use this script runs in another.
2424
: ${ASSUMEROLE:=""}
2525

26+
# Possibility to provide a custom useradd program
27+
: ${USERADD_PROGRAM:="/usr/sbin/useradd"}
28+
29+
# Possibility to provide custom useradd arguments
30+
: ${USERADD_ARGS:="--create-home --shell /bin/bash"}
31+
2632
function setup_aws_credentials() {
2733
local stscredentials
2834
if [[ ! -z "${ASSUMEROLE}" ]]
@@ -101,7 +107,7 @@ function create_or_update_local_user() {
101107
fi
102108

103109
id "${username}" >/dev/null 2>&1 \
104-
|| /usr/sbin/useradd --create-home --shell /bin/bash "${username}" \
110+
|| ${USERADD_PROGRAM} ${USERADD_ARGS} "${username}" \
105111
&& /bin/chown -R "${username}:${username}" "$(eval echo ~$username)"
106112
/usr/sbin/usermod -G "${localusergroups}" "${username}"
107113

install.sh

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,26 @@
22

33
show_help() {
44
cat << EOF
5-
Usage: ${0##*/} [-hv] [-a ARN] [-i GROUP,GROUP,...] [-l GROUP,GROUP,...] [-s GROUP]
5+
Usage: ${0##*/} [-hv] [-a ARN] [-i GROUP,GROUP,...] [-l GROUP,GROUP,...] [-s GROUP] [-p PROGRAM] [-u "ARGUMENTS"]
66
Install import_users.sh and authorized_key_commands.
77
8-
-h display this help and exit
9-
-v verbose mode.
10-
11-
-a arn Assume a role before contacting AWS IAM to get users and keys.
12-
This can be used if you define your users in one AWS account, while the EC2
13-
instance you use this script runs in another.
14-
-i group,group Which IAM groups have access to this instance
15-
Comma seperated list of IAM groups. Leave empty for all available IAM users
16-
-l group,group Give the users these local UNIX groups
17-
Comma seperated list
18-
-s group Specify an IAM group for users who should be given sudo privileges, or leave
19-
empty to not change sudo access, or give it the value '##ALL##' to have all
20-
users be given sudo rights.
8+
-h display this help and exit
9+
-v verbose mode.
10+
11+
-a arn Assume a role before contacting AWS IAM to get users and keys.
12+
This can be used if you define your users in one AWS account, while the EC2
13+
instance you use this script runs in another.
14+
-i group,group Which IAM groups have access to this instance
15+
Comma seperated list of IAM groups. Leave empty for all available IAM users
16+
-l group,group Give the users these local UNIX groups
17+
Comma seperated list
18+
-s group Specify an IAM group for users who should be given sudo privileges, or leave
19+
empty to not change sudo access, or give it the value '##ALL##' to have all
20+
users be given sudo rights.
21+
-p program Specify your useradd program to use.
22+
Defaults to '/usr/sbin/useradd'
23+
-u "useradd args" Specify arguments to use with useradd.
24+
Defaults to '--create-home --shell /bin/bash'
2125
2226
2327
EOF
@@ -27,6 +31,8 @@ IAM_GROUPS=""
2731
SUDO_GROUP=""
2832
LOCAL_GROUPS=""
2933
ASSUME_ROLE=""
34+
USERADD_PROGRAM=""
35+
USERADD_ARGS=""
3036

3137
while getopts :hva:i:l:s: opt
3238
do
@@ -50,6 +56,12 @@ do
5056
a)
5157
ASSUME_ROLE="$OPTARG"
5258
;;
59+
p)
60+
USERADD_PROGRAM="$OPTARG"
61+
;;
62+
u)
63+
USERADD_ARGS="$OPTARG"
64+
;;
5365
\?)
5466
echo "Invalid option: -$OPTARG" >&2
5567
show_help
@@ -107,6 +119,16 @@ then
107119
echo "ASSUMEROLE=\"${ASSUME_ROLE}\"" >> /etc/aws-ec2-ssh.conf
108120
fi
109121

122+
if [ "${USERADD_PROGRAM}" != "" ]
123+
then
124+
echo "USERADD_PROGRAM=\"${USERADD_PROGRAM}\"" >> /etc/aws-ec2-ssh.conf
125+
fi
126+
127+
if [ "${USERADD_ARGS}" != "" ]
128+
then
129+
echo "USERADD_ARGS=\"${USERADD_ARGS}\"" >> /etc/aws-ec2-ssh.conf
130+
fi
131+
110132
sed -i 's:#AuthorizedKeysCommand none:AuthorizedKeysCommand /opt/authorized_keys_command.sh:g' /etc/ssh/sshd_config
111133
sed -i 's:#AuthorizedKeysCommandUser nobody:AuthorizedKeysCommandUser nobody:g' /etc/ssh/sshd_config
112134

0 commit comments

Comments
 (0)