Browse code

Add inc/rootwrap

Rootwrap shouldn't be a unique snowflake. Plus the binaries tend
to be called assuming PATH will find them. Not so with venvs
so we need to work around that brokenness.

Configure Cinder and Nova to use configure_rootwrap().

Change-Id: I8ee1f66014875caf20a2d14ff6ef3672673ba85a

Dean Troyer authored on 2015/03/30 04:16:44
Showing 4 changed files
... ...
@@ -15,6 +15,7 @@ FUNC_DIR=$(cd $(dirname "${BASH_SOURCE:-$0}") && pwd)
15 15
 source ${FUNC_DIR}/functions-common
16 16
 source ${FUNC_DIR}/inc/ini-config
17 17
 source ${FUNC_DIR}/inc/python
18
+source ${FUNC_DIR}/inc/rootwrap
18 19
 
19 20
 # Save trace setting
20 21
 XTRACE=$(set +o | grep xtrace)
21 22
new file mode 100644
... ...
@@ -0,0 +1,77 @@
0
+#!/bin/bash
1
+#
2
+# **inc/rootwrap** - Rootwrap functions
3
+#
4
+# Handle rootwrap's foibles
5
+
6
+# Uses: ``STACK_USER``
7
+# Defines: ``SUDO_SECURE_PATH_FILE``
8
+
9
+# Save trace setting
10
+INC_ROOT_TRACE=$(set +o | grep xtrace)
11
+set +o xtrace
12
+
13
+# Accumulate all additions to sudo's ``secure_path`` in one file read last
14
+# so they all work in a venv configuration
15
+SUDO_SECURE_PATH_FILE=${SUDO_SECURE_PATH_FILE:-/etc/sudoers.d/zz-secure-path}
16
+
17
+# Add a directory to the common sudo ``secure_path``
18
+# add_sudo_secure_path dir
19
+function add_sudo_secure_path {
20
+    local dir=$1
21
+    local line
22
+
23
+    # This is pretty simplistic for now - assume only the first line is used
24
+    if [[ -r SUDO_SECURE_PATH_FILE ]]; then
25
+        line=$(head -1 $SUDO_SECURE_PATH_FILE)
26
+    else
27
+        line="Defaults:$STACK_USER secure_path=/usr/local/sbin:/usr/local/bin:/usr/sbin:/sbin:/usr/bin:/bin"
28
+    fi
29
+
30
+    # Only add ``dir`` if it is not already present
31
+    if [[ $line =~ $dir ]]; then
32
+        echo "${line}:$dir" | sudo tee $SUDO_SECURE_PATH_FILE
33
+        sudo chmod 400 $SUDO_SECURE_PATH_FILE
34
+        sudo chown root:root $SUDO_SECURE_PATH_FILE
35
+    fi
36
+}
37
+
38
+# Configure rootwrap
39
+# Make a load of assumptions otherwise we'll have 6 arguments
40
+# configure_rootwrap project bin conf-src-dir
41
+function configure_rootwrap {
42
+    local project=$1                    # xx
43
+    local rootwrap_bin=$2               # /opt/stack/xx.venv/bin/xx-rootwrap
44
+    local rootwrap_conf_src_dir=$3      # /opt/stack/xx/etc/xx
45
+
46
+    # Start fresh with rootwrap filters
47
+    sudo rm -rf /etc/${project}/rootwrap.d
48
+    sudo install -d -o root -g root -m 755 /etc/${project}/rootwrap.d
49
+    sudo install -o root -g root -m 644 $rootwrap_conf_src_dir/rootwrap.d/*.filters /etc/${project}/rootwrap.d
50
+
51
+    # Set up rootwrap.conf, pointing to /etc/*/rootwrap.d
52
+    sudo install -o root -g root -m 644 $rootwrap_conf_src_dir/rootwrap.conf /etc/${project}/rootwrap.conf
53
+    sudo sed -e "s:^filters_path=.*$:filters_path=/etc/${project}/rootwrap.d:" -i /etc/${project}/rootwrap.conf
54
+
55
+    # Specify rootwrap.conf as first parameter to rootwrap
56
+    rootwrap_sudo_cmd="$rootwrap_bin /etc/${project}/rootwrap.conf *"
57
+
58
+    # Set up the rootwrap sudoers
59
+    local tempfile=$(mktemp)
60
+    echo "$STACK_USER ALL=(root) NOPASSWD: $rootwrap_sudo_cmd" >$tempfile
61
+    chmod 0440 $tempfile
62
+    sudo chown root:root $tempfile
63
+    sudo mv $tempfile /etc/sudoers.d/${project}-rootwrap
64
+
65
+    # Add bin dir to sudo's secure_path because rootwrap is being called
66
+    # without a path because BROKEN.
67
+    add_sudo_secure_path $(dirname $rootwrap_bin)
68
+}
69
+
70
+
71
+# Restore xtrace
72
+$INC_ROOT_TRACE
73
+
74
+# Local variables:
75
+# mode: shell-script
76
+# End:
... ...
@@ -171,42 +171,6 @@ function cleanup_cinder {
171 171
     fi
172 172
 }
173 173
 
174
-# Deploy new rootwrap filters files and configure sudo
175
-# configure_cinder_rootwrap() - configure Cinder's rootwrap
176
-function configure_cinder_rootwrap {
177
-    local cinder_rootwrap=$CINDER_BIN_DIR/cinder-rootwrap
178
-
179
-    # Wipe any existing rootwrap.d files first
180
-    if [[ -d $CINDER_CONF_DIR/rootwrap.d ]]; then
181
-        sudo rm -rf $CINDER_CONF_DIR/rootwrap.d
182
-    fi
183
-
184
-    # Deploy filters to /etc/cinder/rootwrap.d
185
-    sudo install -d -o root -g root -m 755 $CINDER_CONF_DIR/rootwrap.d
186
-    sudo install -o root -g root -m 644 $CINDER_DIR/etc/cinder/rootwrap.d/*.filters $CINDER_CONF_DIR/rootwrap.d
187
-
188
-    # Set up rootwrap.conf, pointing to /etc/cinder/rootwrap.d
189
-    sudo install -o root -g root -m 644 $CINDER_DIR/etc/cinder/rootwrap.conf $CINDER_CONF_DIR
190
-    sudo sed -e "s:^filters_path=.*$:filters_path=$CINDER_CONF_DIR/rootwrap.d:" -i $CINDER_CONF_DIR/rootwrap.conf
191
-
192
-    # Specify rootwrap.conf as first parameter to rootwrap
193
-    ROOTWRAP_CSUDOER_CMD="$cinder_rootwrap $CINDER_CONF_DIR/rootwrap.conf *"
194
-
195
-    # Set up the rootwrap sudoers for cinder
196
-    local tempfile=`mktemp`
197
-    echo "Defaults:$STACK_USER secure_path=$CINDER_BIN_DIR:/sbin:/usr/sbin:/usr/bin:/bin:/usr/local/sbin:/usr/local/bin" >$tempfile
198
-    echo "$STACK_USER ALL=(root) NOPASSWD: $ROOTWRAP_CSUDOER_CMD" >>$tempfile
199
-    chmod 0440 $tempfile
200
-    sudo chown root:root $tempfile
201
-    sudo mv $tempfile /etc/sudoers.d/cinder-rootwrap
202
-
203
-    # So rootwrap and PATH are broken beyond belief.  WTF relies on a SECURE operation
204
-    # to blindly follow PATH???  We learned that was a bad idea in the 80's!
205
-    # So to fix this in a venv, we must exploit the very hole we want to close by dropping
206
-    # a copy of the venv rootwrap binary into /usr/local/bin.
207
-    #sudo cp -p $cinder_rootwrap /usr/local/bin
208
-}
209
-
210 174
 # configure_cinder() - Set config files, create data dirs, etc
211 175
 function configure_cinder {
212 176
     sudo install -d -o $STACK_USER -m 755 $CINDER_CONF_DIR
... ...
@@ -215,7 +179,7 @@ function configure_cinder {
215 215
 
216 216
     rm -f $CINDER_CONF
217 217
 
218
-    configure_cinder_rootwrap
218
+    configure_rootwrap cinder $CINDER_BIN_DIR/cinder-rootwrap $CINDER_DIR/etc/cinder
219 219
 
220 220
     cp $CINDER_DIR/etc/cinder/api-paste.ini $CINDER_API_PASTE_INI
221 221
 
... ...
@@ -223,42 +223,6 @@ function cleanup_nova {
223 223
     #fi
224 224
 }
225 225
 
226
-# Deploy new rootwrap filters files and configure sudo
227
-# configure_nova_rootwrap() - configure Nova's rootwrap
228
-function configure_nova_rootwrap {
229
-    nova_rootwrap=$NOVA_BIN_DIR/nova-rootwrap
230
-
231
-    # Wipe any existing rootwrap.d files first
232
-    if [[ -d $NOVA_CONF_DIR/rootwrap.d ]]; then
233
-        sudo rm -rf $NOVA_CONF_DIR/rootwrap.d
234
-    fi
235
-
236
-    # Deploy filters to /etc/nova/rootwrap.d
237
-    sudo install -d -o root -g root -m 755 $NOVA_CONF_DIR/rootwrap.d
238
-    sudo install -o root -g root -m 644  $NOVA_DIR/etc/nova/rootwrap.d/*.filters $NOVA_CONF_DIR/rootwrap.d
239
-
240
-    # Set up rootwrap.conf, pointing to /etc/nova/rootwrap.d
241
-    sudo install -o root -g root -m 644 $NOVA_DIR/etc/nova/rootwrap.conf $NOVA_CONF_DIR
242
-    sudo sed -e "s:^filters_path=.*$:filters_path=$NOVA_CONF_DIR/rootwrap.d:" -i $NOVA_CONF_DIR/rootwrap.conf
243
-
244
-    # Specify rootwrap.conf as first parameter to nova-rootwrap
245
-    local rootwrap_sudoer_cmd="$nova_rootwrap $NOVA_CONF_DIR/rootwrap.conf *"
246
-
247
-    # Set up the rootwrap sudoers for nova
248
-    local tempfile=`mktemp`
249
-    echo "Defaults:$STACK_USER secure_path=$NOVA_BIN_DIR:/sbin:/usr/sbin:/usr/bin:/bin:/usr/local/sbin:/usr/local/bin" >$tempfile
250
-    echo "$STACK_USER ALL=(root) NOPASSWD: $rootwrap_sudoer_cmd" >>$tempfile
251
-    chmod 0440 $tempfile
252
-    sudo chown root:root $tempfile
253
-    sudo mv $tempfile /etc/sudoers.d/nova-rootwrap
254
-
255
-    # So rootwrap and PATH are broken beyond belief.  WTF relies on a SECURE operation
256
-    # to blindly follow PATH???  We learned that was a bad idea in the 80's!
257
-    # So to fix this in a venv, we must exploit the very hole we want to close by dropping
258
-    # a copy of the venv rootwrap binary into /usr/local/bin.
259
-    #sudo cp -p $nova_rootwrap /usr/local/bin
260
-}
261
-
262 226
 # configure_nova() - Set config files, create data dirs, etc
263 227
 function configure_nova {
264 228
     # Put config files in ``/etc/nova`` for everyone to find
... ...
@@ -266,7 +230,7 @@ function configure_nova {
266 266
 
267 267
     install_default_policy nova
268 268
 
269
-    configure_nova_rootwrap
269
+    configure_rootwrap nova $NOVA_BIN_DIR/nova-rootwrap $NOVA_DIR/etc/nova
270 270
 
271 271
     if [[ "$ENABLED_SERVICES" =~ "n-api" ]]; then
272 272
         # Get the sample configuration file in place