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

Commit 82d06a6

Browse files
mvanbaakmichaelwittig
authored andcommitted
Clean usernames we get from IAM as early as possible. (#54)
We need the IAM usernames cleaned to a format unix understands as early as possible, so the code to detect which users should be removed uses the same format for both iam users and local users. Closes #49 I left the original functions intact, as they would become a bit messy with temp. variables where we would loop over again inside the function itself. Looked cleaner to just pass them through another function.
1 parent 691582f commit 82d06a6

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

import_users.sh

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,23 @@ function get_iam_users() {
7676
fi
7777
}
7878

79+
# Run all found iam users through clean_iam_username
80+
function get_clean_iam_users() {
81+
local raw_username
82+
83+
for raw_username in $(get_iam_users); do
84+
clean_iam_username "${raw_username}" | sed "s/\r//g"
85+
done
86+
}
87+
7988
# Get previously synced users
8089
function get_local_users() {
8190
/usr/bin/getent group ${LOCAL_MARKER_GROUP} \
8291
| cut -d : -f4- \
8392
| sed "s/,/ /g"
8493
}
8594

95+
# Get IAM users of the group marked with sudo access
8696
function get_sudoers_users() {
8797
[[ -z "${SUDOERSGROUP}" ]] || [[ "${SUDOERSGROUP}" == "##ALL##" ]] ||
8898
aws iam get-group \
@@ -91,16 +101,23 @@ function get_sudoers_users() {
91101
--output text
92102
}
93103

104+
# Get the unix usernames of the IAM users within the sudo group
105+
function get_clean_sudoers_users() {
106+
local raw_username
107+
108+
for raw_username in $(get_sudoers_users); do
109+
clean_iam_username "${raw_username}"
110+
done
111+
}
112+
94113
# Create or update a local user based on info from the IAM group
95114
function create_or_update_local_user() {
96-
local iamusername
97115
local username
98116
local sudousers
99117
local localusergroups
100118

101-
iamusername="${1}"
102-
username="${2}"
103-
sudousers="${3}"
119+
username="${1}"
120+
sudousers="${2}"
104121
localusergroups="${LOCAL_MARKER_GROUP}"
105122

106123
# check that username contains only alphanumeric, period (.), underscore (_), and hyphen (-) for a safe eval
@@ -125,9 +142,9 @@ function create_or_update_local_user() {
125142
then
126143
SaveUserFileName=$(echo "${username}" | tr "." " ")
127144
SaveUserSudoFilePath="/etc/sudoers.d/$SaveUserFileName"
128-
if [[ "${SUDOERSGROUP}" == "##ALL##" ]] || echo "${sudousers}" | grep "^${iamusername}\$" > /dev/null
145+
if [[ "${SUDOERSGROUP}" == "##ALL##" ]] || echo "${sudousers}" | grep "^${username}\$" > /dev/null
129146
then
130-
echo "${SaveUserName} ALL=(ALL) NOPASSWD:ALL" > "${SaveUserSudoFilePath}"
147+
echo "${username} ALL=(ALL) NOPASSWD:ALL" > "${SaveUserSudoFilePath}"
131148
else
132149
[[ ! -f "${SaveUserSudoFilePath}" ]] || rm "${SaveUserSudoFilePath}"
133150
fi
@@ -170,21 +187,20 @@ function sync_accounts() {
170187
local removed_users
171188
local user
172189

173-
iam_users=$(get_iam_users | sort | uniq)
174-
sudo_users=$(get_sudoers_users | sort | uniq)
190+
iam_users=$(get_clean_iam_users | sort | uniq)
191+
sudo_users=$(get_clean_sudoers_users | sort | uniq)
175192
local_users=$(get_local_users | sort | uniq)
176193

177194
intersection=$(echo ${local_users} ${iam_users} | tr " " "\n" | sort | uniq -D | uniq)
178195
removed_users=$(echo ${local_users} ${intersection} | tr " " "\n" | sort | uniq -u)
179196

180197
# Add or update the users found in IAM
181198
for user in ${iam_users}; do
182-
SaveUserName=$(clean_iam_username "${user}")
183-
if [ "${#SaveUserName}" -le "32" ]
199+
if [ "${#user}" -le "32" ]
184200
then
185-
create_or_update_local_user "${user}" "${SaveUserName}" "$sudo_users"
201+
create_or_update_local_user "${user}" "$sudo_users"
186202
else
187-
echo "Can not import IAM user ${user}. Local user name ${SaveUserName} is longer than 32 characters."
203+
echo "Can not import IAM user ${user}. User name is longer than 32 characters."
188204
fi
189205
done
190206

0 commit comments

Comments
 (0)