forked from openshift/origin
/
who_can.go
76 lines (61 loc) · 1.67 KB
/
who_can.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
package policy
import (
"fmt"
"github.com/golang/glog"
"github.com/spf13/cobra"
authorizationapi "github.com/openshift/origin/pkg/authorization/api"
"github.com/openshift/origin/pkg/client"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
)
type whoCanOptions struct {
bindingNamespace string
client client.Interface
verb string
resource string
}
func NewCmdWhoCan(f *clientcmd.Factory) *cobra.Command {
options := &whoCanOptions{}
cmd := &cobra.Command{
Use: "who-can",
Short: "who-can <verb> <resource>",
Long: `who-can <verb> <resource>`,
Run: func(cmd *cobra.Command, args []string) {
if !options.complete(cmd) {
return
}
var err error
if options.client, _, err = f.Clients(); err != nil {
glog.Fatalf("Error getting client: %v", err)
}
if options.bindingNamespace, err = f.DefaultNamespace(); err != nil {
glog.Fatalf("Error getting client: %v", err)
}
if err := options.run(); err != nil {
glog.Fatal(err)
}
},
}
return cmd
}
func (o *whoCanOptions) complete(cmd *cobra.Command) bool {
args := cmd.Flags().Args()
if len(args) != 2 {
cmd.Help()
return false
}
o.verb = args[0]
o.resource = args[1]
return true
}
func (o *whoCanOptions) run() error {
resourceAccessReview := &authorizationapi.ResourceAccessReview{}
resourceAccessReview.Resource = o.resource
resourceAccessReview.Verb = o.verb
resourceAccessReviewResponse, err := o.client.ResourceAccessReviews(o.bindingNamespace).Create(resourceAccessReview)
if err != nil {
return err
}
fmt.Printf("Users: %v\n", resourceAccessReviewResponse.Users)
fmt.Printf("Groups: %v\n", resourceAccessReviewResponse.Groups)
return nil
}