|
| 1 | +/* |
| 2 | +Copyright 2019 Cornelius Weig |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package client |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/pkg/errors" |
| 21 | + "github.com/sirupsen/logrus" |
| 22 | + "github.com/spf13/viper" |
| 23 | + "k8s.io/api/authorization/v1" |
| 24 | + authv1 "k8s.io/client-go/kubernetes/typed/authorization/v1" |
| 25 | + "sync" |
| 26 | +) |
| 27 | + |
| 28 | +/* |
| 29 | +restConfig, _ := flags.ToRESTConfig() |
| 30 | +authClient, _ := authv1.NewForConfig(restConfig) |
| 31 | +allowed, err := CheckResourceAccessPar(authClient, resources) |
| 32 | +if err != nil { |
| 33 | + return nil, errors.Wrap(err, "check resource access") |
| 34 | +} |
| 35 | +
|
| 36 | +allowedResourceTypes := ToResourceTypes(allowed)*/ |
| 37 | + |
| 38 | +func CheckResourceAccessPar(authClient *authv1.AuthorizationV1Client, grs []groupResource) (allowed []groupResource, err error) { |
| 39 | + reviews := authClient.SelfSubjectAccessReviews() |
| 40 | + |
| 41 | + allowedChan := make(chan groupResource) |
| 42 | + forbiddenChan := make(chan groupResource) |
| 43 | + |
| 44 | + group := sync.WaitGroup{} |
| 45 | + |
| 46 | + namespace := viper.GetString("namespace") |
| 47 | + for _, gr := range grs { |
| 48 | + namespace := namespace |
| 49 | + gr := gr |
| 50 | + group.Add(1) |
| 51 | + go func(allowed, forbidden chan<- groupResource) { |
| 52 | + defer group.Done() |
| 53 | + |
| 54 | + // This seems to be a bug in kubernetes. If namespace is set for non-namespaced |
| 55 | + // resources, the access is reported as "allowed", but in fact it is forbidden. |
| 56 | + if !gr.APIResource.Namespaced { |
| 57 | + namespace = "" |
| 58 | + } |
| 59 | + |
| 60 | + review := v1.SelfSubjectAccessReview{ |
| 61 | + Spec: v1.SelfSubjectAccessReviewSpec{ |
| 62 | + ResourceAttributes: &v1.ResourceAttributes{ |
| 63 | + Verb: "list", |
| 64 | + Resource: gr.APIResource.Name, |
| 65 | + Group: gr.APIGroup, |
| 66 | + Namespace: namespace, |
| 67 | + }, |
| 68 | + }, |
| 69 | + } |
| 70 | + accessReview, e := reviews.Create(&review) |
| 71 | + if e != nil { |
| 72 | + err = errors.Wrap(e, "retrieve authority") |
| 73 | + return |
| 74 | + } |
| 75 | + logrus.Info(accessReview) |
| 76 | + if accessReview.Status.Allowed { |
| 77 | + allowed <- gr |
| 78 | + } else { |
| 79 | + forbidden <- gr |
| 80 | + } |
| 81 | + }(allowedChan, forbiddenChan) |
| 82 | + } |
| 83 | + |
| 84 | + forbidden := []groupResource{} |
| 85 | + go func(c <-chan groupResource) { |
| 86 | + for gr := range c { |
| 87 | + forbidden = append(forbidden, gr) |
| 88 | + } |
| 89 | + }(forbiddenChan) |
| 90 | + go func(c <-chan groupResource) { |
| 91 | + for gr := range c { |
| 92 | + allowed = append(allowed, gr) |
| 93 | + } |
| 94 | + }(allowedChan) |
| 95 | + |
| 96 | + group.Wait() |
| 97 | + |
| 98 | + close(allowedChan) |
| 99 | + close(forbiddenChan) |
| 100 | + |
| 101 | + if len(forbidden) > 0 { |
| 102 | + logrus.Warnf("The following resources may not be read: %s", ToResourceTypes(forbidden)) |
| 103 | + } |
| 104 | + |
| 105 | + logrus.Debugf("Readable: %s", ToResourceTypes(allowed)) |
| 106 | + |
| 107 | + return |
| 108 | +} |
| 109 | + |
| 110 | +func CheckResourceAccess(authClient *authv1.AuthorizationV1Client, grs []groupResource) (allowed []groupResource, err error) { |
| 111 | + forbidden := []groupResource{} |
| 112 | + reviews := authClient.SelfSubjectAccessReviews() |
| 113 | + |
| 114 | + namespace := viper.GetString("namespace") |
| 115 | + for _, gr := range grs { |
| 116 | + namespace := namespace |
| 117 | + gr := gr |
| 118 | + // This seems to be a bug in kubernetes. If namespace is set for non-namespaced |
| 119 | + // resources, the access is reported as "allowed", but in fact it is forbidden. |
| 120 | + if !gr.APIResource.Namespaced { |
| 121 | + namespace = "" |
| 122 | + } |
| 123 | + |
| 124 | + review := v1.SelfSubjectAccessReview{ |
| 125 | + Spec: v1.SelfSubjectAccessReviewSpec{ |
| 126 | + ResourceAttributes: &v1.ResourceAttributes{ |
| 127 | + Verb: "list", |
| 128 | + Resource: gr.APIResource.Name, |
| 129 | + Group: gr.APIGroup, |
| 130 | + Namespace: namespace, |
| 131 | + }, |
| 132 | + }, |
| 133 | + } |
| 134 | + accessReview, e := reviews.Create(&review) |
| 135 | + if e != nil { |
| 136 | + err = errors.Wrap(e, "retrieve authority") |
| 137 | + return |
| 138 | + } |
| 139 | + logrus.Info(accessReview) |
| 140 | + if accessReview.Status.Allowed { |
| 141 | + allowed = append(allowed, gr) |
| 142 | + } else { |
| 143 | + forbidden = append(forbidden, gr) |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + if len(forbidden) > 0 { |
| 148 | + logrus.Warnf("The following resources may not be read: %s", ToResourceTypes(forbidden)) |
| 149 | + } |
| 150 | + |
| 151 | + logrus.Debugf("Readable: %s", ToResourceTypes(allowed)) |
| 152 | + |
| 153 | + return |
| 154 | +} |
0 commit comments