Skip to main content

Mind Castle - Build a wall around your secrets

A universal store for your secret data. Don't delay securing you or your customer's data by deliberating over cloud secret stores. Mind Castle makes it easy to get started, and easy to switch between cloud secret stores.

Mind Castle currently supports:

  • AWS KMS
  • Local Encryption
  • None (passthrough - Mind Castle doesn't modify your data)

Architecture

Mind Castle comes in three parts:

  • A unified interface for several secret stores.
  • An SQLAlchemy column type, and a Django model field, that transparently store and retrieve secrets for you.
  • A migration tool to convert your existing DB column data into secrets.

Some other notes:

  • Mind Castle is configured and secret stores are initialised at import time. That means env-vars used for configuration need to be defined when Mind Castle is imported.
  • Mind Castle makes no attempt to manage secrets in memory. Memory management in Python is futile, and if you need that level of control it's best to use another language.

Install

pip install mind-castle

Configure

You can configure Mind Castle by setting environment variables for your chosen secret store. To see what configuration options are required for each store:

$ python -m mind_castle
╭────────────────────────────────────────── MIND CASTLE ───────────────────────────────────────────╮
│                                                                                                  │
│                                          Secret Stores                                           │
│ ┏━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ │
│ ┃ Store Type      ┃ Required env var                    ┃ Optional env var                     ┃ │
│ ┡━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │
│ │ localencryption │ MIND_CASTLE_LOCAL_ENCRYPTION_SECRET │                                      │ │
│ │                 │                                     │                                      │ │
│ │ awskms          │ MIND_CASTLE_AWS_REGION              │ MIND_CASTLE_AWS_KMS_DATAKEY_CACHE    │ │
│ │                 │ MIND_CASTLE_AWS_ACCESS_KEY_ID       │ MIND_CASTLE_AWS_KMS_DATAKEY_CACHE_T… │ │
│ │                 │ MIND_CASTLE_AWS_SECRET_ACCESS_KEY   │ MIND_CASTLE_AWS_KMS_DATAKEY_CACHE_C… │ │
│ │                 │ MIND_CASTLE_AWS_KMS_KEY_ARN         │                                      │ │
│ │                 │ --- OR ---                          │                                      │ │
│ │                 │ MIND_CASTLE_AWS_REGION              │ MIND_CASTLE_AWS_KMS_DATAKEY_CACHE    │ │
│ │                 │ MIND_CASTLE_AWS_USE_ENV_AUTH        │ MIND_CASTLE_AWS_KMS_DATAKEY_CACHE_T… │ │
│ │                 │ MIND_CASTLE_AWS_KMS_KEY_ARN         │ MIND_CASTLE_AWS_KMS_DATAKEY_CACHE_C… │ │
│ │                 │                                     │                                      │ │
│ └─────────────────┴─────────────────────────────────────┴──────────────────────────────────────┘ │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯

Mind Castle warns about missing configuration only when some required variables are present and no complete configuration alternative is available. For awskms, setting MIND_CASTLE_AWS_REGION, MIND_CASTLE_AWS_USE_ENV_AUTH, and MIND_CASTLE_AWS_KMS_KEY_ARN enables environment authentication without a warning about missing static AWS credentials. A missing KMS key ARN still warns and leaves the store unconfigured.

"none" is a fourth store type you can pass to a column or field. It is a passthrough handled by the column type itself rather than a store, so it takes no configuration and does not appear in that table. Anything else raises ValueError: Trying to put secret to unknown/unconfigured store '<name>' the first time you save.

AWS KMS data-key cache (optional)

The awskms store uses envelope encryption: each secret is sealed with its own data key, which KMS must decrypt on every read. When the same secret is read repeatedly (e.g. frequent full re-fetches), this drives repeated KMS Decrypt calls. You can opt in to caching the decrypted data keys in-process:

Env var Description Default
MIND_CASTLE_AWS_KMS_DATAKEY_CACHE Enable the cache (true/1/yes) off
MIND_CASTLE_AWS_KMS_DATAKEY_CACHE_TTL Cache entry TTL, seconds 60
MIND_CASTLE_AWS_KMS_DATAKEY_CACHE_CAPACITY Max cached data keys 1000

Notes:

  • Caching applies to decryption only; create_secret still generates a fresh data key per secret. Legacy non-envelope secrets are never cached.
  • A revoked/rotated KMS key keeps working from cache until the TTL expires — keep the TTL short. Invalid or non-positive TTL/capacity disables the cache.
  • The cache is per-process (each worker has its own) and holds decrypted data keys in memory (up to CAPACITY, until TTL). Config is read at startup; changing it requires a restart.

Use

Migrating Existing Data

A migration tool is provided to move your existing data into a secret store. The tool assumes you have a database that can be connected to using SQLAlchemy and create_engine(<db_uri>).

You will need to configure your selected secret store using environment variables as described above. The migration tool depends on iterfzf, which isn't installed by the base package - install it with pip install "mind-castle[migrate]".

You can see what other options the migration tool accepts with:

$ python migration_tool.py --help

 Usage: migration_tool.py [OPTIONS] DB_URI

╭─ Arguments ─────────────────────────────────────────────────────────────────────────╮
│ *    db_uri      TEXT  [default: None] [required]                                   │
╰─────────────────────────────────────────────────────────────────────────────────────╯
╭─ Options ───────────────────────────────────────────────────────────────────────────╮
│ --target-table                        TEXT  [default: None]                         │
│ --target-column                       TEXT  [default: None]                         │
│ --dry-run           --no-dry-run            [default: dry-run]                      │
│ --demigrate         --no-demigrate          [default: no-demigrate]                 │
│ --to-secret-type                      TEXT  [default: json]                         │
│ --help                                      Show this message and exit.             │
╰─────────────────────────────────────────────────────────────────────────────────────╯

The easiest thing to do is specify --to-secret-type and let the tool guide you through the rest. e.g.:

$ MIND_CASTLE_LOCAL_ENCRYPTION_SECRET="<your_base64_key>" python migration_tool.py "postgresql://<user>:<pass>@<host>:5432/<database>" --to-secret-type localencryption

The tool will list tables and columns in the selected DB for you, and ask you to select each. Once a dry run is complete and you also have a backup of your database, you can add --no-dry-run to migrate the data for real.

SQLAlchemy Model

Once any existing data is migrated, you can update your SQLAlchemy model to include a SecretData column:

from mind_castle.sqlalchemy_type import SecretData

class MyDBModel(Base):
    name = Column(String, nullable=False)
    created_at = Column(DateTime, default=datetime.datetime.now)
    secret_data = Column(SecretData("localencryption"))

Your secrets will then be safely stored in AWS (or encrypted locally, or anywhere else you like)! The storage and retrieval of secrets will be completely transparent to your application.

Django Model

For a Django model, use mind_castle.django_type.SecretData instead - it's a JSONField subclass with the same encrypt/decrypt behavior. Install the django extra (pip install "mind-castle[django]") first.

from mind_castle.django_type import SecretData

class MyDBModel(models.Model):
    name = models.CharField(max_length=255)
    created_at = models.DateTimeField(auto_now_add=True)
    secret_data = SecretData("localencryption", null=True)

Notes specific to the Django field:

  • Filtering or looking up by value (MyDBModel.objects.filter(secret_data=...), secret_data__somekey=...) raises FieldError on any store except "none" - the stored value is ciphertext with a fresh nonce per encryption, so a value-equality comparison could otherwise silently match nothing. Null checks still work, written either way: filter(secret_data=None) and filter(secret_data__isnull=True).
  • Admin and ModelForm rendering show the decrypted value, matching a normal JSONField's editable UX. dumpdata/serializers.serialize() always export the encrypted envelope, and loaddata reads that envelope back without re-encrypting it, so a fixture round-trips.

How values are compared, changed and moved around

These three rules apply to both the SQLAlchemy and the Django field types.

Changing a value

Reassigning the field and editing it in place both work:

obj.secret_data = {**obj.secret_data, "key": "new"}   # reassign
obj.secret_data["key"] = "new"                        # edit in place

A decrypted value is handed back inside a wrapper that keeps a copy of what it decrypted, so an edit to the contents is still noticed and the value is re-encrypted on save.

One caveat on SQLAlchemy: editing in place does not mark the attribute dirty, which is a general SQLAlchemy behaviour and not specific to this type. Nothing is written unless something else on that row changes, or you call flag_modified(obj, "secret_data"). Django writes every field on save(), so it needs nothing extra.

Pickling

Pickling a decrypted value, or a whole model instance holding one, writes the encrypted envelope into the pickle and decrypts it again on load. That is what lets an instance cross a process or network boundary (Django's cache framework, a Celery task argument, multiprocessing) without the plaintext travelling. The loading process needs the matching store configured or the load raises. Pickling a value that was edited since it was decrypted also raises, because the envelope would still hold the old one.

What counts as an encrypted value

On any store except "none", a stored dict carrying both a mind_castle_secret_type and an encrypted_value key is treated as ciphertext, and one that does not is passed through as plaintext. That single rule is used everywhere a value crosses a boundary: reading a column, loading a fixture, unpickling. A dict that claims to be an envelope but does not parse as one is a corrupt row and raises, rather than being handed back as if it were your data.

Releasing

Update version in pyproject.toml, then run uv lock to update the local package version in uv.lock. Run the test suite on the Python versions in CI and build the release artifacts:

uv run --frozen --python 3.10 pytest -v
uv run --frozen --python 3.12 pytest -v
uv build

After the change merges to main, publish a GitHub Release with a tag matching the package version. The Publish on PyPI workflow builds and publishes that release. Confirm the workflow succeeds and the version is available on PyPI before updating a consuming application's dependency pin.

TODO

  • Make migration script work for non-json columns
  • Support deleting secrets when row is deleted
  • Implement prefixes/folders for secrets
  • Explain how secrets are stored
  • Enforce tests on PR / branch protections
  • Wire secret_key_prefix through to the stores, or drop it from the signature

Metadata

Release files for mind-castle 0.7.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mind-castle 0.7.2
File Size Uploaded Provenance Actions
mind_castle-0.7.2.tar.gz 133.1 kB Without provenance Details for mind_castle-0.7.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mind-castle 0.7.2
File Interpreter ABI Platform Actions
mind_castle-0.7.2-py3-none-any.whl Python 3 none any Details for mind_castle-0.7.2-py3-none-any.whl

Total release size: 168.1 kB

Release files / mind_castle-0.7.2.tar.gz

Download URL mind_castle-0.7.2.tar.gz
Size 133.1 kB
Tags Source
SHA-256 checksum
How to use checksums
11f0feea226ceb6038da80cd12e16e951d6ef67b45a58898cb9a2458201c8cfd
BLAKE2b-256 checksum
How to use checksums
37bd349c1b4e1f8a7c8f589bea19d85b70c4a022037660fd2c39412bf2f83a7d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.4 {"installer":{"name":"uv","version":"0.13.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / mind_castle-0.7.2-py3-none-any.whl

Download URL mind_castle-0.7.2-py3-none-any.whl
Size 35.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
529f756239e55ef601654002a09d543c6316b42bf679643338180c4f4999a523
BLAKE2b-256 checksum
How to use checksums
f781280a36456e00f2688b71904381f0977c431bfcedb46017d0122869739465
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.4 {"installer":{"name":"uv","version":"0.13.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for mind-castle 0.7.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.7.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page