...

Source file src/k8s.io/kubernetes/test/integration/certificates/field_selector_test.go

Documentation: k8s.io/kubernetes/test/integration/certificates

     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  	"crypto/ed25519"
    22  	"crypto/rand"
    23  	"crypto/x509"
    24  	"crypto/x509/pkix"
    25  	"encoding/pem"
    26  	"testing"
    27  
    28  	certv1 "k8s.io/api/certificates/v1"
    29  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    30  	clientset "k8s.io/client-go/kubernetes"
    31  	certclientset "k8s.io/client-go/kubernetes/typed/certificates/v1"
    32  	kubeapiservertesting "k8s.io/kubernetes/cmd/kube-apiserver/app/testing"
    33  
    34  	"k8s.io/kubernetes/test/integration/framework"
    35  )
    36  
    37  // Verifies that the 'spec.signerName' field can be correctly used as a field selector on LIST requests
    38  func TestCSRSignerNameFieldSelector(t *testing.T) {
    39  	server := kubeapiservertesting.StartTestServerOrDie(t, nil, nil, framework.SharedEtcd())
    40  	defer server.TearDownFn()
    41  
    42  	client := clientset.NewForConfigOrDie(server.ClientConfig)
    43  	csrClient := client.CertificatesV1().CertificateSigningRequests()
    44  	csr1 := createTestingCSR(t, csrClient, "csr-1", "example.com/signer-name-1", "")
    45  	csr2 := createTestingCSR(t, csrClient, "csr-2", "example.com/signer-name-2", "")
    46  	// csr3 has the same signerName as csr2 so we can ensure multiple items are returned when running a filtered
    47  	// LIST call.
    48  	csr3 := createTestingCSR(t, csrClient, "csr-3", "example.com/signer-name-2", "")
    49  
    50  	signerOneList, err := client.CertificatesV1().CertificateSigningRequests().List(context.TODO(), metav1.ListOptions{FieldSelector: "spec.signerName=example.com/signer-name-1"})
    51  	if err != nil {
    52  		t.Errorf("unable to list CSRs with spec.signerName=example.com/signer-name-1")
    53  		return
    54  	}
    55  	if len(signerOneList.Items) != 1 {
    56  		t.Errorf("expected one CSR to be returned but got %d", len(signerOneList.Items))
    57  	} else if signerOneList.Items[0].Name != csr1.Name {
    58  		t.Errorf("expected CSR named 'csr-1' to be returned but got %q", signerOneList.Items[0].Name)
    59  	}
    60  
    61  	signerTwoList, err := client.CertificatesV1().CertificateSigningRequests().List(context.TODO(), metav1.ListOptions{FieldSelector: "spec.signerName=example.com/signer-name-2"})
    62  	if err != nil {
    63  		t.Errorf("unable to list CSRs with spec.signerName=example.com/signer-name-2")
    64  		return
    65  	}
    66  	if len(signerTwoList.Items) != 2 {
    67  		t.Errorf("expected one CSR to be returned but got %d", len(signerTwoList.Items))
    68  	} else if signerTwoList.Items[0].Name != csr2.Name {
    69  		t.Errorf("expected CSR named 'csr-2' to be returned but got %q", signerTwoList.Items[0].Name)
    70  	} else if signerTwoList.Items[1].Name != csr3.Name {
    71  		t.Errorf("expected CSR named 'csr-3' to be returned but got %q", signerTwoList.Items[1].Name)
    72  	}
    73  }
    74  
    75  func createTestingCSR(t *testing.T, certClient certclientset.CertificateSigningRequestInterface, name, signerName, groupName string) *certv1.CertificateSigningRequest {
    76  	csr, err := certClient.Create(context.TODO(), buildTestingCSR(name, signerName, groupName), metav1.CreateOptions{})
    77  	if err != nil {
    78  		t.Fatalf("failed to create testing CSR: %v", err)
    79  	}
    80  	return csr
    81  }
    82  
    83  func buildTestingCSR(name, signerName, groupName string) *certv1.CertificateSigningRequest {
    84  	return &certv1.CertificateSigningRequest{
    85  		ObjectMeta: metav1.ObjectMeta{
    86  			Name: name,
    87  		},
    88  		Spec: certv1.CertificateSigningRequestSpec{
    89  			SignerName: signerName,
    90  			Request:    pemWithGroup(groupName),
    91  			// this is the old defaulting for usages
    92  			Usages: []certv1.KeyUsage{certv1.UsageDigitalSignature, certv1.UsageKeyEncipherment},
    93  		},
    94  	}
    95  }
    96  
    97  func pemWithGroup(group string) []byte {
    98  	template := &x509.CertificateRequest{
    99  		Subject: pkix.Name{
   100  			Organization: []string{group},
   101  		},
   102  	}
   103  	return pemWithTemplate(template)
   104  }
   105  
   106  func pemWithTemplate(template *x509.CertificateRequest) []byte {
   107  	_, key, err := ed25519.GenerateKey(rand.Reader)
   108  	if err != nil {
   109  		panic(err)
   110  	}
   111  
   112  	csrDER, err := x509.CreateCertificateRequest(rand.Reader, template, key)
   113  	if err != nil {
   114  		panic(err)
   115  	}
   116  
   117  	csrPemBlock := &pem.Block{
   118  		Type:  "CERTIFICATE REQUEST",
   119  		Bytes: csrDER,
   120  	}
   121  
   122  	p := pem.EncodeToMemory(csrPemBlock)
   123  	if p == nil {
   124  		panic("invalid pem block")
   125  	}
   126  
   127  	return p
   128  }
   129  

View as plain text