/
authorisedkeys.go
130 lines (119 loc) · 4.04 KB
/
authorisedkeys.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
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package keyupdater
import (
"github.com/juju/errors"
"github.com/juju/names/v5"
"github.com/juju/utils/v3/ssh"
"github.com/juju/juju/apiserver/common"
apiservererrors "github.com/juju/juju/apiserver/errors"
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/rpc/params"
"github.com/juju/juju/state"
"github.com/juju/juju/state/watcher"
)
// KeyUpdater defines the methods on the keyupdater API end point.
type KeyUpdater interface {
AuthorisedKeys(args params.Entities) (params.StringsResults, error)
WatchAuthorisedKeys(args params.Entities) (params.NotifyWatchResults, error)
}
// KeyUpdaterAPI implements the KeyUpdater interface and is the concrete
// implementation of the api end point.
type KeyUpdaterAPI struct {
state *state.State
model *state.Model
resources facade.Resources
authorizer facade.Authorizer
getCanRead common.GetAuthFunc
}
var _ KeyUpdater = (*KeyUpdaterAPI)(nil)
// WatchAuthorisedKeys starts a watcher to track changes to the authorised ssh keys
// for the specified machines.
// The current implementation relies on global authorised keys being stored in the model config.
// This will change as new user management and authorisation functionality is added.
func (api *KeyUpdaterAPI) WatchAuthorisedKeys(arg params.Entities) (params.NotifyWatchResults, error) {
results := make([]params.NotifyWatchResult, len(arg.Entities))
canRead, err := api.getCanRead()
if err != nil {
return params.NotifyWatchResults{}, err
}
for i, entity := range arg.Entities {
tag, err := names.ParseTag(entity.Tag)
if err != nil {
results[i].Error = apiservererrors.ServerError(err)
continue
}
// 1. Check permissions
if !canRead(tag) {
results[i].Error = apiservererrors.ServerError(apiservererrors.ErrPerm)
continue
}
// 2. Check entity exists
if _, err := api.state.FindEntity(tag); err != nil {
if errors.IsNotFound(err) {
results[i].Error = apiservererrors.ServerError(apiservererrors.ErrPerm)
} else {
results[i].Error = apiservererrors.ServerError(err)
}
continue
}
// 3. Watch for changes
watch := api.model.WatchForModelConfigChanges()
// Consume the initial event.
if _, ok := <-watch.Changes(); ok {
results[i].NotifyWatcherId = api.resources.Register(watch)
} else {
err = watcher.EnsureErr(watch)
}
results[i].Error = apiservererrors.ServerError(err)
}
return params.NotifyWatchResults{Results: results}, nil
}
// AuthorisedKeys reports the authorised ssh keys for the specified machines.
// The current implementation relies on global authorised keys being stored in the model config.
// This will change as new user management and authorisation functionality is added.
func (api *KeyUpdaterAPI) AuthorisedKeys(arg params.Entities) (params.StringsResults, error) {
if len(arg.Entities) == 0 {
return params.StringsResults{}, nil
}
results := make([]params.StringsResult, len(arg.Entities))
// For now, authorised keys are global, common to all machines.
var keys []string
config, configErr := api.model.ModelConfig()
if configErr == nil {
keys = ssh.SplitAuthorisedKeys(config.AuthorizedKeys())
}
canRead, err := api.getCanRead()
if err != nil {
return params.StringsResults{}, err
}
for i, entity := range arg.Entities {
tag, err := names.ParseTag(entity.Tag)
if err != nil {
results[i].Error = apiservererrors.ServerError(err)
continue
}
// 1. Check permissions
if !canRead(tag) {
results[i].Error = apiservererrors.ServerError(apiservererrors.ErrPerm)
continue
}
// 2. Check entity exists
if _, err := api.state.FindEntity(tag); err != nil {
if errors.IsNotFound(err) {
results[i].Error = apiservererrors.ServerError(apiservererrors.ErrPerm)
} else {
results[i].Error = apiservererrors.ServerError(err)
}
continue
}
// 3. Get keys
if configErr == nil {
results[i].Result = keys
} else {
err = configErr
}
results[i].Error = apiservererrors.ServerError(err)
}
return params.StringsResults{Results: results}, nil
}