-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathkeys.go
More file actions
178 lines (131 loc) · 3.37 KB
/
keys.go
File metadata and controls
178 lines (131 loc) · 3.37 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package cmd
import (
"fmt"
"io/ioutil"
"path"
"regexp"
"strconv"
"strings"
"github.com/deis/deis/client-go/controller/api"
"github.com/deis/deis/client-go/controller/client"
"github.com/deis/deis/client-go/controller/models/keys"
)
// KeysList lists a user's keys.
func KeysList(results int) error {
c, err := client.New()
if err != nil {
return err
}
if results == defaultLimit {
results = c.ResponseLimit
}
keys, count, err := keys.List(c, results)
if err != nil {
return err
}
fmt.Printf("=== %s Keys%s", c.Username, limitCount(len(keys), count))
for _, key := range keys {
fmt.Printf("%s %s...%s\n", key.ID, key.Public[:16], key.Public[len(key.Public)-10:])
}
return nil
}
// KeyRemove removes keys.
func KeyRemove(keyID string) error {
c, err := client.New()
if err != nil {
return err
}
fmt.Printf("Removing %s SSH Key...", keyID)
if err = keys.Delete(c, keyID); err != nil {
fmt.Println()
return err
}
fmt.Println(" done")
return nil
}
// KeyAdd adds keys.
func KeyAdd(keyLocation string) error {
c, err := client.New()
if err != nil {
return err
}
var key api.KeyCreateRequest
if keyLocation == "" {
key, err = chooseKey()
} else {
key, err = getKey(keyLocation)
}
if err != nil {
return err
}
fmt.Printf("Uploading %s to deis...", path.Base(key.Name))
if _, err = keys.New(c, key.ID, key.Public); err != nil {
fmt.Println()
return err
}
fmt.Println(" done")
return nil
}
func chooseKey() (api.KeyCreateRequest, error) {
keys, err := listKeys()
if err != nil {
return api.KeyCreateRequest{}, err
}
fmt.Println("Found the following SSH public keys:")
for i, key := range keys {
fmt.Printf("%d) %s %s\n", i+1, path.Base(key.Name), key.ID)
}
fmt.Println("0) Enter path to pubfile (or use keys:add <key_path>)")
var selected string
fmt.Print("Which would you like to use with Deis? ")
fmt.Scanln(&selected)
numSelected, err := strconv.Atoi(selected)
if err != nil {
return api.KeyCreateRequest{}, err
}
if numSelected > len(keys)+1 {
return api.KeyCreateRequest{}, fmt.Errorf("%d is not a valid option", numSelected)
}
if numSelected == 0 {
var filename string
fmt.Print("Enter the path to the pubkey file: ")
fmt.Scanln(&filename)
return getKey(filename)
}
return keys[numSelected-1], nil
}
func listKeys() ([]api.KeyCreateRequest, error) {
folder := path.Join(client.FindHome(), ".ssh")
files, err := ioutil.ReadDir(folder)
if err != nil {
return nil, err
}
var keys []api.KeyCreateRequest
for _, file := range files {
if path.Ext(file.Name()) == ".pub" {
key, err := getKey(path.Join(folder, file.Name()))
if err == nil {
keys = append(keys, key)
} else {
fmt.Println(err)
}
}
}
return keys, nil
}
func getKey(filename string) (api.KeyCreateRequest, error) {
regex := regexp.MustCompile("^(ssh-...|ecdsa-[^ ]+) ([^ ]+) ?(.*)")
contents, err := ioutil.ReadFile(filename)
if err != nil {
return api.KeyCreateRequest{}, err
}
if regex.Match(contents) {
capture := regex.FindStringSubmatch(string(contents))
if capture[3] != "" {
return api.KeyCreateRequest{ID: capture[3], Public: string(contents), Name: filename}, nil
}
id := strings.Split(path.Base(filename), ".")[0]
return api.KeyCreateRequest{ID: id, Public: string(contents), Name: filename}, nil
}
return api.KeyCreateRequest{}, fmt.Errorf("%s is not a valid ssh key", filename)
}