@@ -33,6 +33,11 @@ func (k *Kubernetes) ResourcesList(ctx context.Context, gvk *schema.GroupVersion
3333 if err != nil {
3434 return nil , err
3535 }
36+
37+ if ! k .isAllowed (gvk ) {
38+ return nil , fmt .Errorf ("resource not allowed: %s" , gvk .String ())
39+ }
40+
3641 // Check if operation is allowed for all namespaces (applicable for namespaced resources)
3742 isNamespaced , _ := k .isNamespaced (gvk )
3843 if isNamespaced && ! k .canIUse (ctx , gvr , namespace , "list" ) && namespace == "" {
@@ -49,6 +54,11 @@ func (k *Kubernetes) ResourcesGet(ctx context.Context, gvk *schema.GroupVersionK
4954 if err != nil {
5055 return nil , err
5156 }
57+
58+ if ! k .isAllowed (gvk ) {
59+ return nil , fmt .Errorf ("resource not allowed: %s" , gvk .String ())
60+ }
61+
5262 // If it's a namespaced resource and namespace wasn't provided, try to use the default configured one
5363 if namespaced , nsErr := k .isNamespaced (gvk ); nsErr == nil && namespaced {
5464 namespace = k .NamespaceOrDefault (namespace )
@@ -75,6 +85,11 @@ func (k *Kubernetes) ResourcesDelete(ctx context.Context, gvk *schema.GroupVersi
7585 if err != nil {
7686 return err
7787 }
88+
89+ if ! k .isAllowed (gvk ) {
90+ return fmt .Errorf ("resource not allowed: %s" , gvk .String ())
91+ }
92+
7893 // If it's a namespaced resource and namespace wasn't provided, try to use the default configured one
7994 if namespaced , nsErr := k .isNamespaced (gvk ); nsErr == nil && namespaced {
8095 namespace = k .NamespaceOrDefault (namespace )
@@ -136,6 +151,11 @@ func (k *Kubernetes) resourcesCreateOrUpdate(ctx context.Context, resources []*u
136151 if rErr != nil {
137152 return nil , rErr
138153 }
154+
155+ if ! k .isAllowed (& gvk ) {
156+ return nil , fmt .Errorf ("resource not allowed: %s" , gvk .String ())
157+ }
158+
139159 namespace := obj .GetNamespace ()
140160 // If it's a namespaced resource and namespace wasn't provided, try to use the default configured one
141161 if namespaced , nsErr := k .isNamespaced (& gvk ); nsErr == nil && namespaced {
@@ -163,6 +183,30 @@ func (k *Kubernetes) resourceFor(gvk *schema.GroupVersionKind) (*schema.GroupVer
163183 return & m .Resource , nil
164184}
165185
186+ // isAllowed checks the resource is in denied list or not.
187+ // If it is in denied list, this function returns false.
188+ func (k * Kubernetes ) isAllowed (gvk * schema.GroupVersionKind ) bool {
189+ if k .manager .StaticConfig == nil {
190+ return true
191+ }
192+
193+ for _ , val := range k .manager .StaticConfig .DeniedResources {
194+ // If kind is empty, that means Group/Version pair is denied entirely
195+ if val .Kind == "" {
196+ if gvk .Group == val .Group && gvk .Version == val .Version {
197+ return false
198+ }
199+ }
200+ if gvk .Group == val .Group &&
201+ gvk .Version == val .Version &&
202+ gvk .Kind == val .Kind {
203+ return false
204+ }
205+ }
206+
207+ return true
208+ }
209+
166210func (k * Kubernetes ) isNamespaced (gvk * schema.GroupVersionKind ) (bool , error ) {
167211 apiResourceList , err := k .manager .discoveryClient .ServerResourcesForGroupVersion (gvk .GroupVersion ().String ())
168212 if err != nil {
0 commit comments