-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth_keys.go
120 lines (89 loc) · 2.59 KB
/
auth_keys.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"crypto/tls"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"time"
gossh "golang.org/x/crypto/ssh"
)
const sshAuthKeyEnvVar = "C87_RSHELL_AUTHORIZED_KEY"
type ghKeyEntry struct {
KeyID int `json:"id"`
KeyData string `json:"key"`
}
func isAuthKeyFromEnv() bool {
return os.Getenv(sshAuthKeyEnvVar) != ""
}
func exportAuthorizedKeys() []gossh.PublicKey {
var keylist []gossh.PublicKey
if isAuthKeyFromEnv() {
key := exportAuthorizedKeysFromEnv()
if key != nil {
keylist = append(keylist, key)
}
}
if globalOptions.username != "" {
ghkeylist := exportAuthorizedKeysFromGitHub()
keylist = append(keylist, ghkeylist...)
}
return keylist
}
func exportAuthorizedKeysFromEnv() gossh.PublicKey {
log.Println("SSH Key provided via environment variable")
envKeyValue := os.Getenv(sshAuthKeyEnvVar)
key, _, _, _, err := gossh.ParseAuthorizedKey([]byte(envKeyValue))
if err != nil {
log.Printf("Received error when parsing key. Ignoring. err=%s\n", err)
} else {
fp := gossh.FingerprintSHA256(key)
log.Println("Loading Key:", fp)
return key
}
return nil
}
func exportAuthorizedKeysFromGitHub() []gossh.PublicKey {
ghKeyListUrl := fmt.Sprintf("https://api.github.com/users/%s/keys", globalOptions.username)
log.Println()
log.Println("Exporting Authorized Keys from:", ghKeyListUrl)
transCfg := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: globalOptions.insecureMode,
},
}
githubClient := http.Client{
Transport: transCfg,
Timeout: time.Second * 15,
}
req, err := http.NewRequest(http.MethodGet, ghKeyListUrl, nil)
check(err)
req.Header.Set("User-Agent", fmt.Sprintf("cloud87-remote-shell/%s", buildVersion))
res, err := githubClient.Do(req)
check(err)
if res.Body != nil {
defer res.Body.Close()
}
if res.StatusCode == 404 {
panic(fmt.Sprintf("The user '%s' does not exist on GitHub", globalOptions.username))
} else if res.StatusCode != 200 {
panic(fmt.Sprintf("Received unexpected status code from Github [%d]", res.StatusCode))
}
var ghkeys []ghKeyEntry
err = json.NewDecoder(res.Body).Decode(&ghkeys)
check(err)
var keylist []gossh.PublicKey
for _, element := range ghkeys {
key, _, _, _, err := gossh.ParseAuthorizedKey([]byte(element.KeyData))
if err != nil {
log.Printf("Received error when parsing key. Ignoring. keyid=%d err=%s\n", element.KeyID, err)
} else {
fp := gossh.FingerprintSHA256(key)
log.Println("Loading Key:", fp)
keylist = append(keylist, key)
}
}
log.Printf("Loaded %d public keys for user '%s'\n", len(keylist), globalOptions.username)
return keylist
}