123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661 |
- /*
- Copyright 2018 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 kubeconfig
- import (
- "bytes"
- "crypto"
- "crypto/x509"
- "fmt"
- "io"
- "os"
- "path/filepath"
- "reflect"
- "testing"
- "github.com/lithammer/dedent"
- "k8s.io/client-go/tools/clientcmd"
- clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
- kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
- kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
- kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
- certstestutil "k8s.io/kubernetes/cmd/kubeadm/app/util/certs"
- pkiutil "k8s.io/kubernetes/cmd/kubeadm/app/util/pkiutil"
- testutil "k8s.io/kubernetes/cmd/kubeadm/test"
- kubeconfigtestutil "k8s.io/kubernetes/cmd/kubeadm/test/kubeconfig"
- )
- func TestGetKubeConfigSpecsFailsIfCADoesntExists(t *testing.T) {
- // Create temp folder for the test case (without a CA)
- tmpdir := testutil.SetupTempDir(t)
- defer os.RemoveAll(tmpdir)
- // Creates an InitConfiguration pointing to the pkidir folder
- cfg := &kubeadmapi.InitConfiguration{
- ClusterConfiguration: kubeadmapi.ClusterConfiguration{
- CertificatesDir: tmpdir,
- },
- }
- // Executes getKubeConfigSpecs
- if _, err := getKubeConfigSpecs(cfg); err == nil {
- t.Error("getKubeConfigSpecs didnt failed when expected")
- }
- }
- func TestGetKubeConfigSpecs(t *testing.T) {
- // Create temp folder for the test case
- tmpdir := testutil.SetupTempDir(t)
- defer os.RemoveAll(tmpdir)
- // Adds a pki folder with a ca certs to the temp folder
- pkidir := testutil.SetupPkiDirWithCertificateAuthorithy(t, tmpdir)
- // Creates InitConfigurations pointing to the pkidir folder
- cfgs := []*kubeadmapi.InitConfiguration{
- {
- LocalAPIEndpoint: kubeadmapi.APIEndpoint{AdvertiseAddress: "1.2.3.4", BindPort: 1234},
- ClusterConfiguration: kubeadmapi.ClusterConfiguration{
- CertificatesDir: pkidir,
- },
- NodeRegistration: kubeadmapi.NodeRegistrationOptions{Name: "valid-node-name"},
- },
- {
- LocalAPIEndpoint: kubeadmapi.APIEndpoint{AdvertiseAddress: "1.2.3.4", BindPort: 1234},
- ClusterConfiguration: kubeadmapi.ClusterConfiguration{
- ControlPlaneEndpoint: "api.k8s.io",
- CertificatesDir: pkidir,
- },
- NodeRegistration: kubeadmapi.NodeRegistrationOptions{Name: "valid-node-name"},
- },
- {
- LocalAPIEndpoint: kubeadmapi.APIEndpoint{AdvertiseAddress: "1.2.3.4", BindPort: 1234},
- ClusterConfiguration: kubeadmapi.ClusterConfiguration{
- ControlPlaneEndpoint: "api.k8s.io:4321",
- CertificatesDir: pkidir,
- },
- NodeRegistration: kubeadmapi.NodeRegistrationOptions{Name: "valid-node-name"},
- },
- {
- LocalAPIEndpoint: kubeadmapi.APIEndpoint{AdvertiseAddress: "1.2.3.4", BindPort: 1234},
- ClusterConfiguration: kubeadmapi.ClusterConfiguration{
- ControlPlaneEndpoint: "api.k8s.io",
- CertificatesDir: pkidir,
- },
- NodeRegistration: kubeadmapi.NodeRegistrationOptions{Name: "valid-node-name"},
- },
- {
- LocalAPIEndpoint: kubeadmapi.APIEndpoint{AdvertiseAddress: "1.2.3.4", BindPort: 1234},
- ClusterConfiguration: kubeadmapi.ClusterConfiguration{
- ControlPlaneEndpoint: "api.k8s.io:4321",
- CertificatesDir: pkidir,
- },
- NodeRegistration: kubeadmapi.NodeRegistrationOptions{Name: "valid-node-name"},
- },
- }
- for i, cfg := range cfgs {
- var assertions = []struct {
- kubeConfigFile string
- clientName string
- organizations []string
- }{
- {
- kubeConfigFile: kubeadmconstants.AdminKubeConfigFileName,
- clientName: "kubernetes-admin",
- organizations: []string{kubeadmconstants.SystemPrivilegedGroup},
- },
- {
- kubeConfigFile: kubeadmconstants.KubeletKubeConfigFileName,
- clientName: fmt.Sprintf("%s%s", kubeadmconstants.NodesUserPrefix, cfg.NodeRegistration.Name),
- organizations: []string{kubeadmconstants.NodesGroup},
- },
- {
- kubeConfigFile: kubeadmconstants.ControllerManagerKubeConfigFileName,
- clientName: kubeadmconstants.ControllerManagerUser,
- },
- {
- kubeConfigFile: kubeadmconstants.SchedulerKubeConfigFileName,
- clientName: kubeadmconstants.SchedulerUser,
- },
- }
- for _, assertion := range assertions {
- t.Run(fmt.Sprintf("%d-%s", i, assertion.clientName), func(t *testing.T) {
- // Executes getKubeConfigSpecs
- specs, err := getKubeConfigSpecs(cfg)
- if err != nil {
- t.Fatal("getKubeConfigSpecs failed!")
- }
- var spec *kubeConfigSpec
- var ok bool
- // assert the spec for the kubeConfigFile exists
- if spec, ok = specs[assertion.kubeConfigFile]; !ok {
- t.Errorf("getKubeConfigSpecs didn't create spec for %s ", assertion.kubeConfigFile)
- return
- }
- // Assert clientName
- if spec.ClientName != assertion.clientName {
- t.Errorf("getKubeConfigSpecs for %s clientName is %s, expected %s", assertion.kubeConfigFile, spec.ClientName, assertion.clientName)
- }
- // Assert Organizations
- if spec.ClientCertAuth == nil || !reflect.DeepEqual(spec.ClientCertAuth.Organizations, assertion.organizations) {
- t.Errorf("getKubeConfigSpecs for %s Organizations is %v, expected %v", assertion.kubeConfigFile, spec.ClientCertAuth.Organizations, assertion.organizations)
- }
- // Asserts InitConfiguration values injected into spec
- controlPlaneEndpoint, err := kubeadmutil.GetControlPlaneEndpoint(cfg.ControlPlaneEndpoint, &cfg.LocalAPIEndpoint)
- if err != nil {
- t.Error(err)
- }
- if spec.APIServer != controlPlaneEndpoint {
- t.Errorf("getKubeConfigSpecs didn't injected cfg.APIServer endpoint into spec for %s", assertion.kubeConfigFile)
- }
- // Asserts CA certs and CA keys loaded into specs
- if spec.CACert == nil {
- t.Errorf("getKubeConfigSpecs didn't loaded CACert into spec for %s!", assertion.kubeConfigFile)
- }
- if spec.ClientCertAuth == nil || spec.ClientCertAuth.CAKey == nil {
- t.Errorf("getKubeConfigSpecs didn't loaded CAKey into spec for %s!", assertion.kubeConfigFile)
- }
- })
- }
- }
- }
- func TestBuildKubeConfigFromSpecWithClientAuth(t *testing.T) {
- // Creates a CA
- caCert, caKey := certstestutil.SetupCertificateAuthorithy(t)
- // Executes buildKubeConfigFromSpec passing a KubeConfigSpec with a ClientAuth
- config := setupdKubeConfigWithClientAuth(t, caCert, caKey, "https://1.2.3.4:1234", "myClientName", "test-cluster", "myOrg1", "myOrg2")
- // Asserts spec data are propagated to the kubeconfig
- kubeconfigtestutil.AssertKubeConfigCurrentCluster(t, config, "https://1.2.3.4:1234", caCert)
- kubeconfigtestutil.AssertKubeConfigCurrentAuthInfoWithClientCert(t, config, caCert, "myClientName", "myOrg1", "myOrg2")
- }
- func TestBuildKubeConfigFromSpecWithTokenAuth(t *testing.T) {
- // Creates a CA
- caCert, _ := certstestutil.SetupCertificateAuthorithy(t)
- // Executes buildKubeConfigFromSpec passing a KubeConfigSpec with a Token
- config := setupdKubeConfigWithTokenAuth(t, caCert, "https://1.2.3.4:1234", "myClientName", "123456", "test-cluster")
- // Asserts spec data are propagated to the kubeconfig
- kubeconfigtestutil.AssertKubeConfigCurrentCluster(t, config, "https://1.2.3.4:1234", caCert)
- kubeconfigtestutil.AssertKubeConfigCurrentAuthInfoWithToken(t, config, "myClientName", "123456")
- }
- func TestCreateKubeConfigFileIfNotExists(t *testing.T) {
- // Creates a CAs
- caCert, caKey := certstestutil.SetupCertificateAuthorithy(t)
- anotherCaCert, anotherCaKey := certstestutil.SetupCertificateAuthorithy(t)
- // build kubeconfigs (to be used to test kubeconfigs equality/not equality)
- config := setupdKubeConfigWithClientAuth(t, caCert, caKey, "https://1.2.3.4:1234", "test-cluster", "myOrg1", "myOrg2")
- configWithAnotherClusterCa := setupdKubeConfigWithClientAuth(t, anotherCaCert, anotherCaKey, "https://1.2.3.4:1234", "test-cluster", "myOrg1", "myOrg2")
- configWithAnotherClusterAddress := setupdKubeConfigWithClientAuth(t, caCert, caKey, "https://3.4.5.6:3456", "myOrg1", "test-cluster", "myOrg2")
- var tests = []struct {
- name string
- existingKubeConfig *clientcmdapi.Config
- kubeConfig *clientcmdapi.Config
- expectedError bool
- }{
- { // if there is no existing KubeConfig, creates the kubeconfig
- name: "KubeConfig doesn't exist",
- kubeConfig: config,
- },
- { // if KubeConfig is equal to the existingKubeConfig - refers to the same cluster -, use the existing (Test idempotency)
- name: "KubeConfig refers to the same cluster",
- existingKubeConfig: config,
- kubeConfig: config,
- },
- { // if KubeConfig is not equal to the existingKubeConfig - refers to the another cluster (a cluster with another Ca) -, raise error
- name: "KubeConfig refers to the cluster with another CA",
- existingKubeConfig: config,
- kubeConfig: configWithAnotherClusterCa,
- expectedError: true,
- },
- { // if KubeConfig is not equal to the existingKubeConfig - refers to the another cluster (a cluster with another address) -, raise error
- name: "KubeConfig referst to the cluster with another address",
- existingKubeConfig: config,
- kubeConfig: configWithAnotherClusterAddress,
- expectedError: true,
- },
- }
- for _, test := range tests {
- t.Run(test.name, func(t *testing.T) {
- // Create temp folder for the test case
- tmpdir := testutil.SetupTempDir(t)
- defer os.RemoveAll(tmpdir)
- // Writes the existing kubeconfig file to disk
- if test.existingKubeConfig != nil {
- if err := createKubeConfigFileIfNotExists(tmpdir, "test.conf", test.existingKubeConfig); err != nil {
- t.Errorf("createKubeConfigFileIfNotExists failed")
- }
- }
- // Writes the kubeconfig file to disk
- err := createKubeConfigFileIfNotExists(tmpdir, "test.conf", test.kubeConfig)
- if test.expectedError && err == nil {
- t.Errorf("createKubeConfigFileIfNotExists didn't failed when expected to fail")
- }
- if !test.expectedError && err != nil {
- t.Errorf("createKubeConfigFileIfNotExists failed")
- }
- // Assert that the created file is there
- testutil.AssertFileExists(t, tmpdir, "test.conf")
- })
- }
- }
- func TestCreateKubeconfigFilesAndWrappers(t *testing.T) {
- var tests = []struct {
- name string
- createKubeConfigFunction func(outDir string, cfg *kubeadmapi.InitConfiguration) error
- expectedFiles []string
- expectedError bool
- }{
- { // Test createKubeConfigFiles fails for unknown kubeconfig is requested
- name: "createKubeConfigFiles",
- createKubeConfigFunction: func(outDir string, cfg *kubeadmapi.InitConfiguration) error {
- return createKubeConfigFiles(outDir, cfg, "unknown.conf")
- },
- expectedError: true,
- },
- { // Test CreateJoinControlPlaneKubeConfigFiles (wrapper to createKubeConfigFile)
- name: "CreateJoinControlPlaneKubeConfigFiles",
- createKubeConfigFunction: CreateJoinControlPlaneKubeConfigFiles,
- expectedFiles: []string{
- kubeadmconstants.AdminKubeConfigFileName,
- kubeadmconstants.ControllerManagerKubeConfigFileName,
- kubeadmconstants.SchedulerKubeConfigFileName,
- },
- },
- }
- for _, test := range tests {
- t.Run(test.name, func(t *testing.T) {
- // Create temp folder for the test case
- tmpdir := testutil.SetupTempDir(t)
- defer os.RemoveAll(tmpdir)
- // Adds a pki folder with a ca certs to the temp folder
- pkidir := testutil.SetupPkiDirWithCertificateAuthorithy(t, tmpdir)
- // Creates an InitConfiguration pointing to the pkidir folder
- cfg := &kubeadmapi.InitConfiguration{
- LocalAPIEndpoint: kubeadmapi.APIEndpoint{AdvertiseAddress: "1.2.3.4", BindPort: 1234},
- ClusterConfiguration: kubeadmapi.ClusterConfiguration{
- CertificatesDir: pkidir,
- },
- }
- // Execs the createKubeConfigFunction
- err := test.createKubeConfigFunction(tmpdir, cfg)
- if test.expectedError && err == nil {
- t.Errorf("createKubeConfigFunction didn't failed when expected to fail")
- return
- }
- if !test.expectedError && err != nil {
- t.Errorf("createKubeConfigFunction failed")
- return
- }
- // Assert expected files are there
- testutil.AssertFileExists(t, tmpdir, test.expectedFiles...)
- })
- }
- }
- func TestWriteKubeConfigFailsIfCADoesntExists(t *testing.T) {
- // Temporary folders for the test case (without a CA)
- tmpdir := testutil.SetupTempDir(t)
- defer os.RemoveAll(tmpdir)
- // Creates an InitConfiguration pointing to the tmpdir folder
- cfg := &kubeadmapi.InitConfiguration{
- ClusterConfiguration: kubeadmapi.ClusterConfiguration{
- CertificatesDir: tmpdir,
- },
- }
- var tests = []struct {
- name string
- writeKubeConfigFunction func(out io.Writer) error
- }{
- {
- name: "WriteKubeConfigWithClientCert",
- writeKubeConfigFunction: func(out io.Writer) error {
- return WriteKubeConfigWithClientCert(out, cfg, "myUser", []string{"myOrg"})
- },
- },
- {
- name: "WriteKubeConfigWithToken",
- writeKubeConfigFunction: func(out io.Writer) error {
- return WriteKubeConfigWithToken(out, cfg, "myUser", "12345")
- },
- },
- }
- for _, test := range tests {
- t.Run(test.name, func(t *testing.T) {
- buf := new(bytes.Buffer)
- // executes writeKubeConfigFunction
- if err := test.writeKubeConfigFunction(buf); err == nil {
- t.Error("writeKubeConfigFunction didnt failed when expected")
- }
- })
- }
- }
- func TestWriteKubeConfig(t *testing.T) {
- // Temporary folders for the test case
- tmpdir := testutil.SetupTempDir(t)
- defer os.RemoveAll(tmpdir)
- // Adds a pki folder with a ca cert to the temp folder
- pkidir := testutil.SetupPkiDirWithCertificateAuthorithy(t, tmpdir)
- // Retrieves ca cert for assertions
- caCert, _, err := pkiutil.TryLoadCertAndKeyFromDisk(pkidir, kubeadmconstants.CACertAndKeyBaseName)
- if err != nil {
- t.Fatalf("couldn't retrieve ca cert: %v", err)
- }
- // Creates an InitConfiguration pointing to the pkidir folder
- cfg := &kubeadmapi.InitConfiguration{
- LocalAPIEndpoint: kubeadmapi.APIEndpoint{AdvertiseAddress: "1.2.3.4", BindPort: 1234},
- ClusterConfiguration: kubeadmapi.ClusterConfiguration{
- CertificatesDir: pkidir,
- },
- }
- var tests = []struct {
- name string
- writeKubeConfigFunction func(out io.Writer) error
- withClientCert bool
- withToken bool
- }{
- {
- name: "WriteKubeConfigWithClientCert",
- writeKubeConfigFunction: func(out io.Writer) error {
- return WriteKubeConfigWithClientCert(out, cfg, "myUser", []string{"myOrg"})
- },
- withClientCert: true,
- },
- {
- name: "WriteKubeConfigWithToken",
- writeKubeConfigFunction: func(out io.Writer) error {
- return WriteKubeConfigWithToken(out, cfg, "myUser", "12345")
- },
- withToken: true,
- },
- }
- for _, test := range tests {
- t.Run(test.name, func(t *testing.T) {
- buf := new(bytes.Buffer)
- // executes writeKubeConfigFunction
- if err := test.writeKubeConfigFunction(buf); err != nil {
- t.Error("writeKubeConfigFunction failed")
- return
- }
- // reads kubeconfig written to stdout
- config, err := clientcmd.Load(buf.Bytes())
- if err != nil {
- t.Errorf("Couldn't read kubeconfig file from buffer: %v", err)
- return
- }
- // checks that CLI flags are properly propagated
- kubeconfigtestutil.AssertKubeConfigCurrentCluster(t, config, "https://1.2.3.4:1234", caCert)
- if test.withClientCert {
- // checks that kubeconfig files have expected client cert
- kubeconfigtestutil.AssertKubeConfigCurrentAuthInfoWithClientCert(t, config, caCert, "myUser")
- }
- if test.withToken {
- // checks that kubeconfig files have expected token
- kubeconfigtestutil.AssertKubeConfigCurrentAuthInfoWithToken(t, config, "myUser", "12345")
- }
- })
- }
- }
- func TestValidateKubeConfig(t *testing.T) {
- caCert, caKey := certstestutil.SetupCertificateAuthorithy(t)
- anotherCaCert, anotherCaKey := certstestutil.SetupCertificateAuthorithy(t)
- config := setupdKubeConfigWithClientAuth(t, caCert, caKey, "https://1.2.3.4:1234", "test-cluster", "myOrg1")
- configWithAnotherClusterCa := setupdKubeConfigWithClientAuth(t, anotherCaCert, anotherCaKey, "https://1.2.3.4:1234", "test-cluster", "myOrg1")
- configWithAnotherServerURL := setupdKubeConfigWithClientAuth(t, caCert, caKey, "https://4.3.2.1:4321", "test-cluster", "myOrg1")
- tests := map[string]struct {
- existingKubeConfig *clientcmdapi.Config
- kubeConfig *clientcmdapi.Config
- expectedError bool
- }{
- "kubeconfig don't exist": {
- kubeConfig: config,
- expectedError: true,
- },
- "kubeconfig exist and has invalid ca": {
- existingKubeConfig: configWithAnotherClusterCa,
- kubeConfig: config,
- expectedError: true,
- },
- "kubeconfig exist and has invalid server url": {
- existingKubeConfig: configWithAnotherServerURL,
- kubeConfig: config,
- expectedError: true,
- },
- "kubeconfig exist and is valid": {
- existingKubeConfig: config,
- kubeConfig: config,
- expectedError: false,
- },
- }
- for name, test := range tests {
- t.Run(name, func(t *testing.T) {
- tmpdir := testutil.SetupTempDir(t)
- defer os.RemoveAll(tmpdir)
- if test.existingKubeConfig != nil {
- if err := createKubeConfigFileIfNotExists(tmpdir, "test.conf", test.existingKubeConfig); err != nil {
- t.Errorf("createKubeConfigFileIfNotExists failed")
- }
- }
- err := validateKubeConfig(tmpdir, "test.conf", test.kubeConfig)
- if (err != nil) != test.expectedError {
- t.Fatalf(dedent.Dedent(
- "validateKubeConfig failed\n%s\nexpected error: %t\n\tgot: %t\nerror: %v"),
- name,
- test.expectedError,
- (err != nil),
- err,
- )
- }
- })
- }
- }
- func TestValidateKubeconfigsForExternalCA(t *testing.T) {
- tmpDir := testutil.SetupTempDir(t)
- defer os.RemoveAll(tmpDir)
- pkiDir := filepath.Join(tmpDir, "pki")
- initConfig := &kubeadmapi.InitConfiguration{
- ClusterConfiguration: kubeadmapi.ClusterConfiguration{
- CertificatesDir: pkiDir,
- },
- LocalAPIEndpoint: kubeadmapi.APIEndpoint{
- BindPort: 1234,
- AdvertiseAddress: "1.2.3.4",
- },
- }
- // creates CA, write to pkiDir and remove ca.key to get into external CA condition
- caCert, caKey := certstestutil.SetupCertificateAuthorithy(t)
- if err := pkiutil.WriteCertAndKey(pkiDir, kubeadmconstants.CACertAndKeyBaseName, caCert, caKey); err != nil {
- t.Fatalf("failure while saving CA certificate and key: %v", err)
- }
- if err := os.Remove(filepath.Join(pkiDir, kubeadmconstants.CAKeyName)); err != nil {
- t.Fatalf("failure while deleting ca.key: %v", err)
- }
- // create a valid config
- config := setupdKubeConfigWithClientAuth(t, caCert, caKey, "https://1.2.3.4:1234", "test-cluster", "myOrg1")
- // create a config with another CA
- anotherCaCert, anotherCaKey := certstestutil.SetupCertificateAuthorithy(t)
- configWithAnotherClusterCa := setupdKubeConfigWithClientAuth(t, anotherCaCert, anotherCaKey, "https://1.2.3.4:1234", "test-cluster", "myOrg1")
- // create a config with another server URL
- configWithAnotherServerURL := setupdKubeConfigWithClientAuth(t, caCert, caKey, "https://4.3.2.1:4321", "test-cluster", "myOrg1")
- tests := map[string]struct {
- filesToWrite map[string]*clientcmdapi.Config
- initConfig *kubeadmapi.InitConfiguration
- expectedError bool
- }{
- "files don't exist": {
- initConfig: initConfig,
- expectedError: true,
- },
- "some files don't exist": {
- filesToWrite: map[string]*clientcmdapi.Config{
- kubeadmconstants.AdminKubeConfigFileName: config,
- kubeadmconstants.KubeletKubeConfigFileName: config,
- },
- initConfig: initConfig,
- expectedError: true,
- },
- "some files have invalid CA": {
- filesToWrite: map[string]*clientcmdapi.Config{
- kubeadmconstants.AdminKubeConfigFileName: config,
- kubeadmconstants.KubeletKubeConfigFileName: config,
- kubeadmconstants.ControllerManagerKubeConfigFileName: configWithAnotherClusterCa,
- kubeadmconstants.SchedulerKubeConfigFileName: config,
- },
- initConfig: initConfig,
- expectedError: true,
- },
- "some files have invalid Server Url": {
- filesToWrite: map[string]*clientcmdapi.Config{
- kubeadmconstants.AdminKubeConfigFileName: config,
- kubeadmconstants.KubeletKubeConfigFileName: config,
- kubeadmconstants.ControllerManagerKubeConfigFileName: config,
- kubeadmconstants.SchedulerKubeConfigFileName: configWithAnotherServerURL,
- },
- initConfig: initConfig,
- expectedError: true,
- },
- "all files are valid": {
- filesToWrite: map[string]*clientcmdapi.Config{
- kubeadmconstants.AdminKubeConfigFileName: config,
- kubeadmconstants.KubeletKubeConfigFileName: config,
- kubeadmconstants.ControllerManagerKubeConfigFileName: config,
- kubeadmconstants.SchedulerKubeConfigFileName: config,
- },
- initConfig: initConfig,
- expectedError: false,
- },
- }
- for name, test := range tests {
- t.Run(name, func(t *testing.T) {
- tmpdir := testutil.SetupTempDir(t)
- defer os.RemoveAll(tmpdir)
- for name, config := range test.filesToWrite {
- if err := createKubeConfigFileIfNotExists(tmpdir, name, config); err != nil {
- t.Errorf("createKubeConfigFileIfNotExists failed: %v", err)
- }
- }
- err := ValidateKubeconfigsForExternalCA(tmpdir, test.initConfig)
- if (err != nil) != test.expectedError {
- t.Fatalf(dedent.Dedent(
- "ValidateKubeconfigsForExternalCA failed\n%s\nexpected error: %t\n\tgot: %t\nerror: %v"),
- name,
- test.expectedError,
- (err != nil),
- err,
- )
- }
- })
- }
- }
- // setupdKubeConfigWithClientAuth is a test utility function that wraps buildKubeConfigFromSpec for building a KubeConfig object With ClientAuth
- func setupdKubeConfigWithClientAuth(t *testing.T, caCert *x509.Certificate, caKey crypto.Signer, APIServer, clientName, clustername string, organizations ...string) *clientcmdapi.Config {
- spec := &kubeConfigSpec{
- CACert: caCert,
- APIServer: APIServer,
- ClientName: clientName,
- ClientCertAuth: &clientCertAuth{
- CAKey: caKey,
- Organizations: organizations,
- },
- }
- config, err := buildKubeConfigFromSpec(spec, clustername)
- if err != nil {
- t.Fatal("buildKubeConfigFromSpec failed!")
- }
- return config
- }
- // setupdKubeConfigWithClientAuth is a test utility function that wraps buildKubeConfigFromSpec for building a KubeConfig object With Token
- func setupdKubeConfigWithTokenAuth(t *testing.T, caCert *x509.Certificate, APIServer, clientName, token, clustername string) *clientcmdapi.Config {
- spec := &kubeConfigSpec{
- CACert: caCert,
- APIServer: APIServer,
- ClientName: clientName,
- TokenAuth: &tokenAuth{
- Token: token,
- },
- }
- config, err := buildKubeConfigFromSpec(spec, clustername)
- if err != nil {
- t.Fatal("buildKubeConfigFromSpec failed!")
- }
- return config
- }
|