def test_no_decryption_key_but_decryption_requested(actions, parametrized_item): encryption_key = JceNameLocalDelegatedKey.generate("AES", 256) signing_key = JceNameLocalDelegatedKey.generate("HmacSHA256", 256) encrypting_cmp = StaticCryptographicMaterialsProvider( encryption_materials=RawEncryptionMaterials( encryption_key=encryption_key, signing_key=signing_key)) decrypting_cmp = StaticCryptographicMaterialsProvider( decryption_materials=RawDecryptionMaterials( verification_key=signing_key)) encrypted_item = encrypt_python_item( parametrized_item, CryptoConfig(materials_provider=encrypting_cmp, encryption_context=EncryptionContext(), attribute_actions=actions), ) with pytest.raises(DecryptionError) as excinfo: decrypt_python_item( encrypted_item, CryptoConfig(materials_provider=decrypting_cmp, encryption_context=EncryptionContext(), attribute_actions=actions), ) excinfo.match( "Attribute actions ask for some attributes to be decrypted but no decryption key is available" )
def test_with_item(wrapped_cmp): config = CryptoConfig(materials_provider=wrapped_cmp, encryption_context=EncryptionContext(attributes={}), attribute_actions=AttributeActions()) item = {'test': 'item', 'with': 'some data'} new_config = config.with_item(item) assert config.encryption_context.attributes == {} assert new_config.encryption_context.attributes == item
def test_with_item(wrapped_cmp): config = CryptoConfig( materials_provider=wrapped_cmp, encryption_context=EncryptionContext(attributes={}), attribute_actions=AttributeActions(), ) item = {"test": "item", "with": "some data"} new_config = config.with_item(item) assert config.encryption_context.attributes == {} assert new_config.encryption_context.attributes == item
class CryptoItems: with open('aws') as v: aws_access_key_id = str(v.readline().strip()) aws_secret_access_key = str(v.readline().strip()) dbResource = boto3.resource('dynamodb', aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key, region_name='us-east-1').Table('Users') # crypto key and material provider aws_cmk_id = 'arn:aws:kms:us-east-1:910140038075:key/353f6f4c-0d0b-47b1-99fc-3aeec929b973' aws_kms_cmp = AwsKmsCryptographicMaterialsProvider(key_id=aws_cmk_id) # how the crypto is applied to attributes crypto_actions = AttributeActions( default_action=CryptoAction.DO_NOTHING, attribute_actions={'password': CryptoAction.ENCRYPT_AND_SIGN}) crypto_context = EncryptionContext(table_name='Users') custom_crypto_config = CryptoConfig(materials_provider=aws_kms_cmp, attribute_actions=crypto_actions, encryption_context=crypto_context) encrypted_resource = EncryptedTable(table=dbResource, materials_provider=aws_kms_cmp, attribute_actions=crypto_actions)
def _item_cycle_check(materials_provider, attribute_actions, item): crypto_config = CryptoConfig( materials_provider=materials_provider, encryption_context=EncryptionContext(), attribute_actions=attribute_actions, ) cycle_item_check(item, crypto_config)
def get_main_key(self, key_id: str) -> MainKey: index_key = {"key_id": key_id} encryption_context = EncryptionContext( table_name=self._table_name, partition_key_name="key_id", attributes=dict_to_ddb(index_key), ) crypto_config = CryptoConfig( materials_provider=self._materials_provider, encryption_context=encryption_context, attribute_actions=self._actions, ) encrypted_item = self._get_item(key_id=key_id) if encrypted_item["restricted"]: raise Exception("Access restricted.") decrypted_item = decrypt_python_item(encrypted_item, crypto_config) return MainKey( key_id=key_id, key_bytes=decrypted_item["key"].value, )
def create_main_key(self, key_id: str, key_length=256, key_bytes=None) -> MainKey: index_key = {"key_id": key_id} if key_bytes is None: key_bytes = self._key_bytes_generator(key_length) plaintext_item = { "restricted": False, "on_hold": False, "key": key_bytes, } plaintext_item.update(index_key) encryption_context = EncryptionContext( table_name=self._table_name, partition_key_name="key_id", attributes=dict_to_ddb(index_key), ) crypto_config = CryptoConfig( materials_provider=self._materials_provider, encryption_context=encryption_context, attribute_actions=self._actions, ) encrypted_item = encrypt_python_item(plaintext_item, crypto_config) self._put_item(key_id=key_id, encrypted_item=encrypted_item) return MainKey( key_id=key_id, key_bytes=key_bytes, )
def _item_check( materials_provider, table_name, table_index, ciphertext_item, plaintext_item, attribute_actions, prep ): prep() # Test scenario setup that needs to happen inside the test cmp = materials_provider() # Some of the materials providers need to be constructed inside the test encryption_context = EncryptionContext( table_name=table_name, partition_key_name=table_index['partition'], sort_key_name=table_index.get('sort', None), attributes=ciphertext_item ) crypto_config = CryptoConfig( materials_provider=cmp, encryption_context=encryption_context, attribute_actions=attribute_actions ) decrypted_item = decrypt_dynamodb_item(ciphertext_item.copy(), crypto_config) assert set(decrypted_item.keys()) == set(plaintext_item.keys()) for key in decrypted_item: if key == 'version': continue assert decrypted_item[key] == plaintext_item[key]
def test_aws_kms_item_cycle_hypothesis_veryslow(all_aws_kms_cmp_builders, hypothesis_actions, item): crypto_config = CryptoConfig( materials_provider=all_aws_kms_cmp_builders(), encryption_context=EncryptionContext(), attribute_actions=hypothesis_actions, ) functional_test_utils.cycle_item_check(item, crypto_config)
def test_aws_kms_item_cycle(all_aws_kms_cmp_builders, parametrized_actions, parametrized_item): crypto_config = CryptoConfig( materials_provider=all_aws_kms_cmp_builders(), encryption_context=EncryptionContext(), attribute_actions=parametrized_actions, ) functional_test_utils.cycle_item_check(parametrized_item, crypto_config)
def test_no_encryption_key_but_encryption_requested(actions, parametrized_item): signing_key = JceNameLocalDelegatedKey.generate("HmacSHA256", 256) cmp = StaticCryptographicMaterialsProvider(encryption_materials=RawEncryptionMaterials(signing_key=signing_key)) crypto_config = CryptoConfig( materials_provider=cmp, encryption_context=EncryptionContext(), attribute_actions=actions ) with pytest.raises(EncryptionError) as excinfo: encrypt_python_item(parametrized_item, crypto_config) excinfo.match("Attribute actions ask for some attributes to be encrypted but no encryption key is available")
def test_aws_kms_diverse_indexes(aws_kms_cmp, item): """Verify that AWS KMS cycle works for items with all possible combinations for primary index attribute types.""" crypto_config = CryptoConfig( materials_provider=aws_kms_cmp, encryption_context=EncryptionContext( partition_key_name='partition_key', sort_key_name='sort_key', attributes=dict_to_ddb(item)), attribute_actions=AttributeActions(attribute_actions={ key: CryptoAction.SIGN_ONLY for key in _primary_key_names })) functional_test_utils.cycle_item_check(item, crypto_config)
def decrypt_dynamodb_item( item, key_store: KeyStore, encryption_context: EncryptionContext, attribute_actions: AttributeActions, ): materials_provider = KeyStoreMaterialsProvider(key_store=key_store, ) crypto_config = CryptoConfig( materials_provider=materials_provider, encryption_context=encryption_context, attribute_actions=attribute_actions, ) return aws_decrypt_dynamodb_item(item, crypto_config)
def test_only_sign_item(parametrized_item): signing_key = JceNameLocalDelegatedKey.generate("HmacSHA256", 256) cmp = StaticCryptographicMaterialsProvider( encryption_materials=RawEncryptionMaterials(signing_key=signing_key), decryption_materials=RawDecryptionMaterials(verification_key=signing_key), ) actions = AttributeActions(default_action=CryptoAction.SIGN_ONLY) crypto_config = CryptoConfig( materials_provider=cmp, encryption_context=EncryptionContext(), attribute_actions=actions ) signed_item = encrypt_python_item(parametrized_item, crypto_config) material_description = signed_item[ReservedAttributes.MATERIAL_DESCRIPTION.value].value assert MaterialDescriptionKeys.ATTRIBUTE_ENCRYPTION_MODE.value.encode("utf-8") not in material_description decrypt_python_item(signed_item, crypto_config)
def _item_check(materials_provider, table_name, table_index, ciphertext_item, plaintext_item, attribute_actions): cmp = materials_provider() # Some of the materials providers need to be constructed inside the test encryption_context = EncryptionContext( table_name=table_name, partition_key_name=table_index["partition"], sort_key_name=table_index.get("sort", None), attributes=ciphertext_item, ) crypto_config = CryptoConfig( materials_provider=cmp, encryption_context=encryption_context, attribute_actions=attribute_actions ) decrypted_item = decrypt_dynamodb_item(ciphertext_item.copy(), crypto_config) assert set(decrypted_item.keys()) == set(plaintext_item.keys()) for key in decrypted_item: if key == "version": continue assert decrypted_item[key] == plaintext_item[key]
def crypto_config_from_table_info(materials_provider, attribute_actions, table_info): """Build a crypto config from the provided values and table info. :returns: crypto config and updated kwargs :rtype: tuple(CryptoConfig, dict) """ ec_kwargs = table_info.encryption_context_values if table_info.primary_index is not None: ec_kwargs.update({ 'partition_key_name': table_info.primary_index.partition, 'sort_key_name': table_info.primary_index.sort }) return CryptoConfig(materials_provider=materials_provider, encryption_context=EncryptionContext(**ec_kwargs), attribute_actions=attribute_actions)
def static_cmp_crypto_config(): return CryptoConfig( materials_provider=build_static_jce_cmp("AES", 256, "HmacSHA256", 256), encryption_context=EncryptionContext(), attribute_actions=AttributeActions(), )
def encrypt_item(table_name, aws_cmk_id): """Demonstrate use of EncryptedTable to transparently encrypt an item.""" index_key = { 'partition_attribute': 'is this', 'sort_attribute': 55 } plaintext_item = { 'example': 'data', 'some numbers': 99, 'and some binary': Binary(b'\x00\x01\x02'), 'leave me': 'alone' # We want to ignore this attribute } # Collect all of the attributes that will be encrypted (used later). encrypted_attributes = set(plaintext_item.keys()) encrypted_attributes.remove('leave me') # Collect all of the attributes that will not be encrypted (used later). unencrypted_attributes = set(index_key.keys()) unencrypted_attributes.add('leave me') # Add the index pairs to the item. plaintext_item.update(index_key) # Create a normal table resource. table = boto3.resource('dynamodb').Table(table_name) # Use the TableInfo helper to collect information about the indexes. table_info = TableInfo(name=table_name) table_info.refresh_indexed_attributes(table.meta.client) # Create a crypto materials provider using the specified AWS KMS key. aws_kms_cmp = AwsKmsCryptographicMaterialsProvider(key_id=aws_cmk_id) encryption_context = EncryptionContext( table_name=table_name, partition_key_name=table_info.primary_index.partition, sort_key_name=table_info.primary_index.sort, # The only attributes that are used by the AWS KMS cryptographic materials providers # are the primary index attributes. # These attributes need to be in the form of a DynamoDB JSON structure, so first # convert the standard dictionary. attributes=dict_to_ddb(index_key) ) # Create attribute actions that tells the encrypted table to encrypt all attributes, # only sign the primary index attributes, and ignore the one identified attribute to # ignore. actions = AttributeActions( default_action=CryptoAction.ENCRYPT_AND_SIGN, attribute_actions={'leave me': CryptoAction.DO_NOTHING} ) actions.set_index_keys(*table_info.protected_index_keys()) # Build the crypto config to use for this item. # When using the higher-level helpers, this is handled for you. crypto_config = CryptoConfig( materials_provider=aws_kms_cmp, encryption_context=encryption_context, attribute_actions=actions ) # Encrypt the plaintext item directly encrypted_item = encrypt_python_item(plaintext_item, crypto_config) # You could now put the encrypted item to DynamoDB just as you would any other item. # table.put_item(Item=encrypted_item) # We will skip this for the purposes of this example. # Decrypt the encrypted item directly decrypted_item = decrypt_python_item(encrypted_item, crypto_config) # Verify that all of the attributes are different in the encrypted item for name in encrypted_attributes: assert encrypted_item[name] != plaintext_item[name] assert decrypted_item[name] == plaintext_item[name] # Verify that all of the attributes that should not be encrypted were not. for name in unencrypted_attributes: assert decrypted_item[name] == encrypted_item[name] == plaintext_item[name]