def deserialize(serialized_material_description): # type: (dynamodb_types.BINARY_ATTRIBUTE) -> Dict[Text, Text] """Deserialize a serialized material description attribute into a material description dictionary. :param dict serialized_material_description: DynamoDB attribute value containing serialized material description. :returns: Material description dictionary :rtype: dict :raises InvalidMaterialDescriptionError: if malformed version :raises InvalidMaterialDescriptionVersionError: if unknown version is found """ try: _raw_material_description = serialized_material_description[Tag.BINARY.dynamodb_tag] material_description_bytes = io.BytesIO(_raw_material_description) total_bytes = len(_raw_material_description) except (TypeError, KeyError): message = 'Invalid material description' _LOGGER.exception(message) raise InvalidMaterialDescriptionError(message) # We don't currently do anything with the version, but do check to make sure it is the one we know about. _read_version(material_description_bytes) material_description = {} try: while material_description_bytes.tell() < total_bytes: name = to_str(decode_value(material_description_bytes)) value = to_str(decode_value(material_description_bytes)) material_description[name] = value except struct.error: message = 'Invalid material description' _LOGGER.exception(message) raise InvalidMaterialDescriptionError(message) return material_description
def encryption_materials(self, encryption_context): # type: (EncryptionContext) -> RawEncryptionMaterials """Provide encryption materials. :param EncryptionContext encryption_context: Encryption context for request :returns: Encryption materials :rtype: RawEncryptionMaterials """ initial_material, encrypted_initial_material = self._generate_initial_material( encryption_context) encryption_material_description = encryption_context.material_description.copy( ) encryption_material_description.update({ _COVERED_ATTR_CTX_KEY: _KEY_COVERAGE, MaterialDescriptionKeys.CONTENT_KEY_WRAPPING_ALGORITHM.value: "kms", MaterialDescriptionKeys.CONTENT_ENCRYPTION_ALGORITHM.value: self._content_key_info.description, MaterialDescriptionKeys.ITEM_SIGNATURE_ALGORITHM.value: self._signing_key_info.description, MaterialDescriptionKeys.WRAPPED_DATA_KEY.value: to_str(base64.b64encode(encrypted_initial_material)), }) return RawEncryptionMaterials( signing_key=self._mac_key(initial_material, self._signing_key_info), encryption_key=self._encryption_key(initial_material, self._content_key_info), material_description=encryption_material_description, )
def _transform_number_value(value): # (bytes) -> dynamodb_types.STRING """Transforms a serialized number value. :param bytes value: Raw deserialized value :rtype: dynamodb_encryption_sdk.internal.dynamodb_types.STRING """ raw_value = codecs.decode(value, TEXT_ENCODING) decimal_value = Decimal(to_str(raw_value)).normalize() return "{0:f}".format(decimal_value)
def test_to_str(data, expected_output): test = to_str(data) assert test == expected_output