/
kerberos-setup.sh
executable file
·230 lines (208 loc) · 10.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
#!/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.
#
#
# This script is provided as an example of how to parse the Kerberos CSV file.
# It is for illustrative purposes only and should not be used for any other purpose.
#
usage () {
echo "Usage: keytabs.sh <HOST_PRINCIPAL_KEYTABLE.csv>";
echo " <HOST_PRINCIPAL_KEYTABLE.csv>: CSV file generated by 'Enable Security Wizard' of Ambari";
exit 1;
}
###################
## processCSVFile()
###################
processCSVFile () {
csvFile=$1;
csvFile=$(printf '%q' "$csvFile")
sed -i '/^\s*$/d' $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.addprinc;
rm -f commands.xst
rm -f commands.xst.cp
rm -f commands.tar
seenHosts="";
seenPrincipals="";
hbaseSpengo="";
hbasesrvkeytab="";
spengokeytab="";
hdfsSpengo="";
echo "mkdir -p ./tmp_keytabs" >> commands.mkdir;
# sed 1d $csvFile |
while read -r line; do
hostName=`echo $line|cut -d , -f 1`;
service=`echo $line|cut -d , -f 2`;
principal=`echo $line|cut -d , -f 3`;
localUserName=`echo $line|cut -d , -f 5`;
keytabFile=`echo $line|cut -d , -f 6 | cut -d , -f 6 | rev | cut -d '/' -f 1 | rev`;
fullKeytabFilePath=`echo $line|cut -d , -f 6`;
keytabFilePath=${fullKeytabFilePath%/*};
owner=`echo $line|cut -d , -f 7`;
group=`echo $line|cut -d , -f 9`;
acl=`echo $line|cut -d , -f 11`;
if [[ $seenHosts != *$hostName* ]]; then
echo "mkdir -p ./keytabs_$hostName" >> commands.mkdir;
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
if [[ $service == "/spnego" ]]; then
spengokeytab="`pwd`/tmp_keytabs/$keytabFile";
hdfsSpengo="true";
fi
if [[ $service == "hbase_rest_server_spnego" ]]; then
hbasesrvkeytab="`pwd`/tmp_keytabs/$keytabFile";
hbaseSpengo="true";
else
echo "kadmin.local -q \"xst -k $tmpKeytabFile $principal\"" >> commands.xst;
fi
fi
if [[ ! -d $newKeytabPath ]]; then
echo "mkdir -p $newKeytabPath" >> commands.mkdir;
fi
echo "cp $tmpKeytabFile $newKeytabFile" >> commands.xst.cp
done < <(sed 1d $csvFile) ;
echo $hbaseSpengo;
if [[ $hbaseSpengo == "true" && $hdfsSpengo == "true" ]]; then
echo "ktutil < <(echo -e \"rkt ${hbasesrvkeytab}\\nrkt ${spengokeytab}\\nwkt ${hbasesrvkeytab}\\nexit\")" >> commands.xst;
hbaseSpengo="";
hdfsSpengo="";
fi
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;
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;
echo "" >> generate_keytabs.sh;
echo "###########################################################################" >> generate_keytabs.sh;
echo "# Changing access permissions of host specific keytab files" >> generate_keytabs.sh;
echo "###########################################################################" >> 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.addprinc >> generate_keytabs.sh;
rm -f commands.xst >> generate_keytabs.sh;
rm -f commands.xst.cp >> generate_keytabs.sh;
rm -f commands.tar >> generate_keytabs.sh;
}
########################
## 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
}
########################
## checkSSH () : If passwordless ssh for root is not configured then exit
########################
checkSSH () {
host=$1
ssh -oPasswordAuthentication=no -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $host "exit 0" && return_value=0 || return_value=$? && true
if [[ $return_value != 0 ]]; then
echo "ERROR: Passwordless ssh for user root is not configured for host $host"
exit 1;
fi
}
########################
## installRngtools () : Install and start rng-tools
########################
installRngtools () {
$inst_cmd $rng_tools
echo $inst_cmd $rng_utils
if [ $os == 'ubuntu12' ] || [ $os == 'suse11' ]; then
echo "HRNGDEVICE=/dev/urandom" >> /etc/default/rng-tools
/etc/init.d/rng-tools start || true
elif [ $os == 'redhat5' ]; then
/sbin/rngd -r /dev/urandom -o /dev/random -f -t .001 --background
else
sed -i "s/\(EXTRAOPTIONS *= *\).*/\1\"-r \/dev\/urandom\"/" "/etc/sysconfig/rngd"
# start rngd
/etc/init.d/rngd start
fi
}
if (($# != 1)); then
usage
fi
set -e
checkUser
processCSVFile $@