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

only backup cleartext file if it exists #176

Merged
merged 1 commit into from
Nov 29, 2023
Merged

Conversation

shivak
Copy link
Contributor

@shivak shivak commented Mar 19, 2023

Avoids complaints from cp about nonexistent files.

@ryantm
Copy link
Owner

ryantm commented Mar 20, 2023

@shivak What scenarios cause this complaint?

Maybe this fixes your problem https://github.com/ryantm/agenix/pull/157/files ?

@CobaltCause
Copy link
Contributor

I run into this in the following situation:

$ mkdir -p secrets/wireguard
$ wg genkey | agenix -v -e secrets/wireguard/red.age -i ~/.ssh/id_ed25519
+ test 4 -gt 0
+ case "$1" in
+ shift
+ test 3 -gt 0
+ export FILE=secrets/wireguard/red.age
+ FILE=secrets/wireguard/red.age
+ shift
+ test 2 -gt 0
+ case "$1" in
+ shift
+ test 1 -gt 0
+ DEFAULT_DECRYPT+=(--identity "$1")
+ shift
+ test 0 -gt 0
+ RULES=./secrets.nix
+ trap cleanup 0 2 3 15
+ '[' 0 -eq 1 ']'
+ '[' 0 -eq 1 ']'
+ edit secrets/wireguard/red.age
+ FILE=secrets/wireguard/red.age
++ keys secrets/wireguard/red.age
++ /nix/store/513i3g7cqxzy6a2smnika69qwy9rwbga-nix-2.13.3/bin/nix-instantiate --json --eval --strict -E '(let rules = import ./secrets.nix; in rules."secrets/wireguard/red.age".publicKeys)'
++ /nix/store/hagvhrwy8jzj97kc7nyy9vr18xkg7xvk-jq-1.6-bin/bin/jq -r '.[]'
+ KEYS='ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICfle747LQBahWbu5VId7zGOlxeILis9BPh/RexQtfQJ benjamin@computer.surgery
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDvEursdg5B0QAKd8jWzHN+OfYX0OTuy3M3dMzKUDW6g charles@computer.surgery
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMeir8vZy/O5V1sPRdXybU0jhT8LlRoIdIYq4qxgLjAR root@red'
++ /nix/store/jsk5vfpxnjmh89ddyk9lh2nzpl8dky23-mktemp-1.7/bin/mktemp -d
+ CLEARTEXT_DIR=/tmp/tmp.IDEYZozAzO
++ basename secrets/wireguard/red.age
+ CLEARTEXT_FILE=/tmp/tmp.IDEYZozAzO/red.age
+ DEFAULT_DECRYPT+=(-o "$CLEARTEXT_FILE")
+ decrypt secrets/wireguard/red.age 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICfle747LQBahWbu5VId7zGOlxeILis9BPh/RexQtfQJ benjamin@computer.surgery
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDvEursdg5B0QAKd8jWzHN+OfYX0OTuy3M3dMzKUDW6g charles@computer.surgery
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMeir8vZy/O5V1sPRdXybU0jhT8LlRoIdIYq4qxgLjAR root@red'
+ FILE=secrets/wireguard/red.age
+ KEYS='ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICfle747LQBahWbu5VId7zGOlxeILis9BPh/RexQtfQJ benjamin@computer.surgery
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDvEursdg5B0QAKd8jWzHN+OfYX0OTuy3M3dMzKUDW6g charles@computer.surgery
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMeir8vZy/O5V1sPRdXybU0jhT8LlRoIdIYq4qxgLjAR root@red'
+ '[' -z 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICfle747LQBahWbu5VId7zGOlxeILis9BPh/RexQtfQJ benjamin@computer.surgery
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDvEursdg5B0QAKd8jWzHN+OfYX0OTuy3M3dMzKUDW6g charles@computer.surgery
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMeir8vZy/O5V1sPRdXybU0jhT8LlRoIdIYq4qxgLjAR root@red' ']'
+ '[' -f secrets/wireguard/red.age ']'
+ cp /tmp/tmp.IDEYZozAzO/red.age /tmp/tmp.IDEYZozAzO/red.age.before
cp: cannot stat '/tmp/tmp.IDEYZozAzO/red.age': No such file or directory
+ '[' -t 0 ']'
+ EDITOR='cp /dev/stdin'
+ cp /dev/stdin /tmp/tmp.IDEYZozAzO/red.age
+ '[' '!' -f /tmp/tmp.IDEYZozAzO/red.age ']'
+ '[' -f secrets/wireguard/red.age ']'
+ ENCRYPT=()
+ IFS=
+ read -r key
+ ENCRYPT+=(--recipient "$key")
+ IFS=
+ read -r key
+ ENCRYPT+=(--recipient "$key")
+ IFS=
+ read -r key
+ ENCRYPT+=(--recipient "$key")
+ IFS=
+ read -r key
++ /nix/store/jsk5vfpxnjmh89ddyk9lh2nzpl8dky23-mktemp-1.7/bin/mktemp -d
+ REENCRYPTED_DIR=/tmp/tmp.K2avYXIoZS
++ basename secrets/wireguard/red.age
+ REENCRYPTED_FILE=/tmp/tmp.K2avYXIoZS/red.age
+ ENCRYPT+=(-o "$REENCRYPTED_FILE")
+ /nix/store/176fb66dbfj3294chdnha0nlqyj3n3ax-rage-0.9.0/bin/rage --recipient 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICfle747LQBahWbu5VId7zGOlxeILis9BPh/RexQtfQJ benjamin@computer.surgery' --recipient 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDvEursdg5B0QAKd8jWzHN+OfYX0OTuy3M3dMzKUDW6g charles@computer.surgery' --recipient 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMeir8vZy/O5V1sPRdXybU0jhT8LlRoIdIYq4qxgLjAR root@red' -o /tmp/tmp.K2avYXIoZS/red.age
+ mv -f /tmp/tmp.K2avYXIoZS/red.age secrets/wireguard/red.age
+ cleanup
+ '[' -n x ']'
+ rm -rf /tmp/tmp.IDEYZozAzO
+ '[' -n x ']'
+ rm -rf /tmp/tmp.K2avYXIoZS
+ exit 0
+ cleanup
+ '[' -n x ']'
+ rm -rf /tmp/tmp.IDEYZozAzO
+ '[' -n x ']'
+ rm -rf /tmp/tmp.K2avYXIoZS

@jankaifer
Copy link

I faced the same problem in #211
And I ended up suggesting the same fix in #212, I'll close my PR as a duplicate, thanks @CobaltCause for noticing.

@zmrocze
Copy link

zmrocze commented Nov 12, 2023

Running into this when creating a fresh file with agenix -e:

# agenix -e my-credentials.age
cp: cannot stat '/run/user/1000/tmp.DVnIKHCzPb/my-credentials.age': No such file or directory

where my-credentials.age doesn't exist before the command is run (and does afterwards, seemingly the command succeeded)

@jankaifer
Copy link

jankaifer commented Nov 17, 2023

@ryantm could we merge this, please?
When I started using agenix it took me a few hours to debug this error (it doesn't actually cause problems, but the error messages printed by this issue confused me).

@n8henrie
Copy link
Collaborator

Why is this ! -f ... || instead of -f ... &&? Seems like the latter is both shorter and more readable. Is there some edge case behavior I'm not thinking of?

@jankaifer
Copy link

I think that -f ... && would work fine. That's how I did it in https://github.com/ryantm/agenix/pull/212/files that I closed as a duplicate (but I used an actual if).

Avoids complaints from `cp` about nonexistent files.
Copy link
Owner

@ryantm ryantm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If someone wants to fix it to be -f && fine but lets merge if this is causing people pain.

@ryantm ryantm merged commit 13ac9ac into ryantm:main Nov 29, 2023
2 checks passed
@ryantm ryantm added the fix Bug fix label Dec 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix Bug fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants