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

🌱 Update util functions for secret generation #3979

Merged
merged 1 commit into from
Dec 8, 2020
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
1 change: 1 addition & 0 deletions util/kubeconfig/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ func GenerateSecretWithOwner(clusterName client.ObjectKey, data []byte, owner me
Data: map[string][]byte{
secret.KubeconfigDataName: data,
},
Type: clusterv1.ClusterSecretType,
}
}

Expand Down
3 changes: 3 additions & 0 deletions util/kubeconfig/kubeconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ users:
Data: map[string][]byte{
secret.KubeconfigDataName: []byte(validKubeConfig),
},
Type: clusterv1.ClusterSecretType,
}
)

Expand Down Expand Up @@ -281,6 +282,7 @@ func TestCreateSecretWithOwner(t *testing.T) {
key := client.ObjectKey{Name: "test1-kubeconfig", Namespace: "test"}
g.Expect(c.Get(ctx, key, s)).To(Succeed())
g.Expect(s.OwnerReferences).To(ContainElement(owner))
g.Expect(s.Type).To(Equal(clusterv1.ClusterSecretType))

clientConfig, err := clientcmd.NewClientConfigFromBytes(s.Data[secret.KubeconfigDataName])
g.Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -343,6 +345,7 @@ func TestCreateSecret(t *testing.T) {
APIVersion: clusterv1.GroupVersion.String(),
},
))
g.Expect(s.Type).To(Equal(clusterv1.ClusterSecretType))

clientConfig, err := clientcmd.NewClientConfigFromBytes(s.Data[secret.KubeconfigDataName])
g.Expect(err).NotTo(HaveOccurred())
Expand Down
39 changes: 23 additions & 16 deletions util/secret/certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,29 +241,14 @@ func (c Certificates) EnsureAllExist() error {
return nil
}

// TODO: consider moving a generating function into the Certificate object itself?
type certGenerator func() (*certs.KeyPair, error)

// Generate will generate any certificates that do not have KeyPair data.
func (c Certificates) Generate() error {
for _, certificate := range c {
if certificate.KeyPair == nil {
var generator certGenerator
switch certificate.Purpose {
case APIServerEtcdClient: // Do not generate the APIServerEtcdClient key pair. It is user supplied
continue
case ServiceAccount:
generator = generateServiceAccountKeys
default:
generator = generateCACert
}

kp, err := generator()
err := certificate.Generate()
if err != nil {
return err
}
certificate.KeyPair = kp
certificate.Generated = true
}
}
return nil
Expand Down Expand Up @@ -345,6 +330,7 @@ func (c *Certificate) AsSecret(clusterName client.ObjectKey, owner metav1.OwnerR
TLSKeyDataName: c.KeyPair.Key,
TLSCrtDataName: c.KeyPair.Cert,
},
Type: clusterv1.ClusterSecretType,
}

if c.Generated {
Expand Down Expand Up @@ -375,6 +361,27 @@ func (c *Certificate) AsFiles() []bootstrapv1.File {
return out
}

func (c *Certificate) Generate() error {
// Do not generate the APIServerEtcdClient key pair. It is user supplied
if c.Purpose == APIServerEtcdClient {
return nil
}

generator := generateCACert
if c.Purpose == ServiceAccount {
generator = generateServiceAccountKeys
}

kp, err := generator()
if err != nil {
return err
}
c.KeyPair = kp
c.Generated = true

return nil
}

// AsFiles converts a slice of certificates into bootstrap files.
func (c Certificates) AsFiles() []bootstrapv1.File {
clusterCA := c.GetByPurpose(ClusterCA)
Expand Down