-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestricted_root.go
47 lines (40 loc) · 1.49 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
41
42
43
44
45
46
47
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package apiserver
import (
"github.com/juju/errors"
"github.com/juju/utils/set"
"github.com/juju/juju/rpc"
"github.com/juju/juju/rpc/rpcreflect"
)
// restrictedRoot restricts API calls to the environment manager and
// user manager when accessed through the root path on the API server.
type restrictedRoot struct {
rpc.MethodFinder
}
// newRestrictedRoot returns a new restrictedRoot.
func newRestrictedRoot(finder rpc.MethodFinder) *restrictedRoot {
return &restrictedRoot{finder}
}
// The restrictedRootNames are the root names that can be accessed at the root
// of the API server. Any facade added here needs to work across environment
// boundaries.
var restrictedRootNames = set.NewStrings(
"EnvironmentManager",
"UserManager",
)
// FindMethod returns a not supported error if the rootName is not one
// of the facades available at the server root when there is no active
// environment.
func (r *restrictedRoot) FindMethod(rootName string, version int, methodName string) (rpcreflect.MethodCaller, error) {
// The lookup of the name is done first to return a not found error if the
// user is looking for a method that we just don't have.
caller, err := r.MethodFinder.FindMethod(rootName, version, methodName)
if err != nil {
return nil, err
}
if !restrictedRootNames.Contains(rootName) {
return nil, errors.NotSupportedf("logged in to server, no environment, %q", rootName)
}
return caller, nil
}