Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_bytearray_input(self):
data = Binary(bytearray([1]))
self.assertEqual(b'\x01', data)
self.assertEqual(b'\x01', data.value)
def test_integer_throws_error(self):
with self.assertRaises(TypeError):
Binary(1)
def _many_items():
values = ("a string", 1234, Binary(b"binary \x00\x88 value"))
partition_keys = (("partition_key", value) for value in values)
sort_keys = (("sort_key", value) for value in values)
for pairs in itertools.product(partition_keys, sort_keys):
item = dict(pairs)
yield pytest.param(item, id=str(item))
def test_not_equal(self):
self.assertTrue(Binary(b'\x01') != b'\x02')
def test_non_ascii_bytes_input(self):
# Binary data that is out of ASCII range
data = Binary(b'\x88')
self.assertEqual(b'\x88', data)
self.assertEqual(b'\x88', data.value)
def test_unicode_throws_error(self):
with self.assertRaises(TypeError):
Binary(u'\u00e9')
def encrypt_item(table_name, rsa_wrapping_private_key_bytes, rsa_signing_private_key_bytes):
"""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) # generated code confuse pylint: disable=no-member
# Create a crypto materials provider using the provided wrapping and signing keys.
# We show private keys used here, but public keys could be used as well, allowing
# only wrapping or signature verification.
def _transform_binary_value(value):
# (bytes) -> bytes
"""Transforms a serialized binary value.
:param bytes value: Raw deserialized value
:rtype: bytes
"""
if isinstance(value, Binary):
return value.value
return value
def create_user(stage):
table_name = get_table_name(stage)
table = boto3.resource('dynamodb').Table(table_name)
username = raw_input('Username: ').strip()
password = getpass.getpass('Password: ').strip()
password_fields = encode_password(password)
item = {
'username': username,
'hash': password_fields['hash'],
'salt': Binary(password_fields['salt']),
'rounds': password_fields['rounds'],
'hashed': Binary(password_fields['hashed']),
}
table.put_item(Item=item)