Skip to content

Commit

Permalink
Add an identity to the agent
Browse files Browse the repository at this point in the history
  • Loading branch information
blampe committed Mar 27, 2024
1 parent f9b5ed8 commit b455d90
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions examples/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package examples

import (
"crypto/rand"
"crypto/rsa"
"net"
"os"
"path/filepath"
Expand All @@ -13,31 +15,37 @@ import (
)

func TestMain(m *testing.M) {
if os.Getenv("SSH_AUTH_SOCK") == "" {
sock := sshagent()
os.Setenv("SSH_AUTH_SOCK", sock)
}
sock := sshagent()
os.Setenv("SSH_AUTH_SOCK", sock)

os.Exit(m.Run())
}

// sshagent crates an in-memory SSH agent with one identity.
func sshagent() string {
dir := os.TempDir()
sock := filepath.Join(dir, "test.sock")

// In case it already exists.
_ = os.Remove(sock)

l, err := net.Listen("unix", sock)
if err != nil {
panic(err)
}

a := agent.NewKeyring()
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
panic(err)
}
_ = a.Add(agent.AddedKey{PrivateKey: key})

go func() {
conn, err := l.Accept()
if err != nil {
panic(err)
}
a := agent.NewKeyring()
agent.ServeAgent(a, conn)
}()

Expand Down

0 comments on commit b455d90

Please sign in to comment.