# Data source rotation

# Data source rotation

Procedures for changing a running data source without losing reports.

## Rotating the mailbox password

1. Change the password at the mailbox provider.
2. In Drillmark, open Settings, select the source, and update the password field.
3. Save. The frontend re-encrypts with the current `ENCRYPTION_KEY` and writes the new ciphertext to `data_sources.encrypted_password`.
4. The next incoming-queue run uses the new credential. Previous run's parsed records are unaffected.

If the source polls between step 1 and step 3, the run marks the source failed. It retries on the next scheduled fire with the new credential; no reports are lost because they are still sitting in the inbox folder.

## Rotating the OAuth refresh token

Same flow as password rotation, against the oauth-refresh-token field on the source.

## Rotating `ENCRYPTION_KEY`

There is no online primitive. Changing `ENCRYPTION_KEY` invalidates every `encrypted_password` and `oauth_refresh_token_enc` row:

1. Take a database backup (see [Backups](/docs/drillmark/operations/backups)).
2. Disable every source from Settings. Disabled sources are skipped by the incoming-queue job.
3. Stop the api, change the env var, restart.
4. For each source, re-enter the IMAP password (and OAuth refresh token if used). The new value is encrypted under the new key.
5. Re-enable each source.

There is no scheduled rotation requirement today; rotate only on compromise or a documented policy cycle.

## Moving a source between organizations

1. Open Settings, select the source.
2. Change the organization dropdown.
3. Save.

Parsed records keep their original organization attribution. Only new reports after the save land under the new organization. If you want historical records to follow, that is a database operation; back up first and reassign on the `data_sources.organization_id` column.

## Retiring a source cleanly

1. Disable the source. The incoming-queue job stops polling it; existing records remain visible.
2. Observe for one cycle to confirm no new reports arrive in the mailbox (e.g. the DMARC publishing DNS record no longer names this mailbox).
3. Delete the source from Settings. Parsed records remain in the database; the raw blob retention policy applies.

Deleting a source does not cascade to the parsed records. Dashboards continue to show the historical data; the sender attribution is unaffected.

## Replacing a source with a new credential provider

When moving a mailbox from password auth to OAuth (or between OAuth providers):

1. Add a new data source with the new credential. Configure its folders to the same `inbox` / `processed` / `failed` / `duplicate` names as the old source.
2. Verify the new source polls successfully (one incoming-queue run).
3. Disable the old source.
4. After a cycle, delete the old source.

This avoids a window where neither source is polling the mailbox.
