forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
who_can.go
89 lines (71 loc) · 2.21 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
77
78
79
80
81
82
83
84
85
86
87
88
89
package policy
import (
"errors"
"fmt"
"io"
"strings"
"github.com/spf13/cobra"
kcmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
authorizationapi "github.com/openshift/origin/pkg/authorization/api"
"github.com/openshift/origin/pkg/client"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
)
const WhoCanRecommendedName = "who-can"
type whoCanOptions struct {
bindingNamespace string
client client.Interface
verb string
resource string
}
func NewCmdWhoCan(name, fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
options := &whoCanOptions{}
cmd := &cobra.Command{
Use: "who-can VERB RESOURCE",
Short: "Indicates which users can perform the action on the resource.",
Long: `Indicates which users can perform the action on the resource.`,
Run: func(cmd *cobra.Command, args []string) {
if err := options.complete(args); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageError(cmd, err.Error()))
}
var err error
if options.client, _, err = f.Clients(); err != nil {
kcmdutil.CheckErr(err)
}
if options.bindingNamespace, err = f.DefaultNamespace(); err != nil {
kcmdutil.CheckErr(err)
}
if err := options.run(); err != nil {
kcmdutil.CheckErr(err)
}
},
}
return cmd
}
func (o *whoCanOptions) complete(args []string) error {
if len(args) != 2 {
return errors.New("You must specify two arguments: verb and resource")
}
o.verb = args[0]
o.resource = args[1]
return nil
}
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
}
if len(resourceAccessReviewResponse.Users) == 0 {
fmt.Printf("Users: none\n\n")
} else {
fmt.Printf("Users: %s\n\n", strings.Join(resourceAccessReviewResponse.Users.List(), "\n "))
}
if len(resourceAccessReviewResponse.Groups) == 0 {
fmt.Printf("Groups: none\n\n")
} else {
fmt.Printf("Groups: %s\n\n", strings.Join(resourceAccessReviewResponse.Groups.List(), "\n "))
}
return nil
}