コード例 #1
0
 def _upload_chunk(self, chunk_offset, chunk_data):
     block_id = url_quote(_encode_base64('{0:032d}'.format(chunk_offset)))
     self.blob_service._put_block(
         self.container_name,
         self.blob_name,
         chunk_data,
         block_id,
         validate_content=self.validate_content,
         lease_id=self.lease_id,
         timeout=self.timeout,
     )
     return BlobBlock(block_id)
コード例 #2
0
 def _upload_chunk(self, chunk_offset, chunk_data):
     block_id = url_quote(_encode_base64('{0:032d}'.format(chunk_offset)))
     self.blob_service._put_block(
         self.container_name,
         self.blob_name,
         chunk_data,
         block_id,
         validate_content=self.validate_content,
         lease_id=self.lease_id,
         timeout=self.timeout,
     )
     return BlobBlock(block_id)
コード例 #3
0
def _encrypt_queue_message(message, key_encryption_key):
    '''
    Encrypts the given plain text message using AES256 in CBC mode with 128 bit padding.
    Wraps the generated content-encryption-key using the user-provided key-encryption-key (kek). 
    Returns a json-formatted string containing the encrypted message and the encryption metadata.

    :param object message:
        The plain text messge to be encrypted.
    :param object key_encryption_key:
        The user-provided key-encryption-key. Must implement the following methods:
        wrap_key(key)--wraps the specified key using an algorithm of the user's choice.
        get_key_wrap_algorithm()--returns the algorithm used to wrap the specified symmetric key.
        get_kid()--returns a string key id for this key-encryption-key.
    :return: A json-formatted string containing the encrypted message and the encryption metadata.
    :rtype: str
    '''

    _validate_not_none('message', message)
    _validate_not_none('key_encryption_key', key_encryption_key)
    _validate_key_encryption_key_wrap(key_encryption_key)

    # AES256 uses 256 bit (32 byte) keys and always with 16 byte blocks
    content_encryption_key = os.urandom(32)
    initialization_vector = os.urandom(16)

    # Queue encoding functions all return unicode strings, and encryption should
    # operate on binary strings.
    message = message.encode('utf-8')

    cipher = _generate_AES_CBC_cipher(content_encryption_key,
                                      initialization_vector)

    # PKCS7 with 16 byte blocks ensures compatibility with AES.
    padder = PKCS7(128).padder()
    padded_data = padder.update(message) + padder.finalize()

    # Encrypt the data.
    encryptor = cipher.encryptor()
    encrypted_data = encryptor.update(padded_data) + encryptor.finalize()

    # Build the dictionary structure.
    queue_message = {
        'EncryptedMessageContents':
        _encode_base64(encrypted_data),
        'EncryptionData':
        _generate_encryption_data_dict(key_encryption_key,
                                       content_encryption_key,
                                       initialization_vector)
    }

    return dumps(queue_message)
コード例 #4
0
def _convert_block_list_to_xml(block_id_list):
    '''
    <?xml version="1.0" encoding="utf-8"?>
    <BlockList>
      <Committed>first-base64-encoded-block-id</Committed>
      <Uncommitted>second-base64-encoded-block-id</Uncommitted>
      <Latest>third-base64-encoded-block-id</Latest>
    </BlockList>

    Convert a block list to xml to send.

    block_id_list:
        A list of BlobBlock containing the block ids and block state that are used in put_block_list.
    Only get block from latest blocks.
    '''
    if block_id_list is None:
        return ''

    block_list_element = ETree.Element('BlockList')

    # Enabled
    for block in block_id_list:
        if block.id is None:
            raise ValueError(_ERROR_INVALID_BLOCK_ID)
        id = xml_escape(_str(format(_encode_base64(block.id))))
        ETree.SubElement(block_list_element, block.state).text = id

    # Add xml declaration and serialize
    try:
        stream = BytesIO()
        ETree.ElementTree(block_list_element).write(stream,
                                                    xml_declaration=True,
                                                    encoding='utf-8',
                                                    method='xml')
    except:
        raise
    finally:
        output = stream.getvalue()
        stream.close()

    # return xml value
    return output
コード例 #5
0
    def _put_block(self, container_name, blob_name, block, block_id,
                   validate_content=False, lease_id=None, timeout=None):
        '''
        See put_block for more details. This helper method
        allows for encryption or other such special behavior because
        it is safely handled by the library. These behaviors are
        prohibited in the public version of this function.
        '''

        _validate_not_none('container_name', container_name)
        _validate_not_none('blob_name', blob_name)
        _validate_not_none('block', block)
        _validate_not_none('block_id', block_id)
        request = HTTPRequest()
        request.method = 'PUT'
        request.host_locations = self._get_host_locations()
        request.path = _get_path(container_name, blob_name)
        request.query = {
            'comp': 'block',
            'blockid': _encode_base64(_to_str(block_id)),
            'timeout': _int_to_str(timeout),
        }
        request.headers = {
            'x-ms-lease-id': _to_str(lease_id)
        }
        request.body = _get_data_bytes_or_stream_only('block', block)
        if hasattr(request.body, 'read'):
            if _len_plus(request.body) is None:
                try:
                    data = b''
                    for chunk in iter(lambda: request.body.read(4096), b""):
                        data += chunk
                    request.body = data
                except AttributeError:
                    raise ValueError(_ERROR_VALUE_SHOULD_BE_STREAM.format('request.body'))

        if validate_content:
            computed_md5 = _get_content_md5(request.body)
            request.headers['Content-MD5'] = _to_str(computed_md5)

        self._perform_request(request)
コード例 #6
0
    def _put_block(self, container_name, blob_name, block, block_id,
                   validate_content=False, lease_id=None, timeout=None):
        '''
        See put_block for more details. This helper method
        allows for encryption or other such special behavior because
        it is safely handled by the library. These behaviors are
        prohibited in the public version of this function.
        '''

        _validate_not_none('container_name', container_name)
        _validate_not_none('blob_name', blob_name)
        _validate_not_none('block', block)
        _validate_not_none('block_id', block_id)
        request = HTTPRequest()
        request.method = 'PUT'
        request.host_locations = self._get_host_locations()
        request.path = _get_path(container_name, blob_name)
        request.query = {
            'comp': 'block',
            'blockid': _encode_base64(_to_str(block_id)),
            'timeout': _int_to_str(timeout),
        }
        request.headers = {
            'x-ms-lease-id': _to_str(lease_id)
        }
        request.body = _get_data_bytes_or_stream_only('block', block)
        if hasattr(request.body, 'read'):
            if _len_plus(request.body) is None:
                try:
                    data = b''
                    for chunk in iter(lambda: request.body.read(4096), b""):
                        data += chunk
                    request.body = data
                except AttributeError:
                    raise ValueError(_ERROR_VALUE_SHOULD_BE_STREAM.format('request.body'))

        if validate_content:
            computed_md5 = _get_content_md5(request.body)
            request.headers['Content-MD5'] = _to_str(computed_md5)

        self._perform_request(request)
コード例 #7
0
def _convert_block_list_to_xml(block_id_list):
    '''
    <?xml version="1.0" encoding="utf-8"?>
    <BlockList>
      <Committed>first-base64-encoded-block-id</Committed>
      <Uncommitted>second-base64-encoded-block-id</Uncommitted>
      <Latest>third-base64-encoded-block-id</Latest>
    </BlockList>

    Convert a block list to xml to send.

    block_id_list:
        A list of BlobBlock containing the block ids and block state that are used in put_block_list.
    Only get block from latest blocks.
    '''
    if block_id_list is None:
        return ''

    block_list_element = ETree.Element('BlockList')

    # Enabled
    for block in block_id_list:
        if block.id is None:
            raise ValueError(_ERROR_INVALID_BLOCK_ID)
        id = xml_escape(_str(format(_encode_base64(block.id))))
        ETree.SubElement(block_list_element, block.state).text = id

    # Add xml declaration and serialize
    try:
        stream = BytesIO()
        ETree.ElementTree(block_list_element).write(stream, xml_declaration=True, encoding='utf-8', method='xml')
    except:
        raise
    finally:
        output = stream.getvalue()
        stream.close()

    # return xml value
    return output
コード例 #8
0
 def _assert_default_entity_json_no_metadata(self, entity):
     '''
     Asserts that the entity passed in matches the default entity.
     '''
     self.assertEqual(entity.age, '39')
     self.assertEqual(entity.sex, 'male')
     self.assertEqual(entity.married, True)
     self.assertEqual(entity.deceased, False)
     self.assertFalse(hasattr(entity, "optional"))
     self.assertFalse(hasattr(entity, "aquarius"))
     self.assertEqual(entity.ratio, 3.1)
     self.assertEqual(entity.evenratio, 3.0)
     self.assertEqual(entity.large, '933311100')
     self.assertEqual(entity.Birthday, '1973-10-04T00:00:00Z')
     self.assertEqual(entity.birthday, '1970-10-04T00:00:00Z')
     self.assertEqual(entity.binary, _encode_base64(b'binary'))
     self.assertIsInstance(entity.other, EntityProperty)
     self.assertEqual(entity.other.type, EdmType.INT32)
     self.assertEqual(entity.other.value, 20)
     self.assertEqual(entity.clsid, 'c9da6455-213d-42c9-9a79-3e9149a57833')
     self.assertTrue(hasattr(entity, "Timestamp"))
     self.assertIsInstance(entity.Timestamp, datetime)
     self.assertIsNotNone(entity.etag)
コード例 #9
0
def _to_entity_binary(value):
    return EdmType.BINARY, _encode_base64(value)
コード例 #10
0
ファイル: _serialization.py プロジェクト: wuyou8933/Leetcode
def _to_entity_binary(value):
    return EdmType.BINARY, _encode_base64(value)