diff --git a/test/integration/auth/authz_config_test.go b/test/integration/auth/authz_config_test.go new file mode 100644 index 00000000000..f5be54e3540 --- /dev/null +++ b/test/integration/auth/authz_config_test.go @@ -0,0 +1,95 @@ +/* +Copyright 2023 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package auth + +import ( + "context" + "os" + "path/filepath" + "testing" + + authorizationv1 "k8s.io/api/authorization/v1" + rbacv1 "k8s.io/api/rbac/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apiserver/pkg/features" + utilfeature "k8s.io/apiserver/pkg/util/feature" + clientset "k8s.io/client-go/kubernetes" + featuregatetesting "k8s.io/component-base/featuregate/testing" + kubeapiservertesting "k8s.io/kubernetes/cmd/kube-apiserver/app/testing" + "k8s.io/kubernetes/test/integration/authutil" + "k8s.io/kubernetes/test/integration/framework" +) + +func TestAuthzConfig(t *testing.T) { + defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.StructuredAuthorizationConfiguration, true)() + + dir := t.TempDir() + configFileName := filepath.Join(dir, "config.yaml") + if err := os.WriteFile(configFileName, []byte(` +apiVersion: apiserver.config.k8s.io/v1alpha1 +kind: AuthorizationConfiguration +authorizers: +- type: RBAC + name: rbac +`), os.FileMode(0644)); err != nil { + t.Fatal(err) + } + + server := kubeapiservertesting.StartTestServerOrDie( + t, + nil, + []string{"--authorization-config=" + configFileName}, + framework.SharedEtcd(), + ) + t.Cleanup(server.TearDownFn) + + adminClient := clientset.NewForConfigOrDie(server.ClientConfig) + + sar := &authorizationv1.SubjectAccessReview{Spec: authorizationv1.SubjectAccessReviewSpec{ + User: "alice", + ResourceAttributes: &authorizationv1.ResourceAttributes{ + Namespace: "foo", + Verb: "create", + Group: "", + Version: "v1", + Resource: "configmaps", + }, + }} + result, err := adminClient.AuthorizationV1().SubjectAccessReviews().Create(context.TODO(), sar, metav1.CreateOptions{}) + if err != nil { + t.Fatal(err) + } + if result.Status.Allowed { + t.Fatal("expected denied, got allowed") + } + + authutil.GrantUserAuthorization(t, context.TODO(), adminClient, "alice", + rbacv1.PolicyRule{ + Verbs: []string{"create"}, + APIGroups: []string{""}, + Resources: []string{"configmaps"}, + }, + ) + + result, err = adminClient.AuthorizationV1().SubjectAccessReviews().Create(context.TODO(), sar, metav1.CreateOptions{}) + if err != nil { + t.Fatal(err) + } + if !result.Status.Allowed { + t.Fatal("expected allowed, got denied") + } +}