From 225044b061de1f11e60ffe1bf460a6dbf3cdf061 Mon Sep 17 00:00:00 2001 From: Filippo Valsorda Date: Sat, 16 Jan 2021 16:07:46 +0100 Subject: [PATCH] cmd/age: automatically load default SSH key paths --- cmd/age/age.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/cmd/age/age.go b/cmd/age/age.go index 953fbaf..3d70ed1 100644 --- a/cmd/age/age.go +++ b/cmd/age/age.go @@ -12,6 +12,7 @@ import ( "flag" "fmt" "io" + "io/ioutil" _log "log" "os" "runtime/debug" @@ -292,8 +293,26 @@ func decrypt(keys []string, in io.Reader, out io.Writer) { &LazyScryptIdentity{passphrasePrompt}, } - // TODO: check the default SSH location if no arguments are provided - // (~/.ssh/id_rsa, ~/.ssh/id_ed25519). + // If they exist and are well-formed, load the default SSH keys. If they are + // passphrase protected, the passphrase will only be requested if the + // identity matches a recipient stanza. + for _, path := range []string{ + os.ExpandEnv("$HOME/.ssh/id_rsa"), + os.ExpandEnv("$HOME/.ssh/id_ed25519"), + } { + content, err := ioutil.ReadFile(path) + if err != nil { + continue + } + ids, err := parseSSHIdentity(path, content) + if err != nil { + // If the key is explicitly requested, this error will be caught + // below, otherwise ignore it silently. + continue + } + identities = append(identities, ids...) + } + for _, name := range keys { ids, err := parseIdentitiesFile(name) if err != nil {