Skip to content

Commit

Permalink
Fix host key validation
Browse files Browse the repository at this point in the history
  • Loading branch information
MightyMoud committed Sep 20, 2024
1 parent 22454bf commit 08c7c6f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
6 changes: 6 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ var initCmd = &cobra.Command{
viper.Set("dockerUsername", dockerUsername)
viper.Set("certEmail", certEmail)

keyAddSshCommand := exec.Command("sh", "-s", "-", server)
keyAddSshCommand.Stdin = strings.NewReader(utils.SshKeysScript)
if sshAddErr := keyAddSshCommand.Run(); sshAddErr != nil {
panic(sshAddErr)
}

multi := pterm.DefaultMultiPrinter
setupProgressBar, _ := pterm.DefaultProgressbar.WithTotal(6).WithWriter(multi.NewWriter()).Start("Sidekick Booting up (2m estimated) ")
rootLoginSpinner, _ := utils.GetSpinner().WithWriter(multi.NewWriter()).Start("Logging into with root")
Expand Down
6 changes: 0 additions & 6 deletions cmd/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ var launchCmd = &cobra.Command{
os.Exit(1)
}

keyAddSshCommand := exec.Command("sh", "-s", "-", viper.Get("serverAddress").(string))
keyAddSshCommand.Stdin = strings.NewReader(utils.SshKeysScript)
if sshAddErr := keyAddSshCommand.Run(); sshAddErr != nil {
panic(sshAddErr)
}

if utils.FileExists("./dockerfile") {
pterm.Info.Println("Dockerfile detected - scanning file for details")
} else {
Expand Down
20 changes: 12 additions & 8 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import (
"github.com/spf13/viper"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"golang.org/x/crypto/ssh/knownhosts"
"os/user"

"gopkg.in/yaml.v3"
)

Expand All @@ -40,7 +43,7 @@ type CommandsStage struct {
SpinnerFailMessage string
}

func GetSshClient(server string, user string) (*ssh.Client, error) {
func GetSshClient(server string, sshUser string) (*ssh.Client, error) {
sshPort := "22"
// connect to local ssh-agent to grab all keys
sshAgentSock := os.Getenv("SSH_AUTH_SOCK")
Expand All @@ -66,18 +69,19 @@ func GetSshClient(server string, user string) (*ssh.Client, error) {
return nil, err
}

currentUser, err := user.Current()
knownHostsCallback, err := knownhosts.New(fmt.Sprintf("%s/.ssh/known_hosts", currentUser.HomeDir))
if err != nil {
log.Fatalf("Error loading known hosts: %v", err)
os.Exit(1)
}
// now that we have our key, we need to start ssh client sesssion
// ƒirst we make some config we pass later
config := &ssh.ClientConfig{
User: user,
User: sshUser,
Auth: []ssh.AuthMethod{
// passing the public keys to callback to get the auth methods
ssh.PublicKeysCallback(agentClient.Signers),
},
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
// use OpenSSH's known_hosts file if you care about host validation
return nil
},
HostKeyCallback: knownHostsCallback,
}

// create SSH client with the said config and connect to server
Expand Down

0 comments on commit 08c7c6f

Please sign in to comment.