django-cryptography icon indicating copy to clipboard operation
django-cryptography copied to clipboard

Support for rotation of secrets

Open StephanErb opened this issue 5 years ago • 5 comments

Hi everyone,

we accidentally leaked the SECRET_KEY of a test environment that was used for decrypting model fields using this library. In such a case, to be on the safe side, one has to rotate the leaked key. While I am aware that we can run a migration similar to this one here it would be great if django-cryptography would has an officially supported way how to rotate encryption keys:

Proposal A) Ship a Django command to perform such a secret rotation:

$ django-admin rotate_encryption_key <current secret key> <new secret key> 

Afterwards the settings.py needs to be adjusted to reference the new key.

Proposal B) If Proposal A is hard to implement, an easier but less secure alternative option would be to support two kinds of secret specifications:

  • a secret used for encryption (i.e. SECRET_KEY or CRYPTOGRAPHY_KEY as used today)
  • a list of secrets used for decryption (e.g. CRYPTOGRAPHY_PREVIOUS_KEYS=['foo', 'bar'])

In case a field cannot be decrypted using either SECRET_KEY or CRYPTOGRAPHY_KEY all secrets in CRYPTOGRAPHY_PREVIOUS_KEYS are tried in order. This would enable us to at lest have all new secrets encrypted with a new key, while the encryption of other fields is still supported.

Thanks!

StephanErb avatar Mar 24 '20 18:03 StephanErb

I would think that Proposal A is the easiest to adopt!

thismatters avatar Apr 12 '21 20:04 thismatters

I would love to see this integrated :)

rj76 avatar Apr 25 '21 06:04 rj76

@StephanErb It would seem reasonable to also support changing the salt. What do you think?

thismatters avatar Apr 27 '21 15:04 thismatters

Has there been any progress on this feature? Or supporting multiple keys for decryption?

zaeem-maqsood avatar Sep 22 '21 14:09 zaeem-maqsood

@zaeem-maqsood I don't think a PR has been put forward for rotating keys.

thismatters avatar Sep 29 '21 14:09 thismatters