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

If .sops.yaml contains a path_regex sops_encrypt doesn't work #153

Open
onedr0p opened this issue Jul 4, 2023 · 9 comments
Open

If .sops.yaml contains a path_regex sops_encrypt doesn't work #153

onedr0p opened this issue Jul 4, 2023 · 9 comments
Labels
bug Something isn't working

Comments

@onedr0p
Copy link

onedr0p commented Jul 4, 2023

Hi πŸ‘‹πŸΌ

I am trying to use this module but it doesn't work when I have a .sops.yaml configuration file present. If I remove the .sops.yaml config file my task works.

env

βœ– ansible --version
ansible [core 2.15.1]
  config file = None
  configured module search path = ['/Users/devin/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /opt/homebrew/Cellar/ansible/8.1.0/libexec/lib/python3.11/site-packages/ansible
  ansible collection location = /Users/devin/.ansible/collections:/usr/share/ansible/collections
  executable location = /opt/homebrew/bin/ansible
  python version = 3.11.4 (main, Jun 20 2023, 17:23:00) [Clang 14.0.3 (clang-1403.0.22.14.1)] (/opt/homebrew/Cellar/ansible/8.1.0/libexec/bin/python3.11)
  jinja version = 3.1.2
  libyaml = True
❯ ansible-galaxy collection list | grep sops
community.sops                1.6.4

sops configuration file

# /tmp/test/.sops.yaml
---
creation_rules:
  - path_regex: kubernetes/.*\.sops\.ya?ml
    encrypted_regex: "^(data|stringData)$"
    key_groups:
      - age:
          - "{{ bootstrap_age_public_key }}"
  - path_regex: ansible/.*\.sops\.ya?ml
    key_groups:
      - age:
          - "{{ bootstrap_age_public_key }}"

example task

    - name: Template out encrypted files
      community.sops.sops_encrypt:
        path: "/tmp/test/kubernetes/test.sops.yaml"
        encrypted_regex: ^(data|stringData)$
        age: ["{{ age_public_key }}"]
        content_yaml: "{{ lookup('file', 'templates/test.sops.yaml') | from_yaml }}"
        mode: "0755"
        force: true

ansible logs

TASK [Template out encrypted files] ******************************************************************************************
failed: [localhost] (item={'src': 'kubernetes/cert-manager-secret.sops.yaml.j2', 'dest': './kubernetes/test.sops.yaml', 'encrypt': True}) => {"ansible_loop_var": "item", "changed": false, "msg": "error with file to stdout: ErrorGeneric exited with code 1: error loading config: no matching creation rules found\n"}

It would be nice if we could unset config_path from always being present as I have confirmed that without this present I can encrypt. However maybe there is something else going on?

@onedr0p
Copy link
Author

onedr0p commented Jul 4, 2023

Actually this looks might be related #104

@onedr0p
Copy link
Author

onedr0p commented Jul 4, 2023

For now I added a tasks to rename the sops configuration file, use sops_encrypt and then restore the sops configuration file. I wonder if there is a more elegant way to solve this though?

@felixfontein
Copy link
Collaborator

It is exactly #104: /dev/stdin is not covered by any of your creation_rules, so sops itself balks. This is essentially getsops/sops#594.

I hope that with the new maintainers of sops we now have a chance to get this fixed (i.e. something implemented that helps to work around this).

@felixfontein felixfontein added the bug Something isn't working label Jul 4, 2023
@felixfontein
Copy link
Collaborator

LOL, I just realized I managed to overlook that you are one of the new maintainers :D Sorry for that.

@felixfontein
Copy link
Collaborator

You should be able to reproduce this when changing directory to /tmp/test/kubernetes and running echo 'foo: bar' | sops --encrypted-regex '^(data|stringData)$' --age '...' --encrypt /dev/stdin.

While using --output as suggested in getsops/sops#594 helps when doing this on the CLI (by adding --output test.sops.yaml), it won't help sops_encrypt since it needs to make sure the file is written with the correct permissions etc. in an atomic fashion. For that, we cannot use --output (and even if we could, it would be the wrong path to use). So basically we need another way to tell sops "assume this filename when looking up the creation rules", or (alternatively) a way to tell it "use what we provide on the command line, and not what is in .sops.yaml". AFAIK even the latter is not possible at the moment (and even if it would, we would need a new feature to allow using it).

@onedr0p
Copy link
Author

onedr0p commented Jul 5, 2023

LOL, I just realized I managed to overlook that you are one of the new maintainers :D Sorry for that.

No problem :D glad to see you are also helping out there as well!

I'll rename this issue and hopefully we can get a fix for it once upstream adds support for the issue you linked.

@onedr0p onedr0p changed the title Unset config_path (--config) If .sops.yaml contains a path_regex this module doesn't work Jul 5, 2023
@felixfontein felixfontein changed the title If .sops.yaml contains a path_regex this module doesn't work If .sops.yaml contains a path_regex sops_encrypt doesn't work Jul 6, 2023
@clearlybaffled
Copy link

clearlybaffled commented Aug 20, 2023

I'm not sure if this is exactly a good idea, but I just made a new creation_rule with path_regex: /dev/stdin and it works without complaining...

Thanks for creating this ticket btw, i just spent a couple of hours trying figure out why it worked just fine on the command line but not in ansible. It was a couple rounds of google before i landed here..

@onedr0p
Copy link
Author

onedr0p commented Aug 20, 2023

@clearlybaffled that works, not sure why I didn't think of that. Thanks for the tip!

@felixfontein
Copy link
Collaborator

I've started with getsops/sops#1332, which allows to fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants