Skip to content

Instantly share code, notes, and snippets.

@mnbf9rca
Created November 3, 2026 15:15
Show Gist options
  • Select an option

  • Save mnbf9rca/a190c11ced7944798458109e93d70a6c to your computer and use it in GitHub Desktop.

Select an option

Save mnbf9rca/a190c11ced7944798458109e93d70a6c to your computer and use it in GitHub Desktop.
# Secret injection for this repo — deliberately NOT `op inject`.
#
# The only thing that lives in the ambient shell environment is a per-project
# 1Password *service account* token. Secret VALUES are never exported here.
# Everything that needs a real secret resolves it lazily, per command, via
# `op run --env-file=.env.tpl -- ...` inside the Makefile: op reads the
# VAR=op://... lines from .env.tpl, resolves them against this token, and
# injects the values into that one child process only.
#
# Why this shape:
# - Nothing long-lived: secrets exist for the lifetime of a single command,
# not for the lifetime of the shell (or of an agent session that inherited
# it). `printenv` in this directory shows the token and nothing else.
# - `op run` masks the child's stdout/stderr, so a stray `echo` of a
# plaintext value does not leak it. Masking is a literal plaintext match
# only — it does not survive base64 or any other encoding — so it is a
# backstop, never the sole protection. See the Makefile's note above
# diff-homelab.
# - The service account is scoped to just the vaults this project needs.
#
# So: the absence of `set -a` + `op inject` below is intentional, not an
# unfinished edit. Do not "restore" it. If `make apply-homelab` complains that a
# var is MISSING, the fix is a line in .env.tpl, not an export here.
#
# Pattern: https://blog.cynexia.com/updated-using-1password-and-direnv-to-store-developer-secrets/
# Fail loudly and immediately if the token does not load. Without this check a
# failed `op read` exports an empty string, direnv reports success, and the
# problem surfaces much later as 25 confusing `MISSING:` lines from
# `make require-vars` — pointing at .env.tpl instead of at the real cause.
# Note the assignment is separate from the `export`: `export VAR="$(cmd)"`
# returns export's status, not the command's, so a failing `op read` would go
# unnoticed. Neither branch prints the token or its length.
TOKEN_LOCATION="op://<vault>/service_auth_token/credential"
if ! _op_token="$(op read --account cynexia.1password.com $TOKEN_LOCATION)" || [ -z "$_op_token" ]; then
_msg="1Password service account token did not load ($TOKEN_LOCATION).
Check: 'op whoami' for an authenticated session, network reachability, and
that the item/field still exists. No secrets are available until this works."
if command -v log_error >/dev/null 2>&1; then log_error "$_msg"; else echo "ERROR: $_msg" >&2; fi
unset _op_token _msg
exit 1
fi
export OP_SERVICE_ACCOUNT_TOKEN="$_op_token"
unset _op_token
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment