Skip to content

Conversation

@vgrozdanic
Copy link
Member

@vgrozdanic vgrozdanic commented Dec 29, 2025

Adds docs about usage of encryption fields at sentry.

it also includes a more in-detail section on how things work under the hood (for SRE and infra teams)

Closes TET-523: Docs for adding encrypted field to the model and TET-522: Docs for migrating existing fields to encrypted ones

@vercel
Copy link

vercel bot commented Dec 29, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
develop-docs Ready Ready Preview, Comment Jan 12, 2026 2:22pm
1 Skipped Deployment
Project Deployment Review Updated (UTC)
sentry-docs Ignored Ignored Preview Jan 12, 2026 2:22pm

@vgrozdanic vgrozdanic marked this pull request as ready for review January 12, 2026 09:52
@vgrozdanic vgrozdanic requested review from a team and oioki January 12, 2026 09:52
@linear
Copy link

linear bot commented Jan 12, 2026

- backend
- encryption
- django
sidebar_order: 10
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This puts the document at the top of Backend -> Application Domains - don't think we should be putting it there, since this will not be used by most people. Would rather put it after Metrics, which would be order 190.

Suggested change
sidebar_order: 10
sidebar_order: 190

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sense


### [EncryptedJSONField](#encryptedjsonfield)

A drop-in replacement for Django's `JSONField` that encrypts JSON data.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this replacement support the arguments that the original JSONField supports, especially special encoders & decoders?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, unfortunately it doesn't, i'll remove a term "drop-in" as it is not really a drop-in replacement.

@shellmayr
Copy link
Member

Reads well already! I think structurally, we can improve this by reordering a bit and grouping this slightly differently. It may pay off to have two sub-pages for using encrypted fields, and for all of the admin work around it:

User-facing:

  1. Explanation about what it does (already exists) and when to use it ("What to encrypt") - this will be most important for developers who have never heard of it
  2. Basic setup including querying
  3. Field types
  4. Troubleshooting
  5. How it works under the hood

Admin-related:

  1. Configuration
  2. Key Rotation
  3. Key Management

Comment on lines +65 to +75
secret = EncryptedCharField()
secret_hash = models.CharField(max_length=64, db_index=True)

def save(self, *args, **kwargs):
if self.secret:
self.secret_hash = hashlib.sha256(self.secret.encode()).hexdigest()
super().save(*args, **kwargs)

# Query by hash
MyModel.objects.filter(secret_hash=hashlib.sha256(b"my-value").hexdigest())
```

This comment was marked as outdated.

Comment on lines +40 to +50
# Using the model
creds = TempestCredentials.objects.create(
client_id="my-client",
client_secret="super-secret-value",
metadata={"api_version": "v2", "scopes": ["read", "write"]}
)

# Reading works transparently
print(creds.client_secret) # Prints: "super-secret-value"
print(creds.metadata) # Prints: {"api_version": "v2", "scopes": ["read", "write"]}
```

This comment was marked as outdated.

Comment on lines 110 to 122
### Adding New Encrypted Fields

Add the field to your model and generate a migration:

```python
class MyModel(models.Model):
api_key = EncryptedCharField(null=True, blank=True)
```

```bash
sentry django makemigrations
sentry upgrade
```
Copy link
Member

@shellmayr shellmayr Jan 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be the same code-field - currently this is rendered next to each other, most people will not see it:

Image

I think you need to move the text between the two for it to be rendered correctly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohh damn, nice catch

```python
from cryptography.fernet import Fernet

key = Fernet.generate_key()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any cryptographic options we should put here? Or are these all centrally "managed"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nothing, there are not special options for Fernet

Copy link
Member

@shellmayr shellmayr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great now from my perspective 👍

Comment on lines +164 to +174
class Migration(CheckedMigration):
is_post_deployment = True

dependencies = [
("myapp", "0002_alter_mymodel_api_key"),
]

operations = [
migrations.RunPython(encrypt_existing_data, migrations.RunPython.noop),
]
```
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The documentation code example for forcing encryption uses migrations.RunPython without importing the migrations module, which will cause a NameError during migration.
Severity: MEDIUM

🔍 Detailed Analysis

The code example provided in the encrypted fields documentation for creating a data migration is incomplete. It uses migrations.RunPython within the operations list, but it fails to import the migrations module. When a developer copies this code to create a migration file and runs it, the process will fail with a NameError: name 'migrations' is not defined. This will block the migration process, preventing developers from successfully following the documentation to encrypt fields.

💡 Suggested Fix

Add the line from django.db import migrations at the top of the code example to ensure the migrations module is available when the code is executed.

🤖 Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: develop-docs/backend/application-domains/encrypted-fields/index.mdx#L156-L174

Potential issue: The code example provided in the encrypted fields documentation for
creating a data migration is incomplete. It uses `migrations.RunPython` within the
`operations` list, but it fails to import the `migrations` module. When a developer
copies this code to create a migration file and runs it, the process will fail with a
`NameError: name 'migrations' is not defined`. This will block the migration process,
preventing developers from successfully following the documentation to encrypt fields.

Did we get this right? 👍 / 👎 to inform future reviews.
Reference ID: 8479019

@vgrozdanic vgrozdanic merged commit 5f3ccad into master Jan 12, 2026
14 checks passed
@vgrozdanic vgrozdanic deleted the vg/add-encrypted-fields-docs branch January 12, 2026 14:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants