fix(age): read SSH keys and plugin identities, as the age CLI did
The switch to the age library read only native age keys, so an AGE_IDENTITY holding an SSH private key or a plugin identity (a YubiKey) stopped working. Both parse again; a plugin that asks for input fails with a message saying so, and a passphrase-protected identity file is refused by name.
This commit is contained in:
5 files changed
+105
-4
No files matched your search
@@ -14,11 +14,13 @@ require (
|
||||
github.com/google/go-github/v78 v78.0.0
|
||||
github.com/hashicorp/go-plugin v1.8.0
|
||||
github.com/hashicorp/vault/api v1.23.0
|
||||
golang.org/x/crypto v0.55.0
|
||||
google.golang.org/grpc v1.84.0
|
||||
google.golang.org/protobuf v1.36.12
|
||||
)
|
||||
|
||||
require (
|
||||
filippo.io/edwards25519 v1.2.0 // indirect
|
||||
filippo.io/hpke v0.4.0 // indirect
|
||||
github.com/42wim/httpsig v1.2.4 // indirect
|
||||
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
|
||||
@@ -46,9 +48,9 @@ require (
|
||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||
github.com/oklog/run v1.1.0 // indirect
|
||||
github.com/ryanuber/go-glob v1.0.0 // indirect
|
||||
golang.org/x/crypto v0.55.0 // indirect
|
||||
golang.org/x/net v0.57.0 // indirect
|
||||
golang.org/x/sys v0.47.0 // indirect
|
||||
golang.org/x/term v0.45.0 // indirect
|
||||
golang.org/x/text v0.41.0 // indirect
|
||||
golang.org/x/time v0.12.0 // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260706201446-f0a921348800 // indirect
|
||||
|
||||
@@ -4,6 +4,8 @@ code.gitea.io/sdk/gitea v0.25.1 h1:yywxWwoV+SdjHtbC6unBiXojWdZOtoHuGhEazEXeWuE=
|
||||
code.gitea.io/sdk/gitea v0.25.1/go.mod h1:uDFWYBU8dgZsgOHwe6C/6olxvf8FHguNB3wW1i83fgg=
|
||||
filippo.io/age v1.3.2 h1:r6RSZLFSMm6rzKepZ7ZAYkKCu14f3/Me8c7uKYh7C8c=
|
||||
filippo.io/age v1.3.2/go.mod h1:TH/Yr2sSRhCKbaH4XPxpUV0Us8Gv6txYUpiZQWz8Evk=
|
||||
filippo.io/edwards25519 v1.2.0 h1:crnVqOiS4jqYleHd9vaKZ+HKtHfllngJIiOpNpoJsjo=
|
||||
filippo.io/edwards25519 v1.2.0/go.mod h1:xzAOLCNug/yB62zG1bQ8uziwrIqIuxhctzJT18Q77mc=
|
||||
filippo.io/hpke v0.4.0 h1:p575VVQ6ted4pL+it6M00V/f2qTZITO0zgmdKCkd5+A=
|
||||
filippo.io/hpke v0.4.0/go.mod h1:EmAN849/P3qdeK+PCMkDpDm83vRHM5cDipBJ8xbQLVY=
|
||||
github.com/42wim/httpsig v1.2.4 h1:mI5bH0nm4xn7K18fo1K3okNDRq8CCJ0KbBYWyA6r8lU=
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
// age resolves a key from an age-encrypted file of "key=value" lines. It needs
|
||||
// AGE_FILE (the encrypted file) and AGE_IDENTITY (the age private-key file).
|
||||
// AGE_FILE (the encrypted file) and AGE_IDENTITY (the identity file: native
|
||||
// age keys, an unencrypted SSH private key, or plugin identities whose
|
||||
// age-plugin-* binary is on PATH).
|
||||
// Blank lines and "#" comments are skipped, a value may contain "=", and an
|
||||
// empty value counts as missing.
|
||||
package main
|
||||
@@ -10,10 +12,14 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"filippo.io/age"
|
||||
"filippo.io/age/agessh"
|
||||
"filippo.io/age/armor"
|
||||
"filippo.io/age/plugin"
|
||||
|
||||
"github.com/samcharles93/archipelago/sdk/secretengine"
|
||||
)
|
||||
@@ -35,7 +41,7 @@ func decrypt(file, identityFile string) ([]byte, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read identity: %w", err)
|
||||
}
|
||||
identities, err := age.ParseIdentities(bytes.NewReader(keys))
|
||||
identities, err := parseIdentities(keys)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse identity %s: %w", identityFile, err)
|
||||
}
|
||||
@@ -55,6 +61,69 @@ func decrypt(file, identityFile string) ([]byte, error) {
|
||||
return plain, nil
|
||||
}
|
||||
|
||||
// parseIdentities reads an identity file as the age CLI does: an SSH private
|
||||
// key, or lines of native age keys and plugin identities (such as a YubiKey's
|
||||
// AGE-PLUGIN-YUBIKEY-...). A passphrase-protected file cannot be used: there
|
||||
// is no one to type the passphrase.
|
||||
func parseIdentities(keys []byte) ([]age.Identity, error) {
|
||||
trimmed := bytes.TrimSpace(keys)
|
||||
switch {
|
||||
case bytes.HasPrefix(trimmed, []byte("-----BEGIN")):
|
||||
id, err := agessh.ParseIdentity(trimmed)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("SSH key: %w", err)
|
||||
}
|
||||
return []age.Identity{id}, nil
|
||||
case bytes.HasPrefix(trimmed, []byte("age-encryption.org/")), bytes.HasPrefix(trimmed, []byte(armor.Header)):
|
||||
return nil, errors.New("the identity file is passphrase-protected, which a secret engine cannot unlock")
|
||||
}
|
||||
var identities []age.Identity
|
||||
var native []string
|
||||
for line := range strings.SplitSeq(string(trimmed), "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
switch {
|
||||
case line == "" || strings.HasPrefix(line, "#"):
|
||||
case strings.HasPrefix(line, "AGE-PLUGIN-"):
|
||||
id, err := plugin.NewIdentity(line, unattended())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("plugin identity: %w", err)
|
||||
}
|
||||
identities = append(identities, id)
|
||||
default:
|
||||
native = append(native, line)
|
||||
}
|
||||
}
|
||||
if len(native) > 0 {
|
||||
ids, err := age.ParseIdentities(strings.NewReader(strings.Join(native, "\n")))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
identities = append(identities, ids...)
|
||||
}
|
||||
if len(identities) == 0 {
|
||||
return nil, errors.New("no identities found")
|
||||
}
|
||||
return identities, nil
|
||||
}
|
||||
|
||||
// unattended answers a plugin that wants a person: nobody is there. A plugin
|
||||
// that needs only a touch (a hardware key's presence check) still works.
|
||||
func unattended() *plugin.ClientUI {
|
||||
return &plugin.ClientUI{
|
||||
DisplayMessage: func(name, message string) error {
|
||||
slog.Info("age plugin message", "plugin", name, "message", message)
|
||||
return nil
|
||||
},
|
||||
RequestValue: func(name, prompt string, _ bool) (string, error) {
|
||||
return "", fmt.Errorf("age plugin %s asked for input (%q), which a secret engine cannot give", name, prompt)
|
||||
},
|
||||
Confirm: func(name, prompt, _, _ string) (bool, error) {
|
||||
return false, fmt.Errorf("age plugin %s asked for confirmation (%q), which a secret engine cannot give", name, prompt)
|
||||
},
|
||||
WaitTimer: func(name string) { slog.Info("age plugin is waiting, likely for a hardware key touch", "plugin", name) },
|
||||
}
|
||||
}
|
||||
|
||||
// lookup finds key in "key=value" lines.
|
||||
func lookup(plain []byte, key string) (string, error) {
|
||||
for line := range strings.SplitSeq(string(plain), "\n") {
|
||||
|
||||
@@ -2,12 +2,16 @@ package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/ed25519"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"filippo.io/age"
|
||||
"filippo.io/age/agessh"
|
||||
"golang.org/x/crypto/ssh"
|
||||
|
||||
"github.com/samcharles93/archipelago/sdk/secretengine"
|
||||
)
|
||||
@@ -49,6 +53,30 @@ func TestResolve(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// An unencrypted SSH key, which the age CLI this engine replaced accepted.
|
||||
pub, priv, err := ed25519.GenerateKey(nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sshPub, _ := ssh.NewPublicKey(pub)
|
||||
recipient, err := agessh.NewEd25519Recipient(sshPub)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var sshSealed bytes.Buffer
|
||||
w, _ = age.Encrypt(&sshSealed, recipient)
|
||||
_, _ = w.Write([]byte("token=via-ssh\n"))
|
||||
_ = w.Close()
|
||||
block, err := ssh.MarshalPrivateKey(priv, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("AGE_FILE", write("ssh.age", sshSealed.Bytes()))
|
||||
t.Setenv("AGE_IDENTITY", write("id_ed25519", pem.EncodeToMemory(block)))
|
||||
if got, err := resolve(t.Context(), "token"); err != nil || got != "via-ssh" {
|
||||
t.Fatalf("SSH identity: %q, %v", got, err)
|
||||
}
|
||||
|
||||
other, _ := age.GenerateX25519Identity()
|
||||
t.Setenv("AGE_IDENTITY", write("other.txt", []byte(other.String()+"\n")))
|
||||
if _, err := resolve(t.Context(), "token"); err == nil || errors.Is(err, secretengine.ErrNotFound) {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
apiVersion: dev.archie.package.v1
|
||||
displayName: age
|
||||
description: Resolve secret references from an age-encrypted key=value file.
|
||||
version: 1.1.0
|
||||
version: 1.1.1
|
||||
contributes:
|
||||
extensions:
|
||||
- surface: secretengine
|
||||
|
||||
Reference in new issue
Block a user