diff options
| author | MotoKen <motokentsai@gmail.com> | 2011-12-27 14:44:11 +0800 |
|---|---|---|
| committer | MotoKen <motokentsai@gmail.com> | 2011-12-27 15:01:25 +0800 |
| commit | 5bdc35b11574439057cfbdee17cf83c40da2cea2 (patch) | |
| tree | 3acff3728483a7ced26e82780a6b574fdd2286d0 /nova/api | |
| parent | cff2ddcbd532ed54d60dbfbdbb7720f4634487b9 (diff) | |
| download | nova-5bdc35b11574439057cfbdee17cf83c40da2cea2.tar.gz nova-5bdc35b11574439057cfbdee17cf83c40da2cea2.tar.xz nova-5bdc35b11574439057cfbdee17cf83c40da2cea2.zip | |
Adds EC2 ImportKeyPair API support.
Implements import_key_pair instead of import_public_key.
Change-Id: I256b76aeb4ff94030a58692bfd12b2d7c53ae461
Diffstat (limited to 'nova/api')
| -rw-r--r-- | nova/api/ec2/__init__.py | 2 | ||||
| -rw-r--r-- | nova/api/ec2/cloud.py | 39 |
2 files changed, 27 insertions, 14 deletions
diff --git a/nova/api/ec2/__init__.py b/nova/api/ec2/__init__.py index 3c1250f97..fcd08531a 100644 --- a/nova/api/ec2/__init__.py +++ b/nova/api/ec2/__init__.py @@ -278,7 +278,7 @@ class Authorizer(wsgi.Middleware): 'CreateKeyPair': ['all'], 'DeleteKeyPair': ['all'], 'DescribeSecurityGroups': ['all'], - 'ImportPublicKey': ['all'], + 'ImportKeyPair': ['all'], 'AuthorizeSecurityGroupIngress': ['netadmin'], 'RevokeSecurityGroupIngress': ['netadmin'], 'CreateSecurityGroup': ['netadmin'], diff --git a/nova/api/ec2/cloud.py b/nova/api/ec2/cloud.py index 1f5405dea..a965b84b6 100644 --- a/nova/api/ec2/cloud.py +++ b/nova/api/ec2/cloud.py @@ -388,26 +388,39 @@ class CloudController(object): 'keyMaterial': data['private_key']} # TODO(vish): when context is no longer an object, pass it here - def import_public_key(self, context, key_name, public_key, - fingerprint=None): + def _get_fingerprint(self, public_key): + tmpdir = tempfile.mkdtemp() + pubfile = os.path.join(tmpdir, 'temp.pub') + fh = open(pubfile, 'w') + fh.write(public_key) + fh.close() + try: + (out, err) = utils.execute('ssh-keygen', '-l', '-f', + '%s' % (pubfile)) + return out.split(' ')[1] + except: + raise + finally: + shutil.rmtree(tmpdir) + + def import_key_pair(self, context, key_name, public_key_material, + **kwargs): LOG.audit(_("Import key %s"), key_name, context=context) + try: + db.key_pair_get(context, context.user_id, key_name) + raise exception.KeyPairExists(key_name=key_name) + except exception.NotFound: + pass + public_key = base64.b64decode(public_key_material) + fingerprint = self._get_fingerprint(public_key) key = {} key['user_id'] = context.user_id key['name'] = key_name key['public_key'] = public_key - if fingerprint is None: - tmpdir = tempfile.mkdtemp() - pubfile = os.path.join(tmpdir, 'temp.pub') - fh = open(pubfile, 'w') - fh.write(public_key) - fh.close() - (out, err) = utils.execute('ssh-keygen', '-q', '-l', '-f', - '%s' % (pubfile)) - fingerprint = out.split(' ')[1] - shutil.rmtree(tmpdir) key['fingerprint'] = fingerprint db.key_pair_create(context, key) - return True + return {'keyName': key_name, + 'keyFingerprint': fingerprint} def delete_key_pair(self, context, key_name, **kwargs): LOG.audit(_("Delete key pair %s"), key_name, context=context) |
