-
Notifications
You must be signed in to change notification settings - Fork 56
Auth only CMP #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Auth only CMP #35
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,28 +60,38 @@ def encrypt_dynamodb_item(item, crypto_config): | |
crypto_config.materials_provider.refresh() | ||
encryption_materials = crypto_config.encryption_materials() | ||
|
||
# Add the attribute encryption mode to the inner material description | ||
# TODO: This is awkward...see if we can break this out any | ||
encryption_mode = MaterialDescriptionValues.CBC_PKCS5_ATTRIBUTE_ENCRYPTION.value | ||
inner_material_description = encryption_materials.material_description.copy() | ||
inner_material_description[ | ||
MaterialDescriptionKeys.ATTRIBUTE_ENCRYPTION_MODE.value | ||
] = encryption_mode | ||
try: | ||
encryption_materials.encryption_key | ||
except AttributeError: | ||
if crypto_config.attribute_actions.contains_action(CryptoAction.ENCRYPT_AND_SIGN): | ||
raise EncryptionError( | ||
'Attribute actions ask for some attributes to be encrypted but no encryption key is available' | ||
) | ||
|
||
encrypted_item = item.copy() | ||
else: | ||
# Add the attribute encryption mode to the inner material description | ||
# TODO: This is awkward...see if we can break this out any | ||
encryption_mode = MaterialDescriptionValues.CBC_PKCS5_ATTRIBUTE_ENCRYPTION.value | ||
inner_material_description[ | ||
MaterialDescriptionKeys.ATTRIBUTE_ENCRYPTION_MODE.value | ||
] = encryption_mode | ||
|
||
algorithm_descriptor = encryption_materials.encryption_key.algorithm + encryption_mode | ||
algorithm_descriptor = encryption_materials.encryption_key.algorithm + encryption_mode | ||
|
||
encrypted_item = {} | ||
for name, attribute in item.items(): | ||
if crypto_config.attribute_actions.action(name) is not CryptoAction.ENCRYPT_AND_SIGN: | ||
encrypted_item[name] = attribute.copy() | ||
continue | ||
encrypted_item = {} | ||
for name, attribute in item.items(): | ||
if crypto_config.attribute_actions.action(name) is not CryptoAction.ENCRYPT_AND_SIGN: | ||
encrypted_item[name] = attribute.copy() | ||
continue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if I'm reading this correctly, you can just use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fair enough; updating |
||
|
||
encrypted_item[name] = encrypt_attribute( | ||
attribute_name=name, | ||
attribute=attribute, | ||
encryption_key=encryption_materials.encryption_key, | ||
algorithm=algorithm_descriptor | ||
) | ||
encrypted_item[name] = encrypt_attribute( | ||
attribute_name=name, | ||
attribute=attribute, | ||
encryption_key=encryption_materials.encryption_key, | ||
algorithm=algorithm_descriptor | ||
) | ||
|
||
signature_attribute = sign_item(encrypted_item, encryption_materials.signing_key, crypto_config) | ||
encrypted_item[ReservedAttributes.SIGNATURE.value] = signature_attribute | ||
|
@@ -162,12 +172,22 @@ def decrypt_dynamodb_item(item, crypto_config): | |
|
||
decryption_materials = inner_crypto_config.decryption_materials() | ||
|
||
verify_item_signature(signature_attribute, item, decryption_materials.verification_key, inner_crypto_config) | ||
|
||
try: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I might also do this as a hasattr test as above. |
||
decryption_key = decryption_materials.decryption_key | ||
except AttributeError: | ||
if inner_crypto_config.attribute_actions.contains_action(CryptoAction.ENCRYPT_AND_SIGN): | ||
raise DecryptionError( | ||
'Attribute actions ask for some attributes to be decrypted but no decryption key is available' | ||
) | ||
|
||
return item.copy() | ||
|
||
decryption_mode = inner_crypto_config.encryption_context.material_description.get( | ||
MaterialDescriptionKeys.ATTRIBUTE_ENCRYPTION_MODE.value | ||
) | ||
algorithm_descriptor = decryption_materials.decryption_key.algorithm + decryption_mode | ||
|
||
verify_item_signature(signature_attribute, item, decryption_materials.verification_key, inner_crypto_config) | ||
algorithm_descriptor = decryption_key.algorithm + decryption_mode | ||
|
||
# Once the signature has been verified, actually decrypt the item attributes. | ||
decrypted_item = {} | ||
|
@@ -179,7 +199,7 @@ def decrypt_dynamodb_item(item, crypto_config): | |
decrypted_item[name] = decrypt_attribute( | ||
attribute_name=name, | ||
attribute=attribute, | ||
decryption_key=decryption_materials.decryption_key, | ||
decryption_key=decryption_key, | ||
algorithm=algorithm_descriptor | ||
) | ||
return decrypted_item | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Copyright 2018 Amazon.com, Inc. or its affiliates. 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. A copy of | ||
# the License is located at | ||
# | ||
# http://aws.amazon.com/apache2.0/ | ||
# | ||
# or in the "license" file accompanying this file. This file 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. | ||
"""Dummy stub to make linters work better.""" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Copyright 2018 Amazon.com, Inc. or its affiliates. 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. A copy of | ||
# the License is located at | ||
# | ||
# http://aws.amazon.com/apache2.0/ | ||
# | ||
# or in the "license" file accompanying this file. This file 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. | ||
"""Functional test suite for ``dynamodb_encryption_sdk.materials.raw``.""" | ||
import pytest | ||
|
||
from dynamodb_encryption_sdk.delegated_keys.jce import JceNameLocalDelegatedKey | ||
from dynamodb_encryption_sdk.materials.raw import RawDecryptionMaterials, RawEncryptionMaterials | ||
|
||
pytestmark = [pytest.mark.functional, pytest.mark.local] | ||
|
||
|
||
def test_no_encryption_key(): | ||
signing_key = JceNameLocalDelegatedKey.generate('HmacSHA512', 256) | ||
encryption_materials = RawEncryptionMaterials(signing_key=signing_key) | ||
|
||
with pytest.raises(AttributeError) as excinfo: | ||
encryption_materials.encryption_key | ||
|
||
excinfo.match('No encryption key available') | ||
|
||
|
||
def test_no_decryption_key(): | ||
verification_key = JceNameLocalDelegatedKey.generate('HmacSHA512', 256) | ||
decryption_materials = RawDecryptionMaterials(verification_key=verification_key) | ||
|
||
with pytest.raises(AttributeError) as excinfo: | ||
decryption_materials.decryption_key | ||
|
||
excinfo.match('No decryption key available') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be cleaner to do this with a conditional rather than a try/except/else clause, particularly because you aren't actually trying to do anything in the try clause. Start with:
if hasattr(encryption_materials, 'encryption_key'):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Asking for forgiveness instead of permission" is a general Python convention.
http://docs.quantifiedcode.com/python-anti-patterns/readability/asking_for_permission_instead_of_forgiveness_when_working_with_files.html
It's also generally faster, though that's not really going to have any affect here.