Skip to content

Commit a41ee45

Browse files
committed
Address review comments
1 parent f0820c0 commit a41ee45

File tree

4 files changed

+42
-31
lines changed

4 files changed

+42
-31
lines changed

util/certs/certs.go

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -135,24 +135,34 @@ func NewSigner(keyEncryptionAlgorithm bootstrapv1.EncryptionAlgorithmType) (cryp
135135
return pk, errors.WithStack(err)
136136
}
137137

138-
// EncodePrivateKeyPEMFromSigner returns PEM-encoded private key data.
139-
func EncodePrivateKeyPEMFromSigner(key crypto.Signer) ([]byte, error) {
140-
privateBytes, err := x509.MarshalPKCS8PrivateKey(key)
141-
if err != nil {
142-
return nil, fmt.Errorf("unable to marshal private key: %v", err)
143-
}
144-
145-
block := pem.Block{
146-
Type: "RSA PRIVATE KEY",
147-
Bytes: privateBytes,
138+
// EncodePrivateKeyPEMFromSigner converts a known private key type of RSA or ECDSA to
139+
// a PEM encoded block or returns an error.
140+
func EncodePrivateKeyPEMFromSigner(key crypto.PrivateKey) ([]byte, error) {
141+
switch t := key.(type) {
142+
case *ecdsa.PrivateKey:
143+
derBytes, err := x509.MarshalECPrivateKey(t)
144+
if err != nil {
145+
return nil, err
146+
}
147+
block := &pem.Block{
148+
Type: "EC PRIVATE KEY",
149+
Bytes: derBytes,
150+
}
151+
return pem.EncodeToMemory(block), nil
152+
case *rsa.PrivateKey:
153+
block := &pem.Block{
154+
Type: "RSA PRIVATE KEY",
155+
Bytes: x509.MarshalPKCS1PrivateKey(t),
156+
}
157+
return pem.EncodeToMemory(block), nil
158+
default:
159+
return nil, fmt.Errorf("private key is not a recognized type: %T", key)
148160
}
149-
150-
return pem.EncodeToMemory(&block), nil
151161
}
152162

153163
// EncodePublicKeyPEMFromSigner returns PEM-encoded public key data.
154-
func EncodePublicKeyPEMFromSigner(key crypto.Signer) ([]byte, error) {
155-
der, err := x509.MarshalPKIXPublicKey(key.Public())
164+
func EncodePublicKeyPEMFromSigner(key crypto.PublicKey) ([]byte, error) {
165+
der, err := x509.MarshalPKIXPublicKey(key)
156166
if err != nil {
157167
return []byte{}, errors.WithStack(err)
158168
}

util/kubeconfig/kubeconfig.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,14 @@ func FromSecret(ctx context.Context, c client.Reader, cluster client.ObjectKey)
5454
}
5555

5656
// New creates a new Kubeconfig using the cluster name and specified endpoint.
57-
func New(clusterName, endpoint string, caCert *x509.Certificate, caKey crypto.Signer, options ...KubeConfigurationOption) (*api.Config, error) {
57+
func New(clusterName, endpoint string, caCert *x509.Certificate, caKey crypto.Signer, options ...KubeConfigOption) (*api.Config, error) {
5858
cfg := &certs.Config{
5959
CommonName: "kubernetes-admin",
6060
Organization: []string{"system:masters"},
6161
Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
6262
}
6363

64-
userName := fmt.Sprintf("%s-admin", clusterName)
65-
contextName := fmt.Sprintf("%s@%s", userName, clusterName)
66-
67-
kubeConfigOptions := &KubeConfigurationOptions{}
64+
kubeConfigOptions := &KubeConfigOptions{}
6865
kubeConfigOptions.ApplyOptions(options)
6966

7067
clientKey, err := certs.NewSigner(kubeConfigOptions.keyEncryptionAlgorithm)
@@ -82,6 +79,9 @@ func New(clusterName, endpoint string, caCert *x509.Certificate, caKey crypto.Si
8279
return nil, errors.Wrap(err, "unable to encode private key")
8380
}
8481

82+
userName := fmt.Sprintf("%s-admin", clusterName)
83+
contextName := fmt.Sprintf("%s@%s", userName, clusterName)
84+
8585
return &api.Config{
8686
Clusters: map[string]*api.Cluster{
8787
clusterName: {
@@ -106,7 +106,7 @@ func New(clusterName, endpoint string, caCert *x509.Certificate, caKey crypto.Si
106106
}
107107

108108
// CreateSecret creates the Kubeconfig secret for the given cluster.
109-
func CreateSecret(ctx context.Context, c client.Client, cluster *clusterv1.Cluster, options ...KubeConfigurationOption) error {
109+
func CreateSecret(ctx context.Context, c client.Client, cluster *clusterv1.Cluster, options ...KubeConfigOption) error {
110110
name := util.ObjectKey(cluster)
111111
return CreateSecretWithOwner(ctx, c, name, cluster.Spec.ControlPlaneEndpoint.String(), metav1.OwnerReference{
112112
APIVersion: clusterv1.GroupVersion.String(),
@@ -117,7 +117,7 @@ func CreateSecret(ctx context.Context, c client.Client, cluster *clusterv1.Clust
117117
}
118118

119119
// CreateSecretWithOwner creates the Kubeconfig secret for the given cluster name, namespace, endpoint, and owner reference.
120-
func CreateSecretWithOwner(ctx context.Context, c client.Client, clusterName client.ObjectKey, endpoint string, owner metav1.OwnerReference, options ...KubeConfigurationOption) error {
120+
func CreateSecretWithOwner(ctx context.Context, c client.Client, clusterName client.ObjectKey, endpoint string, owner metav1.OwnerReference, options ...KubeConfigOption) error {
121121
server, err := url.JoinPath("https://", endpoint)
122122
if err != nil {
123123
return err
@@ -189,7 +189,7 @@ func NeedsClientCertRotation(configSecret *corev1.Secret, threshold time.Duratio
189189
}
190190

191191
// RegenerateSecret creates and stores a new Kubeconfig in the given secret.
192-
func RegenerateSecret(ctx context.Context, c client.Client, configSecret *corev1.Secret, options ...KubeConfigurationOption) error {
192+
func RegenerateSecret(ctx context.Context, c client.Client, configSecret *corev1.Secret, options ...KubeConfigOption) error {
193193
clusterName, _, err := secret.ParseSecretName(configSecret.Name)
194194
if err != nil {
195195
return errors.Wrap(err, "failed to parse secret name")
@@ -213,7 +213,7 @@ func RegenerateSecret(ctx context.Context, c client.Client, configSecret *corev1
213213
return c.Update(ctx, configSecret)
214214
}
215215

216-
func generateKubeconfig(ctx context.Context, c client.Client, clusterName client.ObjectKey, endpoint string, options ...KubeConfigurationOption) ([]byte, error) {
216+
func generateKubeconfig(ctx context.Context, c client.Client, clusterName client.ObjectKey, endpoint string, options ...KubeConfigOption) ([]byte, error) {
217217
clusterCA, err := secret.GetFromNamespacedName(ctx, c, clusterName, secret.ClusterCA)
218218
if err != nil {
219219
if apierrors.IsNotFound(err) {

util/kubeconfig/options.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@ package kubeconfig
1818

1919
import bootstrapv1 "sigs.k8s.io/cluster-api/api/bootstrap/kubeadm/v1beta2"
2020

21-
// KubeConfigurationOption helps to modify KubeConfigurationOptions.
22-
type KubeConfigurationOption interface {
21+
// KubeConfigOption helps to modify KubeConfigOptions.
22+
type KubeConfigOption interface { //nolint:revive
2323
// ApplyKubeConfigurationOption applies this options to the given kube configuration options.
24-
ApplyKubeConfigurationOption(*KubeConfigurationOptions)
24+
ApplyKubeConfigurationOption(*KubeConfigOptions)
2525
}
2626

27-
// KubeConfigurationOptions allows to set options for generating kube configuration.
28-
type KubeConfigurationOptions struct {
27+
// KubeConfigOptions allows to set options for generating kube configuration.
28+
type KubeConfigOptions struct { //nolint:revive
2929
keyEncryptionAlgorithm bootstrapv1.EncryptionAlgorithmType
3030
}
3131

3232
// ApplyOptions applies the given list options on these options,
3333
// and then returns itself (for convenient chaining).
34-
func (o *KubeConfigurationOptions) ApplyOptions(opts []KubeConfigurationOption) {
34+
func (o *KubeConfigOptions) ApplyOptions(opts []KubeConfigOption) {
3535
for _, opt := range opts {
3636
opt.ApplyKubeConfigurationOption(o)
3737
}
@@ -41,6 +41,6 @@ func (o *KubeConfigurationOptions) ApplyOptions(opts []KubeConfigurationOption)
4141
type KeyEncryptionAlgorithm string
4242

4343
// ApplyKubeConfigurationOption applies this configuration to the given kube configuration options.
44-
func (t KeyEncryptionAlgorithm) ApplyKubeConfigurationOption(opts *KubeConfigurationOptions) {
44+
func (t KeyEncryptionAlgorithm) ApplyKubeConfigurationOption(opts *KubeConfigOptions) {
4545
opts.keyEncryptionAlgorithm = bootstrapv1.EncryptionAlgorithmType(t)
4646
}

util/secret/certificates.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ func (c *Certificate) Generate() error {
435435
}
436436
c.KeyPair = kp
437437
c.Generated = true
438+
438439
return nil
439440
}
440441

@@ -501,7 +502,7 @@ func generateServiceAccountKeys(_ int32, keyEncryptionAlgorithm bootstrapv1.Encr
501502
if err != nil {
502503
return nil, err
503504
}
504-
saPub, err := certs.EncodePublicKeyPEMFromSigner(saCreds)
505+
saPub, err := certs.EncodePublicKeyPEMFromSigner(saCreds.Public())
505506
if err != nil {
506507
return nil, err
507508
}

0 commit comments

Comments
 (0)