-
-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: for reversion, filter only for value that have been migrate…
…d, indicated by the 'aes-cbc' prefix
- Loading branch information
Showing
2 changed files
with
58 additions
and
8 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
corehq/apps/geospatial/migrations/0009_geoconfig_api_key_cbc_encryption.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from django.db import migrations | ||
from django.db.migrations import RunPython | ||
|
||
from corehq.util.django_migrations import skip_on_fresh_install | ||
from corehq.motech.const import ALGO_AES, ALGO_AES_CBC | ||
from corehq.motech.utils import ( | ||
reencrypt_ecb_to_cbc_mode, | ||
reencrypt_cbc_to_ecb_mode, | ||
b64_aes_cbc_encrypt, | ||
) | ||
|
||
|
||
@skip_on_fresh_install | ||
def reencrypt_api_keys(apps, schema_editor): | ||
GeoConfig = apps.get_model('geospatial', 'GeoConfig') | ||
|
||
geo_configs_to_update = GeoConfig.objects.exclude( | ||
api_token__startswith=f'${ALGO_AES_CBC}$' | ||
).exclude(api_token=None) | ||
|
||
for config in geo_configs_to_update: | ||
if config.api_token.startswith(f'${ALGO_AES}$'): | ||
config.api_token = reencrypt_ecb_to_cbc_mode(config.api_token, | ||
f'${ALGO_AES}$') | ||
else: | ||
ciphertext = b64_aes_cbc_encrypt(config.api_token) | ||
config.api_token = f'${ALGO_AES_CBC}${ciphertext}' | ||
config.save() | ||
|
||
|
||
def reversion_api_keys(apps, schema_editor): | ||
GeoConfig = apps.get_model('geospatial', 'GeoConfig') | ||
|
||
geo_configs_to_revert = GeoConfig.objects.filter( | ||
api_token__startswith=f'${ALGO_AES_CBC}$' | ||
) | ||
|
||
for config in geo_configs_to_revert: | ||
config.api_token = reencrypt_cbc_to_ecb_mode(config.api_token, | ||
f'${ALGO_AES_CBC}$') | ||
config.save() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('geospatial', '0008_geoconfig_flag_assigned_cases'), | ||
] | ||
|
||
operations = [ | ||
RunPython(reencrypt_api_keys, reverse_code=reversion_api_keys), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters