Skip to main content

Facade that handles encryption and decryption of dictionaries using a private key from AWS secret store.

Project description

DataHider

A small Facade over Fernet and Boto3.

What for

  • Turn data into a string that is
    • Encrypted, meaning it can't be tampered with
    • URL safe base 64, meaning you can use it anywhere, anyway
      • It may still have ending =s
  • Decode above such strings back into data.
  • Without having to manually bother with encryption keys.

"Data"?

Anything accepted by json.loads. Which means any composition according to this recursive definition:

Data is

  • Dictionary of str -> Data
  • List of Data
  • Tuple of Data
  • Bool
  • Number
  • String
  • None

How not to use

Pittfall 1: AWS marks secrets for deletion and locks them

Creating a key and then deleting it results in the keyname being "locked" by AWS:

botocore.errorfactory.InvalidRequestException: An error occurred (InvalidRequestException) when calling the GetSecretValue operation: You can't perform this operation on the secret because it was marked for deletion.

AWS Secrets Manager Secrets have a default retention (recovery window) period of 30 days after deletion to give you the chance to recover it - in case it was deleted by mistake or you change your mind. So even if it shows on the AWS console as deleted, it still exists in AWS's API records.

Pittfall 2: private keys are cached

So maybe you set the secret retention time to 0.

If you do that there's something else you need to take into account.

Each DataHider object caches the encryption key. This presents susceptibility to the following scenario:

  • make instance A with keyname N (which will generate a new key K)
  • make instance B with keyname N
  • delete the key using B
    • A will still encrypt and decrypt without errors as the private key is cached. But any other instance will not be able to decrypt what comes out of A.
  • make instance C with keyname N (which will generate a new key L)
    • A will "speak a different language" compared to instance C:
    • A uses K, but C uses L, despite both agreeing on the key name N.

In this scenario, data loss occurs when messages encrypted by A are attempted to be decrypted by C, and vice versa.

How to use

You need to be logged in on AWS. The encryption key is stored in AWS secrets manager.

Example 1: Creating an instance of DataHider

If the key already exists, it will use the existing key. If not, it will generate a new key.

from datahider import DataHider

data_hider_instance = DataHider(keyname="your_key_name")

Example 2: Encrypting and Base64 Encoding Data

data_to_encrypt = {"user": "John Doe", "age": 25, "city": "Example City"}
encrypted_data_base64 = data_hider_instance.encrypt_and_base64(data_to_encrypt)
print(f"Encrypted and Base64 encoded data: {encrypted_data_base64}")

Example 3: Decrypting Base64 Encoded Data

decrypted_data = data_hider_instance.decrypt_base64_blob(encrypted_data_base64)
print(f"Decrypted data: {decrypted_data}")

Example 4: Removing the Key (Use with caution!)

This will delete the key from Secrets Manager. You will lose access to data encrypted with this key.

remover = data_hider_instance.dangerous_remove_key_you_will_lose_data()

Note: Make sure to handle exceptions appropriately in a production environment.

DataHider does not catch exceptions it can't handle. It does not define new exception classes.

Copyright

This code is the intellectual property of Nova Technology B.V.

Unauthorized reproduction is prohibited.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

datahider-0.1.1.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

datahider-0.1.1-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file datahider-0.1.1.tar.gz.

File metadata

  • Download URL: datahider-0.1.1.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.4 Linux/6.2.0-39-generic

File hashes

Hashes for datahider-0.1.1.tar.gz
Algorithm Hash digest
SHA256 150f27c8836524cb0434bef1a92e5cfecd2a708dcb55878da7ee47328b2ed673
MD5 342c5c1ba0c764d4df399355611e2b18
BLAKE2b-256 ff3e2d2a5cc63393867fe34139f19b8e41d97489ffb29cdd13140e8c22dc5fea

See more details on using hashes here.

File details

Details for the file datahider-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: datahider-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.4 Linux/6.2.0-39-generic

File hashes

Hashes for datahider-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2d8c8e3801260a69562de0bed9d9b2bf09afe22dab4b5bd7586c69bab56982fe
MD5 e88907c76a0af8011353934150c13b97
BLAKE2b-256 c0dc2e2162f20f75c65ffbeed11a8728eb669a41c37092cc9ab1b386e5c40fe8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page