Skip to content

Commit

Permalink
Merge pull request #81 from cezarmathe/main
Browse files Browse the repository at this point in the history
  • Loading branch information
slok authored May 15, 2021
2 parents 62fc672 + 1021e8d commit 130a427
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
15 changes: 14 additions & 1 deletion internal/key/age/age.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"io"
"os"
"regexp"
"strings"

"filippo.io/age"
"filippo.io/age/agessh"
Expand Down Expand Up @@ -76,7 +78,7 @@ func NewFactory(passphraseReader io.Reader, logger log.Logger) key.Factory {
},
privateKeyParsers: []func(string) (age.Identity, error){
parseSSHIdentityFunc(passphraseReader, logger),
func(d string) (age.Identity, error) { return age.ParseX25519Identity(d) },
parseAgeIdentityFunc(),
},
}
}
Expand Down Expand Up @@ -174,3 +176,14 @@ func askPasswordStdin(r io.Reader, logger log.Logger) func() ([]byte, error) {
return p, nil
}
}

var removeCommentRegexp = regexp.MustCompile("(?m)(^#.*$)")

func parseAgeIdentityFunc() func(s string) (age.Identity, error) {
return func(d string) (age.Identity, error) {
d = removeCommentRegexp.ReplaceAllString(d, "")
d = strings.TrimSpace(d)

return age.ParseX25519Identity(d)
}
}
20 changes: 20 additions & 0 deletions internal/key/age/age_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,26 @@ kBvzR+ftEy9KmPtg==
`,
expErr: false,
},

"X25519 keys with comments should be valid.": {
key: `
# This is a comment.
# This is another comment.
AGE-SECRET-KEY-1J2DCTK0T408RJK2KX5QM3RLT4MFXEZYGP327CNP347PKTQ22UYUQXJ3N4X
`,
expErr: false,
},
"X25519 keys with other text inside should be invalid.": {
key: `
# This is a comment.
# This is another comment.
a b c
AGE-SECRET-KEY-1J2DCTK0T408RJK2KX5QM3RLT4MFXEZYGP327CNP347PKTQ22UYUQXJ3N4X
d e
f
`,
expErr: true,
},
}

for name, test := range tests {
Expand Down
4 changes: 3 additions & 1 deletion tests/manual/keys-priv/key7
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
AGE-SECRET-KEY-169UTU9G4YWRWEW5KTAJEACLHZR2G8TFMW8AUE0S4NRH9R3ZNR66QJW4Y56
# This is a comment
# this is another comment
AGE-SECRET-KEY-169UTU9G4YWRWEW5KTAJEACLHZR2G8TFMW8AUE0S4NRH9R3ZNR66QJW4Y56

0 comments on commit 130a427

Please sign in to comment.