18
18
""" Keypair management extension"""
23
from nova.api.openstack import extensions
25
24
from nova.api.openstack import wsgi
26
25
from nova.api.openstack import xmlutil
27
from nova.api.openstack import extensions
28
from nova import crypto
26
from nova.compute import api as compute_api
30
27
from nova import exception
51
48
class KeypairController(object):
52
50
""" Keypair API controller for the OpenStack API """
54
# TODO(ja): both this file and nova.api.ec2.cloud.py have similar logic.
55
# move the common keypair logic to nova.compute.API?
61
private_key, public_key, fingerprint = crypto.generate_key_pair()
62
return {'private_key': private_key,
63
'public_key': public_key,
64
'fingerprint': fingerprint}
66
def _validate_keypair_name(self, value):
67
safechars = "_-" + string.digits + string.ascii_letters
68
clean_value = "".join(x for x in value if x in safechars)
69
if clean_value != value:
70
msg = _("Keypair name contains unsafe characters")
71
raise webob.exc.HTTPBadRequest(explanation=msg)
52
self.api = compute_api.KeypairAPI()
73
54
@wsgi.serializers(xml=KeypairTemplate)
74
55
def create(self, req, body):
90
71
params = body['keypair']
91
72
name = params['name']
92
self._validate_keypair_name(name)
94
if not 0 < len(name) < 256:
95
msg = _('Keypair name must be between 1 and 255 characters long')
75
if 'public_key' in params:
76
keypair = self.api.import_key_pair(context,
77
context.user_id, name,
80
keypair = self.api.create_key_pair(context, context.user_id,
83
return {'keypair': keypair}
85
except exception.KeypairLimitExceeded:
86
msg = _("Quota exceeded, too many key pairs.")
87
raise webob.exc.HTTPRequestEntityTooLarge(
89
headers={'Retry-After': 0})
90
except exception.InvalidKeypair:
91
msg = _("Keypair data is invalid")
96
92
raise webob.exc.HTTPBadRequest(explanation=msg)
97
# NOTE(ja): generation is slow, so shortcut invalid name exception
99
db.key_pair_get(context, context.user_id, name)
93
except exception.KeyPairExists:
100
94
msg = _("Key pair '%s' already exists.") % name
101
95
raise webob.exc.HTTPConflict(explanation=msg)
102
except exception.NotFound:
105
keypair = {'user_id': context.user_id,
108
# import if public_key is sent
109
if 'public_key' in params:
111
fingerprint = crypto.generate_fingerprint(params['public_key'])
112
except exception.InvalidKeypair:
113
msg = _("Keypair data is invalid")
114
raise webob.exc.HTTPBadRequest(explanation=msg)
116
keypair['public_key'] = params['public_key']
117
keypair['fingerprint'] = fingerprint
119
generated_key = self._gen_key()
120
keypair['private_key'] = generated_key['private_key']
121
keypair['public_key'] = generated_key['public_key']
122
keypair['fingerprint'] = generated_key['fingerprint']
124
db.key_pair_create(context, keypair)
125
return {'keypair': keypair}
127
97
def delete(self, req, id):
131
101
context = req.environ['nova.context']
132
102
authorize(context)
134
db.key_pair_destroy(context, context.user_id, id)
104
self.api.delete_key_pair(context, context.user_id, id)
135
105
except exception.KeypairNotFound:
136
106
raise webob.exc.HTTPNotFound()
137
107
return webob.Response(status_int=202)
144
114
context = req.environ['nova.context']
145
115
authorize(context)
146
key_pairs = db.key_pair_get_all_by_user(context, context.user_id)
116
key_pairs = self.api.get_key_pairs(context, context.user_id)
148
118
for key_pair in key_pairs:
149
119
rval.append({'keypair': {