github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/apiserver/restricted_root.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package apiserver 5 6 import ( 7 "github.com/juju/rpcreflect" 8 9 "github.com/juju/juju/rpc" 10 ) 11 12 // restrictRoot wraps the provided root so that the check function is 13 // called on all method lookups. If the check returns an error the API 14 // call is blocked. 15 func restrictRoot(root rpc.Root, check func(string, string) error) *restrictedRoot { 16 return &restrictedRoot{ 17 Root: root, 18 check: check, 19 } 20 } 21 22 type restrictedRoot struct { 23 rpc.Root 24 check func(facadeName, methodName string) error 25 } 26 27 // FindMethod implements rpc.Root. 28 func (r *restrictedRoot) FindMethod(facadeName string, version int, methodName string) (rpcreflect.MethodCaller, error) { 29 if err := r.check(facadeName, methodName); err != nil { 30 return nil, err 31 } 32 return r.Root.FindMethod(facadeName, version, methodName) 33 } 34 35 // restrictAll blocks all API requests, returned a fixed error. 36 func restrictAll(root rpc.Root, err error) *restrictedRoot { 37 return restrictRoot(root, func(string, string) error { 38 return err 39 }) 40 }