Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions sftp/common/sftp.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !windows

package common

import (
Expand Down
109 changes: 109 additions & 0 deletions sftp/common/sftp_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package common

import (
"bufio"
"fmt"
"net/url"
"os/exec"
"strings"

"github.com/pkg/sftp"
)

func CheckParamSupport(params map[string]string) error {
for _, key := range []string{"ssh_auth_sock", "ssh_private_key", "ssh_private_key_ttl"} {
if _, exists := params[key]; exists {
return fmt.Errorf("%q not support on Windows", key)
}
}

return nil
}

func Connect(endpoint *url.URL, params map[string]string) (*sftp.Client, error) {
if endpoint == nil {
return nil, fmt.Errorf("nil endpoint")
}

host := endpoint.Hostname()
if host == "" {
return nil, fmt.Errorf("missing hostname in endpoint: %q", endpoint.String())
}

if err := CheckParamSupport(params); err != nil {
return nil, err
}

var args []string

args = append(args, "-o", "BatchMode=yes")

if params["insecure_ignore_host_key"] == "true" {
args = append(args, "-o", "StrictHostKeyChecking=no")
}

if id := params["identity"]; id != "" {
args = append(args, "-i", id)
}

// username resolution: forbid both user@host AND username param
if endpoint.User != nil && params["username"] != "" {
return nil, fmt.Errorf("can not use user@host foo syntax and username parameter")
} else if endpoint.User != nil {
args = append(args, "-l", endpoint.User.Username())
} else if params["username"] != "" {
args = append(args, "-l", params["username"])
}

if p := endpoint.Port(); p != "" {
args = append(args, "-p", p)
}

args = append(args, host)
args = append(args, "-s", "sftp")

cmd := exec.Command("ssh", args...)

stderr, err := cmd.StderrPipe()
if err != nil {
return nil, err
}

var sshErr error
go func() {
sc := bufio.NewScanner(stderr)
for sc.Scan() {
line := sc.Text()
if strings.HasPrefix(line, "Warning:") {
continue
}
sshErr = fmt.Errorf("ssh command error: %q", line)
}
}()

stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
stdin, err := cmd.StdinPipe()
if err != nil {
return nil, err
}

if err := cmd.Start(); err != nil {
return nil, err
}

// reap process
go func() { _ = cmd.Wait() }()

client, err := sftp.NewClientPipe(stdout, stdin)
if err != nil {
if sshErr != nil {
return nil, sshErr
}
return nil, err
}

return client, nil
}
6 changes: 3 additions & 3 deletions sftp/exporter/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@
"ssh_auth_sock": {
"type": "string",
"minLength": 1,
"description": "Path to SSH agent socket (exported as SSH_AUTH_SOCK for ssh/ssh-add)"
"description": "Path to SSH agent socket (exported as SSH_AUTH_SOCK for ssh/ssh-add) (not supported on Windows)"
},
"ssh_private_key": {
"type": "string",
"minLength": 1,
"description": "SSH private key material (PEM/OpenSSH) to load into ssh-agent via ssh-add -"
"description": "SSH private key material (PEM/OpenSSH) to load into ssh-agent via ssh-add - (not supported on Windows)"
},
"ssh_private_key_ttl": {
"type": "string",
"default": "5s",
"description": "TTL for ssh-add -t (e.g. 5s, 1m, 1h)"
"description": "TTL for ssh-add -t (e.g. 5s, 1m, 1h) (not supported on Windows)"
}
},
"allOf": [
Expand Down
6 changes: 3 additions & 3 deletions sftp/importer/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@
"ssh_auth_sock": {
"type": "string",
"minLength": 1,
"description": "Path to SSH agent socket (exported as SSH_AUTH_SOCK for ssh/ssh-add)"
"description": "Path to SSH agent socket (exported as SSH_AUTH_SOCK for ssh/ssh-add) (not supported on Windows)"
},
"ssh_private_key": {
"type": "string",
"minLength": 1,
"description": "SSH private key material (PEM/OpenSSH) to load into ssh-agent via ssh-add -"
"description": "SSH private key material (PEM/OpenSSH) to load into ssh-agent via ssh-add - (not supported on Windows)"
},
"ssh_private_key_ttl": {
"type": "string",
"default": "5s",
"description": "TTL for ssh-add -t (e.g. 5s, 1m, 1h)"
"description": "TTL for ssh-add -t (e.g. 5s, 1m, 1h) (not supported on Windows)"
}
},
"allOf": [
Expand Down
6 changes: 3 additions & 3 deletions sftp/storage/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@
"insecure_ignore_host_key": {
"type": "boolean",
"default": false,
"description": "Disable host key verification (ssh StrictHostKeyChecking=no) (dangerous; use only for testing)"
"description": "Disable host key verification (ssh StrictHostKeyChecking=no) (dangerous; use only for testing) (not supported on Windows)"
},
"ssh_auth_sock": {
"type": "string",
"minLength": 1,
"description": "Path to SSH agent socket (exported as SSH_AUTH_SOCK for ssh/ssh-add)"
"description": "Path to SSH agent socket (exported as SSH_AUTH_SOCK for ssh/ssh-add) (not supported on Windows)"
},
"ssh_private_key": {
"type": "string",
"minLength": 1,
"description": "SSH private key material (PEM/OpenSSH) to load into ssh-agent via ssh-add -"
"description": "SSH private key material (PEM/OpenSSH) to load into ssh-agent via ssh-add - (not supported on Windows)"
},
"ssh_private_key_ttl": {
"type": "string",
Expand Down