77 "strings"
88
99 "github.com/replicatedhq/troubleshoot/pkg/redact"
10+ authorizationv1 "k8s.io/api/authorization/v1"
1011 corev1 "k8s.io/api/core/v1"
1112 apiextensionsv1beta1clientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1beta1"
1213 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -32,6 +33,8 @@ type ClusterResourcesOutput struct {
3233 ImagePullSecretsErrors []byte `json:"cluster-resources/image-pull-secrets-errors.json,omitempty"`
3334 Nodes []byte `json:"cluster-resources/nodes.json,omitempty"`
3435 NodesErrors []byte `json:"cluster-resources/nodes-errors.json,omitempty"`
36+ AuthCanI map [string ][]byte `json:"cluster-resources/auth-cani-list,omitempty"`
37+ AuthCanIErrors []byte `json:"cluster-resources/auth-cani-list-errors.json,omitempty"`
3538}
3639
3740func ClusterResources (ctx * Context ) ([]byte , error ) {
@@ -131,6 +134,14 @@ func ClusterResources(ctx *Context) ([]byte, error) {
131134 return nil , err
132135 }
133136
137+ // auth cani
138+ authCanI , authCanIErrors := authCanI (client , namespaceNames )
139+ clusterResourcesOutput .AuthCanI = authCanI
140+ clusterResourcesOutput .AuthCanIErrors , err = marshalNonNil (authCanIErrors )
141+ if err != nil {
142+ return nil , err
143+ }
144+
134145 if ctx .Redact {
135146 clusterResourcesOutput , err = clusterResourcesOutput .Redact ()
136147 if err != nil {
@@ -360,6 +371,71 @@ func nodes(client *kubernetes.Clientset) ([]byte, []string) {
360371 return b , nil
361372}
362373
374+ func authCanI (client * kubernetes.Clientset , namespaces []string ) (map [string ][]byte , map [string ]string ) {
375+ // https://github.com/kubernetes/kubernetes/blob/master/pkg/kubectl/cmd/auth/cani.go
376+
377+ authListByNamespace := make (map [string ][]byte )
378+ errorsByNamespace := make (map [string ]string )
379+
380+ for _ , namespace := range namespaces {
381+ fmt .Println (namespace )
382+ sar := & authorizationv1.SelfSubjectRulesReview {
383+ Spec : authorizationv1.SelfSubjectRulesReviewSpec {
384+ Namespace : namespace ,
385+ },
386+ }
387+ response , err := client .AuthorizationV1 ().SelfSubjectRulesReviews ().Create (sar )
388+ if err != nil {
389+ errorsByNamespace [namespace ] = err .Error ()
390+ continue
391+ }
392+
393+ // // breakdownRules := []rbacv1.PolicyRule{}
394+ // // for _, rule := range convertToPolicyRule(response.Status) {
395+ // // breakdownRules = append(breakdownRules, rbacutil.BreakdownRule(rule)...)
396+ // // }
397+
398+ // // compactRules, err := rbacutil.CompactRules(breakdownRules)
399+ // // if err != nil {
400+ // // errorsByNamespace[namespace] = err.Error()
401+ // // continue
402+ // // }
403+
404+ b , err := json .MarshalIndent (response .Status , "" , " " )
405+ if err != nil {
406+ errorsByNamespace [namespace ] = err .Error ()
407+ continue
408+ }
409+
410+ fmt .Printf ("%s\n " , b )
411+ authListByNamespace [namespace + ".json" ] = b
412+ }
413+
414+ return authListByNamespace , errorsByNamespace
415+ }
416+
417+ // not exprted from: https://github.com/kubernetes/kubernetes/blob/master/pkg/kubectl/cmd/auth/cani.go#L339
418+ func convertToPolicyRule (status authorizationv1.SubjectRulesReviewStatus ) []rbacv1.PolicyRule {
419+ ret := []rbacv1.PolicyRule {}
420+ for _ , resource := range status .ResourceRules {
421+ ret = append (ret , rbacv1.PolicyRule {
422+ Verbs : resource .Verbs ,
423+ APIGroups : resource .APIGroups ,
424+ Resources : resource .Resources ,
425+ ResourceNames : resource .ResourceNames ,
426+ })
427+ }
428+
429+ for _ , nonResource := range status .NonResourceRules {
430+ ret = append (ret , rbacv1.PolicyRule {
431+ Verbs : nonResource .Verbs ,
432+ NonResourceURLs : nonResource .NonResourceURLs ,
433+ })
434+ }
435+
436+ return ret
437+ }
438+
363439func (c * ClusterResourcesOutput ) Redact () (* ClusterResourcesOutput , error ) {
364440 namespaces , err := redact .Redact (c .Namespaces )
365441 if err != nil {
@@ -393,6 +469,7 @@ func (c *ClusterResourcesOutput) Redact() (*ClusterResourcesOutput, error) {
393469 if err != nil {
394470 return nil , err
395471 }
472+
396473 return & ClusterResourcesOutput {
397474 Namespaces : namespaces ,
398475 NamespacesErrors : c .NamespacesErrors ,
@@ -412,5 +489,7 @@ func (c *ClusterResourcesOutput) Redact() (*ClusterResourcesOutput, error) {
412489 CustomResourceDefinitionsErrors : c .CustomResourceDefinitionsErrors ,
413490 ImagePullSecrets : c .ImagePullSecrets ,
414491 ImagePullSecretsErrors : c .ImagePullSecretsErrors ,
492+ AuthCanI : c .AuthCanI ,
493+ AuthCanIErrors : c .AuthCanIErrors ,
415494 }, nil
416495}
0 commit comments