-
Notifications
You must be signed in to change notification settings - Fork 2
/
sftp.go
147 lines (118 loc) · 3.69 KB
/
sftp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package xfer
import (
"fmt"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
)
type ParsedSftpUri struct {
User string
Host string
Port int
PrivateKeyPath string
RemotePath string
}
const DefaultSshPort int = 22
func parseSftpUri(uri string, privateKeys map[string]string) (*ParsedSftpUri, error) {
// sftp://user[:pass]@host[:port]/path/to/file
u, err := url.Parse(uri)
if err != nil {
return nil, err
}
if u.User == nil || u.User.Username() == "" {
return nil, fmt.Errorf("empty username in sftp uri %s", uri)
}
userName := u.User.Username()
privateKeyPath, ok := privateKeys[userName]
if !ok {
return nil, fmt.Errorf("username %s in sftp uri %s not found in environment configuration", userName, uri)
}
hostParts := strings.Split(u.Host, ":")
if len(hostParts) < 2 {
return &ParsedSftpUri{userName, hostParts[0], DefaultSshPort, privateKeyPath, u.Path}, nil
}
port, err := strconv.ParseInt(hostParts[1], 10, 32)
if err != nil {
return nil, fmt.Errorf("cannot parse port in sftp uri %s", uri)
}
return &ParsedSftpUri{userName, hostParts[0], int(port), privateKeyPath, u.Path}, nil
}
func DownloadSftpFile(uri string, privateKeys map[string]string, dstFile *os.File) error {
parsedUri, err := parseSftpUri(uri, privateKeys)
if err != nil {
return err
}
// Assume empty key password ""
sshClientConfig, err := NewSshClientConfig(parsedUri.User, parsedUri.PrivateKeyPath, "")
if err != nil {
return err
}
sshUrl := fmt.Sprintf("%s:%d", parsedUri.Host, parsedUri.Port)
sshClient, err := ssh.Dial("tcp", sshUrl, sshClientConfig)
if err != nil {
return fmt.Errorf("dial to %s failed: %s", uri, err.Error())
}
defer sshClient.Close()
sftpClient, err := sftp.NewClient(sshClient)
if err != nil {
return fmt.Errorf("cannot create sftp client to %s: %s", uri, err.Error())
}
defer sftpClient.Close()
srcFile, err := sftpClient.Open(parsedUri.RemotePath)
if err != nil {
return fmt.Errorf("cannot open target file for sftp download %s: %s", uri, err.Error())
}
defer srcFile.Close()
_, err = dstFile.ReadFrom(srcFile)
if err != nil {
return fmt.Errorf("cannot read for download %s: %s", uri, err.Error())
}
return nil
}
func UploadSftpFile(srcPath string, uri string, privateKeys map[string]string) error {
parsedUri, err := parseSftpUri(uri, privateKeys)
if err != nil {
return err
}
// Assume empty key password ""
sshClientConfig, err := NewSshClientConfig(parsedUri.User, parsedUri.PrivateKeyPath, "")
if err != nil {
return err
}
sshUrl := fmt.Sprintf("%s:%d", parsedUri.Host, parsedUri.Port)
sshClient, err := ssh.Dial("tcp", sshUrl, sshClientConfig)
if err != nil {
return fmt.Errorf("dial to %s failed: %s", uri, err.Error())
}
defer sshClient.Close()
sftpClient, err := sftp.NewClient(sshClient)
if err != nil {
return fmt.Errorf("cannot create sftp client to %s: %s", uri, err.Error())
}
defer sftpClient.Close()
if err := sftpClient.MkdirAll(filepath.Dir(parsedUri.RemotePath)); err != nil {
return fmt.Errorf("cannot create target dir for %s: %s", uri, err.Error())
}
srcFile, err := os.Open(srcPath)
if err != nil {
return fmt.Errorf("cannot open for upload %s: %s", srcPath, err.Error())
}
defer srcFile.Close()
dstFile, err := sftpClient.Create(parsedUri.RemotePath)
if err != nil {
return fmt.Errorf("cannot create on upload %s: %s", uri, err.Error())
}
_, err = dstFile.ReadFrom(srcFile)
if err != nil {
return fmt.Errorf("cannot read for upload %s to %s : %s", srcPath, uri, err.Error())
}
err = dstFile.Sync()
if err != nil {
return fmt.Errorf("cannot flush for upload %s to %s : %s", srcPath, uri, err.Error())
}
return nil
}