Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Commit

Permalink
resorve dup code
Browse files Browse the repository at this point in the history
  • Loading branch information
zqingqing1 committed Apr 10, 2018
1 parent ca3d4b7 commit 09502b4
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 70 deletions.
8 changes: 3 additions & 5 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,10 @@ func autofillApimodel(dc *deployCmd) {
if dc.containerService.Properties.LinuxProfile != nil && (dc.containerService.Properties.LinuxProfile.SSH.PublicKeys == nil ||
len(dc.containerService.Properties.LinuxProfile.SSH.PublicKeys) == 0 ||
dc.containerService.Properties.LinuxProfile.SSH.PublicKeys[0].KeyData == "") {
creator := &acsengine.SSHCreator{
Translator: &i18n.Translator{
Locale: dc.locale,
},
translator := &i18n.Translator{
Locale: dc.locale,
}
_, publicKey, err := creator.CreateSaveSSH(dc.containerService.Properties.LinuxProfile.AdminUsername, dc.outputDirectory)
_, publicKey, err := acsengine.CreateSaveSSH(dc.containerService.Properties.LinuxProfile.AdminUsername, dc.outputDirectory, translator)
if err != nil {
log.Fatal("Failed to generate SSH Key")
}
Expand Down
41 changes: 6 additions & 35 deletions pkg/acsengine/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,24 @@ import (
"crypto/rand"
"crypto/rsa"
"fmt"
"io"

"github.com/Azure/acs-engine/pkg/i18n"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
)

// SSHCreator represents the object that creates SSH key pair
type SSHCreator struct {
Translator *i18n.Translator
}
"github.com/Azure/acs-engine/pkg/helpers"

const (
// SSHKeySize is the size (in bytes) of SSH key to create
SSHKeySize = 4096
"github.com/Azure/acs-engine/pkg/i18n"
)

// CreateSaveSSH generates and stashes an SSH key pair.
func (s *SSHCreator) CreateSaveSSH(username, outputDirectory string) (privateKey *rsa.PrivateKey, publicKeyString string, err error) {
privateKey, publicKeyString, err = s.CreateSSH(rand.Reader)
func CreateSaveSSH(username, outputDirectory string, s *i18n.Translator) (privateKey *rsa.PrivateKey, publicKeyString string, err error) {

privateKey, publicKeyString, err = helpers.CreateSSH(rand.Reader, s)
if err != nil {
return nil, "", err
}

privateKeyPem := privateKeyToPem(privateKey)

f := &FileSaver{
Translator: s.Translator,
Translator: s,
}

err = f.SaveFile(outputDirectory, fmt.Sprintf("%s_rsa", username), privateKeyPem)
Expand All @@ -41,22 +31,3 @@ func (s *SSHCreator) CreateSaveSSH(username, outputDirectory string) (privateKey

return privateKey, publicKeyString, nil
}

// CreateSSH creates an SSH key pair.
func (s *SSHCreator) CreateSSH(rg io.Reader) (privateKey *rsa.PrivateKey, publicKeyString string, err error) {
log.Debugf("ssh: generating %dbit rsa key", SSHKeySize)
privateKey, err = rsa.GenerateKey(rg, SSHKeySize)
if err != nil {
return nil, "", s.Translator.Errorf("failed to generate private key for ssh: %q", err)
}

publicKey := privateKey.PublicKey
sshPublicKey, err := ssh.NewPublicKey(&publicKey)
if err != nil {
return nil, "", s.Translator.Errorf("failed to create openssh public key string: %q", err)
}
authorizedKeyBytes := ssh.MarshalAuthorizedKey(sshPublicKey)
authorizedKey := string(authorizedKeyBytes)

return privateKey, authorizedKey, nil
}
10 changes: 7 additions & 3 deletions pkg/acsengine/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package acsengine
import (
"math/rand"
"testing"

"github.com/Azure/acs-engine/pkg/helpers"

"github.com/Azure/acs-engine/pkg/i18n"
)

func TestCreateSSH(t *testing.T) {
Expand Down Expand Up @@ -63,11 +67,11 @@ EPDesL0rH+3s1CKpgkhYdbJ675GFoGoq+X21QaqsdvoXmmuJF9qq9Tq+JaWloUNq
-----END RSA PRIVATE KEY-----
`

creator := &SSHCreator{
Translator: nil,
translator := &i18n.Translator{
Locale: nil,
}

privateKey, publicKey, err := creator.CreateSSH(rg)
privateKey, publicKey, err := helpers.CreateSSH(rg, translator)
if err != nil {
t.Fatalf("failed to generate SSH: %s", err)
}
Expand Down
28 changes: 1 addition & 27 deletions pkg/api/apiloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package api

import (
"crypto/rand"
"crypto/rsa"
"encoding/json"
"io"
"io/ioutil"
"reflect"

Expand All @@ -20,7 +18,6 @@ import (
"github.com/Azure/acs-engine/pkg/helpers"
"github.com/Azure/acs-engine/pkg/i18n"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
)

// Apiloader represents the object that loads api model
Expand Down Expand Up @@ -235,7 +232,7 @@ func (a *Apiloader) LoadContainerServiceForAgentPoolOnlyCluster(contents []byte,
if managedCluster.Properties.LinuxProfile == nil {
linuxProfile := &v20180331.LinuxProfile{}
linuxProfile.AdminUsername = "azureuser"
publicKey, err := a.createSSH(rand.Reader)
_, publicKey, err := helpers.CreateSSH(rand.Reader, a.Translator)
if err != nil {
return nil, IsSSHAutoGenerated, err
}
Expand Down Expand Up @@ -381,26 +378,3 @@ func setContainerServiceDefaultsv20170131(c *v20170131.ContainerService) {
}
}
}

const (
// SSHKeySize is the size (in bytes) of SSH key to create
SSHKeySize = 4096
)

func (a *Apiloader) createSSH(rg io.Reader) (publicKeyString string, err error) {
log.Debugf("ssh: generating %dbit rsa key", SSHKeySize)
privateKey, err := rsa.GenerateKey(rg, SSHKeySize)
if err != nil {
return "", a.Translator.Errorf("failed to generate private key for ssh: %q", err)
}

publicKey := privateKey.PublicKey
sshPublicKey, err := ssh.NewPublicKey(&publicKey)
if err != nil {
return "", a.Translator.Errorf("failed to create openssh public key string: %q", err)
}
authorizedKeyBytes := ssh.MarshalAuthorizedKey(sshPublicKey)
authorizedKey := string(authorizedKeyBytes)

return authorizedKey, nil
}
28 changes: 28 additions & 0 deletions pkg/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@ package helpers
import (
// "fmt"
"bytes"
"crypto/rsa"
"encoding/json"
"io"

"github.com/Azure/acs-engine/pkg/i18n"
"golang.org/x/crypto/ssh"
)

const (
// SSHKeySize is the size (in bytes) of SSH key to create
SSHKeySize = 4096
)

// JSONMarshalIndent marshals formatted JSON w/ optional SetEscapeHTML
Expand Down Expand Up @@ -46,3 +56,21 @@ func PointerToBool(b bool) *bool {
p := b
return &p
}

// CreateSSH creates an SSH key pair.
func CreateSSH(rg io.Reader, s *i18n.Translator) (privateKey *rsa.PrivateKey, publicKeyString string, err error) {
privateKey, err = rsa.GenerateKey(rg, SSHKeySize)
if err != nil {
return nil, "", s.Errorf("failed to generate private key for ssh: %q", err)
}

publicKey := privateKey.PublicKey
sshPublicKey, err := ssh.NewPublicKey(&publicKey)
if err != nil {
return nil, "", s.Errorf("failed to create openssh public key string: %q", err)
}
authorizedKeyBytes := ssh.MarshalAuthorizedKey(sshPublicKey)
authorizedKey := string(authorizedKeyBytes)

return privateKey, authorizedKey, nil
}

0 comments on commit 09502b4

Please sign in to comment.