~ubuntu-branches/ubuntu/saucy/nova/saucy-proposed

« back to all changes in this revision

Viewing changes to nova/api/openstack/compute/contrib/keypairs.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-05-24 13:12:53 UTC
  • mfrom: (1.1.55)
  • Revision ID: package-import@ubuntu.com-20120524131253-ommql08fg1en06ut
Tags: 2012.2~f1-0ubuntu1
* New upstream release.
* Prepare for quantal:
  - Dropped debian/patches/upstream/0006-Use-project_id-in-ec2.cloud._format_image.patch
  - Dropped debian/patches/upstream/0005-Populate-image-properties-with-project_id-again.patch
  - Dropped debian/patches/upstream/0004-Fixed-bug-962840-added-a-test-case.patch
  - Dropped debian/patches/upstream/0003-Allow-unprivileged-RADOS-users-to-access-rbd-volumes.patch
  - Dropped debian/patches/upstream/0002-Stop-libvirt-test-from-deleting-instances-dir.patch
  - Dropped debian/patches/upstream/0001-fix-bug-where-nova-ignores-glance-host-in-imageref.patch 
  - Dropped debian/patches/0001-fix-useexisting-deprecation-warnings.patch
* debian/control: Add python-keystone as a dependency. (LP: #907197)
* debian/patches/kombu_tests_timeout.patch: Refreshed.
* debian/nova.conf, debian/nova-common.postinst: Convert to new ini
  file configuration
* debian/patches/nova-manage_flagfile_location.patch: Refreshed

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
""" Keypair management extension"""
19
19
 
20
 
import string
21
 
 
22
20
import webob
23
21
import webob.exc
24
22
 
 
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
29
 
from nova import db
 
26
from nova.compute import api as compute_api
30
27
from nova import exception
31
28
 
32
29
 
49
46
 
50
47
 
51
48
class KeypairController(object):
 
49
 
52
50
    """ Keypair API controller for the OpenStack API """
53
 
 
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?
56
 
 
57
 
    def _gen_key(self):
58
 
        """
59
 
        Generate a key
60
 
        """
61
 
        private_key, public_key, fingerprint = crypto.generate_key_pair()
62
 
        return {'private_key': private_key,
63
 
                'public_key': public_key,
64
 
                'fingerprint': fingerprint}
65
 
 
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)
 
51
    def __init__(self):
 
52
        self.api = compute_api.KeypairAPI()
72
53
 
73
54
    @wsgi.serializers(xml=KeypairTemplate)
74
55
    def create(self, req, body):
89
70
        authorize(context)
90
71
        params = body['keypair']
91
72
        name = params['name']
92
 
        self._validate_keypair_name(name)
93
 
 
94
 
        if not 0 < len(name) < 256:
95
 
            msg = _('Keypair name must be between 1 and 255 characters long')
 
73
 
 
74
        try:
 
75
            if 'public_key' in params:
 
76
                keypair = self.api.import_key_pair(context,
 
77
                                              context.user_id, name,
 
78
                                              params['public_key'])
 
79
            else:
 
80
                keypair = self.api.create_key_pair(context, context.user_id,
 
81
                                                   name)
 
82
 
 
83
            return {'keypair': keypair}
 
84
 
 
85
        except exception.KeypairLimitExceeded:
 
86
            msg = _("Quota exceeded, too many key pairs.")
 
87
            raise webob.exc.HTTPRequestEntityTooLarge(
 
88
                        explanation=msg,
 
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
98
 
        try:
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:
103
 
            pass
104
 
 
105
 
        keypair = {'user_id': context.user_id,
106
 
                   'name': name}
107
 
 
108
 
        # import if public_key is sent
109
 
        if 'public_key' in params:
110
 
            try:
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)
115
 
 
116
 
            keypair['public_key'] = params['public_key']
117
 
            keypair['fingerprint'] = fingerprint
118
 
        else:
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']
123
 
 
124
 
        db.key_pair_create(context, keypair)
125
 
        return {'keypair': keypair}
126
96
 
127
97
    def delete(self, req, id):
128
98
        """
131
101
        context = req.environ['nova.context']
132
102
        authorize(context)
133
103
        try:
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)
143
113
        """
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)
147
117
        rval = []
148
118
        for key_pair in key_pairs:
149
119
            rval.append({'keypair': {