31 lines
535 B
Go
31 lines
535 B
Go
|
package ssh
|
||
|
|
||
|
import (
|
||
|
"io/ioutil"
|
||
|
"net"
|
||
|
"os"
|
||
|
|
||
|
"golang.org/x/crypto/ssh"
|
||
|
"golang.org/x/crypto/ssh/agent"
|
||
|
)
|
||
|
|
||
|
func SSHAgent() ssh.AuthMethod {
|
||
|
if sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
|
||
|
return ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers)
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func PublicKeyFile(file string) ssh.AuthMethod {
|
||
|
buffer, err := ioutil.ReadFile(file)
|
||
|
if err != nil {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
key, err := ssh.ParsePrivateKey(buffer)
|
||
|
if err != nil {
|
||
|
return nil
|
||
|
}
|
||
|
return ssh.PublicKeys(key)
|
||
|
}
|