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

Commit 97bae15

Browse files
sistemi-etimemichaelwittig
authored andcommitted
Add support for EC2 Tags (#74)
* Add support to EC2 Tags
1 parent 22709cc commit 97bae15

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ A picture is worth a thousand words:
6262
5. Paste your public SSH key into the text-area and click the **Upload SSH public key** button to save
6363
2. Attach the IAM permissions defined in `iam_ssh_policy.json` to the EC2 instances (by creating an IAM role and an Instance Profile)
6464
3. Run the `install.sh` script as `root` on the EC2 instances. Run `install.sh -h` for help.
65-
4. Connect to your EC2 instances now using `ssh $Username@$PublicName` with `$Username` being your IAM user, and `$PublicName` being your server's name or IP address
65+
4. The configuration file is placed into `/etc/aws-ec2-ssh.conf`
66+
5. Connect to your EC2 instances now using `ssh $Username@$PublicName` with `$Username` being your IAM user, and `$PublicName` being your server's name or IP address
6667

6768
## IAM user names and Linux user names
6869

@@ -92,8 +93,10 @@ one or more of the following lines:
9293

9394
```
9495
ASSUMEROLE="IAM-role-arn" # IAM Role ARN for multi account. See below for more info
95-
IAM_AUTHORIZED_GROUPS="GROUPNAMES" # Comma seperated list of IAM groups to import
96+
IAM_AUTHORIZED_GROUPS="GROUPNAMES" # Comma separated list of IAM groups to import
9697
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
99+
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
97100
SUDOERSGROUP="GROUPNAME" # Deprecated! IAM group that should have sudo access. Please use SUDOERS_GROUPS as this variable will be removed in future release.
98101
LOCAL_MARKER_GROUP="iam-synced-users" # Dedicated UNIX group to mark imported users. Used for deleting removed IAM users
99102
LOCAL_GROUPS="GROUPNAMES" # Comma seperated list of UNIX groups to add the users in

iam_ssh_policy.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@
2222
"Resource": [
2323
"arn:aws:iam::<YOUR_USERS_ACCOUNT_ID_HERE>:user/*"
2424
]
25+
},
26+
{
27+
"Sid": "Stmt1500475854000",
28+
"Effect": "Allow",
29+
"Action": [
30+
"ec2:DescribeTags"
31+
],
32+
"Resource": [
33+
"*"
34+
]
2535
}
2636
]
2737
}

import_users.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ fi
4444
# Possibility to provide custom useradd arguments
4545
: ${USERADD_ARGS:="--create-home --shell /bin/bash"}
4646

47+
# Initizalize INSTANCE and REGION variables
48+
INSTANCE_ID=$(curl -s http://instance-data//latest/meta-data/instance-id)
49+
REGION=$(curl -s http://instance-data//latest/dynamic/instance-identity/document | jq -r .region)
50+
51+
4752
function log() {
4853
/usr/bin/logger -i -p auth.info -t aws-ec2-ssh "$@"
4954
}
@@ -66,6 +71,20 @@ function setup_aws_credentials() {
6671
fi
6772
}
6873

74+
# Get list of iam groups from tag
75+
function get_iam_groups_from_tag() {
76+
if [ "${IAM_AUTHORIZED_GROUPS_TAG}" ]
77+
then
78+
IAM_AUTHORIZED_GROUPS=$(\
79+
aws ec2 describe-tags \
80+
--filters "Name=resource-id,Values=$INSTANCE_ID" "Name=key,Values=$IAM_AUTHORIZED_GROUPS_TAG" \
81+
--region=$REGION \
82+
--output=json \
83+
| jq -r .Tags[0].Value \
84+
)
85+
fi
86+
}
87+
6988
# Get all IAM users (optionally limited by IAM groups)
7089
function get_iam_users() {
7190
local group
@@ -102,6 +121,20 @@ function get_local_users() {
102121
| sed "s/,/ /g"
103122
}
104123

124+
# Get list of IAM groups marked with sudo access from tag
125+
function get_sudoers_groups_from_tag() {
126+
if [ "${SUDOERS_GROUPS_TAG}" ]
127+
then
128+
SUDOERS_GROUPS=$(\
129+
aws ec2 describe-tags \
130+
--filters "Name=resource-id,Values=$INSTANCE_ID" "Name=key,Values=$SUDOERS_GROUPS_TAG" \
131+
--region=$REGION \
132+
--output=json \
133+
| jq -r .Tags[0].Value \
134+
)
135+
fi
136+
}
137+
105138
# Get IAM users of the groups marked with sudo access
106139
function get_sudoers_users() {
107140
local group
@@ -211,6 +244,10 @@ function sync_accounts() {
211244
local removed_users
212245
local user
213246

247+
# init group and sudoers from tags
248+
get_iam_groups_from_tag
249+
get_sudoers_groups_from_tag
250+
214251
iam_users=$(get_clean_iam_users | sort | uniq)
215252
sudo_users=$(get_clean_sudoers_users | sort | uniq)
216253
local_users=$(get_local_users | sort | uniq)

0 commit comments

Comments
 (0)