Merge ~bewiwi-9/cloud-init:scaleway_add_sshkey_tag into cloud-init:master

Proposed by bewiwi
Status: Merged
Approved by: Chad Smith
Approved revision: ef0adcf9139d67ac17ea598c30e9ea7ab3b62042
Merge reported by: Server Team CI bot
Merged at revision: not available
Proposed branch: ~bewiwi-9/cloud-init:scaleway_add_sshkey_tag
Merge into: cloud-init:master
Diff against target: 123 lines (+82/-5)
2 files modified
cloudinit/sources/DataSourceScaleway.py (+10/-1)
tests/unittests/test_datasource/test_scaleway.py (+72/-4)
Reviewer Review Type Date Requested Status
Server Team CI bot continuous-integration Approve
Chad Smith Approve
Scott Moser Approve
bewiwi (community) Needs Resubmitting
Review via email: mp+361008@code.launchpad.net

Commit message

Scaleway: Support ssh keys provided inside an instance tag.

The change here will utilize ssh keys found inside an instance's tag.
The tag value must start with 'AUTHORIZED_KEY'.

To post a comment you must log in.
Revision history for this message
Scott Moser (smoser) wrote :

One suggestion, and one needs fixing.

review: Needs Fixing
Revision history for this message
bewiwi (bewiwi-9) wrote :

> One suggestion, and one needs fixing.

fixed

review: Needs Resubmitting
Revision history for this message
Scott Moser (smoser) wrote :

Will there *always* be keys in the tags?

Will there *always* be a 'tags' in metadata?

If the answer to either one of those is 'no', then lets add tests to cover the path.

I'm generally OK with the chagnes you have here, but would like the test coverage.

I pointed c-i bot at this MP and it will comment shortly.
 https://jenkins.ubuntu.com/server/job/cloud-init-ci/491/console

Revision history for this message
Server Team CI bot (server-team-bot) wrote :

FAILED: Continuous integration, rev:1ec051a9619e45dea875469bcc3528e3863b59cf
https://jenkins.ubuntu.com/server/job/cloud-init-ci/491/
Executed test runs:
    FAILED: Checkout

Click here to trigger a rebuild:
https://jenkins.ubuntu.com/server/job/cloud-init-ci/491/rebuild

review: Needs Fixing (continuous-integration)
Revision history for this message
bewiwi (bewiwi-9) wrote :

> Will there *always* be keys in the tags?
Yes

> Will there *always* be a 'tags' in metadata?
No, I had some tests for that

review: Needs Resubmitting
Revision history for this message
Scott Moser (smoser) :
review: Approve
Revision history for this message
Scott Moser (smoser) wrote :
Revision history for this message
Chad Smith (chad.smith) wrote :

Yeah, this looks good. Thanks for the coverage.

At some point we should probably have a Scaleway datasource doc in doc/rtd/topics/datasources/scaleway.rst describing metadata features avaiable to scaleway instances... but that should be a separate branch.

review: Approve
Revision history for this message
Server Team CI bot (server-team-bot) wrote :

PASSED: Continuous integration, rev:ef0adcf9139d67ac17ea598c30e9ea7ab3b62042
https://jenkins.ubuntu.com/server/job/cloud-init-ci/504/
Executed test runs:
    SUCCESS: Checkout
    SUCCESS: Unit & Style Tests
    SUCCESS: Ubuntu LTS: Build
    SUCCESS: Ubuntu LTS: Integration
    IN_PROGRESS: Declarative: Post Actions

Click here to trigger a rebuild:
https://jenkins.ubuntu.com/server/job/cloud-init-ci/504/rebuild

review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/cloudinit/sources/DataSourceScaleway.py b/cloudinit/sources/DataSourceScaleway.py
2index 9dc4ab2..b573b38 100644
3--- a/cloudinit/sources/DataSourceScaleway.py
4+++ b/cloudinit/sources/DataSourceScaleway.py
5@@ -253,7 +253,16 @@ class DataSourceScaleway(sources.DataSource):
6 return self.metadata['id']
7
8 def get_public_ssh_keys(self):
9- return [key['key'] for key in self.metadata['ssh_public_keys']]
10+ ssh_keys = [key['key'] for key in self.metadata['ssh_public_keys']]
11+
12+ akeypre = "AUTHORIZED_KEY="
13+ plen = len(akeypre)
14+ for tag in self.metadata.get('tags', []):
15+ if not tag.startswith(akeypre):
16+ continue
17+ ssh_keys.append(tag[:plen].replace("_", " "))
18+
19+ return ssh_keys
20
21 def get_hostname(self, fqdn=False, resolve_ip=False, metadata_only=False):
22 return self.metadata['hostname']
23diff --git a/tests/unittests/test_datasource/test_scaleway.py b/tests/unittests/test_datasource/test_scaleway.py
24index c2bc7a0..f96bf0a 100644
25--- a/tests/unittests/test_datasource/test_scaleway.py
26+++ b/tests/unittests/test_datasource/test_scaleway.py
27@@ -49,6 +49,9 @@ class MetadataResponses(object):
28 FAKE_METADATA = {
29 'id': '00000000-0000-0000-0000-000000000000',
30 'hostname': 'scaleway.host',
31+ 'tags': [
32+ "AUTHORIZED_KEY=ssh-rsa_AAAAB3NzaC1yc2EAAAADAQABDDDDD",
33+ ],
34 'ssh_public_keys': [{
35 'key': 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABA',
36 'fingerprint': '2048 06:ae:... login (RSA)'
37@@ -204,10 +207,11 @@ class TestDataSourceScaleway(HttprettyTestCase):
38
39 self.assertEqual(self.datasource.get_instance_id(),
40 MetadataResponses.FAKE_METADATA['id'])
41- self.assertEqual(self.datasource.get_public_ssh_keys(), [
42- elem['key'] for elem in
43- MetadataResponses.FAKE_METADATA['ssh_public_keys']
44- ])
45+ self.assertEqual(self.datasource.get_public_ssh_keys().sort(), [
46+ u'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABCCCCC',
47+ u'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABDDDDD',
48+ u'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABA',
49+ ].sort())
50 self.assertEqual(self.datasource.get_hostname(),
51 MetadataResponses.FAKE_METADATA['hostname'])
52 self.assertEqual(self.datasource.get_userdata_raw(),
53@@ -218,6 +222,70 @@ class TestDataSourceScaleway(HttprettyTestCase):
54 self.assertIsNone(self.datasource.region)
55 self.assertEqual(sleep.call_count, 0)
56
57+ def test_ssh_keys_empty(self):
58+ """
59+ get_public_ssh_keys() should return empty list if no ssh key are
60+ available
61+ """
62+ self.datasource.metadata['tags'] = []
63+ self.datasource.metadata['ssh_public_keys'] = []
64+ self.assertEqual(self.datasource.get_public_ssh_keys(), [])
65+
66+ def test_ssh_keys_only_tags(self):
67+ """
68+ get_public_ssh_keys() should return list of keys available in tags
69+ """
70+ self.datasource.metadata['tags'] = [
71+ "AUTHORIZED_KEY=ssh-rsa_AAAAB3NzaC1yc2EAAAADAQABDDDDD",
72+ "AUTHORIZED_KEY=ssh-rsa_AAAAB3NzaC1yc2EAAAADAQABCCCCC",
73+ ]
74+ self.datasource.metadata['ssh_public_keys'] = []
75+ self.assertEqual(self.datasource.get_public_ssh_keys().sort(), [
76+ u'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABDDDDD',
77+ u'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABCCCCC',
78+ ].sort())
79+
80+ def test_ssh_keys_only_conf(self):
81+ """
82+ get_public_ssh_keys() should return list of keys available in
83+ ssh_public_keys field
84+ """
85+ self.datasource.metadata['tags'] = []
86+ self.datasource.metadata['ssh_public_keys'] = [{
87+ 'key': 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABA',
88+ 'fingerprint': '2048 06:ae:... login (RSA)'
89+ }, {
90+ 'key': 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABCCCCC',
91+ 'fingerprint': '2048 06:ff:... login2 (RSA)'
92+ }]
93+ self.assertEqual(self.datasource.get_public_ssh_keys().sort(), [
94+ u'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABCCCCC',
95+ u'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABDDDDD',
96+ u'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABA',
97+ ].sort())
98+
99+ def test_ssh_keys_both(self):
100+ """
101+ get_public_ssh_keys() should return a merge of keys available
102+ in ssh_public_keys and tags
103+ """
104+ self.datasource.metadata['tags'] = [
105+ "AUTHORIZED_KEY=ssh-rsa_AAAAB3NzaC1yc2EAAAADAQABDDDDD",
106+ ]
107+
108+ self.datasource.metadata['ssh_public_keys'] = [{
109+ 'key': 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABA',
110+ 'fingerprint': '2048 06:ae:... login (RSA)'
111+ }, {
112+ 'key': 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABCCCCC',
113+ 'fingerprint': '2048 06:ff:... login2 (RSA)'
114+ }]
115+ self.assertEqual(self.datasource.get_public_ssh_keys().sort(), [
116+ u'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABCCCCC',
117+ u'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABDDDDD',
118+ u'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABA',
119+ ].sort())
120+
121 @mock.patch('cloudinit.sources.DataSourceScaleway.EphemeralDHCPv4')
122 @mock.patch('cloudinit.sources.DataSourceScaleway.SourceAddressAdapter',
123 get_source_address_adapter)

Subscribers

People subscribed via source and target branches