Support alternate 'public-keys' format for NoCloud service.

The 'public-keys' in the metadata is sometimes just a simple list of keys
instead of a list dicts with an 'openssh-key', e.g:

```yaml
instance-id: my-instance-id
local-hostname: my-host-name
public-keys:
  - ssh-rsa ...
```

Change-Id: I46c1228e1e1ea5bbf34144480633907a1ea20317
This commit is contained in:
Luqman Aden 2023-02-09 14:57:01 -08:00
parent d6e28339eb
commit aeeef30df8
2 changed files with 14 additions and 1 deletions

View File

@ -307,7 +307,10 @@ class NoCloudConfigDriveService(baseconfigdrive.BaseConfigDriveService):
if not raw_ssh_keys: if not raw_ssh_keys:
return [] return []
return [raw_ssh_keys[key].get('openssh-key') for key in raw_ssh_keys] if all(isinstance(elem, str) for elem in raw_ssh_keys):
return raw_ssh_keys
else:
return [raw_ssh_keys[k].get('openssh-key') for k in raw_ssh_keys]
def get_network_details(self): def get_network_details(self):
debian_net_config = self._get_meta_data().get('network-interfaces') debian_net_config = self._get_meta_data().get('network-interfaces')

View File

@ -247,6 +247,16 @@ class TestNoCloudConfigDriveService(unittest.TestCase):
result = self._config_drive.get_public_keys() result = self._config_drive.get_public_keys()
self.assertEqual(result, expected_result) self.assertEqual(result, expected_result)
@mock.patch(MODULE_PATH + '.NoCloudConfigDriveService._get_meta_data')
def test_get_public_keys_alt_fmt(self, mock_get_metadata):
fake_key = 'fake key'
expected_result = [fake_key]
mock_get_metadata.return_value = {
'public-keys': [fake_key]
}
result = self._config_drive.get_public_keys()
self.assertEqual(result, expected_result)
@ddt.data(('', ('V2 network metadata is empty', None)), @ddt.data(('', ('V2 network metadata is empty', None)),
('1', ('V2 network metadata is not a dictionary', None)), ('1', ('V2 network metadata is not a dictionary', None)),
('{}', ('V2 network metadata is empty', None)), ('{}', ('V2 network metadata is empty', None)),