Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong decrypt SOPS files #421

Closed
aivanov-citc opened this issue Sep 2, 2021 · 4 comments
Closed

Wrong decrypt SOPS files #421

aivanov-citc opened this issue Sep 2, 2021 · 4 comments
Labels
area/sops SOPS related issues and pull requests invalid This doesn't seem right

Comments

@aivanov-citc
Copy link

aivanov-citc commented Sep 2, 2021

Hey! When switching from version flux 0.13.4 to 0.16.2, files encrypted with SOPs became incorrectly decrypted.

There is file realm.json

{
  "id": "example",
  "realm": "example",
  "notBefore": 0,
  "defaultSignatureAlgorithm": "RS256",
  "revokeRefreshToken": false,
...
}

encrypted by such command

cat realm.json | sops -e -p 565C51B96C276C5DF7C07CA782F13DCA5F7C4DF7 /dev/stdin > realm.json.encrypted
{
  "data": "ENC[AES256_GCM,data:L2g/kuDeWee+xJBF4tPMt7q ...
...
}

Kustomize secretGenerator

secretGenerator:
  - name: config-secret
    files:
    - realm.json=realm.json.encrypted

When decrypting on version flux 0.13.4, we get a correct contents of the secret

{
  "id": "example",
  "realm": "example",
  "notBefore": 0,
  "defaultSignatureAlgorithm": "RS256",
  "revokeRefreshToken": false,
...
}

on version flux 0.16.2 this is

{
  "data": "{\n  \"id\": \"example\",\n  \"realm\": \"example\",\n  \"notBefore\": 0,\n  \"defaultSignatureAlgorithm\": \"RS256\",\n  \"revokeRefreshToken\": false,\n ...
...
}

version sops - 3.7.1

@stefanprodan stefanprodan added area/sops SOPS related issues and pull requests bug Something isn't working help wanted Extra attention is needed labels Sep 2, 2021
@stefanprodan
Copy link
Member

stefanprodan commented Oct 14, 2021

I tested this on my cluster and once the secret is mounted, the json is decoded correctly.

Create realm.json:

{
	"id": "example",
	"realm": "example",
	"notBefore": 0,
	"defaultSignatureAlgorithm": "RS256",
	"revokeRefreshToken": false
}

Encrypt it in-place:

sops --age=$AGEPUB --encrypt --in-place realm.json

Create a deployment that mounts the secret:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: podinfo
  namespace: default
spec:
  selector:
    matchLabels:
      app: podinfo
  template:
    metadata:
      labels:
        app: podinfo
    spec:
      containers:
      - name: podinfod
        image: ghcr.io/stefanprodan/podinfo:6.0.0
        imagePullPolicy: IfNotPresent
        ports:
        - name: http
          containerPort: 9898
          #protocol: TCP
        volumeMounts:
          - name: secrets
            mountPath: "/secrets"
            readOnly: true
      volumes:
        - name: secrets
          secret:
            secretName: json-secret

Create a kustomization that generates the Kubernetes secret:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: default
resources:
  - deployment.yaml
secretGenerator:
  - name: json-secret
    files:
      - realm.json=realm.json

Create a Flux kustomization to apply the above:

apiVersion: kustomize.toolkit.fluxcd.io/v1beta2
kind: Kustomization
metadata:
  name: secrets
  namespace: flux-system
spec:
  decryption:
    provider: sops
    secretRef:
      name: sops-age
  interval: 1m0s
  path: ./secrets/test
  prune: true
  sourceRef:
    kind: GitRepository
    name: flux-system

After Flux reconciles the above, exec into the podinfo pod and print the json:

~ $ cat /secrets/realm.json 
{
	"id": "example",
	"realm": "example",
	"notBefore": 0,
	"defaultSignatureAlgorithm": "RS256",
	"revokeRefreshToken": false
}

@stefanprodan stefanprodan added invalid This doesn't seem right and removed bug Something isn't working help wanted Extra attention is needed labels Oct 14, 2021
@stefanprodan
Copy link
Member

@aivanov-citc the issue here is that you pipe the json into sops without telling SOPS what content type is it. So sops adds the data key as it thinks you are encrypting a binary file.

As I posted above you can encrypt in-place or add --input-type=json to your command like so:

cat realm.json | sops -e --input-type=json -p 565C51B96C276C5DF7C07CA782F13DCA5F7C4DF7 /dev/stdin > realm.json.encrypted

@aivanov-citc
Copy link
Author

aivanov-citc commented Oct 14, 2021

Yes indeed. When specifying the file type, it works in version 0.16.2, but does not work in version 0.13.4. We will have to update both the flux version and reencrypt the files at the same time. Thanks.

@stefanprodan
Copy link
Member

@aivanov-citc please consider upgrading to latest Flux, see here the upgrade guide: fluxcd/flux2#1916

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/sops SOPS related issues and pull requests invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

2 participants