/
keymanager.go
418 lines (376 loc) · 12.8 KB
/
keymanager.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package keymanager
import (
"fmt"
"strings"
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/utils"
"github.com/juju/utils/set"
"github.com/juju/utils/ssh"
"gopkg.in/juju/names.v2"
"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/state"
)
var logger = loggo.GetLogger("juju.apiserver.keymanager")
func init() {
common.RegisterStandardFacade("KeyManager", 1, NewKeyManagerAPI)
}
// The comment values used by juju internal ssh keys.
var internalComments = set.NewStrings([]string{"juju-client-key", "juju-system-key"}...)
// KeyManager defines the methods on the keymanager API end point.
type KeyManager interface {
ListKeys(arg params.ListSSHKeys) (params.StringsResults, error)
AddKeys(arg params.ModifyUserSSHKeys) (params.ErrorResults, error)
DeleteKeys(arg params.ModifyUserSSHKeys) (params.ErrorResults, error)
ImportKeys(arg params.ModifyUserSSHKeys) (params.ErrorResults, error)
}
// KeyManagerAPI implements the KeyUpdater interface and is the concrete
// implementation of the api end point.
type KeyManagerAPI struct {
state *state.State
resources facade.Resources
authorizer facade.Authorizer
canRead func(string) bool
canWrite func(string) bool
check *common.BlockChecker
}
var _ KeyManager = (*KeyManagerAPI)(nil)
// NewKeyManagerAPI creates a new server-side keyupdater API end point.
func NewKeyManagerAPI(st *state.State, resources facade.Resources, authorizer facade.Authorizer) (*KeyManagerAPI, error) {
// Only clients and environment managers can access the key manager service.
if !authorizer.AuthClient() && !authorizer.AuthModelManager() {
return nil, common.ErrPerm
}
env, err := st.Model()
if err != nil {
return nil, errors.Trace(err)
}
// For gccgo interface comparisons, we need a Tag.
owner := names.Tag(env.Owner())
// TODO(wallyworld) - replace stub with real canRead function
// For now, only admins can read authorised ssh keys.
canRead := func(user string) bool {
// Are we a machine agent operating as the system identity?
if user == config.JujuSystemKey {
_, ismachinetag := authorizer.GetAuthTag().(names.MachineTag)
return ismachinetag
}
return authorizer.GetAuthTag() == owner
}
// TODO(wallyworld) - replace stub with real canWrite function
// For now, only admins can write authorised ssh keys for users.
// Machine agents can write the juju-system-key.
canWrite := func(user string) bool {
// Are we a machine agent writing the Juju system key.
if user == config.JujuSystemKey {
_, ismachinetag := authorizer.GetAuthTag().(names.MachineTag)
return ismachinetag
}
// No point looking to see if the user exists as we are not
// yet storing keys on the user.
return authorizer.GetAuthTag() == owner
}
return &KeyManagerAPI{
state: st,
resources: resources,
authorizer: authorizer,
canRead: canRead,
canWrite: canWrite,
check: common.NewBlockChecker(st),
}, nil
}
// ListKeys returns the authorised ssh keys for the specified users.
func (api *KeyManagerAPI) ListKeys(arg params.ListSSHKeys) (params.StringsResults, error) {
if len(arg.Entities.Entities) == 0 {
return params.StringsResults{}, nil
}
results := make([]params.StringsResult, len(arg.Entities.Entities))
// For now, authorised keys are global, common to all users.
var keyInfo []string
cfg, configErr := api.state.ModelConfig()
if configErr == nil {
keys := ssh.SplitAuthorisedKeys(cfg.AuthorizedKeys())
keyInfo = parseKeys(keys, arg.Mode)
}
for i, entity := range arg.Entities.Entities {
// NOTE: entity.Tag isn't a tag, but a username.
if !api.canRead(entity.Tag) {
results[i].Error = common.ServerError(common.ErrPerm)
continue
}
// All keys are global, no need to look up the user.
if configErr == nil {
results[i].Result = keyInfo
}
results[i].Error = common.ServerError(configErr)
}
return params.StringsResults{Results: results}, nil
}
func parseKeys(keys []string, mode ssh.ListMode) (keyInfo []string) {
for _, key := range keys {
fingerprint, comment, err := ssh.KeyFingerprint(key)
if err != nil {
keyInfo = append(keyInfo, fmt.Sprintf("Invalid key: %v", key))
continue
}
// Only including user added keys not internal ones.
if internalComments.Contains(comment) {
continue
}
if mode == ssh.FullKeys {
keyInfo = append(keyInfo, key)
} else {
shortKey := fingerprint
if comment != "" {
shortKey += fmt.Sprintf(" (%s)", comment)
}
keyInfo = append(keyInfo, shortKey)
}
}
return keyInfo
}
func (api *KeyManagerAPI) writeSSHKeys(sshKeys []string) error {
// Write out the new keys.
keyStr := strings.Join(sshKeys, "\n")
attrs := map[string]interface{}{config.AuthorizedKeysKey: keyStr}
// TODO(waigani) 2014-03-17 bug #1293324
// Pass in validation to ensure SSH keys
// have not changed underfoot
err := api.state.UpdateModelConfig(attrs, nil, nil)
if err != nil {
return fmt.Errorf("writing environ config: %v", err)
}
return nil
}
// currentKeyDataForAdd gathers data used when adding ssh keys.
func (api *KeyManagerAPI) currentKeyDataForAdd() (keys []string, fingerprints set.Strings, err error) {
fingerprints = make(set.Strings)
cfg, err := api.state.ModelConfig()
if err != nil {
return nil, nil, fmt.Errorf("reading current key data: %v", err)
}
keys = ssh.SplitAuthorisedKeys(cfg.AuthorizedKeys())
for _, key := range keys {
fingerprint, _, err := ssh.KeyFingerprint(key)
if err != nil {
logger.Warningf("ignoring invalid ssh key %q: %v", key, err)
}
fingerprints.Add(fingerprint)
}
return keys, fingerprints, nil
}
// AddKeys adds new authorised ssh keys for the specified user.
func (api *KeyManagerAPI) AddKeys(arg params.ModifyUserSSHKeys) (params.ErrorResults, error) {
if err := api.check.ChangeAllowed(); err != nil {
return params.ErrorResults{}, errors.Trace(err)
}
result := params.ErrorResults{
Results: make([]params.ErrorResult, len(arg.Keys)),
}
if len(arg.Keys) == 0 {
return result, nil
}
if !api.canWrite(arg.User) {
return params.ErrorResults{}, common.ServerError(common.ErrPerm)
}
// For now, authorised keys are global, common to all users.
sshKeys, currentFingerprints, err := api.currentKeyDataForAdd()
if err != nil {
return params.ErrorResults{}, common.ServerError(fmt.Errorf("reading current key data: %v", err))
}
// Ensure we are not going to add invalid or duplicate keys.
result.Results = make([]params.ErrorResult, len(arg.Keys))
for i, key := range arg.Keys {
fingerprint, _, err := ssh.KeyFingerprint(key)
if err != nil {
result.Results[i].Error = common.ServerError(fmt.Errorf("invalid ssh key: %s", key))
continue
}
if currentFingerprints.Contains(fingerprint) {
result.Results[i].Error = common.ServerError(fmt.Errorf("duplicate ssh key: %s", key))
continue
}
sshKeys = append(sshKeys, key)
}
err = api.writeSSHKeys(sshKeys)
if err != nil {
return params.ErrorResults{}, common.ServerError(err)
}
return result, nil
}
type importedSSHKey struct {
key string
fingerprint string
err error
}
// Override for testing
var RunSSHImportId = runSSHImportId
func runSSHImportId(keyId string) (string, error) {
return utils.RunCommand("ssh-import-id", "-o", "-", keyId)
}
// runSSHKeyImport uses ssh-import-id to find the ssh keys for the specified key ids.
func runSSHKeyImport(keyIds []string) map[string][]importedSSHKey {
importResults := make(map[string][]importedSSHKey, len(keyIds))
for _, keyId := range keyIds {
keyInfo := []importedSSHKey{}
output, err := RunSSHImportId(keyId)
if err != nil {
keyInfo = append(keyInfo, importedSSHKey{err: err})
continue
}
lines := strings.Split(output, "\n")
hasKey := false
for _, line := range lines {
if !strings.HasPrefix(line, "ssh-") {
continue
}
hasKey = true
// ignore key comment (e.g., user@host)
fingerprint, _, err := ssh.KeyFingerprint(line)
keyInfo = append(keyInfo, importedSSHKey{
key: line,
fingerprint: fingerprint,
err: errors.Annotatef(err, "invalid ssh key for %s", keyId),
})
}
if !hasKey {
keyInfo = append(keyInfo, importedSSHKey{
err: errors.Errorf("invalid ssh key id: %s", keyId),
})
}
importResults[keyId] = keyInfo
}
return importResults
}
// ImportKeys imports new authorised ssh keys from the specified key ids for the specified user.
func (api *KeyManagerAPI) ImportKeys(arg params.ModifyUserSSHKeys) (params.ErrorResults, error) {
if err := api.check.ChangeAllowed(); err != nil {
return params.ErrorResults{}, errors.Trace(err)
}
result := params.ErrorResults{
Results: make([]params.ErrorResult, len(arg.Keys)),
}
if len(arg.Keys) == 0 {
return result, nil
}
if !api.canWrite(arg.User) {
return params.ErrorResults{}, common.ServerError(common.ErrPerm)
}
// For now, authorised keys are global, common to all users.
sshKeys, currentFingerprints, err := api.currentKeyDataForAdd()
if err != nil {
return params.ErrorResults{}, common.ServerError(fmt.Errorf("reading current key data: %v", err))
}
importedKeyInfo := runSSHKeyImport(arg.Keys)
// Ensure we are not going to add invalid or duplicate keys.
result.Results = make([]params.ErrorResult, len(importedKeyInfo))
for i, key := range arg.Keys {
compoundErr := ""
for _, keyInfo := range importedKeyInfo[key] {
if keyInfo.err != nil {
compoundErr += fmt.Sprintf("%v\n", keyInfo.err)
continue
}
if currentFingerprints.Contains(keyInfo.fingerprint) {
compoundErr += fmt.Sprintf("%v\n", errors.Errorf("duplicate ssh key: %s", keyInfo.key))
continue
}
sshKeys = append(sshKeys, keyInfo.key)
}
if compoundErr != "" {
result.Results[i].Error = common.ServerError(errors.Errorf(strings.TrimSuffix(compoundErr, "\n")))
}
}
err = api.writeSSHKeys(sshKeys)
if err != nil {
return params.ErrorResults{}, common.ServerError(err)
}
return result, nil
}
// currentKeyDataForDelete gathers data used when deleting ssh keys.
func (api *KeyManagerAPI) currentKeyDataForDelete() (
currentKeys []string, byFingerprint map[string]string, byComment map[string]string, err error) {
cfg, err := api.state.ModelConfig()
if err != nil {
return nil, nil, nil, fmt.Errorf("reading current key data: %v", err)
}
// For now, authorised keys are global, common to all users.
currentKeys = ssh.SplitAuthorisedKeys(cfg.AuthorizedKeys())
// Make two maps that index keys by fingerprint and by comment for fast
// lookup of keys to delete which may be given as either.
byFingerprint = make(map[string]string)
byComment = make(map[string]string)
for _, key := range currentKeys {
fingerprint, comment, err := ssh.KeyFingerprint(key)
if err != nil {
logger.Debugf("keeping unrecognised existing ssh key %q: %v", key, err)
continue
}
byFingerprint[fingerprint] = key
if comment != "" {
byComment[comment] = key
}
}
return currentKeys, byFingerprint, byComment, nil
}
// DeleteKeys deletes the authorised ssh keys for the specified user.
func (api *KeyManagerAPI) DeleteKeys(arg params.ModifyUserSSHKeys) (params.ErrorResults, error) {
if err := api.check.ChangeAllowed(); err != nil {
return params.ErrorResults{}, errors.Trace(err)
}
result := params.ErrorResults{
Results: make([]params.ErrorResult, len(arg.Keys)),
}
if len(arg.Keys) == 0 {
return result, nil
}
if !api.canWrite(arg.User) {
return params.ErrorResults{}, common.ServerError(common.ErrPerm)
}
allKeys, byFingerprint, byComment, err := api.currentKeyDataForDelete()
if err != nil {
return params.ErrorResults{}, common.ServerError(fmt.Errorf("reading current key data: %v", err))
}
// Record the keys to be deleted in the second pass.
keysToDelete := make(set.Strings)
// Find the keys corresponding to the specified key fingerprints or comments.
for i, keyId := range arg.Keys {
// Is given keyId a fingerprint?
key, ok := byFingerprint[keyId]
if ok {
keysToDelete.Add(key)
continue
}
// Not a fingerprint, is it a comment?
key, ok = byComment[keyId]
if ok {
if internalComments.Contains(keyId) {
result.Results[i].Error = common.ServerError(fmt.Errorf("may not delete internal key: %s", keyId))
continue
}
keysToDelete.Add(key)
continue
}
result.Results[i].Error = common.ServerError(fmt.Errorf("invalid ssh key: %s", keyId))
}
var keysToWrite []string
// Add back only the keys that are not deleted, preserving the order.
for _, key := range allKeys {
if !keysToDelete.Contains(key) {
keysToWrite = append(keysToWrite, key)
}
}
if len(keysToWrite) == 0 {
return params.ErrorResults{}, common.ServerError(fmt.Errorf("cannot delete all keys"))
}
err = api.writeSSHKeys(keysToWrite)
if err != nil {
return params.ErrorResults{}, common.ServerError(err)
}
return result, nil
}