Skip to content

Commit a9b7e87

Browse files
Merge pull request #1 from amaysim-au/DOPE-2037-multi-crontab
DOPE-2037 Allow multi configs for user imports
2 parents 73bd78f + dd7916b commit a9b7e87

File tree

3 files changed

+31
-66
lines changed

3 files changed

+31
-66
lines changed

README.md

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ A picture is worth a thousand words:
2424

2525
### Demo with CloudFormation
2626

27-
1. Upload your public SSH key to IAM:
27+
1. Upload your public SSH key to IAM:
2828
1. Open the Users section in the [IAM Management Console](https://console.aws.amazon.com/iam/home#users)
2929
2. Click the row with your user
3030
3. Select the **Security Credentials** tab
@@ -37,24 +37,9 @@ A picture is worth a thousand words:
3737

3838
## How to integrate this system into your environment
3939

40-
### Install via RPM
41-
42-
> Check the [releases](https://github.com/widdix/aws-ec2-ssh/releases) and replace `1.1.0` with the latest released version.
43-
44-
1. Upload your public SSH key to IAM:
45-
1. Open the Users section in the [IAM Management Console](https://console.aws.amazon.com/iam/home#users)
46-
2. Click the row with your user
47-
3. Select the **Security Credentials** tab
48-
4. Click the **Upload SSH public key** button at the bottom of the page
49-
5. Paste your public SSH key into the text-area and click the **Upload SSH public key** button to save
50-
2. Attach the IAM permissions defined in [`iam_ssh_policy.json`](./iam_ssh_policy.json) to the EC2 instances (by creating an IAM role and an Instance Profile)
51-
3. Install the RPM: `rpm -i https://s3-eu-west-1.amazonaws.com/widdix-aws-ec2-ssh-releases-eu-west-1/aws-ec2-ssh-1.4.0-1.el7.centos.noarch.rpm`
52-
4. The configuration file is placed into `/etc/aws-ec2-ssh.conf`
53-
5. The RPM creates a crontab file to run import_users.sh every 10 minutes. This file is placed in `/etc/cron.d/import_users`
54-
5540
### Install via install.sh script
5641

57-
1. Upload your public SSH key to IAM:
42+
1. Upload your public SSH key to IAM:
5843
1. Open the Users section in the [IAM Management Console](https://console.aws.amazon.com/iam/home#users)
5944
2. Click the row with your user
6045
3. Select the **Security Credentials** tab
@@ -88,14 +73,19 @@ Linux user names may only be up to 32 characters long.
8873

8974
## Configuration
9075

91-
There are a couple of things you can configure by editing/creating the file `/etc/aws-ec2-ssh.conf` and adding
92-
one or more of the following lines:
76+
The multi AWS Account Role can configure by editing/creating the file `/etc/aws-ec2-ssh.conf` and adding
77+
the following line:
9378

9479
```
9580
ASSUMEROLE="IAM-role-arn" # IAM Role ARN for multi account. See below for more info
81+
```
82+
83+
There are a couple of things you can configure by creating ENVIRONMENT variables:
84+
85+
```
9686
IAM_AUTHORIZED_GROUPS="GROUPNAMES" # Comma separated list of IAM groups to import
9787
SUDOERS_GROUPS="GROUPNAMES" # Comma seperated list of IAM groups that should have sudo access
98-
IAM_AUTHORIZED_GROUPS_TAG="KeyTag" # Key Tag of EC2 that contains a Comma separated list of IAM groups to import - IAM_AUTHORIZED_GROUPS_TAG will override IAM_AUTHORIZED_GROUPS, you can use only one of them
88+
IAM_AUTHORIZED_GROUPS_TAG="KeyTag" # Key Tag of EC2 that contains a Comma separated list of IAM groups to import - IAM_AUTHORIZED_GROUPS_TAG will override IAM_AUTHORIZED_GROUPS, you can use only one of them
9989
SUDOERS_GROUPS_TAG="KeyTag" # Key Tag of EC2 that contains a Comma separated list of IAM groups that should have sudo access - SUDOERS_GROUPS_TAG will override SUDOERS_GROUPS, you can use only one of them
10090
SUDOERSGROUP="GROUPNAME" # Deprecated! IAM group that should have sudo access. Please use SUDOERS_GROUPS as this variable will be removed in future release.
10191
LOCAL_MARKER_GROUP="iam-synced-users" # Dedicated UNIX group to mark imported users. Used for deleting removed IAM users
@@ -146,5 +136,4 @@ For your EC2 instances, you need a IAM role that allows the `sts:AssumeRole` act
146136
* uid's and gid's across multiple servers might not line up correctly (due to when a server was booted, and what users existed at that time). Could affect NFS mounts or Amazon EFS.
147137
* this solution will work for ~100 IAM users and ~100 EC2 instances. If your setup is much larger (e.g. 10 times more users or 10 times more EC2 instances) you may run into two issues:
148138
* IAM API limitations
149-
* Disk space issues
150139
* **not all IAM user names are allowed in Linux user names** (e.g. if you use email addresses as IAM user names). See section [IAM user names and Linux user names](#iam-user-names-and-linux-user-names) for further details.

import_users.sh

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
#!/bin/bash -e
22

3-
# source configuration if it exists
4-
[ -f /etc/aws-ec2-ssh.conf ] && . /etc/aws-ec2-ssh.conf
5-
63
# Should we actually do something?
74
: ${DONOTSYNC:=0}
85

96
if [ ${DONOTSYNC} -eq 1 ]
107
then
11-
echo "Please configure aws-ec2-ssh by editing /etc/aws-ec2-ssh.conf"
8+
echo "Please unset DONOTSYNC to enable"
129
exit 1
1310
fi
1411

install.sh

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ EOF
2929
}
3030

3131
SSHD_CONFIG_FILE="/etc/ssh/sshd_config"
32-
AUTHORIZED_KEYS_COMMAND_FILE="/opt/authorized_keys_command.sh"
33-
IMPORT_USERS_SCRIPT_FILE="/opt/import_users.sh"
32+
AUTHORIZED_KEYS_COMMAND_FILE="/usr/local/bin/authorized_keys_command.sh"
33+
IMPORT_USERS_SCRIPT_FILE="/usr/local/bin/import_users.sh"
3434
MAIN_CONFIG_FILE="/etc/aws-ec2-ssh.conf"
3535

3636
IAM_GROUPS=""
@@ -80,45 +80,16 @@ do
8080
esac
8181
done
8282

83-
tmpdir=$(mktemp -d)
83+
wget "https://raw.githubusercontent.com/amaysim-au/aws-ec2-ssh/master/authorized_keys_command.sh" -O $AUTHORIZED_KEYS_COMMAND_FILE
84+
chmod +x $AUTHORIZED_KEYS_COMMAND_FILE
8485

85-
cd "$tmpdir"
86+
wget "https://raw.githubusercontent.com/amaysim-au/aws-ec2-ssh/master/import_users.sh" -O $IMPORT_USERS_SCRIPT_FILE
87+
chmod +x $IMPORT_USERS_SCRIPT_FILE
8688

87-
git clone -b master https://github.com/widdix/aws-ec2-ssh.git
88-
89-
cd "$tmpdir/aws-ec2-ssh"
90-
91-
cp authorized_keys_command.sh $AUTHORIZED_KEYS_COMMAND_FILE
92-
cp import_users.sh $IMPORT_USERS_SCRIPT_FILE
93-
94-
if [ "${IAM_GROUPS}" != "" ]
95-
then
96-
echo "IAM_AUTHORIZED_GROUPS=\"${IAM_GROUPS}\"" >> $MAIN_CONFIG_FILE
97-
fi
98-
99-
if [ "${SUDO_GROUPS}" != "" ]
100-
then
101-
echo "SUDOERS_GROUPS=\"${SUDO_GROUPS}\"" >> $MAIN_CONFIG_FILE
102-
fi
103-
104-
if [ "${LOCAL_GROUPS}" != "" ]
105-
then
106-
echo "LOCAL_GROUPS=\"${LOCAL_GROUPS}\"" >> $MAIN_CONFIG_FILE
107-
fi
108-
109-
if [ "${ASSUME_ROLE}" != "" ]
110-
then
111-
echo "ASSUMEROLE=\"${ASSUME_ROLE}\"" >> $MAIN_CONFIG_FILE
112-
fi
113-
114-
if [ "${USERADD_PROGRAM}" != "" ]
115-
then
116-
echo "USERADD_PROGRAM=\"${USERADD_PROGRAM}\"" >> $MAIN_CONFIG_FILE
117-
fi
118-
119-
if [ "${USERADD_ARGS}" != "" ]
120-
then
121-
echo "USERADD_ARGS=\"${USERADD_ARGS}\"" >> $MAIN_CONFIG_FILE
89+
if [ "${ASSUME_ROLE}" != "" ]; then
90+
echo "ASSUMEROLE=\"${ASSUME_ROLE}\"" > $MAIN_CONFIG_FILE
91+
else
92+
echo "ASSUMEROLE=\"\"" > $MAIN_CONFIG_FILE
12293
fi
12394

12495
if grep -q '#AuthorizedKeysCommand none' $SSHD_CONFIG_FILE; then
@@ -137,15 +108,23 @@ else
137108
fi
138109
fi
139110

140-
cat > /etc/cron.d/import_users << EOF
111+
cat > /etc/cron.d/import_users_${IAM_GROUPS} << EOF
112+
IAM_AUTHORIZED_GROUPS="${IAM_GROUPS}"
113+
SUDOERS_GROUPS="${SUDO_GROUPS}"
114+
LOCAL_GROUPS="${LOCAL_GROUPS}"
115+
LOCAL_MARKER_GROUP="iam-synced-users"
116+
ASSUMEROLE="${ASSUME_ROLE}"
117+
USERADD_PROGRAM="${USERADD_PROGRAM}"
118+
USERADD_ARGS="${USERADD_ARGS}"
141119
SHELL=/bin/bash
142120
PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin
143121
MAILTO=root
144122
HOME=/
145123
*/10 * * * * root $IMPORT_USERS_SCRIPT_FILE
146124
EOF
147-
chmod 0644 /etc/cron.d/import_users
125+
chmod 0644 /etc/cron.d/import_users_${IAM_GROUPS}
148126

127+
export IAM_AUTHORIZED_GROUPS SUDOERS_GROUPS LOCAL_GROUPS LOCAL_MARKER_GROUP ASSUMEROLE USERADD_PROGRAM USERADD_ARGS
149128
$IMPORT_USERS_SCRIPT_FILE
150129

151130
# In order to support SELinux in Enforcing mode, we need to tell SELinux that it

0 commit comments

Comments
 (0)