Browse Source

Use proper user and tenant in the owner section of libvirt.xml.

Nova takes instance ownership info from request context when it updates
libvirt.xml, which is not always correct. A real instance owner should
be used to avoid inconsistency in the data stored in the XML file.

Change-Id: Ib1e4803ba4ff17894a0905bcf116225defa5b58a
Closes-Bug: #1607313
(cherry picked from commit 3f2935872d)
changes/97/525997/1
Danil Akhmetov 5 years ago
committed by Gyorgy Szombathelyi
parent
commit
c4d700dc20
  1. 3
      nova/compute/api.py
  2. 46
      nova/tests/unit/virt/libvirt/test_driver.py
  3. 19
      nova/virt/libvirt/driver.py

3
nova/compute/api.py

@ -1503,6 +1503,9 @@ class API(base.Base):
# In case we couldn't find any suitable base_image
system_meta.setdefault('image_base_image_ref', instance.image_ref)
system_meta['owner_user_name'] = context.user_name
system_meta['owner_project_name'] = context.project_name
instance.system_metadata.update(system_meta)
if CONF.use_neutron:

46
nova/tests/unit/virt/libvirt/test_driver.py

@ -635,7 +635,7 @@ def _create_test_instance():
'instance_type_id': '5', # m1.small
'extra_specs': {},
'system_metadata': {
'image_disk_format': 'raw',
'image_disk_format': 'raw'
},
'flavor': flavor,
'new_flavor': None,
@ -1847,6 +1847,8 @@ class LibvirtConnTestCase(test.NoDBTestCase):
test_instance = copy.deepcopy(self.test_instance)
test_instance["display_name"] = "purple tomatoes"
test_instance['system_metadata']['owner_project_name'] = 'sweetshop'
test_instance['system_metadata']['owner_user_name'] = 'cupcake'
ctxt = context.RequestContext(project_id=123,
project_name="aubergine",
@ -1921,13 +1923,13 @@ class LibvirtConnTestCase(test.NoDBTestCase):
self.assertIsInstance(cfg.metadata[0].owner,
vconfig.LibvirtConfigGuestMetaNovaOwner)
self.assertEqual(456,
self.assertEqual("838a72b0-0d54-4827-8fd6-fb1227633ceb",
cfg.metadata[0].owner.userid)
self.assertEqual("pie",
self.assertEqual("cupcake",
cfg.metadata[0].owner.username)
self.assertEqual(123,
self.assertEqual("fake",
cfg.metadata[0].owner.projectid)
self.assertEqual("aubergine",
self.assertEqual("sweetshop",
cfg.metadata[0].owner.projectname)
self.assertIsInstance(cfg.metadata[0].flavor,
@ -1945,6 +1947,40 @@ class LibvirtConnTestCase(test.NoDBTestCase):
self.assertEqual(33550336,
cfg.metadata[0].flavor.swap)
def test_get_guest_config_missing_ownership_info(self):
drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), True)
test_instance = copy.deepcopy(self.test_instance)
ctxt = context.RequestContext(project_id=123,
project_name="aubergine",
user_id=456,
user_name="pie")
flavor = objects.Flavor(name='m1.small',
memory_mb=6,
vcpus=28,
root_gb=496,
ephemeral_gb=8128,
swap=33550336,
extra_specs={})
instance_ref = objects.Instance(**test_instance)
instance_ref.flavor = flavor
image_meta = objects.ImageMeta.from_dict(self.test_image_meta)
disk_info = blockinfo.get_disk_info(CONF.libvirt.virt_type,
instance_ref,
image_meta)
cfg = drvr._get_guest_config(instance_ref,
_fake_network_info(self, 1),
image_meta, disk_info,
context=ctxt)
self.assertEqual("N/A",
cfg.metadata[0].owner.username)
self.assertEqual("N/A",
cfg.metadata[0].owner.projectname)
def test_get_guest_config_lxc(self):
self.flags(virt_type='lxc', group='libvirt')
drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), True)

19
nova/virt/libvirt/driver.py

@ -3817,7 +3817,7 @@ class LibvirtDriver(driver.ComputeDriver):
return dev
def _get_guest_config_meta(self, context, instance):
def _get_guest_config_meta(self, instance):
"""Get metadata config for guest."""
meta = vconfig.LibvirtConfigGuestMetaNovaInstance()
@ -3829,13 +3829,13 @@ class LibvirtDriver(driver.ComputeDriver):
meta.roottype = "image"
meta.rootid = instance.image_ref
if context is not None:
ometa = vconfig.LibvirtConfigGuestMetaNovaOwner()
ometa.userid = context.user_id
ometa.username = context.user_name
ometa.projectid = context.project_id
ometa.projectname = context.project_name
meta.owner = ometa
system_meta = instance.system_metadata
ometa = vconfig.LibvirtConfigGuestMetaNovaOwner()
ometa.userid = instance.user_id
ometa.username = system_meta.get('owner_user_name', 'N/A')
ometa.projectid = instance.project_id
ometa.projectname = system_meta.get('owner_project_name', 'N/A')
meta.owner = ometa
fmeta = vconfig.LibvirtConfigGuestMetaNovaFlavor()
flavor = instance.flavor
@ -4756,8 +4756,7 @@ class LibvirtDriver(driver.ComputeDriver):
guest_numa_config.numatune,
flavor)
guest.metadata.append(self._get_guest_config_meta(context,
instance))
guest.metadata.append(self._get_guest_config_meta(instance))
guest.idmaps = self._get_guest_idmaps()
for event in self._supported_perf_events:

Loading…
Cancel
Save