forked from OpenST/ost-devops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_ssh_user.sh
executable file
·176 lines (132 loc) · 11.5 KB
/
add_ssh_user.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env bash
usage="./${0} --env <staging/production> --sub-env <sandbox/main> --app-stack <ost> --help"
while [ $# -gt 0 ]; do
key=$1
case ${key} in
--env)
shift
export ENV=$1
;;
--sub-env)
shift
export SUB_ENV=$1
;;
--app-stack)
shift
export APP_STACK=$1
;;
--help)
echo $usage
exit 0
;;
*)
echo $usage
exit 1
;;
esac
shift
done
if [[ ${ENV} != "staging" && ${ENV} != "production" ]]; then
echo "Invalid parameter '--env' => ${usage}"
exit 1;
fi
if [[ ${SUB_ENV} != "sandbox" && ${SUB_ENV} != "main" ]]; then
echo "Invalid parameter '--sub-env' => ${usage}"
exit 1;
fi
if [[ ${APP_STACK} != "ost" ]]; then
echo "Invalid parameter '--app-stack' => ${usage}"
exit 1;
fi
stack_users="${ENV}-${SUB_ENV}-${APP_STACK}"
case ${stack_users} in
"production-sandbox-ost")
export final_users=("alpesh" "aman" "anagha" "dhananjay" "kedar" "pankaj" "shlok" "somashekhar" "sunil" "ben" "ryan");
;;
"production-main-ost")
export final_users=("alpesh" "aman" "kedar" "pankaj" "somashekhar" "sunil" "ben" "ryan");
;;
*)
echo "Invalid stack combination for final_users!"
exit 1
;;
esac
declare -A sudo_users=(["bala"]=true ["amanpruthi"]=true);
function create_ssh_user(){
local user_name=$1;
local pub_key=$2;
if [[ -z ${user_name} ]]; then
echo "Invalid user name !"
exit 1;
fi
if [[ -z ${pub_key} ]]; then
echo "Invalid public key for user !"
exit 1;
fi
# Check if user already present else create it
getent passwd ${user_name}
if [[ $? != 0 ]]; then
adduser ${user_name}
if [[ $? != 0 ]]; then
echo "Error creation user ${user_name}"
exit 1;
fi
else
echo "User name (${user_name}) already exists."
fi
# Lock user password login
passwd -l ${user_name}
# Make sudoers entry if eligible
if [[ ${sudo_users[$user_name]} == true ]]; then
create_sudoers_file "${user_name}"
fi
# Create auth key file for user
user_ssh_dir="/home/${user_name}/.ssh";
auth_key_file="${user_ssh_dir}/authorized_keys";
mkdir -p ${user_ssh_dir}
chmod 700 ${user_ssh_dir}
touch ${auth_key_file}
chmod 600 ${auth_key_file}
chown -R ${user_name}:${user_name} ${user_ssh_dir}
# Copy user pub key contents to auth key file
echo ${pub_key} > ${auth_key_file}
echo ""
echo "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
echo ""
}
function create_sudoers_file(){
local user_name=$1;
echo "Updating sudoers file for user: ${user_name}"
file="/etc/sudoers.d/${user_name}"
touch ${file};
chmod 600 ${file};
cat > ${file} <<- EOT
# Created by custom script - $0 on $(date)
# User rules for ${user_name}
centos ALL=(ALL) NOPASSWD:ALL
# User rules for ${user_name}
centos ALL=(ALL) NOPASSWD:ALL
# EOF
EOT
chmod 440 ${file};
}
declare -A ssh_users;
ssh_users["amanpruthi"]="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD5ZSUFObanow4loQiOqzj+g+7hRbuEM6CRNb0jpiiO30Av4ib1vs3Z5AY3ioY0fZz6P/X0rhEGS/IplzwDPlIvcqxaEJ7juyhX+ASR29A3OpGlDpxL7VwWdMDQuYdX6aQ/qRPthd0qKjcuq7jJF3cOdQy6EfDZxLVU7IBPR/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Fxl/xecgvcYAQFdU9Bc1KLINNUnEFDLwy5llr1JDwFx3KLZAzutQf2OXkJ6O3zR4QxMDT4hoDAK82sf9oUtEXx5LrBKP7pdgZ+3d7MCRdvIHNjSIiZfYx8eENFmKSrNhk7l4JkKizFYhupUheZ935ENRJLib6DtzY+/YhCk3LtJLDbS5vhkFGsiV8YGy6jV2ziGem/DS3rxqcwWn028+YwggCKofgNp6N26GmTIpkdNl7ytPFMtGa44YqPCOH6dRis3AZ9C/n574HTj8LCiAQbfhtXuGpiXPPDcJw== ryanbubinski@mbpro15-ryanb.local";
#echo "All keys => ${!ssh_users[@]}"
for user_name in "${final_users[@]}"; do
pub_key=${ssh_users[$user_name]["pub_key"]};
echo "Creating user: ${user_name}"
create_ssh_user "${user_name}" "${pub_key}"
done