forked from varunrao/HDP-deployment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kerberos-setup.sh
305 lines (285 loc) · 13.4 KB
/
kerberos-setup.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/bin/bash
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
############################
## NOTE:
## 1) This script should be executed on NameNode host as that host is guaranteed to have all the users needed while creating keytab file
## 2) The script has been verified to work in gce environment and
## vagrant environment documented at ambari wiki: https://cwiki.apache.org/confluence/display/AMBARI/Quick+Start+Guide
###########################
usage () {
echo "Usage: keytabs.sh <HOST_PRINCIPAL_KEYTABLE.csv> <SSH_LOGIN_KEY_PATH>";
echo " <HOST_PRINCIPAL_KEYTABLE.csv>: CSV file generated by 'Enable Security Wizard' of Ambari";
echo " <SSH_LOGIN_KEY_PATH>: File path to the ssh login key for root user";
exit 1;
}
###################
## processCSVFile()
###################
processCSVFile () {
csvFile=$1;
csvFile=$(printf '%q' "$csvFile")
touch generate_keytabs.sh;
chmod 755 generate_keytabs.sh;
echo "#!/bin/bash" > generate_keytabs.sh;
echo "###########################################################################" >> generate_keytabs.sh;
echo "###########################################################################" >> generate_keytabs.sh;
echo "## " >> generate_keytabs.sh;
echo "## Ambari Security Script Generator" >> generate_keytabs.sh;
echo "## " >> generate_keytabs.sh;
echo "## Ambari security script is generated which should be run on the" >> generate_keytabs.sh;
echo "## Kerberos server machine." >> generate_keytabs.sh;
echo "## " >> generate_keytabs.sh;
echo "## Running the generated script will create host specific keytabs folders." >> generate_keytabs.sh;
echo "## Each of those folders will contain service specific keytab files with " >> generate_keytabs.sh;
echo "## appropriate permissions. There folders should be copied as the appropriate" >> generate_keytabs.sh;
echo "## host's '/etc/security/keytabs' folder" >> generate_keytabs.sh;
echo "###########################################################################" >> generate_keytabs.sh;
echo "###########################################################################" >> generate_keytabs.sh;
rm -f commands.mkdir;
rm -f commands.chmod;
rm -f commands.addprinc;
rm -f commands.xst
rm -f commands.xst.cp
rm -f commands.chown.1
rm -f commands.chmod.1
rm -f commands.chmod.2
rm -f commands.tar
seenHosts="";
seenPrincipals="";
echo "mkdir -p ./tmp_keytabs" >> commands.mkdir;
cat $csvFile | while read line; do
hostName=`echo $line|cut -d , -f 1`;
service=`echo $line|cut -d , -f 2`;
principal=`echo $line|cut -d , -f 3`;
keytabFile=`echo $line|cut -d , -f 4`;
keytabFilePath=`echo $line|cut -d , -f 5`;
owner=`echo $line|cut -d , -f 6`;
group=`echo $line|cut -d , -f 7`;
acl=`echo $line|cut -d , -f 8`;
if [[ $seenHosts != *$hostName* ]]; then
echo "mkdir -p ./keytabs_$hostName" >> commands.mkdir;
echo "chmod 755 ./keytabs_$hostName" >> commands.chmod;
echo "chown -R root:$group `pwd`/keytabs_$hostName" >> commands.chown.1
echo "tar -cvf keytabs_$hostName.tar -C keytabs_$hostName ." >> commands.tar
seenHosts="$seenHosts$hostName";
fi
if [[ $seenPrincipals != *" $principal"* ]]; then
echo -e "kadmin.local -q \"addprinc -randkey $principal\"" >> commands.addprinc;
seenPrincipals="$seenPrincipals $principal"
fi
tmpKeytabFile="`pwd`/tmp_keytabs/$keytabFile";
newKeytabPath="`pwd`/keytabs_$hostName$keytabFilePath";
newKeytabFile="$newKeytabPath/$keytabFile";
if [ ! -f $tmpKeytabFile ]; then
echo "kadmin.local -q \"xst -k $tmpKeytabFile $principal\"" >> commands.xst;
fi
if [ ! -d $newKeytabPath ]; then
echo "mkdir -p $newKeytabPath" >> commands.mkdir;
fi
echo "cp $tmpKeytabFile $newKeytabFile" >> commands.xst.cp
echo "chmod $acl $newKeytabFile" >> commands.chmod.2
echo "chown $owner:$group $newKeytabFile" >> commands.chown.1
done;
echo "" >> generate_keytabs.sh;
echo "" >> generate_keytabs.sh;
echo "###########################################################################" >> generate_keytabs.sh;
echo "# Making host specific keytab folders" >> generate_keytabs.sh;
echo "###########################################################################" >> generate_keytabs.sh;
cat commands.mkdir >> generate_keytabs.sh;
echo "" >> generate_keytabs.sh;
echo "###########################################################################" >> generate_keytabs.sh;
echo "# Changing permissions for host specific keytab folders" >> generate_keytabs.sh;
echo "###########################################################################" >> generate_keytabs.sh;
cat commands.chmod >> generate_keytabs.sh;
echo "" >> generate_keytabs.sh;
echo "###########################################################################" >> generate_keytabs.sh;
echo "# Creating Kerberos Principals" >> generate_keytabs.sh;
echo "###########################################################################" >> generate_keytabs.sh;
cat commands.addprinc >> generate_keytabs.sh;
echo "" >> generate_keytabs.sh;
echo "###########################################################################" >> generate_keytabs.sh;
echo "# Creating Kerberos Principal keytabs in host specific keytab folders" >> generate_keytabs.sh;
echo "###########################################################################" >> generate_keytabs.sh;
cat commands.xst >> generate_keytabs.sh;
cat commands.xst.cp >> generate_keytabs.sh;
echo "" >> generate_keytabs.sh;
echo "###########################################################################" >> generate_keytabs.sh;
echo "# Changing ownerships of host specific keytab files" >> generate_keytabs.sh;
echo "###########################################################################" >> generate_keytabs.sh;
cat commands.chown.1 >> generate_keytabs.sh;
echo "" >> generate_keytabs.sh;
echo "###########################################################################" >> generate_keytabs.sh;
echo "# Changing access permissions of host specific keytab files" >> generate_keytabs.sh;
echo "###########################################################################" >> generate_keytabs.sh;
#cat commands.chmod.1
cat commands.chmod.2 >> generate_keytabs.sh;
echo "" >> generate_keytabs.sh;
echo "###########################################################################" >> generate_keytabs.sh;
echo "# Packaging keytab folders" >> generate_keytabs.sh;
echo "###########################################################################" >> generate_keytabs.sh;
cat commands.tar >> generate_keytabs.sh;
echo "" >> generate_keytabs.sh;
echo "###########################################################################" >> generate_keytabs.sh;
echo "# Cleanup" >> generate_keytabs.sh;
echo "###########################################################################" >> generate_keytabs.sh;
echo "rm -rf ./tmp_keytabs" >> generate_keytabs.sh;
echo "" >> generate_keytabs.sh;
echo "echo \"****************************************************************************\"" >> generate_keytabs.sh;
echo "echo \"****************************************************************************\"" >> generate_keytabs.sh;
echo "echo \"** Copy and extract 'keytabs_[hostname].tar' files onto respective hosts. **\"" >> generate_keytabs.sh;
echo "echo \"** **\"" >> generate_keytabs.sh;
echo "echo \"** Generated keytab files are preserved in the 'tmp_keytabs' folder. **\"" >> generate_keytabs.sh;
echo "echo \"****************************************************************************\"" >> generate_keytabs.sh;
echo "echo \"****************************************************************************\"" >> generate_keytabs.sh;
rm -f commands.mkdir >> generate_keytabs.sh;
rm -f commands.chmod >> generate_keytabs.sh;
rm -f commands.addprinc >> generate_keytabs.sh;
rm -f commands.xst >> generate_keytabs.sh;
rm -f commands.xst.cp >> generate_keytabs.sh;
rm -f commands.chown.1 >> generate_keytabs.sh;
rm -f commands.chmod.1 >> generate_keytabs.sh;
rm -f commands.chmod.2 >> generate_keytabs.sh;
rm -f commands.tar >> generate_keytabs.sh;
# generate keytabs
sh ./generate_keytabs.sh
}
########################
## installKDC () : Install rng tools,pdsh on KDC host and KDC packages on all host. Modify krb5 file
########################
installKDC () {
csvFile=$1;
sshLoginKey=$2;
HOSTNAME=`hostname --fqdn`
scriptDir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
krb5_new_conf=$scriptDir"/krb5.conf"
krb5_conf="/etc/krb5.conf"
# Install rng tools
$inst_cmd rng-tools
if [ $os == 'debian' ] || [ $os == 'suse' ]; then
echo "HRNGDEVICE=/dev/urandom" >> /etc/default/rng-tools
/etc/init.d/rng-tools start
else
sed -i "s/\(EXTRAOPTIONS *= *\).*/\1\"-r \/dev\/urandom\"/" "/etc/sysconfig/rngd"
# start rngd
/etc/init.d/rngd start
fi
# Install kdc server on this host
if [ $os == 'debian' ]; then
$inst_cmd krb5-kdc krb5-admin-server krb5-user libpam-krb5 libpam-ccreds auth-client-config
elif [ $os == 'suse' ] ; then
$inst_cmd krb5 krb5-server krb5-client
else
$inst_cmd krb5-server krb5-libs krb5-auth-dialog krb5-workstation
fi
# Configure /etc/krb5.conf
cp $krb5_conf $krb5_conf".bak"
cp $krb5_new_conf $krb5_conf
sed -i "s/\(kdc *= *\).*kerberos.example.com.*/\1$HOSTNAME/" $krb5_conf
sed -i "s/\(admin_server *= *\).*kerberos.example.com.*/\1$HOSTNAME/" $krb5_conf
# Install rng tools
if [ $os == 'debian' ]; then
echo -ne '\n\n' | kdb5_util create -s
/usr/sbin/service krb5-admin-server start
/usr/sbin/service krb5-kdc start
else
echo -ne '\n\n' | kdb5_util create -s
/sbin/service krb5kdc start
/sbin/service kadmin start
fi
# Install pdsh on this host
$inst_cmd pdsh;
chown root:root -R /usr;
eval `ssh-agent`
ssh-add $sshLoginKey
hostNames='';
while read line; do
hostName=`echo $line|cut -d , -f 1`;
if [ -z "$hostNames" ]; then
hostNames=$hostName;
continue;
fi
if [[ $hostNames != *$hostName* ]]; then
hostNames=$hostNames,$hostName;
fi
done < $csvFile
export PDSH_SSH_ARGS_APPEND="-q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o PreferredAuthentications=publickey"
if [ $os == 'debian' ]; then
pdsh -R ssh -w $hostNames "$inst_cmd krb5-user libpam-krb5 libpam-ccreds auth-client-config"
elif [ $os == 'suse' ] ; then
pdsh -R ssh -w $hostNames "$inst_cmd krb5-client"
else
pdsh -R ssh -w $hostNames "$inst_cmd krb5-workstation"
fi
pdsh -R ssh -w $hostNames "$inst_cmd pdsh"
pdsh -R ssh -w $hostNames chown root:root -R /usr
pdcp -R ssh -w $hostNames $krb5_conf $krb5_conf
}
########################
## distributeKeytabs () : Distribute the tar on all respective hosts root directory and untar it
########################
distributeKeytabs () {
shopt -s nullglob
filearray=(keytabs_*tar)
for i in ${filearray[@]}; do
derivedname=${i%.*}
derivedname=${derivedname##keytabs_}
echo $derivedname
scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $i root@$derivedname:/
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@$derivedname "cd /;tar xvf $i --no-overwrite-dir"
done
}
########################
## getEnvironmentCMD () : get linux distribution type and package manager
########################
getEnvironmentCMD () {
os=`python -c 'import sys; sys.path.append("/usr/lib/python2.6/site-packages/"); from ambari_commons import OSCheck; print OSCheck.get_os_family()'`
case $os in
'debian' )
pkgmgr='apt-get'
inst_cmd="env DEBIAN_FRONTEND=noninteractive /usr/bin/$pkgmgr --force-yes --assume-yes install "
;;
'redhat' )
pkgmgr='yum'
inst_cmd="/usr/bin/$pkgmgr -d 0 -e 0 -y install "
;;
'suse' )
pkgmgr='zypper'
inst_cmd="/usr/bin/$pkgmgr --quiet install --auto-agree-with-licenses --no-confirm "
;;
esac
}
########################
## checkUser () : If the user executing the script is not "root" then exit
########################
checkUser () {
userid=`id -u`;
if (($userid != 0)); then
echo "ERROR: The script needs to be executed by root user"
exit 1;
fi
}
if (($# != 2)); then
usage
fi
checkUser
getEnvironmentCMD
installKDC $@
processCSVFile $@
distributeKeytabs $@