-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestricted_root.go
40 lines (33 loc) · 1.05 KB
/
restricted_root.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package apiserver
import (
"github.com/juju/rpcreflect"
"github.com/juju/juju/rpc"
)
// restrictRoot wraps the provided root so that the check function is
// called on all method lookups. If the check returns an error the API
// call is blocked.
func restrictRoot(root rpc.Root, check func(string, string) error) *restrictedRoot {
return &restrictedRoot{
Root: root,
check: check,
}
}
type restrictedRoot struct {
rpc.Root
check func(facadeName, methodName string) error
}
// FindMethod implements rpc.Root.
func (r *restrictedRoot) FindMethod(facadeName string, version int, methodName string) (rpcreflect.MethodCaller, error) {
if err := r.check(facadeName, methodName); err != nil {
return nil, err
}
return r.Root.FindMethod(facadeName, version, methodName)
}
// restrictAll blocks all API requests, returned a fixed error.
func restrictAll(root rpc.Root, err error) *restrictedRoot {
return restrictRoot(root, func(string, string) error {
return err
})
}