From 7b973dbb0ce64072c02280a92b9079d269b78961 Mon Sep 17 00:00:00 2001 From: ish Date: Wed, 4 Dec 2024 17:18:04 +0900 Subject: [PATCH] server: Remove SSH default options --- server/cmd/cm-honeybee/main.go | 6 - server/lib/ssh/ssh.go | 112 +----------------- .../pkg/api/rest/controller/benchmarkInfo.go | 8 +- .../pkg/api/rest/controller/connectionInfo.go | 4 +- server/pkg/api/rest/controller/import.go | 22 ++-- 5 files changed, 12 insertions(+), 140 deletions(-) diff --git a/server/cmd/cm-honeybee/main.go b/server/cmd/cm-honeybee/main.go index e4bc9b1..e19e62c 100644 --- a/server/cmd/cm-honeybee/main.go +++ b/server/cmd/cm-honeybee/main.go @@ -6,7 +6,6 @@ import ( "github.com/cloud-barista/cm-honeybee/server/db" "github.com/cloud-barista/cm-honeybee/server/lib/config" "github.com/cloud-barista/cm-honeybee/server/lib/rsautil" - "github.com/cloud-barista/cm-honeybee/server/lib/ssh" "github.com/cloud-barista/cm-honeybee/server/pkg/api/rest/controller" "github.com/cloud-barista/cm-honeybee/server/pkg/api/rest/server" "github.com/jollaman999/utils/fileutil" @@ -51,11 +50,6 @@ func init() { logger.Panicln(logger.ERROR, false, err.Error()) } - err = ssh.GenerateSSHIdentityFile() - if err != nil { - logger.Panicln(logger.ERROR, false, err.Error()) - } - controller.OkMessage.Message = "API server is not ready" var wg sync.WaitGroup diff --git a/server/lib/ssh/ssh.go b/server/lib/ssh/ssh.go index e11caae..c453f70 100644 --- a/server/lib/ssh/ssh.go +++ b/server/lib/ssh/ssh.go @@ -2,12 +2,8 @@ package ssh import ( "bytes" - "crypto/rand" - "crypto/rsa" - "crypto/x509" "embed" "encoding/json" - "encoding/pem" "errors" "fmt" "github.com/cloud-barista/cm-honeybee/server/lib/config" @@ -19,7 +15,6 @@ import ( "github.com/cloud-barista/cm-honeybee/server/pkg/api/rest/model" "io" - "os" "path/filepath" "github.com/pkg/sftp" @@ -38,102 +33,13 @@ type Response struct { } type Options struct { - SSHAddress string - SSHPort int - SSHUsername string - SSHPassword string - IdentityFilePath string - IdentityFilePathProvided bool - session *ssh.Session - client *ssh.Client + session *ssh.Session + client *ssh.Client } //go:embed sourceFiles/* var sourceFiles embed.FS -var homeDir string - -func GenerateSSHIdentityFile() error { - var err error - - homeDir, err = os.UserHomeDir() - if err != nil { - return fmt.Errorf("failed to determine user home directory: %v", err) - } - sshDir := filepath.Join(homeDir, ".ssh") - privateKeyPath := filepath.Join(sshDir, "id_rsa") - publicKeyPath := filepath.Join(sshDir, "id_rsa.pub") - - _, err = os.Stat(privateKeyPath) - if err == nil { - return nil - } - - err = os.MkdirAll(sshDir, 0700) - if err != nil { - return err - } - - privateKey, err := rsa.GenerateKey(rand.Reader, 2048) - if err != nil { - return fmt.Errorf("failed to generate ssh private key file: %v", err) - } - - privateKeyPEM := &pem.Block{ - Type: "RSA PRIVATE KEY", - Bytes: x509.MarshalPKCS1PrivateKey(privateKey), - } - - privateKeyFile, err := os.OpenFile(privateKeyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) - if err != nil { - return fmt.Errorf("failed to generate ssh private key file: %v", err) - } - defer func() { - _ = privateKeyFile.Close() - }() - - err = pem.Encode(privateKeyFile, privateKeyPEM) - if err != nil { - return fmt.Errorf("failed to store ssh private key file: %v", err) - } - - publicKey := &privateKey.PublicKey - publicKeyBytes, err := x509.MarshalPKIXPublicKey(publicKey) - if err != nil { - return fmt.Errorf("failed to generate ssh public key file: %v", err) - } - - publicKeyPEM := &pem.Block{ - Type: "RSA PUBLIC KEY", - Bytes: publicKeyBytes, - } - - publicKeyFile, err := os.OpenFile(publicKeyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) - if err != nil { - return fmt.Errorf("failed to generate ssh public key file: %v", err) - } - defer func() { - _ = publicKeyFile.Close() - }() - - err = pem.Encode(publicKeyFile, publicKeyPEM) - if err != nil { - return fmt.Errorf("failed to store ssh public key file: %v", err) - } - - return nil -} - -func DefaultSSHOptions() Options { - return Options{ - SSHPort: 22, - SSHUsername: defaultUsername(), - SSHPassword: "", - IdentityFilePath: filepath.Join(homeDir, ".ssh", "id_rsa"), - IdentityFilePathProvided: false, - } -} - func (o *SSH) NewClientConn(connectionInfo model.ConnectionInfo) error { addr := fmt.Sprintf("%s:%s", connectionInfo.IPAddress, connectionInfo.SSHPort) @@ -444,17 +350,3 @@ func (o *SSH) Close() { _ = o.Options.client.Close() } } - -func defaultUsername() string { - vars := []string{ - "USER", // linux - "USERNAME", // linux, windows - "LOGNAME", // linux - } - for _, env := range vars { - if v := os.Getenv(env); v != "" { - return v - } - } - return "" -} diff --git a/server/pkg/api/rest/controller/benchmarkInfo.go b/server/pkg/api/rest/controller/benchmarkInfo.go index 32e265b..18b3b3e 100644 --- a/server/pkg/api/rest/controller/benchmarkInfo.go +++ b/server/pkg/api/rest/controller/benchmarkInfo.go @@ -88,9 +88,7 @@ func RunBenchmarkInfo(c echo.Context) error { oldSavedBenchmarkInfo = savedBenchmarkInfo } - s := &ssh.SSH{ - Options: ssh.DefaultSSHOptions(), - } + s := &ssh.SSH{} oldSavedBenchmarkInfo.Status = "benchmarking" _ = dao.SavedBenchmarkInfoUpdate(oldSavedBenchmarkInfo) @@ -146,9 +144,7 @@ func StopBenchmarkInfo(c echo.Context) error { return common.ReturnInternalError(c, err, "Error occurred while getting benchmark information.") } - s := &ssh.SSH{ - Options: ssh.DefaultSSHOptions(), - } + s := &ssh.SSH{} err = s.StopBenchmark(*connectionInfo) if err != nil { diff --git a/server/pkg/api/rest/controller/connectionInfo.go b/server/pkg/api/rest/controller/connectionInfo.go index ff461fc..89e4765 100644 --- a/server/pkg/api/rest/controller/connectionInfo.go +++ b/server/pkg/api/rest/controller/connectionInfo.go @@ -133,9 +133,7 @@ func doGetConnectionInfo(connID string, refresh bool) (*model.ConnectionInfo, er } if refresh { - c := &ssh.SSH{ - Options: ssh.DefaultSSHOptions(), - } + c := &ssh.SSH{} err = c.NewClientConn(*connectionInfo) if err != nil { diff --git a/server/pkg/api/rest/controller/import.go b/server/pkg/api/rest/controller/import.go index 788b738..cc29136 100644 --- a/server/pkg/api/rest/controller/import.go +++ b/server/pkg/api/rest/controller/import.go @@ -37,9 +37,7 @@ func doImportInfra(connID string) (*model.SavedInfraInfo, error) { oldSavedInfraInfo = savedInfraInfo } - s := &ssh.SSH{ - Options: ssh.DefaultSSHOptions(), - } + s := &ssh.SSH{} data, err := s.SendGetRequestToAgent(*connectionInfo, "/infra") if err != nil { oldSavedInfraInfo.Status = "failed" @@ -50,7 +48,7 @@ func doImportInfra(connID string) (*model.SavedInfraInfo, error) { return nil, errors.New(errMsg) } - oldSavedInfraInfo.InfraData = string(data) + oldSavedInfraInfo.InfraData = data oldSavedInfraInfo.Status = "success" oldSavedInfraInfo.SavedTime = time.Now() err = dao.SavedInfraInfoUpdate(oldSavedInfraInfo) @@ -88,9 +86,7 @@ func doImportSoftware(connID string, showDefaultPackages bool) (*model.SavedSoft oldSavedSoftwareInfo = savedSoftwareInfo } - s := &ssh.SSH{ - Options: ssh.DefaultSSHOptions(), - } + s := &ssh.SSH{} data, err := s.SendGetRequestToAgent(*connectionInfo, "/software?show_default_packages="+strconv.FormatBool(showDefaultPackages)) if err != nil { oldSavedSoftwareInfo.Status = "failed" @@ -139,9 +135,7 @@ func doImportKubernetes(connID string) (*model.SavedKubernetesInfo, error) { oldSavedKubernetesInfo = savedKubernetesInfo } - s := &ssh.SSH{ - Options: ssh.DefaultSSHOptions(), - } + s := &ssh.SSH{} data, err := s.SendGetRequestToAgent(*connectionInfo, "/kubernetes") if err != nil { oldSavedKubernetesInfo.Status = "failed" @@ -152,7 +146,7 @@ func doImportKubernetes(connID string) (*model.SavedKubernetesInfo, error) { return nil, errors.New(errMsg) } - oldSavedKubernetesInfo.KubernetesData = string(data) + oldSavedKubernetesInfo.KubernetesData = data oldSavedKubernetesInfo.Status = "success" oldSavedKubernetesInfo.SavedTime = time.Now() err = dao.SavedKubernetesInfoUpdate(oldSavedKubernetesInfo) @@ -190,9 +184,7 @@ func doImportHelm(connID string) (*model.SavedHelmInfo, error) { oldSavedHelmInfo = savedHelmInfo } - s := &ssh.SSH{ - Options: ssh.DefaultSSHOptions(), - } + s := &ssh.SSH{} data, err := s.SendGetRequestToAgent(*connectionInfo, "/helm") if err != nil { oldSavedHelmInfo.Status = "failed" @@ -203,7 +195,7 @@ func doImportHelm(connID string) (*model.SavedHelmInfo, error) { return nil, errors.New(errMsg) } - oldSavedHelmInfo.HelmData = string(data) + oldSavedHelmInfo.HelmData = data oldSavedHelmInfo.Status = "success" oldSavedHelmInfo.SavedTime = time.Now() err = dao.SavedHelmInfoUpdate(oldSavedHelmInfo)