Current section
Files
Jump to
Current section
Files
secret_mana
README.md
README.md
<!-- @format -->
# SecretMana
A collection of mix tasks to interact with [age](https://github.com/FiloSottile/age).
Inspired by rails credential management. Born from frustration of desync secrets.
Thanks to [@FiloSottile](https://github.com/FiloSottile/age) for building age <3.
And thanks to the maintainers of [phoenixframework/tailwind](https://github.com/phoenixframework/tailwind/) which I leaned on heavily while implementing this <3.
## Disclaimer
This isn't currently well tested (only on mac). I will try to upgrade this as soon as I am able to test on different platforms.
## Installation
```elixir
def deps do
[
{:secret_mana, "~> 0.3.0"}
]
end
```
## Usage
### Configuration
```
config :secret_mana,
backend: SecretMana.AgeBackend,
otp_app: :my_app
config :secret_mana, SecretMana.AgeBackend,
version: "1.2.1" # default: "1.2.1", used to specify version installed
local_install: true/false # default: "true"; if false installation is always skipped. Then you can either manually set `bin_dir` or let the backend handle the finding of the binaries (by using which/where).
Useful to bundle binaries in deployments
bin_dir: "my_bin_path" # see `local_install`
secret_base_path: "config/custom_secrets_folder" # default: "secrets"; path SecretMana will put files in, useful to scope for different environments
key_file: "my.key" # default: "age.key"; used to rename key-file; stored under base_path
pub_key_file: "my.key.pub" # default: "age.pub"; used to rename pub-key-file; stored under base_path
encrypted_file: "secret.enc" # default: "age.enc"; used to rename secret-file; stored under base_path
file_type: :yaml # default: :json; currently only json and yaml are supported,
binary: "age" # default: "age", only required if binary name differs from default
key_generator_binary: "age-keygen" # default: "age-keygen", only required if keygen-binary name differs from default,
key_env: "SECRET_MANA_PRIVATE_KEY" # Unix only. Default: nil. Name of the variable that holds the private key.
files: ["services/*/secrets/{env}.age"] # default: []; globs of more encrypted files that use the key pair of the environment, relative to the directory of mix.exs. The release step copies them with their relative path.
```
### The private key in a release
Set `key_env` to the name of the variable, not to the key:
```
# config/prod.exs
config :secret_mana, SecretMana.AgeBackend, key_env: "SECRET_MANA_PRIVATE_KEY"
```
Put this in a build-time file such as `config/prod.exs`. A `read!` in `runtime.exs` does not see configuration from the same `runtime.exs`, because `Config.Reader` applies it only after the file returns. The key itself comes from the runtime environment of the release.
On Unix, SecretMana passes the key to age through standard input. On Windows, use the private key file. Decryption raises an error if the named variable is set on Windows. On both platforms, an unset variable selects the key file and an empty variable raises an error.
### Mix Tasks
`mix secret_mana.install` - install
`mix secret_mana.gen.key <env>` - generates a new key pair depending on your config
`mix secret_mana.encrypt <env> <file>` - encrypts a given json/yaml file and stores it accordingly to your config
`EDITOR="code --wait" mix secret_mana.edit <env>` - allows editing your secrets (falls back to `vim`)
`mix secret_mana.rekey <env>` - replaces the key pair of `<env>` and encrypts the configured file and all `files` again. It changes nothing when it cannot decrypt a file, and keeps the old and the new key on disk until all files use the new key.
`edit` and `encrypt` accept `--file <path>`. The task then uses this encrypted file instead of the configured one, with the key pair of `<env>`.
When the file does not exist, `edit` starts with an empty document and creates the file when you save a change.
### Writing secrets from code
```
config = SecretMana.Config.new(:prod, file: "services/billing/secrets/prod.age")
:ok = SecretMana.encrypt_data(config, %{"token" => nil})
```
`encrypt_data/2` needs only the public key. It writes JSON for both file types, because JSON is valid YAML.
### Reading your secrets
Its as simple as this:
```
# read all secrets
SecretMana.read!()
# read specific secret
SecretMana.read!(["foo", "bar"])
# read from another file that uses the key pair of the environment
SecretMana.read!(["token"], file: "services/billing/secrets/prod.age")
# read a whole other file
SecretMana.read!(file: "services/billing/secrets/prod.age")
```
A relative `file` is relative to the directory of `mix.exs` during development. In a release, it is relative to `config/secrets` of the application. `file` must not point to a key file.
`copy_secrets_for_release` copies `secrets/<env>/` and the files that match the `files` globs. Each file from a glob keeps its path relative to the directory of `mix.exs`, under `lib/<app>-<version>/config/secrets/`. So the same `read!(…, file: relative_path)` works in development and in the release. This also applies to a glob match inside `secrets/<env>/`, which the release then has at both paths.
Choose project files for your globs. The release step copies matches as supplied, after the environment directory, so a glob match wins if two inputs share a destination.
By default, supply the private key at runtime. To embed the private key file from disk, call `SecretMana.copy_secrets_for_release(release, true)`. The `key_env` setting only affects decryption.
Note: `read!` will raise an error if the requested key is not found, with helpful information about available keys.
## License
Copyright (c) 2025 Nick Duitz.
age source code is licensed under the [BSD 3-Clause "New" or "Revised" License](https://github.com/FiloSottile/age/blob/main/LICENSE).