summaryrefslogtreecommitdiff
path: root/cloudinit/net
diff options
context:
space:
mode:
authorChad Smith <chad.smith@canonical.com>2017-09-15 20:07:11 -0600
committerScott Moser <smoser@brickies.net>2017-09-18 20:37:10 -0400
commit7eb3460b0d6d3e362a246958a7ea0a9ee5d91d5e (patch)
tree8dc323976cd5ea55ebba37cf0462220dfa7c16df /cloudinit/net
parenteaadf52b1010cf189bde2a6abb3265b890f6d36d (diff)
downloadvyos-cloud-init-7eb3460b0d6d3e362a246958a7ea0a9ee5d91d5e.tar.gz
vyos-cloud-init-7eb3460b0d6d3e362a246958a7ea0a9ee5d91d5e.zip
ec2: Fix maybe_perform_dhcp_discovery to use /var/tmp as a tmpdir
/run/cloud-init/tmp is on a filesystem mounted noexec, so running dchlient in Ec2Local during discovery breaks with 'Permission denied'. This branch allows us to run from a different tmp dir so we have exec rights. LP: #1717627
Diffstat (limited to 'cloudinit/net')
-rw-r--r--cloudinit/net/dhcp.py5
-rw-r--r--cloudinit/net/tests/test_dhcp.py18
2 files changed, 15 insertions, 8 deletions
diff --git a/cloudinit/net/dhcp.py b/cloudinit/net/dhcp.py
index c842c839..05350639 100644
--- a/cloudinit/net/dhcp.py
+++ b/cloudinit/net/dhcp.py
@@ -48,8 +48,9 @@ def maybe_perform_dhcp_discovery(nic=None):
if not dhclient_path:
LOG.debug('Skip dhclient configuration: No dhclient command found.')
return {}
- with temp_utils.tempdir(prefix='cloud-init-dhcp-') as tmpdir:
- return dhcp_discovery(dhclient_path, nic, tmpdir)
+ with temp_utils.tempdir(prefix='cloud-init-dhcp-', needs_exe=True) as tdir:
+ # Use /var/tmp because /run/cloud-init/tmp is mounted noexec
+ return dhcp_discovery(dhclient_path, nic, tdir)
def parse_dhcp_lease_file(lease_file):
diff --git a/cloudinit/net/tests/test_dhcp.py b/cloudinit/net/tests/test_dhcp.py
index 4a37e98a..1324c3d0 100644
--- a/cloudinit/net/tests/test_dhcp.py
+++ b/cloudinit/net/tests/test_dhcp.py
@@ -8,7 +8,7 @@ from cloudinit.net.dhcp import (
InvalidDHCPLeaseFileError, maybe_perform_dhcp_discovery,
parse_dhcp_lease_file, dhcp_discovery)
from cloudinit.util import ensure_file, write_file
-from cloudinit.tests.helpers import CiTestCase
+from cloudinit.tests.helpers import CiTestCase, wrap_and_call
class TestParseDHCPLeasesFile(CiTestCase):
@@ -91,21 +91,27 @@ class TestDHCPDiscoveryClean(CiTestCase):
'Skip dhclient configuration: No dhclient command found.',
self.logs.getvalue())
+ @mock.patch('cloudinit.temp_utils.os.getuid')
@mock.patch('cloudinit.net.dhcp.dhcp_discovery')
@mock.patch('cloudinit.net.dhcp.util.which')
@mock.patch('cloudinit.net.dhcp.find_fallback_nic')
- def test_dhclient_run_with_tmpdir(self, m_fallback, m_which, m_dhcp):
+ def test_dhclient_run_with_tmpdir(self, m_fback, m_which, m_dhcp, m_uid):
"""maybe_perform_dhcp_discovery passes tmpdir to dhcp_discovery."""
- m_fallback.return_value = 'eth9'
+ m_uid.return_value = 0 # Fake root user for tmpdir
+ m_fback.return_value = 'eth9'
m_which.return_value = '/sbin/dhclient'
m_dhcp.return_value = {'address': '192.168.2.2'}
- self.assertEqual(
- {'address': '192.168.2.2'}, maybe_perform_dhcp_discovery())
+ retval = wrap_and_call(
+ 'cloudinit.temp_utils',
+ {'_TMPDIR': {'new': None},
+ 'os.getuid': 0},
+ maybe_perform_dhcp_discovery)
+ self.assertEqual({'address': '192.168.2.2'}, retval)
m_dhcp.assert_called_once()
call = m_dhcp.call_args_list[0]
self.assertEqual('/sbin/dhclient', call[0][0])
self.assertEqual('eth9', call[0][1])
- self.assertIn('/tmp/cloud-init-dhcp-', call[0][2])
+ self.assertIn('/var/tmp/cloud-init/cloud-init-dhcp-', call[0][2])
@mock.patch('cloudinit.net.dhcp.util.subp')
def test_dhcp_discovery_run_in_sandbox(self, m_subp):