diff options
| author | Brian Waldon <bcwaldon@gmail.com> | 2012-01-28 19:43:45 -0800 |
|---|---|---|
| committer | Brian Waldon <bcwaldon@gmail.com> | 2012-01-28 19:43:45 -0800 |
| commit | 9fa7db58c22b3a4ba238d3cb9d39233968439cb1 (patch) | |
| tree | 7fd16518097a04faa992ed1073dde62d4d538009 | |
| parent | 02b872625b94c3c63674d8c64b23f80215b04a15 (diff) | |
Raise 400 if bad kepair data is provided
Fixes bug 902395
Change-Id: If04a0bf432ad4828ac1fc68f6d33dff5f9f57e24
| -rw-r--r-- | nova/api/openstack/compute/contrib/keypairs.py | 7 | ||||
| -rw-r--r-- | nova/crypto.py | 2 | ||||
| -rw-r--r-- | nova/exception.py | 4 | ||||
| -rw-r--r-- | nova/tests/api/openstack/compute/contrib/test_keypairs.py | 15 |
4 files changed, 27 insertions, 1 deletions
diff --git a/nova/api/openstack/compute/contrib/keypairs.py b/nova/api/openstack/compute/contrib/keypairs.py index 67a081c1a..512f8660a 100644 --- a/nova/api/openstack/compute/contrib/keypairs.py +++ b/nova/api/openstack/compute/contrib/keypairs.py @@ -99,7 +99,12 @@ class KeypairController(object): # import if public_key is sent if 'public_key' in params: - fingerprint = crypto.generate_fingerprint(params['public_key']) + try: + fingerprint = crypto.generate_fingerprint(params['public_key']) + except exception.InvalidKeypair: + msg = _("Keypair data is invalid") + raise webob.exc.HTTPBadRequest(explanation=msg) + keypair['public_key'] = params['public_key'] keypair['fingerprint'] = fingerprint else: diff --git a/nova/crypto.py b/nova/crypto.py index 5dd380fb6..b7d297059 100644 --- a/nova/crypto.py +++ b/nova/crypto.py @@ -126,6 +126,8 @@ def generate_fingerprint(public_key): with open(pubfile, 'w') as f: f.write(public_key) return _generate_fingerprint(pubfile) + except exception.ProcessExecutionError: + raise exception.InvalidKeypair() finally: shutil.rmtree(tmpdir) diff --git a/nova/exception.py b/nova/exception.py index d89f53653..25cbf18b3 100644 --- a/nova/exception.py +++ b/nova/exception.py @@ -215,6 +215,10 @@ class Invalid(NovaException): message = _("Unacceptable parameters.") +class InvalidKeypair(Invalid): + message = _("Keypair data is invalid") + + class InvalidRequest(Invalid): message = _("The request is invalid.") diff --git a/nova/tests/api/openstack/compute/contrib/test_keypairs.py b/nova/tests/api/openstack/compute/contrib/test_keypairs.py index 8c2d6c529..7bf65a741 100644 --- a/nova/tests/api/openstack/compute/contrib/test_keypairs.py +++ b/nova/tests/api/openstack/compute/contrib/test_keypairs.py @@ -133,6 +133,21 @@ class KeypairsTest(test.TestCase): self.assertTrue(len(res_dict['keypair']['fingerprint']) > 0) self.assertFalse('private_key' in res_dict['keypair']) + def test_keypair_import_bad_key(self): + body = { + 'keypair': { + 'name': 'create_test', + 'public_key': 'ssh-what negative', + }, + } + + req = webob.Request.blank('/v2/fake/os-keypairs') + req.method = 'POST' + req.body = json.dumps(body) + req.headers['Content-Type'] = 'application/json' + res = req.get_response(fakes.wsgi_app()) + self.assertEqual(res.status_int, 400) + def test_keypair_delete(self): req = webob.Request.blank('/v2/fake/os-keypairs/FAKE') req.method = 'DELETE' |
