/
facade.go
65 lines (55 loc) · 1.73 KB
/
facade.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
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
// Package hostkeyreporter implements the API facade used by the
// hostkeyreporter worker.
package hostkeyreporter
import (
"github.com/juju/names"
"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/state"
)
func init() {
common.RegisterStandardFacade("HostKeyReporter", 1, newFacade)
}
// Backend defines the State API used by the hostkeyreporter facade.
type Backend interface {
SetSSHHostKeys(names.MachineTag, state.SSHHostKeys) error
}
// Facade implements the API required by the hostkeyreporter worker.
type Facade struct {
backend Backend
getCanModify common.GetAuthFunc
}
// New returns a new API facade for the hostkeyreporter worker.
func New(backend Backend, _ *common.Resources, authorizer common.Authorizer) (*Facade, error) {
return &Facade{
backend: backend,
getCanModify: func() (common.AuthFunc, error) {
return authorizer.AuthOwner, nil
},
}, nil
}
// ReportKeys sets the SSH host keys for one or more entities.
func (facade *Facade) ReportKeys(args params.SSHHostKeySet) (params.ErrorResults, error) {
results := params.ErrorResults{
Results: make([]params.ErrorResult, len(args.EntityKeys)),
}
canModify, err := facade.getCanModify()
if err != nil {
return results, err
}
for i, arg := range args.EntityKeys {
tag, err := names.ParseMachineTag(arg.Tag)
if err != nil {
results.Results[i].Error = common.ServerError(common.ErrPerm)
continue
}
err = common.ErrPerm
if canModify(tag) {
err = facade.backend.SetSSHHostKeys(tag, state.SSHHostKeys(arg.PublicKeys))
}
results.Results[i].Error = common.ServerError(err)
}
return results, nil
}