Merge lp:~jtv/maas/no-public-keys-is-not-an-error into lp:~maas-committers/maas/trunk

Proposed by Jeroen T. Vermeulen
Status: Merged
Approved by: Jeroen T. Vermeulen
Approved revision: no longer in the source branch.
Merged at revision: 1166
Proposed branch: lp:~jtv/maas/no-public-keys-is-not-an-error
Merge into: lp:~maas-committers/maas/trunk
Diff against target: 95 lines (+18/-20)
2 files modified
src/metadataserver/api.py (+10/-13)
src/metadataserver/tests/test_api.py (+8/-7)
To merge this branch: bzr merge lp:~jtv/maas/no-public-keys-is-not-an-error
Reviewer Review Type Date Requested Status
Raphaël Badin (community) Approve
Review via email: mp+127969@code.launchpad.net

Commit message

If a node requests its public-keys but there are none, just return a success response without any keys. The tracebacks were distracting, and may or may not have affected commissioning.

Description of the change

Julian ran into the traceback when commissioning. Commissioning was broken for apparently unrelated reasons, but we might as well eliminate the error from our list of suspects.

Jeroen

To post a comment you must log in.
Revision history for this message
Dave Walker (davewalker) wrote :

This has annoyed me for ages! Thanks for tackling this. lgtm

Revision history for this message
Raphaël Badin (rvb) wrote :

Looks good.

[0]

85 + # (bug 1058313). If the node insists, we give it the empty

Why keep the reference to that bug? The bug is about a MAASAPINotFound being raised and it looks to me like this fixes the problem.

review: Approve
Revision history for this message
Jeroen T. Vermeulen (jtv) wrote :

Fair enough. I removed the bug reference.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'src/metadataserver/api.py'
--- src/metadataserver/api.py 2012-10-04 00:01:47 +0000
+++ src/metadataserver/api.py 2012-10-04 11:22:25 +0000
@@ -22,10 +22,7 @@
2222
23from django.conf import settings23from django.conf import settings
24from django.core.exceptions import PermissionDenied24from django.core.exceptions import PermissionDenied
25from django.http import (25from django.http import HttpResponse
26 HttpResponse,
27 HttpResponseNotFound,
28 )
29from django.shortcuts import get_object_or_40426from django.shortcuts import get_object_or_404
30from maasserver.api import (27from maasserver.api import (
31 extract_oauth_key,28 extract_oauth_key,
@@ -337,10 +334,8 @@
337334
338 def public_keys(self, node, version, item):335 def public_keys(self, node, version, item):
339 """ Produce public-keys attribute."""336 """ Produce public-keys attribute."""
340 keys = SSHKey.objects.get_keys_for_user(user=node.owner)337 return make_list_response(
341 if not keys:338 SSHKey.objects.get_keys_for_user(user=node.owner))
342 raise MAASAPINotFound("No registered public keys")
343 return make_list_response(keys)
344339
345340
346class UserDataHandler(MetadataViewHandler):341class UserDataHandler(MetadataViewHandler):
@@ -367,6 +362,7 @@
367 data = {362 data = {
368 'instance-id': 'i-maas-enlistment',363 'instance-id': 'i-maas-enlistment',
369 'local-hostname': "maas-enlisting-node",364 'local-hostname': "maas-enlisting-node",
365 'public-keys': "",
370 }366 }
371367
372 def read(self, request, version, item=None):368 def read(self, request, version, item=None):
@@ -374,12 +370,13 @@
374370
375 # Requesting the list of attributes, not any particular attribute.371 # Requesting the list of attributes, not any particular attribute.
376 if item is None or len(item) == 0:372 if item is None or len(item) == 0:
377 return make_list_response(sorted(self.data.keys()))373 keys = sorted(self.data.keys())
374 # There's nothing in public-keys, so we don't advertise it.
375 # But cloud-init does ask for it and it's not worth logging
376 # a traceback for.
377 keys.remove('public-keys')
378 return make_list_response(keys)
378379
379 # Enlistment asks for SSH keys. We don't give it any, but it's
380 # not an error either.
381 if item == 'public-keys':
382 return HttpResponseNotFound("No SSH keys available for this node.")
383 if item not in self.data:380 if item not in self.data:
384 raise MAASAPINotFound("Unknown metadata attribute: %s" % item)381 raise MAASAPINotFound("Unknown metadata attribute: %s" % item)
385382
386383
=== modified file 'src/metadataserver/tests/test_api.py'
--- src/metadataserver/tests/test_api.py 2012-10-04 00:01:47 +0000
+++ src/metadataserver/tests/test_api.py 2012-10-04 11:22:25 +0000
@@ -272,11 +272,13 @@
272 self.assertIn(272 self.assertIn(
273 'public-keys', response.content.decode('ascii').split('\n'))273 'public-keys', response.content.decode('ascii').split('\n'))
274274
275 def test_public_keys_for_node_without_public_keys_returns_not_found(self):275 def test_public_keys_for_node_without_public_keys_returns_empty(self):
276 url = reverse('metadata-meta-data', args=['latest', 'public-keys'])276 url = reverse('metadata-meta-data', args=['latest', 'public-keys'])
277 client = self.make_node_client()277 client = self.make_node_client()
278 response = client.get(url)278 response = client.get(url)
279 self.assertEqual(httplib.NOT_FOUND, response.status_code)279 self.assertEqual(
280 (httplib.OK, ''),
281 (response.status_code, response.content))
280282
281 def test_public_keys_for_node_returns_list_of_keys(self):283 def test_public_keys_for_node_returns_list_of_keys(self):
282 user, _ = factory.make_user_with_keys(n_keys=2, username='my-user')284 user, _ = factory.make_user_with_keys(n_keys=2, username='my-user')
@@ -675,15 +677,14 @@
675 # just insist content is non-empty. It doesn't matter what it is.677 # just insist content is non-empty. It doesn't matter what it is.
676 self.assertTrue(response.content)678 self.assertTrue(response.content)
677679
678 def test_public_keys_returns_404_but_does_not_raise_exception(self):680 def test_public_keys_returns_empty(self):
679 # An enlisting node has no SSH keys, but it does request them681 # An enlisting node has no SSH keys, but it does request them.
680 # (bug 1058313). The request should fail, but without the log682 # If the node insists, we give it the empty list.
681 # noise of an exception.
682 md_url = reverse(683 md_url = reverse(
683 'enlist-metadata-meta-data', args=['latest', 'public-keys'])684 'enlist-metadata-meta-data', args=['latest', 'public-keys'])
684 response = self.client.get(md_url)685 response = self.client.get(md_url)
685 self.assertEqual(686 self.assertEqual(
686 (httplib.NOT_FOUND, "No SSH keys available for this node."),687 (httplib.OK, ""),
687 (response.status_code, response.content))688 (response.status_code, response.content))
688689
689 def test_metadata_bogus_is_404(self):690 def test_metadata_bogus_is_404(self):