Last active
December 7, 2024 10:10
-
-
Save ejschmitt/8ee8b143d8c3198576c5c07244b3e837 to your computer and use it in GitHub Desktop.
Using SOPS with Elixir for secret storage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ### Set up SOPS ### | |
| https://github.com/getsops/sops | |
| ### secrets.enc.json ### | |
| { | |
| "dev": {"dev_key": "dev value"}, | |
| "prod": {"prod_key": "prod value}, | |
| "common": {"common_key": "value"} | |
| } | |
| ### application.ex #### | |
| def secrets(key) when is_atom(key) do | |
| secrets()[key] | |
| end | |
| def secrets(keys) when is_list(keys) do | |
| get_in(secrets(), keys) | |
| end | |
| def secrets() do | |
| Elixir.Application.get_env(:myapplication, :secrets) || parse_secrets() | |
| end | |
| def parse_secrets() do | |
| {secrets_json, _status} = System.cmd("sops", ["-d", "./secrets.enc.json"]) | |
| parsed_secrets = | |
| Jason.decode!(secrets_json, keys: :atoms) | |
| Map.merge(Map.get(parsed_secrets, :common), Map.get(parsed_secrets, env())) | |
| end | |
| ### config/runtime.exs ### | |
| config :myapplication, :secrets, MyApplication.Application.parse_secrets() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment