@@ -4,11 +4,13 @@ import (
4
4
"bytes"
5
5
"fmt"
6
6
"log"
7
+ "net"
7
8
"os"
8
9
9
10
"github.com/84codes/sparoid.go"
10
11
"github.com/joho/godotenv"
11
12
"golang.org/x/crypto/ssh"
13
+ "golang.org/x/crypto/ssh/agent"
12
14
"golang.org/x/crypto/ssh/knownhosts"
13
15
)
14
16
@@ -23,45 +25,27 @@ func main() {
23
25
if err != nil {
24
26
log .Fatal ("Auth failed" , err )
25
27
}
26
- //var hostKey ssh.PublicKey
27
- // An SSH client is represented with a ClientConn.
28
- //
29
- // To authenticate with the remote server you must pass at least one
30
- // implementation of AuthMethod via the Auth field in ClientConfig,
31
- // and provide a HostKeyCallback.
32
- key , err := os .ReadFile (fmt .Sprintf ("%s/.ssh/id_ed25519" , os .Getenv ("HOME" )))
33
- if err != nil {
34
- log .Fatal ("Failed to load private key" , err )
35
- }
36
28
37
- signer , err := ssh .ParsePrivateKeyWithPassphrase (key , []byte (os .Getenv ("SSH_PASS" )))
29
+ socket := os .Getenv ("SSH_AUTH_SOCK" )
30
+ conn , err := net .Dial ("unix" , socket )
38
31
if err != nil {
39
- log .Fatal ("Failed to parse private key " , err )
32
+ log .Fatalf ("Failed to open SSH_AUTH_SOCK: %v " , err )
40
33
}
41
34
42
- publicKey , err := os .ReadFile (fmt .Sprintf ("%s/.ssh/id_ed25519-cert.pub" , os .Getenv ("HOME" )))
43
- if err != nil {
44
- log .Fatal ("Failed to load public key" , err )
45
- }
46
- pubKey , _ , _ , _ , err := ssh .ParseAuthorizedKey (publicKey )
47
- if err != nil {
48
- log .Fatal ("Failed to parse public key" , err )
49
- }
50
- certSigner , err := ssh .NewCertSigner (pubKey .(* ssh.Certificate ), signer )
51
- if err != nil {
52
- log .Fatalf ("failed to create cert signer: %v" , err )
53
- }
35
+ agentClient := agent .NewClient (conn )
36
+
54
37
knownhostsCallback , err := knownhosts .New (fmt .Sprintf ("%s/.ssh/known_hosts" , os .Getenv ("HOME" )))
55
38
if err != nil {
56
39
log .Fatal ("Failed to load known hosts" , err )
57
40
}
58
41
config := & ssh.ClientConfig {
59
42
User : "ubuntu" ,
60
43
Auth : []ssh.AuthMethod {
61
- ssh .PublicKeys ( certSigner ),
44
+ ssh .PublicKeysCallback ( agentClient . Signers ),
62
45
},
63
46
HostKeyCallback : knownhostsCallback ,
64
47
}
48
+ // An SSH client is represented with a ClientConn.
65
49
client , err := ssh .Dial ("tcp" , fmt .Sprintf ("%s:22" , hostname ), config )
66
50
if err != nil {
67
51
log .Fatal ("Failed to dial: " , err )
0 commit comments