From f393a513d7894ddb800e4dfc87da896600fb7421 Mon Sep 17 00:00:00 2001 From: Davanum Srinivas Date: Wed, 12 Dec 2012 21:11:06 -0500 Subject: Ensure we add a new line when appending to rc.local When we add content to rc.local, if the file already exists then we need to make sure we add the content after a new line explicitly Fixes LP #1089668 Change-Id: I35be1496703b302f732363fa76ce832505eed599 --- nova/tests/test_virt_disk.py | 22 ++++++++++++++++++++++ nova/virt/disk/api.py | 5 ++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/nova/tests/test_virt_disk.py b/nova/tests/test_virt_disk.py index 1177aef6f..902d49704 100644 --- a/nova/tests/test_virt_disk.py +++ b/nova/tests/test_virt_disk.py @@ -84,6 +84,28 @@ class VirtDiskTest(test.TestCase): vfs.teardown() + def test_inject_data_key_with_selinux_append_with_newline(self): + + vfs = vfsguestfs.VFSGuestFS("/some/file", "qcow2") + vfs.setup() + + vfs.replace_file("/etc/rc.d/rc.local", "#!/bin/sh\necho done") + vfs.make_path("etc/selinux") + vfs.make_path("etc/rc.d") + diskapi._inject_key_into_fs("mysshkey", vfs) + + self.assertTrue("/etc/rc.d/rc.local" in vfs.handle.files) + self.assertEquals(vfs.handle.files["/etc/rc.d/rc.local"], + {'isdir': False, + 'content': "#!/bin/sh\necho done\n# Added " + "by Nova to ensure injected ssh keys have " + "the right context\nrestorecon -RF " + "root/.ssh 2>/dev/null || :\n", + 'gid': 100, + 'uid': 100, + 'mode': 0700}) + vfs.teardown() + def test_inject_net(self): vfs = vfsguestfs.VFSGuestFS("/some/file", "qcow2") diff --git a/nova/virt/disk/api.py b/nova/virt/disk/api.py index 9d9d672d7..83f5dd459 100644 --- a/nova/virt/disk/api.py +++ b/nova/virt/disk/api.py @@ -357,11 +357,14 @@ def _setup_selinux_for_keys(fs, sshdir): # and so to append there you'd need something like: # utils.execute('sed', '-i', '${/^exit 0$/d}' rclocal, run_as_root=True) restorecon = [ - '#!/bin/sh\n', + '\n', '# Added by Nova to ensure injected ssh keys have the right context\n', 'restorecon -RF %s 2>/dev/null || :\n' % sshdir, ] + if not fs.has_file(rclocal): + restorecon.insert(0, '#!/bin/sh') + _inject_file_into_fs(fs, rclocal, ''.join(restorecon), append=True) fs.set_permissions(rclocal, 0700) -- cgit