forked from pulp/pulp_ansible
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch token attribute to be encryped and hidden
fixes pulp#1221
- Loading branch information
Showing
5 changed files
with
74 additions
and
2 deletions.
There are no files selected for viewing
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,2 @@ | ||
Switched the attribute `token` on `CollectionRemotes` to be encrypted in the database and not to | ||
be exposed in the API. |
67 changes: 67 additions & 0 deletions
67
pulp_ansible/app/migrations/0044_alter_collectionremote_token.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,67 @@ | ||
# Generated by Django 3.2.15 on 2022-08-29 11:04 | ||
|
||
from django.db import migrations | ||
import pulpcore.app.models.fields | ||
|
||
|
||
def encrypt_remote_fields(apps, schema_editor): | ||
CollectionRemote = apps.get_model("ansible", "CollectionRemote") | ||
|
||
remotes_needing_update = [] | ||
for remote in CollectionRemote.objects.order_by("pulp_id").iterator(): | ||
remote._encrypted_token = remote.token | ||
remotes_needing_update.append(remote) | ||
|
||
if len(remotes_needing_update) > 100: | ||
CollectionRemote.objects.bulk_update(remotes_needing_update, ("_encrypted_token",)) | ||
remotes_needing_update.clear() | ||
|
||
CollectionRemote.objects.bulk_update(remotes_needing_update, ("_encrypted_token",)) | ||
|
||
|
||
def unencrypt_remote_fields(apps, schema_editor): | ||
CollectionRemote = apps.get_model("ansible", "CollectionRemote") | ||
|
||
remotes_needing_update = [] | ||
for remote in CollectionRemote.objects.order_by("pulp_id").iterator(): | ||
remote.token = remote._encrypted_token | ||
remotes_needing_update.append(remote) | ||
|
||
if len(remotes_needing_update) > 100: | ||
CollectionRemote.objects.bulk_update(remotes_needing_update, ("token",)) | ||
remotes_needing_update.clear() | ||
|
||
CollectionRemote.objects.bulk_update(remotes_needing_update, ("token",)) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('ansible', '0043_alter_collectionversionsignature_data'), | ||
] | ||
|
||
operations = [ | ||
# Add new fields to temporarily hold the encrypted values | ||
migrations.AddField( | ||
model_name="collectionremote", | ||
name="_encrypted_token", | ||
field=pulpcore.app.models.fields.EncryptedTextField(null=True), | ||
), | ||
# Populate the new fields with encrypted values computed from the unencrypted fields | ||
migrations.RunPython( | ||
code=encrypt_remote_fields, | ||
reverse_code=unencrypt_remote_fields, | ||
elidable=True, | ||
), | ||
# Remove the unencrypted columns | ||
migrations.RemoveField( | ||
model_name="collectionremote", | ||
name="token", | ||
), | ||
# Replace the formerly-unencrypted columns with the new encrypted ones | ||
migrations.RenameField( | ||
model_name="collectionremote", | ||
old_name="_encrypted_token", | ||
new_name="token", | ||
), | ||
] |
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
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
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