ejson

package module
v1.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: MIT Imports: 8 Imported by: 50 modules, 44 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

ejson

ejson is a utility for managing a collection of secrets in source control. Legacy v1 keys use NaCl Box (Curve25519 + XSalsa20 + Poly1305). Post-quantum hybrid v2 keys use RFC 9180 HPKE (MLKEM1024-P384, HKDF-SHA384, export-only) to derive one 32-byte AES-256-GCM data key per edit session (_ejson_v2_session_<sid>), encrypting each added value with a counted 96-bit big-endian nonce. Secrets are collected in a JSON file, in which all the string values are encrypted. Public keys are embedded in the file, and the decrypter looks up the corresponding private key from its local filesystem.

demo

The main benefits provided by ejson are:

  • Secrets can be safely stored in a git repo.
  • Changes to secrets are auditable on a line-by-line basis with git blame.
  • Anyone with git commit access has access to write new secrets.
  • Decryption access can easily be locked down to production servers only.
  • Secrets change synchronously with application source (as opposed to secrets provisioned by Configuration Management).
  • Simple, well-tested, easily-auditable source.

See the manpages for more technical documentation.

See ejson2env for a useful tool to help with exporting a portion of secrets as environment variables for environments/tools that require this pattern.

Installation

You can download the .deb package from Github Releases.

On development machines (64-bit linux or OS X), the recommended installation method is via Homebrew:

brew tap shopify/shopify
brew install ejson

Workflow

1: Create the Keydir

By default, EJSON looks for keys in /opt/ejson/keys. You can change this by setting EJSON_KEYDIR or passing the -keydir option.

$ mkdir -p /opt/ejson/keys

For Mac OS users. By default you won't have write permissions to /opt/ejson folder. Make sure to run the following command to grant these permissions:

sudo chown -R $(whoami) /opt/ejson
2: Generate a keypair

When called with -w, ejson keygen will write the keypair into the keydir and print the public key. Without -w, it will print both keys to stdout. This is useful if you have to distribute the key to multiple servers via configuration management, etc.

$ ejson keygen
Public Key:
63ccf05a9492e68e12eeb1c705888aebdcc0080af7e594fc402beb24cce9d14f
Private Key:
75b80b4a693156eb435f4ed2fe397e583f461f09fd99ec2bd1bdef0a56cf6e64
$ ejson keygen -w
53393332c6c7c474af603c078f5696c8fe16677a09a711bba299a6c1c1676a59
$ cat /opt/ejson/keys/5339*
888a4291bef9135729357b8c70e5a62b0bbe104a679d829cdbe56d46a4481aaf

By default, keygen generates legacy v1 keys. To generate a post-quantum hybrid v2 keypair, use --scheme v2 or --pqc:

$ ejson keygen --pqc -w
v2:<base64 MLKEM1024-P384 public key>
$ ls /opt/ejson/keys
ejv2-<96-character hex SHA-384>

The full hybrid public key (1,665 bytes, 2,220 base64 characters) is embedded in _public_key so anyone with commit access can still add secrets offline without access to the private keydir. Every tool derives the 101-character key ID (ejv2- + lowercase hex SHA-384 of the public key) directly from _public_key and looks up the 32-byte HPKE seed (64 hex characters) under that name in keydir, GCP Secret Manager (<= 255 chars), or Kubernetes Secrets (<= 253 chars).

3: Create an ejson file

The format is described in more detail later on. For now, create a file that looks something like this. Fill in the <key> with whatever you got back in step 2.

Create this file as test.ejson:

{
  "_public_key": "<key>",
  "_database_username": "1234username",
  "database_password": "1234password"
}
4: Encrypt the file

Running ejson encrypt test.ejson will encrypt any new plaintext keys in the file and leave any existing encrypted keys or keys with property names prefixed with _ untouched:

{
  "_public_key": "63ccf05a9492e68e12eeb1c705888aebdcc0080af7e594fc402beb24cce9d14f",
  "_database_username": "1234username",
  "database_password": "EJ[1:WGj2t4znULHT1IRveMEdvvNXqZzNBNMsJ5iZVy6Dvxs=:kA6ekF8ViYR5ZLeSmMXWsdLfWr7wn9qS:fcHQtdt6nqcNOXa97/M278RX6w==]"
}

Try adding another plaintext secret to the file and run ejson encrypt test.ejson again. The database_password field will not be changed, but the new secret will be encrypted.

5: Decrypt the file

To decrypt the file, you must have a file present in the keydir whose name is the 64-character hex public key (for v1 documents) or the 101-character key ID ejv2-<SHA-384 hex> (for v2 documents), containing the 64-character hex private key / HPKE seed. If you used ejson keygen -w (or ejson keygen --pqc -w), you've already got this covered.

Unlike ejson encrypt, which overwrites the specified files, ejson decrypt only takes one file parameter, and prints the output to stdout:

$ ejson decrypt foo.ejson
{
  "_public_key": "63ccf05a9492e68e12eeb1c705888aebdcc0080af7e594fc402beb24cce9d14f",
  "_database_username": "1234username",
  "database_password": "1234password"
}

To write plaintext to a file instead of stdout, use ejson decrypt -o output.json foo.ejson. On Unix, new output files use mode 0600, further restricted by the process umask. Existing files are updated in place with their permissions and ownership unchanged, including files reached through symlinks or hard links. To share output with another user or group, create the destination with the intended ownership and permissions before decrypting.

Review existing output files before reuse: upgrading does not tighten their permissions. Use a trusted destination in a directory that untrusted users cannot write to. The CLI follows symlinks and does not revoke access from processes that already have the file open. Shell redirection (ejson decrypt foo.ejson > output.json) uses the shell's file-creation permissions, not the CLI's.

Format

The ejson document format is simple, but there are a few points to be aware of:

  1. It's just JSON.
  2. There must be a key at the top level named _public_key. For v1 documents, its value is a 32-byte hex-encoded (64 ASCII hex chars) public key. For v2 documents, its value is v2:<base64> containing the RFC 9180 serialized 1,665-byte MLKEM1024-P384 public key. Each edit session adds one top-level field _ejson_v2_session_<sid> containing the base64 HPKE encapsulation, where <sid> is the lowercase hex of the first 16 bytes of SHA-384(enc) (32 hex chars). Unreferenced session fields are pruned when ejson encrypt runs.
  3. Any string literal that isn't an object key will be encrypted by default (ie. in {"a": "b"}, "b" will be encrypted, but "a" will not.
  4. Numbers, booleans, and nulls aren't encrypted.
  5. If a key begins with an underscore, its corresponding value will not be encrypted. This is used to prevent the _public_key field from being encrypted, and is useful for implementing metadata schemes.
  6. Underscores do not propagate downward. For example, in {"_a": {"b": "c"}}, "c" will be encrypted.
  7. Encrypted values are schema-tagged: v1 uses EJ[1:<eph_pub_b64>:<nonce_b64>:<box_b64>] and v2 uses EJ[2:<sid>:<n>:<ct_b64>] where n is a non-negative integer counter encoded as a 12-byte big-endian AES-256-GCM nonce and AAD is EJ[2:<sid>:<n>:. A v2 file refuses EJ[1:...] values (and vice versa), and duplicate (sid, n) pairs are refused on decrypt. ejson encrypt refuses to keep an existing v2 value that could not be decrypted: a second value with the same (sid, n), including an exact copy, or a ciphertext shorter than the 16-byte authentication tag. Migrate a whole file from v1 to v2 or compact sessions with ejson reencrypt --to-public-key <v2_pub> <file>. Cross-language known-answer test vectors live in testdata/v2_vectors.json.

See also

  • If you use Capistrano for deployment you can use capistrano-ejson to automatically decrypt the secrets on deploy.
  • If you use pre-commit, you can use it to automatically encrypt secrets on commit.

Documentation

Overview

Package ejson implements the primary interface to interact with ejson documents and keypairs. The CLI implemented by cmd/ejson is a fairly thin wrapper around this package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decrypt

func Decrypt(in io.Reader, out io.Writer, keydir string, userSuppliedPrivateKey string) error

Decrypt reads an ejson stream from 'in' and writes the decrypted data to 'out'. For v1 files, the private key is looked up at <keydir>/<64-hex pubkey>. For v2 files, the 32-byte HPKE seed (64 hex chars) is looked up at <keydir>/ejv2-<SHA-384 hex> and verified against _public_key.

func DecryptFile

func DecryptFile(filePath, keydir, userSuppliedPrivateKey string) ([]byte, error)

DecryptFile decrypts filePath and returns the plaintext JSON bytes.

func Encrypt

func Encrypt(in io.Reader, out io.Writer) (int, error)

Encrypt reads all contents from 'in', extracts the pubkey and encrypts any unencrypted string values, writing the result to 'out'.

func EncryptFileInPlace

func EncryptFileInPlace(filePath string) (int, error)

EncryptFileInPlace encrypts any unencrypted fields in filePath in place.

func GenerateKeypair

func GenerateKeypair() (pub string, priv string, err error)

GenerateKeypair creates a legacy v1 Curve25519 keypair, returning hex strings.

func GenerateKeypairForScheme added in v1.7.0

func GenerateKeypairForScheme(scheme string) (pub string, priv string, keyID string, err error)

GenerateKeypairForScheme creates a new ejson keypair for the named scheme ("v1" or "v2"). pub goes into _public_key, priv is the 64-hex private key (or HPKE seed for v2), and keyID is the filename/secret ID used for lookup (the 64-hex public key for v1; "ejv2-" + SHA-384 hex for v2, 101 chars).

func KeyIDForPublicKey added in v1.7.0

func KeyIDForPublicKey(pub string) (string, error)

KeyIDForPublicKey computes the keydir / Secret Manager key identifier for a v1 or v2 _public_key string.

func Reencrypt added in v1.7.0

func Reencrypt(in io.Reader, out io.Writer, keydir, userSuppliedPrivateKey, newPublicKey string) (int, error)

Reencrypt decrypts all values in 'in', optionally switches _public_key to newPublicKey (for whole-file v1 -> v2 migration or key rotation), and re-encrypts every value for the resulting recipient. For a v2 input, the old session fields are replaced by a single fresh session.

Nothing is written to 'out' if the input is refused: when _public_key appears more than once, when a v1 input being migrated to v2 already uses the _ejson_v2_session_ prefix for its own top-level fields, or when a v2 input has a skipped (_-prefixed) field referencing a session being removed.

func ReencryptFileInPlace added in v1.7.0

func ReencryptFileInPlace(filePath, keydir, userSuppliedPrivateKey, newPublicKey string) (int, error)

ReencryptFileInPlace decrypts and re-encrypts filePath in place, optionally migrating it to newPublicKey and compacting v2 sessions down to one.

Types

This section is empty.

Directories

Path Synopsis
cmd
ejson command
Package crypto implements a simple convenience wrapper around golang.org/x/crypto/nacl/box.
Package crypto implements a simple convenience wrapper around golang.org/x/crypto/nacl/box.
Package json implements functions to load the Public key data from an EJSON file, and to walk that data file, encrypting or decrypting any keys which, according to the specification, are marked as encryptable (see README.md for details).
Package json implements functions to load the Public key data from an EJSON file, and to walk that data file, encrypting or decrypting any keys which, according to the specification, are marked as encryptable (see README.md for details).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL