Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include multiple CA certs in exported kubeconfigs #11831

Merged
merged 2 commits into from
Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion nodeup/pkg/model/kube_apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func (b *KubeAPIServerBuilder) writeAuthenticationConfig(c *fi.ModelBuilderConte
b.Cluster.Spec.KubeAPIServer.AuthenticationTokenWebhookConfigFile = fi.String(PathAuthnConfig)

{
caCertificate, err := b.NodeupModelContext.KeyStore.FindCert(fi.CertificateIDCA)
caCertificate, _, err := b.NodeupModelContext.KeyStore.FindPrimaryKeypair(fi.CertificateIDCA)
if err != nil {
return fmt.Errorf("error fetching AWS IAM Authentication CA certificate from keystore: %v", err)
}
Expand All @@ -217,6 +217,7 @@ func (b *KubeAPIServerBuilder) writeAuthenticationConfig(c *fi.ModelBuilderConte
User: "kube-apiserver",
}

// Since we're talking to localhost, we don't need the entire certificate bundle.
cluster.CertificateAuthorityData, err = caCertificate.AsBytes()
if err != nil {
return fmt.Errorf("error encoding AWS IAM Authentication CA certificate: %v", err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/kubeconfig/create_kubecfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ func BuildKubecfg(cluster *kops.Cluster, keyStore fi.Keystore, secretStore fi.Se
if err != nil {
return nil, fmt.Errorf("error fetching CA keypair: %v", err)
}
if keySet != nil && keySet.Primary != nil && keySet.Primary.Certificate != nil {
b.CACert, err = keySet.Primary.Certificate.AsBytes()
if keySet != nil {
b.CACerts, err = keySet.ToCertificateBytes()
if err != nil {
return nil, err
}
Expand Down
23 changes: 13 additions & 10 deletions pkg/kubeconfig/create_kubecfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (

const certData = "-----BEGIN CERTIFICATE-----\nMIIC2DCCAcCgAwIBAgIRALJXAkVj964tq67wMSI8oJQwDQYJKoZIhvcNAQELBQAw\nFTETMBEGA1UEAxMKa3ViZXJuZXRlczAeFw0xNzEyMjcyMzUyNDBaFw0yNzEyMjcy\nMzUyNDBaMBUxEzARBgNVBAMTCmt1YmVybmV0ZXMwggEiMA0GCSqGSIb3DQEBAQUA\nA4IBDwAwggEKAoIBAQDgnCkSmtnmfxEgS3qNPaUCH5QOBGDH/inHbWCODLBCK9gd\nXEcBl7FVv8T2kFr1DYb0HVDtMI7tixRVFDLgkwNlW34xwWdZXB7GeoFgU1xWOQSY\nOACC8JgYTQ/139HBEvgq4sej67p+/s/SNcw34Kk7HIuFhlk1rRk5kMexKIlJBKP1\nYYUYetsJ/QpUOkqJ5HW4GoetE76YtHnORfYvnybviSMrh2wGGaN6r/s4ChOaIbZC\nAn8/YiPKGIDaZGpj6GXnmXARRX/TIdgSQkLwt0aTDBnPZ4XvtpI8aaL8DYJIqAzA\nNPH2b4/uNylat5jDo0b0G54agMi97+2AUrC9UUXpAgMBAAGjIzAhMA4GA1UdDwEB\n/wQEAwIBBjAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQBVGR2r\nhzXzRMU5wriPQAJScszNORvoBpXfZoZ09FIupudFxBVU3d4hV9StKnQgPSGA5XQO\nHE97+BxJDuA/rB5oBUsMBjc7y1cde/T6hmi3rLoEYBSnSudCOXJE4G9/0f8byAJe\nrN8+No1r2VgZvZh6p74TEkXv/l3HBPWM7IdUV0HO9JDhSgOVF1fyQKJxRuLJR8jt\nO6mPH2UX0vMwVa4jvwtkddqk2OAdYQvH9rbDjjbzaiW0KnmdueRo92KHAN7BsDZy\nVpXHpqo1Kzg7D3fpaXCf5si7lqqrd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HQ4EFgQUhPbxEmUbwVOCa+fZgxreFhf67UEwDQYJKoZI\nhvcNAQELBQADQQALMsyK2Q7C/bk27eCvXyZKUfrLvor10hEjwGhv14zsKWDeTj/J\nA1LPYp7U9VtFfgFOkVbkLE9Rstc0ltNrPqxA\n-----END CERTIFICATE-----\n"

// mock a fake status store.
type fakeStatusCloud struct {
Expand Down Expand Up @@ -132,7 +133,9 @@ func buildMinimalCluster(clusterName string, masterPublicName string, lbCert boo
func fakeKeyset() *fi.Keyset {
cert, _ := pki.ParsePEMCertificate([]byte(certData))
key, _ := pki.ParsePEMPrivateKey([]byte(privatekeyData))
nextCert, _ := pki.ParsePEMCertificate([]byte(nextCertificate))
keyset, _ := fi.NewKeyset(cert, key)
_ = keyset.AddItem(nextCert, nil, false)
return keyset
}

Expand Down Expand Up @@ -178,7 +181,7 @@ func TestBuildKubecfg(t *testing.T) {
want: &KubeconfigBuilder{
Context: "testcluster",
Server: "https://testcluster.test.com",
CACert: []byte(certData),
CACerts: []byte(nextCertificate + certData),
User: "testcluster",
},
wantClientCert: true,
Expand All @@ -193,7 +196,7 @@ func TestBuildKubecfg(t *testing.T) {
want: &KubeconfigBuilder{
Context: "testcluster",
Server: "https://testcluster.test.com:8443",
CACert: []byte(certData),
CACerts: []byte(nextCertificate + certData),
User: "testcluster",
},
wantClientCert: true,
Expand All @@ -208,7 +211,7 @@ func TestBuildKubecfg(t *testing.T) {
want: &KubeconfigBuilder{
Context: "testcluster",
Server: "https://testcluster.test.com",
CACert: nil,
CACerts: nil,
User: "testcluster",
},
wantClientCert: true,
Expand All @@ -223,7 +226,7 @@ func TestBuildKubecfg(t *testing.T) {
want: &KubeconfigBuilder{
Context: "testcluster",
Server: "https://testcluster.test.com",
CACert: []byte(certData),
CACerts: []byte(nextCertificate + certData),
User: "testcluster",
},
wantClientCert: false,
Expand All @@ -239,7 +242,7 @@ func TestBuildKubecfg(t *testing.T) {
want: &KubeconfigBuilder{
Context: "testcluster",
Server: "https://testcluster.test.com",
CACert: []byte(certData),
CACerts: []byte(nextCertificate + certData),
User: "myuser",
},
wantClientCert: false,
Expand All @@ -255,7 +258,7 @@ func TestBuildKubecfg(t *testing.T) {
want: &KubeconfigBuilder{
Context: "emptyMasterPublicNameCluster",
Server: "https://api.emptyMasterPublicNameCluster",
CACert: []byte(certData),
CACerts: []byte(nextCertificate + certData),
User: "emptyMasterPublicNameCluster",
},
wantClientCert: false,
Expand All @@ -277,7 +280,7 @@ func TestBuildKubecfg(t *testing.T) {
want: &KubeconfigBuilder{
Context: "testgossipcluster.k8s.local",
Server: "https://elbHostName",
CACert: []byte(certData),
CACerts: []byte(nextCertificate + certData),
User: "testgossipcluster.k8s.local",
},
wantClientCert: false,
Expand All @@ -293,7 +296,7 @@ func TestBuildKubecfg(t *testing.T) {
want: &KubeconfigBuilder{
Context: "testcluster",
Server: "https://testcluster.test.com",
CACert: []byte(certData),
CACerts: []byte(nextCertificate + certData),
User: "testcluster",
AuthenticationExec: []string{
"kops",
Expand All @@ -316,7 +319,7 @@ func TestBuildKubecfg(t *testing.T) {
want: &KubeconfigBuilder{
Context: "testcluster",
Server: "https://internal.testcluster.test.com",
CACert: []byte(certData),
CACerts: []byte(nextCertificate + certData),
User: "testcluster",
},
wantClientCert: true,
Expand All @@ -339,7 +342,7 @@ func TestBuildKubecfg(t *testing.T) {
want: &KubeconfigBuilder{
Context: "testgossipcluster.k8s.local",
Server: "https://nlbHostName:8443",
CACert: []byte(certData),
CACerts: []byte(nextCertificate + certData),
User: "testgossipcluster.k8s.local",
},
wantClientCert: true,
Expand Down
6 changes: 3 additions & 3 deletions pkg/kubeconfig/kubecfg_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type KubeconfigBuilder struct {
KubeUser string
KubePassword string

CACert []byte
CACerts []byte
ClientCert []byte
ClientKey []byte

Expand Down Expand Up @@ -82,7 +82,7 @@ func (c *KubeconfigBuilder) BuildRestConfig() (*rest.Config, error) {
restConfig := &rest.Config{
Host: c.Server,
}
restConfig.CAData = c.CACert
restConfig.CAData = c.CACerts
restConfig.CertData = c.ClientCert
restConfig.KeyData = c.ClientKey
restConfig.Username = c.KubeUser
Expand All @@ -108,7 +108,7 @@ func (b *KubeconfigBuilder) WriteKubecfg(configAccess clientcmd.ConfigAccess) er
cluster = clientcmdapi.NewCluster()
}
cluster.Server = b.Server
cluster.CertificateAuthorityData = b.CACert
cluster.CertificateAuthorityData = b.CACerts

if config.Clusters == nil {
config.Clusters = make(map[string]*clientcmdapi.Cluster)
Expand Down