|
5 | 5 |
|
6 | 6 | from bson.binary import STANDARD
|
7 | 7 | from bson.codec_options import CodecOptions
|
| 8 | +from django.conf import settings |
8 | 9 | from pymongo.encryption import AutoEncryptionOpts, ClientEncryption
|
9 | 10 |
|
| 11 | +# Default settings for MongoDB Client-Side Field Level Encryption (CSFLE) |
| 12 | +# which can be imported into user settings and customized as needed. E.g. |
| 13 | +# |
| 14 | +# import os |
| 15 | +# from django_mongodb_backend import encryption, parse_uri |
| 16 | +# KEY_VAULT_NAMESPACE = encryption.get_key_vault_namespace() |
| 17 | +# KMS_PROVIDERS = encryption.get_kms_providers() |
| 18 | +# KMS_PROVIDER = encryption.KMS_PROVIDER |
| 19 | +# AUTO_ENCRYPTION_OPTS = encryption.get_auto_encryption_opts( |
| 20 | +# key_vault_namespace=KEY_VAULT_NAMESPACE, |
| 21 | +# kms_providers=KMS_PROVIDERS, |
| 22 | +# ) |
| 23 | +# ENCRYPTED_DATABASE_NAME = encryption.ENCRYPTED_DATABASE_NAME |
| 24 | +# ENCRYPTED_APPS = encryption.ENCRYPTED_APPS |
| 25 | +# DATABASE_URL = os.environ.get("MONGODB_URI", "mongodb://localhost:27017") |
| 26 | +# DATABASES = { |
| 27 | +# "default": parse_uri( |
| 28 | +# DATABASE_URL, |
| 29 | +# db_name="test", |
| 30 | +# ), |
| 31 | +# ENCRYPTED_DATABASE_NAME: parse_uri( |
| 32 | +# DATABASE_URL, |
| 33 | +# options={"auto_encryption_opts": AUTO_ENCRYPTION_OPTS}, |
| 34 | +# db_name=ENCRYPTED_DATABASE_NAME, |
| 35 | +# ), |
| 36 | +# } |
| 37 | +# DATABASE_ROUTERS = [encryption.EncryptedRouter()] |
| 38 | + |
10 | 39 | KEY_VAULT_DATABASE_NAME = "keyvault"
|
11 | 40 | KEY_VAULT_COLLECTION_NAME = "__keyVault"
|
12 | 41 | KMS_PROVIDER = "local" # e.g., "aws", "azure", "gcp", "kmip", or "local"
|
| 42 | +ENCRYPTED_DATABASE_NAME = "encrypted" |
| 43 | +ENCRYPTED_APPS = ["encryption_"] |
| 44 | + |
| 45 | + |
| 46 | +class EncryptedRouter: |
| 47 | + def allow_migrate(self, db, app_label, model_name=None, **hints): |
| 48 | + if db == settings.ENCRYPTED_DATABASE_NAME and app_label not in settings.ENCRYPTED_APPS: |
| 49 | + return False |
| 50 | + return None |
13 | 51 |
|
14 | 52 |
|
15 | 53 | class EqualityQuery:
|
|
0 commit comments