Add an identity to the agent
This commit is contained in:
@@ -4,6 +4,8 @@
|
|||||||
package examples
|
package examples
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/rsa"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -13,18 +15,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestMain(m *testing.M) {
|
func TestMain(m *testing.M) {
|
||||||
if os.Getenv("SSH_AUTH_SOCK") == "" {
|
sock := sshagent()
|
||||||
sock := sshagent()
|
os.Setenv("SSH_AUTH_SOCK", sock)
|
||||||
os.Setenv("SSH_AUTH_SOCK", sock)
|
|
||||||
}
|
|
||||||
|
|
||||||
os.Exit(m.Run())
|
os.Exit(m.Run())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// sshagent crates an in-memory SSH agent with one identity.
|
||||||
func sshagent() string {
|
func sshagent() string {
|
||||||
dir := os.TempDir()
|
dir := os.TempDir()
|
||||||
sock := filepath.Join(dir, "test.sock")
|
sock := filepath.Join(dir, "test.sock")
|
||||||
|
|
||||||
|
// In case it already exists.
|
||||||
_ = os.Remove(sock)
|
_ = os.Remove(sock)
|
||||||
|
|
||||||
l, err := net.Listen("unix", sock)
|
l, err := net.Listen("unix", sock)
|
||||||
@@ -32,12 +34,18 @@ func sshagent() string {
|
|||||||
panic(err)
|
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() {
|
go func() {
|
||||||
conn, err := l.Accept()
|
conn, err := l.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
a := agent.NewKeyring()
|
|
||||||
agent.ServeAgent(a, conn)
|
agent.ServeAgent(a, conn)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user