...

Source file src/k8s.io/kubernetes/test/integration/tls/ciphers_test.go

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

     1  /*
     2  Copyright 2017 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 tls
    18  
    19  import (
    20  	"crypto/tls"
    21  	"fmt"
    22  	"net/http"
    23  	"strings"
    24  	"testing"
    25  
    26  	kubeapiservertesting "k8s.io/kubernetes/cmd/kube-apiserver/app/testing"
    27  	"k8s.io/kubernetes/test/integration/framework"
    28  )
    29  
    30  func runBasicSecureAPIServer(t *testing.T, ciphers []string) (kubeapiservertesting.TearDownFunc, int) {
    31  	flags := []string{"--tls-cipher-suites", strings.Join(ciphers, ",")}
    32  	testServer := kubeapiservertesting.StartTestServerOrDie(t, nil, flags, framework.SharedEtcd())
    33  	return testServer.TearDownFn, testServer.ServerOpts.SecureServing.BindPort
    34  }
    35  
    36  func TestAPICiphers(t *testing.T) {
    37  
    38  	basicServerCiphers := []string{"TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305", "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305", "TLS_RSA_WITH_AES_128_CBC_SHA", "TLS_RSA_WITH_AES_256_CBC_SHA", "TLS_RSA_WITH_AES_128_GCM_SHA256", "TLS_RSA_WITH_AES_256_GCM_SHA384", "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA", "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA"}
    39  
    40  	tearDown, port := runBasicSecureAPIServer(t, basicServerCiphers)
    41  	defer tearDown()
    42  	tests := []struct {
    43  		clientCiphers []uint16
    44  		expectedError bool
    45  	}{
    46  		{
    47  			// Not supported cipher
    48  			clientCiphers: []uint16{tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA},
    49  			expectedError: true,
    50  		},
    51  		{
    52  			// Supported cipher
    53  			clientCiphers: []uint16{tls.TLS_RSA_WITH_AES_256_CBC_SHA},
    54  			expectedError: false,
    55  		},
    56  	}
    57  
    58  	for i, test := range tests {
    59  		runTestAPICiphers(t, i, port, test.clientCiphers, test.expectedError)
    60  	}
    61  }
    62  
    63  func runTestAPICiphers(t *testing.T, testID int, kubePort int, clientCiphers []uint16, expectedError bool) {
    64  
    65  	tr := &http.Transport{
    66  		TLSClientConfig: &tls.Config{
    67  			MaxVersion:         tls.VersionTLS12, // Limit to TLS1.2 to allow cipher configuration
    68  			InsecureSkipVerify: true,
    69  			CipherSuites:       clientCiphers,
    70  		},
    71  	}
    72  	client := &http.Client{Transport: tr}
    73  	req, err := http.NewRequest("GET", fmt.Sprintf("https://127.0.0.1:%d", kubePort), nil)
    74  	if err != nil {
    75  		t.Fatal(err)
    76  	}
    77  	resp, err := client.Do(req)
    78  	if err == nil {
    79  		defer resp.Body.Close()
    80  	}
    81  
    82  	if expectedError && err == nil {
    83  		t.Fatalf("%d: expecting error for cipher test, client cipher is supported and it should't", testID)
    84  	} else if err != nil && !expectedError {
    85  		t.Fatalf("%d: not expecting error by client with cipher failed: %+v", testID, err)
    86  	}
    87  }
    88  

View as plain text