-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error when migrating existing fields into pgcrypto fields #246
Comments
Could it be because there is already previous unencrypted data in the column/field you are now trying to encrypt and it won't let you? If so, maybe one approach for a package PR would be to encrypt the entire column of previous unencrypted data then for every new row of data in that newly configured column, encrypt it normally? It may be that this package is only setup to encrypt only fresh new columns without any previous data in it. @fisle Are you using a docker volumes or bind mounts to store the data? Maybe share your docker setup. Run some inspect commands on the volumes/containers and share (or make an image and share the docker hub link). Can you also comment what is happening with your previous unencrypted data in the column you are trying to encrypt? Try setting it up with fresh blank data and report back what happens. Did you run into this error before you updated Django or is this only happening with the new updates? I agree that this is a big issue and should definitely be solved. It is essential for a site/database administrator to be able to make updates to their code especially if an auditor comes along and says your data is not compliant for security encryption standards. The previous data in the column should be encrypted also to protect all of those user's PII data. Another PR approach might be https://docs.djangoproject.com/en/3.0/howto/writing-migrations/ Or adding a new field, encrypting old data into it, and removing old field. Contributors @peterfarrell @Ian-Foote @meshy @adam-thomas @kevinetienne @maxpeterson @Minglee01 @U039b Any advice or insight? |
@9mido @fisle This library does not take care of encrypting unencrypted data in a field when you migrate the same field name. I suggest you create a new field with PGcrypto, write your own forwards migration to copy unencrypted data from field A -> B, delete the old field, and then rename the new field to what you want. |
@peterfarrell I was able to figure it out using the steps mentioned above. But is there a chance this feature will ever make its way to the library so we do not have to do the custom data migrations? Maybe this should go under the limitations section of the docs and provide an example of how to deal with this type of situation. |
@9mido I don't believe this will ever become part of the library as Django does not provide functionality to migrate from one field type to another type automatically. It would be impossible for the library to know how to reliably translate one field to a pgcrypto field and back. For example, change a models.CharField -> models.IntegerField would fail in Django if there was any data that couldn't be cast into integer by the database. However, a PR is welcome if you want to add to the documentation a section on how to migrate unencrypted pre-existing fields to encrypted fields using a |
Hey, thanks for looking at the issue. What I'm most puzzled with is that the migration fails even without any existing data in the database. Is this a 'feature' that was never supposed to be there? I guess I'm gonna have to do some dirty manual work to fix my existing migrations 🤔 |
tldr: everything quoted here is the same as #246 (comment) and #246 (comment) Hi @fisle and @9mido and thank you for taking the time to explain what you did. It looks like you're right and we might have had an unexpected feature! I would be careful however if you had a previous project where you were able to cast a field from a non encrypted to an encrypted as data integrity is not guaranteed. Migration were never really supported except to create/activate the pgcrypto extension in postgres. Migrating data is complicated as there might be a few things to consider such as:
The library has no way of doing all these guesses or to take all these decisions. I think it would be interesting to see if an extension like this exists or if there are any recommendations made available in the django documentations. From what you are describing I can suggest three ways to solve it:
No data is in the db
No data in the table
Migrating in a non-destructive way The goal here is to be able to use to legacy field if something goes wrong. 1st step:
2nd step:
|
@kevinetienne Thank you for the concise tl:dr summary. I have used it as a basis and updated the README documentation with a section on migrating existing unencrypted data to pgcrypto fields. |
Hey,
I am upgrading my Django application from 2.2.9 to Django 3.x and ran into errors on migrations that are converting normal fields (like CharField) into pgcrypto fields (CharPGPSymmetricKeyField in this case).
I'm not sure if this is a bug in Django, psycopg2 or django-pgcrypto-fields. I'm hoping you could assist me.
These migrations will work when running Django 2.2.9 but when I update Django package, they won't work any more.
I've managed to reproduce this in a fresh Django app.
I'm using Ubuntu 19.10, running Python 3.8.5 in a Docker container with the following packages:
django==3.0 (or any 3.x)
django-pgcrypto-fields==2.5.1
psycopg2==2.8.5
PostgreSQL is 11.1 and pgcrypto extension is installed.
Steps to reproduce:
Create migration:
python manage.py makemigrations
Modify model:
python manage.py makemigrations
python manage.py migrate
Traceback
If I was to create my model using pgcrypto fields from the start, it works - but the model in my application was converted later on in the development and now these migrations are in the production aswell.
If you feel like I haven't given enough details, feel free to ask and I will try to explain more.
Thanks in advance for any help.
The text was updated successfully, but these errors were encountered: