k8s.io/kubernetes@v1.29.3/test/integration/certificates/admission_subjectrestriction_test.go (about) 1 /* 2 Copyright 2020 The Kubernetes Authors. 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 certificates 18 19 import ( 20 "context" 21 "testing" 22 23 certv1 "k8s.io/api/certificates/v1" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 clientset "k8s.io/client-go/kubernetes" 26 27 kubeapiservertesting "k8s.io/kubernetes/cmd/kube-apiserver/app/testing" 28 "k8s.io/kubernetes/test/integration/framework" 29 ) 30 31 // Verifies that the CertificateSubjectRestriction admission controller works as expected. 32 func TestCertificateSubjectRestrictionPlugin(t *testing.T) { 33 tests := map[string]struct { 34 signerName string 35 group string 36 error string 37 }{ 38 "should reject a request if signerName is kube-apiserver-client and group is system:masters": { 39 signerName: certv1.KubeAPIServerClientSignerName, 40 group: "system:masters", 41 error: `certificatesigningrequests.certificates.k8s.io "csr" is forbidden: use of kubernetes.io/kube-apiserver-client signer with system:masters group is not allowed`, 42 }, 43 "should admit a request if signerName is kube-apiserver-client and group is NOT system:masters": { 44 signerName: certv1.KubeAPIServerClientSignerName, 45 group: "system:notmasters", 46 }, 47 } 48 for name, test := range tests { 49 t.Run(name, func(t *testing.T) { 50 // Run an apiserver with the default configuration options. 51 s := kubeapiservertesting.StartTestServerOrDie(t, kubeapiservertesting.NewDefaultTestServerOptions(), []string{""}, framework.SharedEtcd()) 52 defer s.TearDownFn() 53 client := clientset.NewForConfigOrDie(s.ClientConfig) 54 55 // Attempt to create the CSR resource. 56 csr := buildTestingCSR("csr", test.signerName, test.group) 57 _, err := client.CertificatesV1().CertificateSigningRequests().Create(context.TODO(), csr, metav1.CreateOptions{}) 58 if err != nil && test.error != err.Error() { 59 t.Errorf("expected error %q but got: %v", test.error, err) 60 } 61 if err == nil && test.error != "" { 62 t.Errorf("expected to get an error %q but got none", test.error) 63 } 64 }) 65 } 66 }