summaryrefslogtreecommitdiffstats
path: root/nova/image/s3.py
blob: ee11c163c2ce227cc864bd72bbf1e1b113a2caf6 (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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# 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.

"""Proxy AMI-related calls from cloud controller to objectstore service."""

import base64
import binascii
import os
import shutil
import tarfile
import tempfile
from xml.etree import ElementTree

import boto.s3.connection
import eventlet

from nova import rpc
import nova.db.api
from nova.common import cfg
from nova import exception
from nova import flags
from nova import image
from nova import log as logging
from nova import utils
from nova.api.ec2 import ec2utils


LOG = logging.getLogger("nova.image.s3")

s3_opts = [
    cfg.StrOpt('image_decryption_dir',
               default='/tmp',
               help='parent dir for tempdir used for image decryption'),
    cfg.StrOpt('s3_access_key',
               default='notchecked',
               help='access key to use for s3 server for images'),
    cfg.StrOpt('s3_secret_key',
               default='notchecked',
               help='secret key to use for s3 server for images'),
    ]

FLAGS = flags.FLAGS
FLAGS.add_options(s3_opts)


class S3ImageService(object):
    """Wraps an existing image service to support s3 based register."""

    def __init__(self, service=None, *args, **kwargs):
        self.service = service or image.get_default_image_service()
        self.service.__init__(*args, **kwargs)

    def get_image_uuid(self, context, image_id):
        return nova.db.api.s3_image_get(context, image_id)['uuid']

    def get_image_id(self, context, image_uuid):
        return nova.db.api.s3_image_get_by_uuid(context, image_uuid)['id']

    def _create_image_id(self, context, image_uuid):
        return nova.db.api.s3_image_create(context, image_uuid)['id']

    def _translate_uuids_to_ids(self, context, images):
        return [self._translate_uuid_to_id(context, img) for img in images]

    def _translate_uuid_to_id(self, context, image):
        def _find_or_create(image_uuid):
            if image_uuid is None:
                return
            try:
                return self.get_image_id(context, image_uuid)
            except exception.NotFound:
                return self._create_image_id(context, image_uuid)

        image_copy = image.copy()

        try:
            image_id = image_copy['id']
        except KeyError:
            pass
        else:
            image_copy['id'] = _find_or_create(image_id)

        for prop in ['kernel_id', 'ramdisk_id']:
            try:
                image_uuid = image_copy['properties'][prop]
            except (KeyError, ValueError):
                pass
            else:
                image_copy['properties'][prop] = _find_or_create(image_uuid)

        return image_copy

    def create(self, context, metadata, data=None):
        """Create an image.

        metadata['properties'] should contain image_location.

        """
        image = self._s3_create(context, metadata)
        return image

    def delete(self, context, image_id):
        image_uuid = self.get_image_uuid(context, image_id)
        self.service.delete(context, image_uuid)

    def update(self, context, image_id, metadata, data=None):
        image_uuid = self.get_image_uuid(context, image_id)
        image = self.service.update(context, image_uuid, metadata, data)
        return self._translate_uuid_to_id(context, image)

    def index(self, context):
        #NOTE(bcwaldon): sort asc to make sure we assign lower ids
        # to older images
        images = self.service.index(context, sort_dir='asc')
        return self._translate_uuids_to_ids(context, images)

    def detail(self, context):
        #NOTE(bcwaldon): sort asc to make sure we assign lower ids
        # to older images
        images = self.service.detail(context, sort_dir='asc')
        return self._translate_uuids_to_ids(context, images)

    def show(self, context, image_id):
        image_uuid = self.get_image_uuid(context, image_id)
        image = self.service.show(context, image_uuid)
        return self._translate_uuid_to_id(context, image)

    def show_by_name(self, context, name):
        image = self.service.show_by_name(context, name)
        return self._translate_uuid_to_id(context, image)

    def get(self, context, image_id):
        image_uuid = self.get_image_uuid(context, image_id)
        return self.get(self, context, image_uuid)

    @staticmethod
    def _conn(context):
        # NOTE(vish): access and secret keys for s3 server are not
        #             checked in nova-objectstore
        access = FLAGS.s3_access_key
        secret = FLAGS.s3_secret_key
        calling = boto.s3.connection.OrdinaryCallingFormat()
        return boto.s3.connection.S3Connection(aws_access_key_id=access,
                                               aws_secret_access_key=secret,
                                               is_secure=False,
                                               calling_format=calling,
                                               port=FLAGS.s3_port,
                                               host=FLAGS.s3_host)

    @staticmethod
    def _download_file(bucket, filename, local_dir):
        key = bucket.get_key(filename)
        local_filename = os.path.join(local_dir, os.path.basename(filename))
        key.get_contents_to_filename(local_filename)
        return local_filename

    def _s3_parse_manifest(self, context, metadata, manifest):
        manifest = ElementTree.fromstring(manifest)
        image_format = 'ami'
        image_type = 'machine'

        try:
            kernel_id = manifest.find('machine_configuration/kernel_id').text
            if kernel_id == 'true':
                image_format = 'aki'
                image_type = 'kernel'
                kernel_id = None
        except Exception:
            kernel_id = None

        try:
            ramdisk_id = manifest.find('machine_configuration/ramdisk_id').text
            if ramdisk_id == 'true':
                image_format = 'ari'
                image_type = 'ramdisk'
                ramdisk_id = None
        except Exception:
            ramdisk_id = None

        try:
            arch = manifest.find('machine_configuration/architecture').text
        except Exception:
            arch = 'x86_64'

        # NOTE(yamahata):
        # EC2 ec2-budlne-image --block-device-mapping accepts
        # <virtual name>=<device name> where
        # virtual name = {ami, root, swap, ephemeral<N>}
        #                where N is no negative integer
        # device name = the device name seen by guest kernel.
        # They are converted into
        # block_device_mapping/mapping/{virtual, device}
        #
        # Do NOT confuse this with ec2-register's block device mapping
        # argument.
        mappings = []
        try:
            block_device_mapping = manifest.findall('machine_configuration/'
                                                    'block_device_mapping/'
                                                    'mapping')
            for bdm in block_device_mapping:
                mappings.append({'virtual': bdm.find('virtual').text,
                                 'device': bdm.find('device').text})
        except Exception:
            mappings = []

        properties = metadata['properties']
        properties['project_id'] = context.project_id
        properties['architecture'] = arch

        def _translate_dependent_image_id(image_key, image_id):
            image_id = ec2utils.ec2_id_to_id(image_id)
            image_uuid = self.get_image_uuid(context, image_id)
            properties[image_key] = image_uuid

        if kernel_id:
            _translate_dependent_image_id('kernel_id', kernel_id)

        if ramdisk_id:
            _translate_dependent_image_id('ramdisk_id', ramdisk_id)

        if mappings:
            properties['mappings'] = mappings

        metadata.update({'disk_format': image_format,
                         'container_format': image_format,
                         'status': 'queued',
                         'is_public': False,
                         'properties': properties})
        metadata['properties']['image_state'] = 'pending'

        #TODO(bcwaldon): right now, this removes user-defined ids.
        # We need to re-enable this.
        image_id = metadata.pop('id', None)

        image = self.service.create(context, metadata)

        # extract the new uuid and generate an int id to present back to user
        image_uuid = image['id']
        image['id'] = self._create_image_id(context, image_uuid)

        # return image_uuid so the caller can still make use of image_service
        return manifest, image, image_uuid

    def _s3_create(self, context, metadata):
        """Gets a manifest from s3 and makes an image."""

        image_path = tempfile.mkdtemp(dir=FLAGS.image_decryption_dir)

        image_location = metadata['properties']['image_location']
        bucket_name = image_location.split('/')[0]
        manifest_path = image_location[len(bucket_name) + 1:]
        bucket = self._conn(context).get_bucket(bucket_name)
        key = bucket.get_key(manifest_path)
        manifest = key.get_contents_as_string()

        manifest, image, image_uuid = self._s3_parse_manifest(context,
                                                              metadata,
                                                              manifest)

        def delayed_create():
            """This handles the fetching and decrypting of the part files."""
            log_vars = {'image_location': image_location,
                        'image_path': image_path}
            metadata['properties']['image_state'] = 'downloading'
            self.service.update(context, image_uuid, metadata)

            try:
                parts = []
                elements = manifest.find('image').getiterator('filename')
                for fn_element in elements:
                    part = self._download_file(bucket,
                                               fn_element.text,
                                               image_path)
                    parts.append(part)

                # NOTE(vish): this may be suboptimal, should we use cat?
                enc_filename = os.path.join(image_path, 'image.encrypted')
                with open(enc_filename, 'w') as combined:
                    for filename in parts:
                        with open(filename) as part:
                            shutil.copyfileobj(part, combined)

            except Exception:
                LOG.exception(_("Failed to download %(image_location)s "
                                "to %(image_path)s"), log_vars)
                metadata['properties']['image_state'] = 'failed_download'
                self.service.update(context, image_uuid, metadata)
                return

            metadata['properties']['image_state'] = 'decrypting'
            self.service.update(context, image_uuid, metadata)

            try:
                hex_key = manifest.find('image/ec2_encrypted_key').text
                encrypted_key = binascii.a2b_hex(hex_key)
                hex_iv = manifest.find('image/ec2_encrypted_iv').text
                encrypted_iv = binascii.a2b_hex(hex_iv)

                dec_filename = os.path.join(image_path, 'image.tar.gz')
                self._decrypt_image(context, enc_filename, encrypted_key,
                                    encrypted_iv, dec_filename)
            except Exception:
                LOG.exception(_("Failed to decrypt %(image_location)s "
                                "to %(image_path)s"), log_vars)
                metadata['properties']['image_state'] = 'failed_decrypt'
                self.service.update(context, image_uuid, metadata)
                return

            metadata['properties']['image_state'] = 'untarring'
            self.service.update(context, image_uuid, metadata)

            try:
                unz_filename = self._untarzip_image(image_path, dec_filename)
            except Exception:
                LOG.exception(_("Failed to untar %(image_location)s "
                                "to %(image_path)s"), log_vars)
                metadata['properties']['image_state'] = 'failed_untar'
                self.service.update(context, image_uuid, metadata)
                return

            metadata['properties']['image_state'] = 'uploading'
            self.service.update(context, image_uuid, metadata)
            try:
                with open(unz_filename) as image_file:
                    self.service.update(context, image_uuid,
                                        metadata, image_file)
            except Exception:
                LOG.exception(_("Failed to upload %(image_location)s "
                                "to %(image_path)s"), log_vars)
                metadata['properties']['image_state'] = 'failed_upload'
                self.service.update(context, image_uuid, metadata)
                return

            metadata['properties']['image_state'] = 'available'
            metadata['status'] = 'active'
            self.service.update(context, image_uuid, metadata)

            shutil.rmtree(image_path)

        eventlet.spawn_n(delayed_create)

        return image

    @staticmethod
    def _decrypt_image(context, encrypted_filename, encrypted_key,
                       encrypted_iv, decrypted_filename):
        elevated = context.elevated()
        try:
            key = rpc.call(elevated, FLAGS.cert_topic,
                           {"method": "decrypt_text",
                            "args": {"project_id": context.project_id,
                                     "text": base64.b64encode(encrypted_key)}})
        except Exception, exc:
            raise exception.Error(_('Failed to decrypt private key: %s')
                                  % exc)
        try:
            iv = rpc.call(elevated, FLAGS.cert_topic,
                          {"method": "decrypt_text",
                           "args": {"project_id": context.project_id,
                                    "text": base64.b64encode(encrypted_iv)}})
        except Exception, exc:
            raise exception.Error(_('Failed to decrypt initialization '
                                    'vector: %s') % exc)

        try:
            utils.execute('openssl', 'enc',
                          '-d', '-aes-128-cbc',
                          '-in', '%s' % (encrypted_filename,),
                          '-K', '%s' % (key,),
                          '-iv', '%s' % (iv,),
                          '-out', '%s' % (decrypted_filename,))
        except exception.ProcessExecutionError, exc:
            raise exception.Error(_('Failed to decrypt image file '
                                    '%(image_file)s: %(err)s') %
                                    {'image_file': encrypted_filename,
                                     'err': exc.stdout})

    @staticmethod
    def _test_for_malicious_tarball(path, filename):
        """Raises exception if extracting tarball would escape extract path"""
        tar_file = tarfile.open(filename, 'r|gz')
        for n in tar_file.getnames():
            if not os.path.abspath(os.path.join(path, n)).startswith(path):
                tar_file.close()
                raise exception.Error(_('Unsafe filenames in image'))
        tar_file.close()

    @staticmethod
    def _untarzip_image(path, filename):
        S3ImageService._test_for_malicious_tarball(path, filename)
        tar_file = tarfile.open(filename, 'r|gz')
        tar_file.extractall(path)
        image_file = tar_file.getnames()[0]
        tar_file.close()
        return os.path.join(path, image_file)