forked from juju/utils
/
clientkeys.go
183 lines (168 loc) · 4.68 KB
/
clientkeys.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
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
179
180
181
182
183
// Copyright 2014 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package ssh
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
"github.com/juju/utils"
"github.com/juju/utils/set"
"golang.org/x/crypto/ssh"
)
const clientKeyName = "juju_id_rsa"
// PublicKeySuffix is the file extension for public key files.
const PublicKeySuffix = ".pub"
var (
clientKeysMutex sync.Mutex
// clientKeys is a cached map of private key filenames
// to ssh.Signers. The private keys are those loaded
// from the client key directory, passed to LoadClientKeys.
clientKeys map[string]ssh.Signer
)
// LoadClientKeys loads the client SSH keys from the
// specified directory, and caches them as a process-wide
// global. If the directory does not exist, it is created;
// if the directory did not exist, or contains no keys, it
// is populated with a new key pair.
//
// If the directory exists, then all pairs of files where one
// has the same name as the other + ".pub" will be loaded as
// private/public key pairs.
//
// Calls to LoadClientKeys will clear the previously loaded
// keys, and recompute the keys.
func LoadClientKeys(dir string) error {
clientKeysMutex.Lock()
defer clientKeysMutex.Unlock()
dir, err := utils.NormalizePath(dir)
if err != nil {
return err
}
if _, err := os.Stat(dir); err == nil {
keys, err := loadClientKeys(dir)
if err != nil {
return err
} else if len(keys) > 0 {
clientKeys = keys
return nil
}
// Directory exists but contains no keys;
// fall through and create one.
}
if err := os.MkdirAll(dir, 0700); err != nil {
return err
}
keyfile, key, err := generateClientKey(dir)
if err != nil {
os.RemoveAll(dir)
return err
}
clientKeys = map[string]ssh.Signer{keyfile: key}
return nil
}
// ClearClientKeys clears the client keys cached in memory.
func ClearClientKeys() {
clientKeysMutex.Lock()
defer clientKeysMutex.Unlock()
clientKeys = nil
}
func generateClientKey(dir string) (keyfile string, key ssh.Signer, err error) {
private, public, err := GenerateKey("juju-client-key")
if err != nil {
return "", nil, err
}
clientPrivateKey, err := ssh.ParsePrivateKey([]byte(private))
if err != nil {
return "", nil, err
}
privkeyFilename := filepath.Join(dir, clientKeyName)
if err = ioutil.WriteFile(privkeyFilename, []byte(private), 0600); err != nil {
return "", nil, err
}
if err := ioutil.WriteFile(privkeyFilename+PublicKeySuffix, []byte(public), 0600); err != nil {
os.Remove(privkeyFilename)
return "", nil, err
}
return privkeyFilename, clientPrivateKey, nil
}
func loadClientKeys(dir string) (map[string]ssh.Signer, error) {
publicKeyFiles, err := publicKeyFiles(dir)
if err != nil {
return nil, err
}
keys := make(map[string]ssh.Signer, len(publicKeyFiles))
for _, filename := range publicKeyFiles {
filename = filename[:len(filename)-len(PublicKeySuffix)]
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
keys[filename], err = ssh.ParsePrivateKey(data)
if err != nil {
return nil, fmt.Errorf("parsing key file %q: %v", filename, err)
}
}
return keys, nil
}
// privateKeys returns the private keys loaded by LoadClientKeys.
func privateKeys() (signers []ssh.Signer) {
clientKeysMutex.Lock()
defer clientKeysMutex.Unlock()
for _, key := range clientKeys {
signers = append(signers, key)
}
return signers
}
// PrivateKeyFiles returns the filenames of private SSH keys loaded by
// LoadClientKeys.
func PrivateKeyFiles() []string {
clientKeysMutex.Lock()
defer clientKeysMutex.Unlock()
keyfiles := make([]string, 0, len(clientKeys))
for f := range clientKeys {
keyfiles = append(keyfiles, f)
}
return keyfiles
}
// PublicKeyFiles returns the filenames of public SSH keys loaded by
// LoadClientKeys.
func PublicKeyFiles() []string {
privkeys := PrivateKeyFiles()
pubkeys := make([]string, len(privkeys))
for i, priv := range privkeys {
pubkeys[i] = priv + PublicKeySuffix
}
return pubkeys
}
// publicKeyFiles returns the filenames of public SSH keys
// in the specified directory (all the files ending with .pub).
func publicKeyFiles(clientKeysDir string) ([]string, error) {
if clientKeysDir == "" {
return nil, nil
}
var keys []string
dir, err := os.Open(clientKeysDir)
if err != nil {
return nil, err
}
names, err := dir.Readdirnames(-1)
dir.Close()
if err != nil {
return nil, err
}
candidates := set.NewStrings(names...)
for _, name := range names {
if !strings.HasSuffix(name, PublicKeySuffix) {
continue
}
// If the private key filename also exists, add the file.
priv := name[:len(name)-len(PublicKeySuffix)]
if candidates.Contains(priv) {
keys = append(keys, filepath.Join(dir.Name(), name))
}
}
return keys, nil
}