Exemplo n.º 1
0
 def test_decrypt_locations_on_list(self):
     url_loc = ['ping', 'pong']
     orig_locations = [{
         'url': l,
         'metadata': {},
         'status': 'active'
     } for l in url_loc]
     encrypted_locs = [
         crypt.urlsafe_encrypt(self.crypt_key, l) for l in url_loc
     ]
     encrypted_locations = [{
         'url': l,
         'metadata': {},
         'status': 'active'
     } for l in encrypted_locs]
     self.assertNotEqual(encrypted_locations, orig_locations)
     db_data = _db_fixture(UUID1,
                           owner=TENANT1,
                           locations=encrypted_locations)
     self.db.image_create(None, db_data)
     image = self.image_repo.list()[0]
     self.assertIn('id', image.locations[0])
     self.assertIn('id', image.locations[1])
     image.locations[0].pop('id')
     image.locations[1].pop('id')
     self.assertEqual(orig_locations, image.locations)
Exemplo n.º 2
0
    def get_all_locations(self):
        """Returns a list of image id and location tuple from scrub queue.

        :returns: a list of image id, location id and uri tuple from
            scrub queue

        """
        ret = []

        for image in self._get_all_images():
            deleted_at = image.get('deleted_at')
            if not deleted_at:
                continue

            # NOTE: Strip off microseconds which may occur after the last '.,'
            # Example: 2012-07-07T19:14:34.974216
            date_str = deleted_at.rsplit('.', 1)[0].rsplit(',', 1)[0]
            delete_time = calendar.timegm(time.strptime(date_str,
                                                        "%Y-%m-%dT%H:%M:%S"))

            if delete_time + self.scrub_time > time.time():
                continue

            for loc in image['location_data']:
                if loc['status'] != 'pending_delete':
                    continue

                if self.metadata_encryption_key:
                    uri = crypt.urlsafe_encrypt(self.metadata_encryption_key,
                                                loc['url'], 64)
                else:
                    uri = loc['url']

                ret.append((image['id'], loc['id'], uri))
        return ret
Exemplo n.º 3
0
 def _format_image_to_db(self, image):
     locations = image.locations
     if CONF.metadata_encryption_key:
         key = CONF.metadata_encryption_key
         ld = []
         for loc in locations:
             url = crypt.urlsafe_encrypt(key, loc['url'])
             ld.append({
                 'url': url,
                 'metadata': loc['metadata'],
                 'status': loc['status'],
                 # NOTE(zhiyan): New location has no ID field.
                 'id': loc.get('id')
             })
         locations = ld
     return {
         'id': image.image_id,
         'name': image.name,
         'status': image.status,
         'created_at': image.created_at,
         'min_disk': image.min_disk,
         'min_ram': image.min_ram,
         'protected': image.protected,
         'locations': locations,
         'checksum': image.checksum,
         'owner': image.owner,
         'disk_format': image.disk_format,
         'container_format': image.container_format,
         'size': image.size,
         'virtual_size': image.virtual_size,
         'is_public': image.visibility == 'public',
         'properties': dict(image.extra_properties),
     }
Exemplo n.º 4
0
 def _format_image_to_db(self, image):
     locations = image.locations
     if CONF.metadata_encryption_key:
         key = CONF.metadata_encryption_key
         ld = []
         for loc in locations:
             url = crypt.urlsafe_encrypt(key, loc['url'])
             ld.append({'url': url, 'metadata': loc['metadata'],
                        'status': loc['status'],
                        # NOTE(zhiyan): New location has no ID field.
                        'id': loc.get('id')})
         locations = ld
     return {
         'id': image.image_id,
         'name': image.name,
         'status': image.status,
         'created_at': image.created_at,
         'min_disk': image.min_disk,
         'min_ram': image.min_ram,
         'protected': image.protected,
         'locations': locations,
         'checksum': image.checksum,
         'owner': image.owner,
         'disk_format': image.disk_format,
         'container_format': image.container_format,
         'size': image.size,
         'virtual_size': image.virtual_size,
         'is_public': image.visibility == 'public',
         'properties': dict(image.extra_properties),
     }
Exemplo n.º 5
0
 def encrypt_metadata(self, image_metadata):
     if self.metadata_encryption_key:
         location_url = image_metadata.get('location')
         if location_url:
             location = crypt.urlsafe_encrypt(self.metadata_encryption_key,
                                              location_url,
                                              64)
             image_metadata['location'] = location
         if image_metadata.get('location_data'):
             ld = []
             for loc in image_metadata['location_data']:
                 if loc['url'] == location_url:
                     url = location
                 else:
                     url = crypt.urlsafe_encrypt(
                         self.metadata_encryption_key, loc['url'], 64)
                 ld.append({'url': url, 'metadata': loc['metadata'],
                            'status': loc['status'],
                            # NOTE(zhiyan): New location has no ID field.
                            'id': loc.get('id')})
             image_metadata['location_data'] = ld
     return image_metadata
Exemplo n.º 6
0
 def test_decrypt_locations_on_list(self):
     url_loc = ['ping', 'pong']
     orig_locations = [{'url': l, 'metadata': {}, 'status': 'active'}
                       for l in url_loc]
     encrypted_locs = [crypt.urlsafe_encrypt(self.crypt_key, l)
                       for l in url_loc]
     encrypted_locations = [{'url': l, 'metadata': {}, 'status': 'active'}
                            for l in encrypted_locs]
     self.assertNotEqual(encrypted_locations, orig_locations)
     db_data = _db_fixture(UUID1, owner=TENANT1,
                           locations=encrypted_locations)
     self.db.image_create(None, db_data)
     image = self.image_repo.list()[0]
     self.assertIn('id', image.locations[0])
     self.assertIn('id', image.locations[1])
     image.locations[0].pop('id')
     image.locations[1].pop('id')
     self.assertEqual(orig_locations, image.locations)
Exemplo n.º 7
0
    def test_encryption(self):
        # Check that original plaintext and unencrypted ciphertext match
        # Check keys of the three allowed lengths
        key_list = ["1234567890abcdef",
                    "12345678901234567890abcd",
                    "1234567890abcdef1234567890ABCDEF"]
        plaintext_list = ['']
        blocksize = 64
        for i in range(3 * blocksize):
            text = os.urandom(i)
            if six.PY3:
                text = text.decode('latin1')
            plaintext_list.append(text)

        for key in key_list:
            for plaintext in plaintext_list:
                ciphertext = crypt.urlsafe_encrypt(key, plaintext, blocksize)
                self.assertIsInstance(ciphertext, str)
                self.assertNotEqual(ciphertext, plaintext)
                text = crypt.urlsafe_decrypt(key, ciphertext)
                self.assertIsInstance(text, str)
                self.assertEqual(plaintext, text)
Exemplo n.º 8
0
def encrypt_location(uri):
    return crypt.urlsafe_encrypt(CONF.metadata_encryption_key, uri, 64)