kustomize-controller: Unable to decrypt a secret via SOPS

With the recent upgrade to kustomize controller to v1.5.0 I’m facing the following issue during reconciliation:

Warning  BuildFailed              2m40s              kustomize-controller  decryption failed for 'foo-token': failed to decrypt and format 'default/foo-token' Secret data: error decrypting sops tree: Error walking tree: Could not decrypt value: Input string glagent-xxx does not match sops' data format

I have a secret resource which is set to empty string in the base layer, and there is a patch that contains actual secret data. Both of them are encrypted using SOPS (and AWS KMS key, but I think that’s irrelevant here). To my surprise kustomize controller shows the secret value itself (in the logs!), and that’s probably the hint as well why it fails. I suspect kustomize is trying to decrypt the secret twice, so on the second attempt SOPS obviously complains that the passed in value doesn’t match the regex it expects.

I confirmed that my GitOps repo sucessfully reconciles with kustomize controller v1.4.0, and does not with v1.5.0. I believe the cullprit is here, but I’m lacking background knowledge and local setup to confirm though.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (10 by maintainers)

Most upvoted comments

@matheuscscp sure thing! Somehow I was initially more interested in making my use case simply work, and only realized the security aspect once you mentioned it.

I would still like to ask, if the use cases I described above are valid and if Flux project would consider adjusting handling of encrypted resources and patches.

I do not consider the case as valid, since mixing encrypted data from different files is not a way to go.

@kamialie I would do it this way

#!/usr/bin/env bash

mkdir -p infra/base/agent
cat >infra/base/agent/kustomization.yaml <<EOF
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
secretGenerator:
  - name: token
    envs:
      - token.sops.env
    options:
      disableNameSuffixHash: true
EOF

mkdir -p infra/base/agent
cat >infra/base/agent/token.sops.env <<EOF
token=value
EOF

mkdir -p infra/base
cat >infra/base/kustomization.yaml <<EOF
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: addons
resources:
  - agent
EOF

mkdir -p infra/dev
cat >infra/dev/kustomization.yaml <<EOF
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - ../base
secretGenerator:
  - name: token
    envs:
      - token.sops.env
    behavior: merge
    options:
      disableNameSuffixHash: true
    
EOF

mkdir -p infra/dev
cat >infra/dev/token.sops.env <<EOF
token=overridden
EOF

Also, update your .sops.yaml to manage *.sops.env, so your token files will be encrypted. That should work.

and the result would be controller spitting out the plaintext value in the logs

We are already addressing this part here: https://github.com/fluxcd/kustomize-controller/pull/1372

Hi @kamialie I’m investigating this bug right now, can you please share more details of how your config is structured? Possibly the exact thing except for the actual secret values of course. Much appreciated! 🙏

Also pinging @vlasov-y, your two PRs #1283 and #1286 seem to be the only ones touching decryption in the change log for kustomize-controller v1.5.0, if you could please shed some light here 🙏

Going through code I guess the assumption at the moment is that base layer is not encrypted, while the patch is. The latest version of the controller would basically simply decrypt earlier. The reason I encounter the error is probably due to base layer containing SOPS metadata, so controller thinks it needs to decrypt that source file, where patch (with already decrypted data) has been applied to. If my assumptions are correct, I can see how I can immediately fix the issue by just removing SOPS metadata from base layer, since encrypting empty string barely serves any purpose, but I can see how this can still not work for everyone. For instance, what if I simply want to rotate a shared secret across environments: my current value would be in the base layer (to avoid duplication), and I’m introducing new value in dev environment.