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

Add -c create or replace option #271

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ e.g. inside your `flake.nix` file:
* from GitHub like https://github.com/ryantm.keys.
4. Create a secret file:
```ShellSession
$ agenix -e secret1.age
$ agenix -c secret1.age
```
It will open a temporary file in the app configured in your $EDITOR environment variable.
When you save that file its content will be encrypted with all the public keys mentioned in the `secrets.nix` file.
Expand Down Expand Up @@ -548,11 +548,13 @@ Overriding `age.secretsMountPoint` example:
```
agenix - edit and rekey age secret files

agenix -c FILE
agenix -e FILE [-i PRIVATE_KEY]
agenix -r [-i PRIVATE_KEY]

options:
-h, --help show help
-c, --create FILE create or replace FILE using $EDITOR
-e, --edit FILE edits FILE using $EDITOR
-r, --rekey re-encrypts all secrets with specified recipients
-d, --decrypt FILE decrypts FILE to STDOUT
Expand Down
1 change: 1 addition & 0 deletions pkgs/agenix.nix
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ in
)

cd $HOME/secrets
echo hello | ${bin} -c secret1.age
test $(${bin} -d secret1.age) = "hello"
'';

Expand Down
32 changes: 27 additions & 5 deletions pkgs/agenix.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ PACKAGE="agenix"
function show_help () {
echo "$PACKAGE - edit and rekey age secret files"
echo " "
echo "$PACKAGE -c FILE"
echo "$PACKAGE -e FILE [-i PRIVATE_KEY]"
echo "$PACKAGE -r [-i PRIVATE_KEY]"
echo ' '
echo 'options:'
echo '-h, --help show help'
# shellcheck disable=SC2016
echo '-c, --create FILE create or replace FILE using $EDITOR'
# shellcheck disable=SC2016
echo '-e, --edit FILE edits FILE using $EDITOR'
echo '-r, --rekey re-encrypts all secrets with specified recipients'
echo '-d, --decrypt FILE decrypts FILE to STDOUT'
Expand Down Expand Up @@ -46,6 +49,7 @@ function err() {
test $# -eq 0 && (show_help && exit 1)

REKEY=0
ENCRYPT_ONLY=0
DECRYPT_ONLY=0
DEFAULT_DECRYPT=(--decrypt)

Expand All @@ -55,6 +59,17 @@ while test $# -gt 0; do
show_help
exit 0
;;
-c|--create)
shift
ENCRYPT_ONLY=1
if test $# -gt 0; then
export FILE=$1
else
echo "no FILE specified"
exit 1
fi
shift
;;
-e|--edit)
shift
if test $# -gt 0; then
Expand Down Expand Up @@ -153,22 +168,29 @@ function edit {
CLEARTEXT_FILE="$CLEARTEXT_DIR/$(basename "$FILE")"
DEFAULT_DECRYPT+=(-o "$CLEARTEXT_FILE")

decrypt "$FILE" "$KEYS" || exit 1

[ ! -f "$CLEARTEXT_FILE" ] || cp "$CLEARTEXT_FILE" "$CLEARTEXT_FILE.before"
# Decrypt file
if [ $ENCRYPT_ONLY -eq 0 ]
then
decrypt "$FILE" "$KEYS" || exit 1
[ ! -f "$CLEARTEXT_FILE" ] || cp "$CLEARTEXT_FILE" "$CLEARTEXT_FILE.before"
else
touch "$CLEARTEXT_FILE.before"
fi

# Prompt file edit
[ -t 0 ] || EDITOR='cp /dev/stdin'

$EDITOR "$CLEARTEXT_FILE"

# Check file status
if [ ! -f "$CLEARTEXT_FILE" ]
then
warn "$FILE wasn't created."
return
fi
[ -f "$FILE" ] && [ "$EDITOR" != ":" ] && @diffBin@ -q "$CLEARTEXT_FILE.before" "$CLEARTEXT_FILE" && warn "$FILE wasn't changed, skipping re-encryption." && return
[ $ENCRYPT_ONLY -eq 0 ] && [ -f "$FILE" ] && [ "$EDITOR" != ":" ] && @diffBin@ -q "$CLEARTEXT_FILE.before" "$CLEARTEXT_FILE" && warn "$FILE wasn't changed, skipping re-encryption." && return

ENCRYPT=()
# Build recipient list
while IFS= read -r key
do
if [ -n "$key" ]; then
Expand Down
4 changes: 4 additions & 0 deletions test/integration.nix
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ pkgs.nixosTest {
# and get it back out via --decrypt
assert "secret1234" in system1.succeed(userDo("agenix -d passwordfile-user1.age"))

# user1 can recreate the secret without decrypting it
system1.succeed(userDo("echo 'secret5678' | agenix -c passwordfile-user1.age"))
assert "secret5678" in system1.succeed(userDo("agenix -d passwordfile-user1.age"))

# finally, the plain text should not linger around anywhere in the filesystem.
system1.fail("grep -r secret1234 /tmp")
'';
Expand Down
Loading