Skip to content

Commit

Permalink
server: Remove SSH default options
Browse files Browse the repository at this point in the history
  • Loading branch information
ish-hcc committed Dec 4, 2024
1 parent c5b110e commit 7b973db
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 140 deletions.
6 changes: 0 additions & 6 deletions server/cmd/cm-honeybee/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
112 changes: 2 additions & 110 deletions server/lib/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -19,7 +15,6 @@ import (
"github.com/cloud-barista/cm-honeybee/server/pkg/api/rest/model"

"io"
"os"
"path/filepath"

"github.com/pkg/sftp"
Expand All @@ -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)

Expand Down Expand Up @@ -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 ""
}
8 changes: 2 additions & 6 deletions server/pkg/api/rest/controller/benchmarkInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions server/pkg/api/rest/controller/connectionInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 7 additions & 15 deletions server/pkg/api/rest/controller/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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"
Expand All @@ -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)
Expand Down

0 comments on commit 7b973db

Please sign in to comment.