forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cani.go
227 lines (190 loc) · 5.87 KB
/
cani.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package policy
import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"reflect"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/printers"
authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
"github.com/openshift/origin/pkg/client"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
)
const CanIRecommendedName = "can-i"
type canIOptions struct {
AllNamespaces bool
ListAll bool
Quiet bool
IgnoreScopes bool
User string
Groups []string
Scopes []string
Namespace string
SelfRulesReviewClient client.SelfSubjectRulesReviewsNamespacer
RulesReviewClient client.SubjectRulesReviewsNamespacer
SARClient client.SubjectAccessReviews
Printer printers.ResourcePrinter
Verb string
Resource schema.GroupVersionResource
ResourceName string
Out io.Writer
}
func NewCmdCanI(name, fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
o := &canIOptions{
Out: out,
}
cmd := &cobra.Command{
Use: name + " VERB RESOURCE [NAME]",
Short: "Check whether an action is allowed",
Long: "Check whether an action is allowed",
Run: func(cmd *cobra.Command, args []string) {
if reflect.DeepEqual(args, []string{"educate", "dolphins"}) {
fmt.Fprintln(o.Out, "Only liggitt can educate dolphins.")
return
}
if err := o.Complete(cmd, f, args); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageError(cmd, err.Error()))
}
allowed, err := o.Run()
kcmdutil.CheckErr(err)
if o.Quiet && !allowed {
os.Exit(2)
}
},
}
cmd.Flags().BoolVar(&o.AllNamespaces, "all-namespaces", o.AllNamespaces, "If true, check the specified action in all namespaces.")
cmd.Flags().BoolVar(&o.ListAll, "list", o.ListAll, "If true, list all the actions you can perform in a namespace, cannot be specified with --all-namespaces or a VERB RESOURCE")
cmd.Flags().BoolVarP(&o.Quiet, "quiet", "q", o.Quiet, "If true, suppress output and just return the exit code.")
cmd.Flags().BoolVar(&o.IgnoreScopes, "ignore-scopes", o.IgnoreScopes, "If true, disregard any scopes present on this request and evaluate considering full permissions.")
cmd.Flags().StringSliceVar(&o.Scopes, "scopes", o.Scopes, "Check the specified action using these scopes. By default, the scopes on the current token will be used.")
cmd.Flags().StringVar(&o.User, "user", o.User, "Check the specified action using this user instead of your user.")
cmd.Flags().StringSliceVar(&o.Groups, "groups", o.Groups, "Check the specified action using these groups instead of your groups.")
kcmdutil.AddPrinterFlags(cmd)
return cmd
}
const (
tabwriterMinWidth = 10
tabwriterWidth = 4
tabwriterPadding = 3
tabwriterPadChar = ' '
tabwriterFlags = 0
)
func (o *canIOptions) Complete(cmd *cobra.Command, f *clientcmd.Factory, args []string) error {
if o.ListAll && o.AllNamespaces {
return errors.New("--list and --all-namespaces are mutually exclusive")
}
if o.IgnoreScopes && len(o.Scopes) > 0 {
return errors.New("--scopes and --ignore-scopes are mutually exclusive")
}
switch len(args) {
case 3:
o.ResourceName = args[2]
fallthrough
case 2:
if o.ListAll {
return errors.New("VERB RESOURCE and --list are mutually exclusive")
}
restMapper, _ := f.Object()
o.Verb = args[0]
o.Resource = resourceFor(restMapper, args[1])
default:
if !o.ListAll {
return errors.New("you must specify two or three arguments: verb, resource, and optional resourceName")
}
}
var err error
oclient, _, err := f.Clients()
if err != nil {
return err
}
o.SelfRulesReviewClient = oclient
o.RulesReviewClient = oclient
o.SARClient = oclient
printer, err := f.PrinterForCommand(cmd, false, nil, printers.PrintOptions{})
if err != nil {
return err
}
o.Printer = printer
o.Namespace = metav1.NamespaceAll
if !o.AllNamespaces {
o.Namespace, _, err = f.DefaultNamespace()
if err != nil {
return err
}
}
if o.Quiet {
o.Out = ioutil.Discard
}
return nil
}
func (o *canIOptions) Run() (bool, error) {
if o.ListAll {
return true, o.listAllPermissions()
}
sar := &authorizationapi.SubjectAccessReview{
Action: authorizationapi.Action{
Namespace: o.Namespace,
Verb: o.Verb,
Group: o.Resource.Group,
Resource: o.Resource.Resource,
ResourceName: o.ResourceName,
},
User: o.User,
Groups: sets.NewString(o.Groups...),
}
if o.IgnoreScopes {
sar.Scopes = []string{}
}
if len(o.Scopes) > 0 {
sar.Scopes = o.Scopes
}
response, err := o.SARClient.SubjectAccessReviews().Create(sar)
if err != nil {
return false, err
}
if response.Allowed {
fmt.Fprintln(o.Out, "yes")
} else {
fmt.Fprint(o.Out, "no")
if len(response.EvaluationError) > 0 {
fmt.Fprintf(o.Out, " - %v", response.EvaluationError)
}
fmt.Fprintln(o.Out)
}
return response.Allowed, nil
}
func (o *canIOptions) listAllPermissions() error {
var rulesReviewResult runtime.Object
if len(o.User) == 0 && len(o.Groups) == 0 {
rulesReview := &authorizationapi.SelfSubjectRulesReview{}
if len(o.Scopes) > 0 {
rulesReview.Spec.Scopes = o.Scopes
}
whatCanIDo, err := o.SelfRulesReviewClient.SelfSubjectRulesReviews(o.Namespace).Create(rulesReview)
if err != nil {
return err
}
rulesReviewResult = whatCanIDo
} else {
rulesReview := &authorizationapi.SubjectRulesReview{
Spec: authorizationapi.SubjectRulesReviewSpec{
User: o.User,
Groups: o.Groups,
Scopes: o.Scopes,
},
}
whatCanYouDo, err := o.RulesReviewClient.SubjectRulesReviews(o.Namespace).Create(rulesReview)
if err != nil {
return err
}
rulesReviewResult = whatCanYouDo
}
return o.Printer.PrintObj(rulesReviewResult, o.Out)
}