summaryrefslogtreecommitdiff
path: root/nova/api/openstack/compute/keypairs.py
blob: 40b702bdb5ab5de3d27c0679c53ec7aa83202929 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# Copyright 2011 OpenStack Foundation
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""Keypair management extension."""

import webob
import webob.exc

from nova.api.openstack import api_version_request
from nova.api.openstack import common
from nova.api.openstack.compute.schemas import keypairs
from nova.api.openstack.compute.views import keypairs as keypairs_view
from nova.api.openstack import wsgi
from nova.api import validation
from nova.compute import api as compute_api
from nova import exception
from nova.objects import keypair as keypair_obj
from nova.policies import keypairs as kp_policies


class KeypairController(wsgi.Controller):

    """Keypair API controller for the OpenStack API."""

    _view_builder_class = keypairs_view.ViewBuilder

    def __init__(self):
        super(KeypairController, self).__init__()
        self.api = compute_api.KeypairAPI()

    @wsgi.Controller.api_version("2.10")
    @wsgi.response(201)
    @wsgi.expected_errors((400, 403, 409))
    @validation.schema(keypairs.create_v210, "2.10", "2.91")
    @validation.schema(keypairs.create_v292, "2.92")
    def create(self, req, body):
        """Create or import keypair.

        Keypair generations are allowed until version 2.91.
        Afterwards, only imports are allowed.

        A policy check restricts users from creating keys for other users

        params: keypair object with:
            name (required) - string
            public_key (optional or required if >=2.92) - string
            type (optional) - string
            user_id (optional) - string
        """
        # handle optional user-id for admin only
        user_id = body['keypair'].get('user_id')
        return self._create(req, body, key_type=True, user_id=user_id)

    @wsgi.Controller.api_version("2.2", "2.9")  # noqa
    @wsgi.response(201)
    @wsgi.expected_errors((400, 403, 409))
    @validation.schema(keypairs.create_v22)
    def create(self, req, body):  # noqa
        """Create or import keypair.

        Sending name will generate a key and return private_key
        and fingerprint.

        Keypair will have the type ssh or x509, specified by type.

        You can send a public_key to add an existing ssh/x509 key.

        params: keypair object with:
            name (required) - string
            public_key (optional) - string
            type (optional) - string
        """
        return self._create(req, body, key_type=True)

    @wsgi.Controller.api_version("2.1", "2.1")  # noqa
    @wsgi.expected_errors((400, 403, 409))
    @validation.schema(keypairs.create_v20, "2.0", "2.0")
    @validation.schema(keypairs.create, "2.1", "2.1")
    def create(self, req, body):  # noqa
        """Create or import keypair.

        Sending name will generate a key and return private_key
        and fingerprint.

        You can send a public_key to add an existing ssh key.

        params: keypair object with:
            name (required) - string
            public_key (optional) - string
        """
        return self._create(req, body)

    def _create(self, req, body, user_id=None, key_type=False):
        context = req.environ['nova.context']
        params = body['keypair']
        name = common.normalize_name(params['name'])
        key_type_value = params.get('type', keypair_obj.KEYPAIR_TYPE_SSH)
        user_id = user_id or context.user_id
        context.can(kp_policies.POLICY_ROOT % 'create',
                    target={'user_id': user_id})

        return_priv_key = False
        try:
            if 'public_key' in params:
                keypair = self.api.import_key_pair(
                    context, user_id, name, params['public_key'],
                    key_type_value)
            else:
                # public_key is a required field starting with 2.92 so this
                # generation should only happen with older versions.
                keypair, private_key = self.api.create_key_pair(
                    context, user_id, name, key_type_value)
                keypair['private_key'] = private_key
                return_priv_key = True
        except exception.KeypairLimitExceeded as e:
            raise webob.exc.HTTPForbidden(explanation=str(e))
        except exception.InvalidKeypair as exc:
            raise webob.exc.HTTPBadRequest(explanation=exc.format_message())
        except exception.KeyPairExists as exc:
            raise webob.exc.HTTPConflict(explanation=exc.format_message())

        return self._view_builder.create(keypair,
                                         private_key=return_priv_key,
                                         key_type=key_type)

    @wsgi.Controller.api_version("2.1", "2.1")
    @validation.query_schema(keypairs.delete_query_schema_v20)
    @wsgi.response(202)
    @wsgi.expected_errors(404)
    def delete(self, req, id):
        self._delete(req, id)

    @wsgi.Controller.api_version("2.2", "2.9")    # noqa
    @validation.query_schema(keypairs.delete_query_schema_v20)
    @wsgi.response(204)
    @wsgi.expected_errors(404)
    def delete(self, req, id):  # noqa
        self._delete(req, id)

    @wsgi.Controller.api_version("2.10")    # noqa
    @validation.query_schema(keypairs.delete_query_schema_v275, '2.75')
    @validation.query_schema(keypairs.delete_query_schema_v210, '2.10', '2.74')
    @wsgi.response(204)
    @wsgi.expected_errors(404)
    def delete(self, req, id):  # noqa
        # handle optional user-id for admin only
        user_id = self._get_user_id(req)
        self._delete(req, id, user_id=user_id)

    def _delete(self, req, id, user_id=None):
        """Delete a keypair with a given name."""
        context = req.environ['nova.context']
        # handle optional user-id for admin only
        user_id = user_id or context.user_id
        context.can(kp_policies.POLICY_ROOT % 'delete',
                    target={'user_id': user_id})
        try:
            self.api.delete_key_pair(context, user_id, id)
        except exception.KeypairNotFound as exc:
            raise webob.exc.HTTPNotFound(explanation=exc.format_message())

    def _get_user_id(self, req):
        if 'user_id' in req.GET.keys():
            user_id = req.GET.getall('user_id')[0]
            return user_id

    @wsgi.Controller.api_version("2.10")
    @validation.query_schema(keypairs.show_query_schema_v275, '2.75')
    @validation.query_schema(keypairs.show_query_schema_v210, '2.10', '2.74')
    @wsgi.expected_errors(404)
    def show(self, req, id):
        # handle optional user-id for admin only
        user_id = self._get_user_id(req)
        return self._show(req, id, key_type=True, user_id=user_id)

    @wsgi.Controller.api_version("2.2", "2.9")  # noqa
    @validation.query_schema(keypairs.show_query_schema_v20)
    @wsgi.expected_errors(404)
    def show(self, req, id):  # noqa
        return self._show(req, id, key_type=True)

    @wsgi.Controller.api_version("2.1", "2.1")  # noqa
    @validation.query_schema(keypairs.show_query_schema_v20)
    @wsgi.expected_errors(404)
    def show(self, req, id):  # noqa
        return self._show(req, id)

    def _show(self, req, id, key_type=False, user_id=None):
        """Return data for the given key name."""
        context = req.environ['nova.context']
        user_id = user_id or context.user_id
        context.can(kp_policies.POLICY_ROOT % 'show',
                    target={'user_id': user_id})

        try:
            keypair = self.api.get_key_pair(context, user_id, id)
        except exception.KeypairNotFound as exc:
            raise webob.exc.HTTPNotFound(explanation=exc.format_message())
        return self._view_builder.show(keypair, key_type=key_type)

    @wsgi.Controller.api_version("2.35")
    @validation.query_schema(keypairs.index_query_schema_v275, '2.75')
    @validation.query_schema(keypairs.index_query_schema_v235, '2.35', '2.74')
    @wsgi.expected_errors(400)
    def index(self, req):
        user_id = self._get_user_id(req)
        return self._index(req, key_type=True, user_id=user_id, links=True)

    @wsgi.Controller.api_version("2.10", "2.34")  # noqa
    @validation.query_schema(keypairs.index_query_schema_v210)
    @wsgi.expected_errors(())
    def index(self, req):  # noqa
        # handle optional user-id for admin only
        user_id = self._get_user_id(req)
        return self._index(req, key_type=True, user_id=user_id)

    @wsgi.Controller.api_version("2.2", "2.9")  # noqa
    @validation.query_schema(keypairs.index_query_schema_v20)
    @wsgi.expected_errors(())
    def index(self, req):  # noqa
        return self._index(req, key_type=True)

    @wsgi.Controller.api_version("2.1", "2.1")  # noqa
    @validation.query_schema(keypairs.index_query_schema_v20)
    @wsgi.expected_errors(())
    def index(self, req):  # noqa
        return self._index(req)

    def _index(self, req, key_type=False, user_id=None, links=False):
        """List of keypairs for a user."""
        context = req.environ['nova.context']
        user_id = user_id or context.user_id
        context.can(kp_policies.POLICY_ROOT % 'index',
                    target={'user_id': user_id})

        if api_version_request.is_supported(req, min_version='2.35'):
            limit, marker = common.get_limit_and_marker(req)
        else:
            limit = marker = None

        try:
            key_pairs = self.api.get_key_pairs(
                context, user_id, limit=limit, marker=marker)
        except exception.MarkerNotFound as e:
            raise webob.exc.HTTPBadRequest(explanation=e.format_message())

        return self._view_builder.index(req, key_pairs, key_type=key_type,
                                        links=links)