Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions internal/dag/executor/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ import (
"os"
"strings"

"github.com/daguflow/dagu/internal/dag"
"github.com/mitchellh/mapstructure"
"golang.org/x/crypto/ssh"

"github.com/daguflow/dagu/internal/dag"
)

type sshExec struct {
Expand All @@ -41,9 +42,31 @@ type sshExecConfig struct {
IP string
Port int
Key string
Password string
StrictHostKeyChecking bool
}

// selectSSHAuthMethod selects the authentication method based on the configuration.
// If the key is provided, it will use the public key authentication method.
// Otherwise, it will use the password authentication method.
func selectSSHAuthMethod(cfg *sshExecConfig) (ssh.AuthMethod, error) {
var (
signer ssh.Signer
err error
)

if len(cfg.Key) != 0 {
// Create the Signer for this private key.
if signer, err = getPublicKeySigner(cfg.Key); err != nil {
return nil, err
}

return ssh.PublicKeys(signer), nil
}

return ssh.Password(cfg.Password), nil
}

func newSSHExec(_ context.Context, step dag.Step) (Executor, error) {
cfg := new(sshExecConfig)
md, err := mapstructure.NewDecoder(
Expand All @@ -66,17 +89,16 @@ func newSSHExec(_ context.Context, step dag.Step) (Executor, error) {
return nil, errStrictHostKey
}

// Create the Signer for this private key.
signer, err := getPublicKeySigner(cfg.Key)
// Select the authentication method.
authMethod, err := selectSSHAuthMethod(cfg)
if err != nil {
return nil, err
}

sshConfig := &ssh.ClientConfig{
User: cfg.User,
Auth: []ssh.AuthMethod{
// Use the PublicKeys method for remote authentication.
ssh.PublicKeys(signer),
authMethod,
},
// nolint: gosec
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Expand Down