/
sftp.go
238 lines (210 loc) · 5.23 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package sftp
import (
"context"
"errors"
"io"
"net"
"net/url"
"os"
"os/user"
"path/filepath"
"reflect"
"strings"
"github.com/pkg/sftp"
"github.com/zeebo/errs"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"golang.org/x/crypto/ssh/knownhosts"
"github.com/jtolio/jam/backends"
"github.com/jtolio/jam/utils"
)
func init() {
backends.Register("sftp", New)
}
// SFTP implements the Backend interface using SFTP
type SFTP struct {
root string
client *sftp.Client
closers []io.Closer
}
// New returns an SFTP backend mounted at the provided path.
func New(ctx context.Context, u *url.URL) (backends.Backend, error) {
var cfg ssh.ClientConfig
if u.User != nil {
cfg.User = u.User.Username()
if password, ok := u.User.Password(); ok {
cfg.Auth = append(cfg.Auth, ssh.Password(password))
}
}
us, err := user.Current()
if err != nil {
return nil, errs.Wrap(err)
}
kh, err := knownhosts.New(filepath.Join(us.HomeDir, ".ssh/known_hosts"))
if err != nil {
return nil, errs.Wrap(err)
}
cfg.HostKeyCallback = kh
var closers []io.Closer
if len(cfg.Auth) == 0 {
agentConn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
if err != nil {
return nil, errs.Wrap(err)
}
closers = append(closers, agentConn)
cfg.Auth = append(cfg.Auth,
ssh.PublicKeysCallback(agent.NewClient(agentConn).Signers))
}
host := u.Host
if _, _, err := net.SplitHostPort(host); err != nil {
if aerr, ok := err.(*net.AddrError); !ok || aerr.Err != "missing port in address" {
return nil, errs.Wrap(err)
}
host += ":22"
}
conn, err := ssh.Dial("tcp", host, &cfg)
if err != nil {
for _, c := range closers {
c.Close()
}
return nil, errs.Wrap(err)
}
closers = append(closers, conn)
client, err := sftp.NewClient(conn, sftp.UseFstat(true))
if err != nil {
for _, c := range closers {
c.Close()
}
return nil, errs.Wrap(err)
}
closers = append(closers, client)
return &SFTP{root: u.Path, client: client, closers: closers}, nil
}
func isNotExist(err error) bool {
if err == nil {
return false
}
if errors.Is(err, sftp.ErrSSHFxNoSuchFile) {
return true
}
if reflect.DeepEqual(err, errors.New("file does not exist")) {
return true
}
return false
}
// Get implements the Backend interface
func (fs *SFTP) Get(ctx context.Context, path string, offset, length int64) (rv io.ReadCloser, err error) {
remotepath := filepath.Join(fs.root, path)
fh, err := fs.client.Open(remotepath)
if err != nil {
if isNotExist(err) {
return nil, errs.Wrap(backends.ErrNotExist)
}
return nil, errs.Wrap(err)
}
if offset > 0 {
_, err = fh.Seek(offset, io.SeekStart)
if err != nil {
fh.Close()
return nil, errs.Wrap(err)
}
}
return fh, nil
}
// Put implements the Backend interface
func (fs *SFTP) Put(ctx context.Context, path string, data io.Reader) (err error) {
defer func() {
if err != nil {
fs.Delete(ctx, path)
}
}()
remotepath := filepath.Join(fs.root, path)
err = fs.client.MkdirAll(filepath.Dir(remotepath))
if err != nil {
return errs.Wrap(err)
}
tmpfile := filepath.Join(filepath.Dir(remotepath), "_"+utils.IdGen())
fh, err := fs.client.OpenFile(tmpfile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC)
if err != nil {
return errs.Wrap(err)
}
defer fh.Close()
defer func() {
if err != nil {
_ = fs.client.Remove(tmpfile)
}
}()
_, err = io.Copy(fh, data)
if err != nil {
return errs.Wrap(err)
}
// TODO: use fsync@openssh.com extension, in case the remote is ext4 or similar
err = fh.Close()
if err != nil {
return errs.Wrap(err)
}
return errs.Wrap(fs.client.PosixRename(tmpfile, remotepath))
}
// Delete implements the Backend interface
func (fs *SFTP) Delete(ctx context.Context, path string) error {
remotepath := filepath.Join(fs.root, path)
if _, err := fs.client.Lstat(remotepath); isNotExist(err) {
return nil
}
err := fs.client.Remove(remotepath)
if err != nil {
_, err := fs.client.Lstat(remotepath)
return errs.New("error: %#v", err)
return errs.Wrap(err)
}
// the rest is not required but is an attempt to be nice and clean up intermediate
// directories after ourselves. remove any parents up to the root that are empty
for {
remotepath = filepath.Dir(remotepath)
rel, err := filepath.Rel(fs.root, remotepath)
if err != nil || rel == "." {
return nil
}
err = fs.client.Remove(remotepath)
if err != nil {
return nil
}
}
}
// List implements the Backend interface
func (fs *SFTP) List(ctx context.Context, prefix string,
cb func(ctx context.Context, path string) error) error {
remotepath := filepath.Join(fs.root, prefix)
if s, err := fs.client.Lstat(remotepath); isNotExist(err) || !s.IsDir() {
return nil
}
walker := fs.client.Walk(filepath.Join(fs.root, prefix))
for walker.Step() {
if err := walker.Err(); err != nil {
return errs.Wrap(err)
}
path := walker.Path()
info := walker.Stat()
if !info.Mode().IsRegular() ||
strings.HasPrefix(filepath.Base(path), "_") {
continue
}
internal, err := filepath.Rel(fs.root, path)
if err != nil {
return errs.Wrap(err)
}
err = cb(ctx, internal)
if err != nil {
return err
}
}
return nil
}
func (fs *SFTP) Close() error {
var eg errs.Group
for i := len(fs.closers) - 1; i >= 0; i-- {
eg.Add(fs.closers[i].Close())
}
fs.closers = nil
return eg.Err()
}