/
getsigner.go
54 lines (47 loc) · 1.38 KB
/
getsigner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"fmt"
"io/ioutil"
"path/filepath"
"github.com/joyent/triton-go/authentication"
)
func GetSigners(account string, keyId string, keyPath string) ([]authentication.Signer, error) {
signers := []authentication.Signer{}
if keyPath != "" {
privateKey, err := ioutil.ReadFile(keyPath)
if err != nil {
Info.Printf("cannot read key file matching %s: %s", keyId, err)
} else {
signer, err := authentication.NewPrivateKeySigner(keyId, privateKey, account)
if err != nil {
Info.Printf("cannot get a signer from %s: %s", keyId, err)
} else {
signers = append(signers, signer)
}
}
}
signer, err := authentication.NewSSHAgentSigner(keyId, account)
if err != nil {
Info.Printf("cannot get a signer from the ssh agent: %s", err)
} else {
signers = append(signers, signer)
}
if len(signers) == 0 {
default_private_key := filepath.Join(HomeDirectory, ".ssh", "id_rsa")
privateKey, err := ioutil.ReadFile(default_private_key)
if err != nil {
Info.Printf("cannot read key file matching %s: %s", keyId, err)
} else {
signer, err := authentication.NewPrivateKeySigner(keyId, privateKey, account)
if err != nil {
Info.Printf("cannot get a signer from %s: %s", keyId, err)
} else {
signers = append(signers, signer)
}
}
}
if len(signers) == 0 {
return signers, fmt.Errorf("no available signers")
}
return signers, nil
}