Skip to content

Commit f3e7a35

Browse files
committed
refactor ssh key generation
1 parent 3c82eaf commit f3e7a35

File tree

6 files changed

+88
-31
lines changed

6 files changed

+88
-31
lines changed

api/v1alpha3/azuremachine_default.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,22 @@ limitations under the License.
1717
package v1alpha3
1818

1919
import (
20-
"crypto/rand"
21-
"crypto/rsa"
2220
"encoding/base64"
2321

24-
"github.com/pkg/errors"
2522
"golang.org/x/crypto/ssh"
23+
24+
utilSSH "sigs.k8s.io/cluster-api-provider-azure/util/ssh"
2625
)
2726

2827
// SetDefaultSSHPublicKey sets the default SSHPublicKey for an AzureMachine
2928
func (m *AzureMachine) SetDefaultSSHPublicKey() error {
3029
sshKeyData := m.Spec.SSHPublicKey
3130
if sshKeyData == "" {
32-
privateKey, perr := rsa.GenerateKey(rand.Reader, 2048)
33-
if perr != nil {
34-
return errors.Wrap(perr, "Failed to generate private key")
31+
_, publicRsaKey, err := utilSSH.GenerateSSHKey()
32+
if err != nil {
33+
return err
3534
}
3635

37-
publicRsaKey, perr := ssh.NewPublicKey(&privateKey.PublicKey)
38-
if perr != nil {
39-
return errors.Wrap(perr, "Failed to generate public key")
40-
}
4136
m.Spec.SSHPublicKey = base64.StdEncoding.EncodeToString(ssh.MarshalAuthorizedKey(publicRsaKey))
4237
}
4338

exp/api/v1alpha3/azuremachinepool_default.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,22 @@ limitations under the License.
1717
package v1alpha3
1818

1919
import (
20-
"crypto/rand"
21-
"crypto/rsa"
2220
"encoding/base64"
2321

24-
"github.com/pkg/errors"
2522
"golang.org/x/crypto/ssh"
23+
24+
utilSSH "sigs.k8s.io/cluster-api-provider-azure/util/ssh"
2625
)
2726

2827
// SetDefaultSSHPublicKey sets the default SSHPublicKey for an AzureMachinePool
2928
func (amp *AzureMachinePool) SetDefaultSSHPublicKey() error {
3029
sshKeyData := amp.Spec.Template.SSHPublicKey
3130
if sshKeyData == "" {
32-
privateKey, perr := rsa.GenerateKey(rand.Reader, 2048)
33-
if perr != nil {
34-
return errors.Wrap(perr, "Failed to generate private key")
31+
_, publicRsaKey, err := utilSSH.GenerateSSHKey()
32+
if err != nil {
33+
return err
3534
}
3635

37-
publicRsaKey, perr := ssh.NewPublicKey(&privateKey.PublicKey)
38-
if perr != nil {
39-
return errors.Wrap(perr, "Failed to generate public key")
40-
}
4136
amp.Spec.Template.SSHPublicKey = base64.StdEncoding.EncodeToString(ssh.MarshalAuthorizedKey(publicRsaKey))
4237
}
4338

exp/api/v1alpha3/azuremanagedcontrolplane_default.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,22 @@ limitations under the License.
1717
package v1alpha3
1818

1919
import (
20-
"crypto/rand"
21-
"crypto/rsa"
2220
"encoding/base64"
2321

24-
"github.com/pkg/errors"
2522
"golang.org/x/crypto/ssh"
23+
24+
utilSSH "sigs.k8s.io/cluster-api-provider-azure/util/ssh"
2625
)
2726

2827
// SetDefaultSSHPublicKey sets the default SSHPublicKey for an AzureManagedControlPlane
2928
func (r *AzureManagedControlPlane) SetDefaultSSHPublicKey() error {
3029
sshKeyData := r.Spec.SSHPublicKey
3130
if sshKeyData == "" {
32-
privateKey, perr := rsa.GenerateKey(rand.Reader, 2048)
33-
if perr != nil {
34-
return errors.Wrap(perr, "Failed to generate private key")
31+
_, publicRsaKey, err := utilSSH.GenerateSSHKey()
32+
if err != nil {
33+
return err
3534
}
3635

37-
publicRsaKey, perr := ssh.NewPublicKey(&privateKey.PublicKey)
38-
if perr != nil {
39-
return errors.Wrap(perr, "Failed to generate public key")
40-
}
4136
r.Spec.SSHPublicKey = base64.StdEncoding.EncodeToString(ssh.MarshalAuthorizedKey(publicRsaKey))
4237
}
4338

templates/flavors/aks/cluster-template.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ spec:
3333
location: "${AZURE_LOCATION}"
3434
defaultPoolRef:
3535
name: "agentpool0"
36-
sshPublicKey: "${AZURE_SSH_PUBLIC_KEY_B64}"
36+
sshPublicKey: ${AZURE_SSH_PUBLIC_KEY_B64:=""}
3737
version: "${KUBERNETES_VERSION}"
3838
---
3939
# Due to the nature of managed Kubernetes and the control plane implementation,

util/ssh/ssh.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright 2020 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 ssh
18+
19+
import (
20+
"crypto/rand"
21+
"crypto/rsa"
22+
23+
"github.com/pkg/errors"
24+
"golang.org/x/crypto/ssh"
25+
)
26+
27+
// GenerateSSHKey generates a private and public ssh key
28+
func GenerateSSHKey() (*rsa.PrivateKey, ssh.PublicKey, error) {
29+
privateKey, perr := rsa.GenerateKey(rand.Reader, 2048)
30+
if perr != nil {
31+
return nil, nil, errors.Wrap(perr, "Failed to generate private key")
32+
}
33+
34+
publicRsaKey, perr := ssh.NewPublicKey(&privateKey.PublicKey)
35+
if perr != nil {
36+
return nil, nil, errors.Wrap(perr, "Failed to generate public key")
37+
}
38+
39+
return privateKey, publicRsaKey, nil
40+
}

util/ssh/ssh_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Copyright 2020 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 ssh
18+
19+
import (
20+
"testing"
21+
22+
. "github.com/onsi/gomega"
23+
)
24+
25+
func TestGenerateSSHKey(t *testing.T) {
26+
g := NewWithT(t)
27+
28+
privateKey, publicKey, err := GenerateSSHKey()
29+
g.Expect(err).NotTo(HaveOccurred())
30+
g.Expect(privateKey).NotTo(BeNil())
31+
g.Expect(publicKey).NotTo(BeNil())
32+
}

0 commit comments

Comments
 (0)