Skip to content

Commit cbbaa6b

Browse files
authored
Support passing standard ssh keys to age encryption (#530)
* Support passing standard ssh keys to age encryption * Cover SSH keys in age test case
1 parent 2652e05 commit cbbaa6b

File tree

5 files changed

+25
-4
lines changed

5 files changed

+25
-4
lines changed

cmd/backup/encrypt_archive.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import (
1010
"io"
1111
"os"
1212
"path"
13+
"strings"
1314

1415
"filippo.io/age"
16+
"filippo.io/age/agessh"
1517
"github.com/ProtonMail/go-crypto/openpgp/armor"
1618
openpgp "github.com/ProtonMail/go-crypto/openpgp/v2"
1719
"github.com/offen/docker-volume-backup/internal/errwrap"
@@ -73,7 +75,7 @@ func (s *script) getConfiguredAgeRecipients() ([]age.Recipient, error) {
7375
recipients := []age.Recipient{}
7476
if len(s.c.AgePublicKeys) > 0 {
7577
for _, pk := range s.c.AgePublicKeys {
76-
pkr, err := age.ParseX25519Recipient(pk)
78+
pkr, err := parseAgeRecipient(pk)
7779
if err != nil {
7880
return nil, errwrap.Wrap(err, "failed to parse age public key")
7981
}
@@ -94,6 +96,18 @@ func (s *script) getConfiguredAgeRecipients() ([]age.Recipient, error) {
9496
return recipients, nil
9597
}
9698

99+
func parseAgeRecipient(arg string) (age.Recipient, error) {
100+
// This logic is adapted from what the age CLI is doing
101+
// stripping some special cases
102+
switch {
103+
case strings.HasPrefix(arg, "age1"):
104+
return age.ParseX25519Recipient(arg)
105+
case strings.HasPrefix(arg, "ssh-"):
106+
return agessh.ParseRecipient(arg)
107+
}
108+
return nil, fmt.Errorf("unknown recipient type: %q", arg)
109+
}
110+
97111
func (s *script) encryptWithAge(rec []age.Recipient) error {
98112
return s.doEncrypt("age", func(ciphertextWriter io.Writer) (io.WriteCloser, error) {
99113
return age.Encrypt(ciphertextWriter, rec...)

docs/reference/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,8 @@ You can populate below template according to your requirements and use it as you
358358
# AGE_PASSPHRASE="<xxx>"
359359
360360
# Backups can be encrypted asymmetrically using age in case publickeys are given.
361-
# Multiple keys need to be provided as a comma separated list. Right now, this only
362-
# support passing age keys, with no support for ssh keys.
361+
# Multiple keys need to be provided as a comma separated list. Right now, this
362+
# supports `age` and `ssh` keys
363363
364364
# AGE_PUBLIC_KEYS="<xxx>"
365365

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ require (
2727
)
2828

2929
require (
30+
filippo.io/edwards25519 v1.1.0 // indirect
3031
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
3132
github.com/cloudflare/circl v1.3.7 // indirect
3233
github.com/containerd/log v0.1.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9
3535
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
3636
filippo.io/age v1.2.1 h1:X0TZjehAZylOIj4DubWYU1vWQxv9bJpo+Uu2/LGhi1o=
3737
filippo.io/age v1.2.1/go.mod h1:JL9ew2lTN+Pyft4RiNGguFfOpewKwSHm5ayKD/A4004=
38+
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
39+
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
3840
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.17.0 h1:g0EZJwz7xkXQiZAI5xi9f3WWFYBlX1CPTrR+NDToRkQ=
3941
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.17.0/go.mod h1:XCW7KnZet0Opnr7HccfUw1PLc4CjHqpcaxW8DHklNkQ=
4042
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.1 h1:1mvYtZfWQAnwNah/C+Z+Jb9rQH95LPE2vlmMuWAHJk8=

test/age-publickey/run.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ PK_A="$(grep -E 'public key' <"$LOCAL_DIR/pk-a.txt" | cut -d: -f2 | xargs)"
1313
age-keygen >"$LOCAL_DIR/pk-b.txt"
1414
PK_B="$(grep -E 'public key' <"$LOCAL_DIR/pk-b.txt" | cut -d: -f2 | xargs)"
1515

16-
export BACKUP_AGE_PUBLIC_KEYS="$PK_A,$PK_B"
16+
ssh-keygen -t ed25519 -m pem -f "$LOCAL_DIR/id_ed25519" -C "docker-volume-backup@local"
17+
PK_C="$(cat $LOCAL_DIR/id_ed25519.pub)"
18+
19+
export BACKUP_AGE_PUBLIC_KEYS="$PK_A,$PK_B,$PK_C"
1720

1821
docker compose up -d --quiet-pull
1922
sleep 5
@@ -41,3 +44,4 @@ do_decrypt() {
4144

4245
do_decrypt "$LOCAL_DIR/pk-a.txt"
4346
do_decrypt "$LOCAL_DIR/pk-b.txt"
47+
do_decrypt "$LOCAL_DIR/id_ed25519"

0 commit comments

Comments
 (0)