# Use SOPS-backed secrets

Use a SOPS-backed Secret when Kix should own the Kubernetes Secret but its
values must remain encrypted in the repository and build output. Kix evaluates
the encrypted file into the cluster artifact, then runs `sops --decrypt`
immediately before it applies the Secret.

This guide uses an Age identity. Install
[SOPS](https://github.com/getsops/sops) and Age on the workstation where you
create and encrypt the secret file, and make sure the machine that runs
`kix deploy` can access the decryption identity (if you use PGP keys instead of
Age or cloud KMS, `gpg` must also be on `PATH`).

## Create an Age identity

Generate an identity and print its recipient:

<Command
  commands={[
    "age-keygen -o age-key.txt",
    "age-keygen -y age-key.txt",
  ]}
/>

Keep `age-key.txt` outside the repository. Your deployment environment needs
the identity, while developers only need the recipient to encrypt changes.

## Encrypt the values

Create the file with the keys your package expects:

```yaml title="credentials.enc.yaml"
username: application-user
password: replace-with-a-generated-password
```

Encrypt it in place. The shell variable keeps the full recipient out of later
commands:

<Command
  commands={[
    'recipient=$(age-keygen -y age-key.txt)',
    'sops encrypt --age "$recipient" --in-place credentials.enc.yaml',
  ]}
/>

Open the file before committing it and confirm that the values have become
`ENC[...]` entries. Commit the encrypted file, but not `age-key.txt`.

## Convert a SOPS file from Flux

Flux repositories often encrypt a full Kubernetes Secret manifest
(`apiVersion`, `kind`, `metadata`, `stringData`, and `type`). Kix expects a
flat key-value map where each top-level key is a Secret key, and refuses a full
manifest with error `[K6746]`.

First, check `.sops.yaml`. Flux rules often set
`encrypted_regex: ^(data|stringData)$`, which leaves every value in a flat file
unencrypted because there is no `data` or `stringData` key. Make sure the rule
matching `credentials.enc.yaml` has no `encrypted_regex`.

If the Flux manifest stores plain-text values under `stringData`, extract
`stringData` and re-encrypt the new file in place:

<Command
  commands={[
    'sops decrypt --extract \'["stringData"]\' flux-file.yaml > credentials.enc.yaml',
    'sops encrypt --age "$recipient" --in-place credentials.enc.yaml',
  ]}
/>

`credentials.enc.yaml` contains plain text between those two commands, so run
them in a private directory and confirm every value is `ENC[...]` before
committing.

If the Flux manifest stores base64-encoded values under `data` instead of
`stringData`, decode them to plain text before encrypting. Kix writes the
decrypted values into the Secret verbatim.

## Declare and consume the Secret

Pass the encrypted file to `scope.mkSecret` and declare its keys explicitly:

<Snippet {...sopsBackedSecret} />

Kix cannot derive the key names from encrypted values, so `keys` is required.
It also lets the `out` helpers catch misspelled keys during evaluation.

This validation checks how the package uses the declared keys, not whether the
encrypted file contains them. If the file omits a declared key, Kix can apply
the Secret successfully and the workload fails when it tries to read the key.
By contrast, the deploy-time readiness check for `scope.mkSecretRef` verifies
that every declared key exists in the live Secret.

Kix records the SHA-256 of the encrypted file in the `kix.run/sops-digest`
annotation. Before it decrypts the file, `kix deploy` checks the file against
that digest and stops if they differ. You do not write a hash yourself. When you
change the encrypted file, the Secret changes in the next build, and the pods
that read it restart.

Consume the Secret in the same way as any other Kix-managed Secret:

<Snippet {...sopsSecretEnv} />

The reference from `deployment` to `credentials` also gives Kix the deploy
ordering between them.

## Check the cluster artifact

Render the cluster before deploying:

<Command {...renderedSecret} />

The output excerpt reports the declared keys and confirms that the rendered
Secret has neither `data` nor `stringData`. The artifact contains a reference
to the encrypted source, not the decrypted values.

## Deploy with access to the identity

Point SOPS at the identity on the deployment machine, then deploy normally:

<Command
  commands={[
    "export SOPS_AGE_KEY_FILE=/secure/path/age-key.txt",
    "kix deploy how-to-platform-sops",
  ]}
  cwd="kix-examples/"
/>

With `kix deploy --operator`, the Kix operator decrypts SOPS files inside the
cluster, so the operator pod must have access to the decryption key. See
[Secrets from SOPS files](/docs/reference/cli/operator-mode/#secrets-from-sops-files)
for how to configure it.

When a Secret is new, changed, or re-applied with `--reconcile`, `kix plan`,
`kix deploy`, and `kix rollback` validate its SOPS file during their
cluster-connected dry run (unchanged Secrets and offline or read-only commands
skip this check; `kix plan --bundle` checks the encrypted file without
decrypting it). Kix stops before applying anything if SOPS is unavailable, the
identity cannot decrypt the file, or any top-level value is a map, a list, or
`null` (`[K6746]`). Use a flat YAML or JSON map where every value is a string,
a number, `true`, or `false`, and store JSON meant for one key as a string
(local deploys also accept dotenv; bundles do not, and INI files fail with
`[K6746]` because `sops` groups keys into section maps).

Pass `kix deploy --on-error continue` if you need to apply unrelated changes
while leaving the refused Secret unchanged and skipping resources that depend
on it. `kix rollback` always stops on `[K6746]` when rolling back to a build
created before the file was converted: pick a newer deployment record, or check
out the target commit, convert its file, and run `kix deploy`.

When the Secret also sets plain keys in `stringData` (such as `username` or
`host`, listed in `plainKeys`), `kix deploy` keeps those keys alongside the
keys decrypted from the SOPS file.

Check that Kubernetes received the two declared keys without printing their
values:

<Command {...secret} />

Give the deployment identity to CI through its secret store or workload
identity mechanism. If another controller should own the Kubernetes Secret,
use an [externally managed Secret](/docs/how-to/platform-capabilities/use-externally-managed-secrets/)
instead.

:::caution
Kubernetes Secret values are base64-encoded, not encrypted by that encoding.
Configure encryption at rest and restrict Secret access in the cluster as you
would for any other Kubernetes Secret.
:::

:::note[Reference]
See [Secrets and Secret refs](/docs/reference/secrets-crds-and-rbac/#secrets-and-secret-refs)
for the complete `scope.mkSecret` API.
:::