From 09502b4cb51d83b44b22b6f89deb6a53d6c28bc3 Mon Sep 17 00:00:00 2001 From: Qingqing Zheng Date: Mon, 9 Apr 2018 17:42:23 -0700 Subject: [PATCH] resorve dup code --- cmd/deploy.go | 8 +++----- pkg/acsengine/ssh.go | 41 ++++++--------------------------------- pkg/acsengine/ssh_test.go | 10 +++++++--- pkg/api/apiloader.go | 28 +------------------------- pkg/helpers/helpers.go | 28 ++++++++++++++++++++++++++ 5 files changed, 45 insertions(+), 70 deletions(-) diff --git a/cmd/deploy.go b/cmd/deploy.go index 5778d6576c..1ff4b0bc7a 100644 --- a/cmd/deploy.go +++ b/cmd/deploy.go @@ -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") } diff --git a/pkg/acsengine/ssh.go b/pkg/acsengine/ssh.go index 2a955311d9..6743a4112a 100644 --- a/pkg/acsengine/ssh.go +++ b/pkg/acsengine/ssh.go @@ -4,26 +4,16 @@ 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 } @@ -31,7 +21,7 @@ func (s *SSHCreator) CreateSaveSSH(username, outputDirectory string) (privateKey privateKeyPem := privateKeyToPem(privateKey) f := &FileSaver{ - Translator: s.Translator, + Translator: s, } err = f.SaveFile(outputDirectory, fmt.Sprintf("%s_rsa", username), privateKeyPem) @@ -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 -} diff --git a/pkg/acsengine/ssh_test.go b/pkg/acsengine/ssh_test.go index 4c6a1c740a..fe4f82f96a 100644 --- a/pkg/acsengine/ssh_test.go +++ b/pkg/acsengine/ssh_test.go @@ -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) { @@ -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) } diff --git a/pkg/api/apiloader.go b/pkg/api/apiloader.go index 23229f5b78..02bade36e1 100644 --- a/pkg/api/apiloader.go +++ b/pkg/api/apiloader.go @@ -2,9 +2,7 @@ package api import ( "crypto/rand" - "crypto/rsa" "encoding/json" - "io" "io/ioutil" "reflect" @@ -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 @@ -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 } @@ -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 -} diff --git a/pkg/helpers/helpers.go b/pkg/helpers/helpers.go index 9affc4fac2..2cade4d925 100644 --- a/pkg/helpers/helpers.go +++ b/pkg/helpers/helpers.go @@ -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 @@ -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 +}