|
| 1 | +# Queryable Encryption helper classes and settings |
| 2 | + |
| 3 | +import os |
| 4 | + |
| 5 | +from .fields import has_encrypted_fields |
| 6 | + |
| 7 | +KMS_CREDENTIALS = { |
| 8 | + "aws": { |
| 9 | + "key": os.getenv("AWS_KEY_ARN", ""), |
| 10 | + "region": os.getenv("AWS_KEY_REGION", ""), |
| 11 | + }, |
| 12 | + "azure": { |
| 13 | + "keyName": os.getenv("AZURE_KEY_NAME", ""), |
| 14 | + "keyVaultEndpoint": os.getenv("AZURE_KEY_VAULT_ENDPOINT", ""), |
| 15 | + }, |
| 16 | + "gcp": { |
| 17 | + "projectId": os.getenv("GCP_PROJECT_ID", ""), |
| 18 | + "location": os.getenv("GCP_LOCATION", ""), |
| 19 | + "keyRing": os.getenv("GCP_KEY_RING", ""), |
| 20 | + "keyName": os.getenv("GCP_KEY_NAME", ""), |
| 21 | + }, |
| 22 | + "kmip": {}, |
| 23 | + "local": {}, |
| 24 | +} |
| 25 | + |
| 26 | +KMS_PROVIDERS = { |
| 27 | + "aws": {}, |
| 28 | + "azure": {}, |
| 29 | + "gcp": {}, |
| 30 | + "kmip": { |
| 31 | + "endpoint": os.getenv("KMIP_KMS_ENDPOINT", "not a valid endpoint"), |
| 32 | + }, |
| 33 | + "local": { |
| 34 | + "key": bytes.fromhex( |
| 35 | + "000102030405060708090a0b0c0d0e0f" |
| 36 | + "101112131415161718191a1b1c1d1e1f" |
| 37 | + "202122232425262728292a2b2c2d2e2f" |
| 38 | + "303132333435363738393a3b3c3d3e3f" |
| 39 | + "404142434445464748494a4b4c4d4e4f" |
| 40 | + "505152535455565758595a5b5c5d5e5f" |
| 41 | + ) |
| 42 | + }, |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +class EncryptedRouter: |
| 47 | + def allow_migrate(self, db, app_label, model_name=None, model=None, **hints): |
| 48 | + if model: |
| 49 | + return db == ("other" if has_encrypted_fields(model) else "default") |
| 50 | + return db == "default" |
| 51 | + |
| 52 | + def db_for_read(self, model, **hints): |
| 53 | + if has_encrypted_fields(model): |
| 54 | + return "other" |
| 55 | + return "default" |
| 56 | + |
| 57 | + db_for_write = db_for_read |
| 58 | + |
| 59 | + def kms_provider(self, model): |
| 60 | + return "local" |
| 61 | + |
| 62 | + |
| 63 | +class EqualityQuery(dict): |
| 64 | + def __init__(self, *, contention=None): |
| 65 | + super().__init__(queryType="equality") |
| 66 | + if contention is not None: |
| 67 | + self["contention"] = contention |
| 68 | + |
| 69 | + |
| 70 | +class RangeQuery(dict): |
| 71 | + def __init__( |
| 72 | + self, *, contention=None, max=None, min=None, precision=None, sparsity=None, trimFactor=None |
| 73 | + ): |
| 74 | + super().__init__(queryType="range") |
| 75 | + options = { |
| 76 | + "contention": contention, |
| 77 | + "max": max, |
| 78 | + "min": min, |
| 79 | + "precision": precision, |
| 80 | + "sparsity": sparsity, |
| 81 | + "trimFactor": trimFactor, |
| 82 | + } |
| 83 | + self.update({k: v for k, v in options.items() if v is not None}) |
0 commit comments