|
| 1 | +/* |
| 2 | +Copyright 2025 The Dapr Authors |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package sftp |
| 15 | + |
| 16 | +import ( |
| 17 | + "errors" |
| 18 | + "fmt" |
| 19 | + "os" |
| 20 | + "sync" |
| 21 | + "sync/atomic" |
| 22 | + |
| 23 | + sftpClient "github.com/pkg/sftp" |
| 24 | + "golang.org/x/crypto/ssh" |
| 25 | + |
| 26 | + "github.com/dapr/kit/logger" |
| 27 | +) |
| 28 | + |
| 29 | +type Client struct { |
| 30 | + sshClient *ssh.Client |
| 31 | + sftpClient *sftpClient.Client |
| 32 | + address string |
| 33 | + config *ssh.ClientConfig |
| 34 | + lock sync.RWMutex |
| 35 | + needsReconnect atomic.Bool |
| 36 | + log logger.Logger |
| 37 | +} |
| 38 | + |
| 39 | +func newClient(address string, config *ssh.ClientConfig, log logger.Logger) (*Client, error) { |
| 40 | + if address == "" || config == nil { |
| 41 | + return nil, errors.New("sftp binding error: client not initialized") |
| 42 | + } |
| 43 | + |
| 44 | + sshClient, err := newSSHClient(address, config) |
| 45 | + if err != nil { |
| 46 | + return nil, err |
| 47 | + } |
| 48 | + |
| 49 | + newSftpClient, err := sftpClient.NewClient(sshClient) |
| 50 | + if err != nil { |
| 51 | + _ = sshClient.Close() |
| 52 | + return nil, fmt.Errorf("sftp binding error: error create sftp client: %w", err) |
| 53 | + } |
| 54 | + |
| 55 | + return &Client{ |
| 56 | + sshClient: sshClient, |
| 57 | + sftpClient: newSftpClient, |
| 58 | + address: address, |
| 59 | + config: config, |
| 60 | + log: log, |
| 61 | + }, nil |
| 62 | +} |
| 63 | + |
| 64 | +func (c *Client) Close() error { |
| 65 | + c.lock.Lock() |
| 66 | + defer c.lock.Unlock() |
| 67 | + |
| 68 | + // Close SFTP first, then SSH |
| 69 | + var sftpErr, sshErr error |
| 70 | + if c.sftpClient != nil { |
| 71 | + sftpErr = c.sftpClient.Close() |
| 72 | + } |
| 73 | + if c.sshClient != nil { |
| 74 | + sshErr = c.sshClient.Close() |
| 75 | + } |
| 76 | + |
| 77 | + // Return the first error encountered |
| 78 | + if sftpErr != nil { |
| 79 | + return sftpErr |
| 80 | + } |
| 81 | + return sshErr |
| 82 | +} |
| 83 | + |
| 84 | +func (c *Client) list(path string) ([]os.FileInfo, error) { |
| 85 | + var fi []os.FileInfo |
| 86 | + |
| 87 | + fn := func() error { |
| 88 | + var err error |
| 89 | + fi, err = c.sftpClient.ReadDir(path) |
| 90 | + return err |
| 91 | + } |
| 92 | + |
| 93 | + err := c.withReconnection(fn) |
| 94 | + if err != nil { |
| 95 | + return nil, err |
| 96 | + } |
| 97 | + |
| 98 | + return fi, nil |
| 99 | +} |
| 100 | + |
| 101 | +func (c *Client) create(path string) (*sftpClient.File, string, error) { |
| 102 | + dir, fileName := sftpClient.Split(path) |
| 103 | + |
| 104 | + var file *sftpClient.File |
| 105 | + |
| 106 | + createFn := func() error { |
| 107 | + cErr := c.sftpClient.MkdirAll(dir) |
| 108 | + if cErr != nil { |
| 109 | + return cErr |
| 110 | + } |
| 111 | + |
| 112 | + file, cErr = c.sftpClient.Create(path) |
| 113 | + if cErr != nil { |
| 114 | + return cErr |
| 115 | + } |
| 116 | + |
| 117 | + return nil |
| 118 | + } |
| 119 | + |
| 120 | + rErr := c.withReconnection(createFn) |
| 121 | + if rErr != nil { |
| 122 | + return nil, "", rErr |
| 123 | + } |
| 124 | + |
| 125 | + return file, fileName, nil |
| 126 | +} |
| 127 | + |
| 128 | +func (c *Client) get(path string) (*sftpClient.File, error) { |
| 129 | + var f *sftpClient.File |
| 130 | + |
| 131 | + fn := func() error { |
| 132 | + var err error |
| 133 | + f, err = c.sftpClient.Open(path) |
| 134 | + return err |
| 135 | + } |
| 136 | + |
| 137 | + err := c.withReconnection(fn) |
| 138 | + if err != nil { |
| 139 | + return nil, err |
| 140 | + } |
| 141 | + |
| 142 | + return f, nil |
| 143 | +} |
| 144 | + |
| 145 | +func (c *Client) delete(path string) error { |
| 146 | + fn := func() error { |
| 147 | + return c.sftpClient.Remove(path) |
| 148 | + } |
| 149 | + |
| 150 | + err := c.withReconnection(fn) |
| 151 | + if err != nil { |
| 152 | + return err |
| 153 | + } |
| 154 | + |
| 155 | + return nil |
| 156 | +} |
| 157 | + |
| 158 | +func (c *Client) ping() error { |
| 159 | + _, err := c.sftpClient.Getwd() |
| 160 | + if err != nil { |
| 161 | + return err |
| 162 | + } |
| 163 | + return nil |
| 164 | +} |
| 165 | + |
| 166 | +func (c *Client) withReconnection(fn func() error) error { |
| 167 | + err := c.do(fn) |
| 168 | + if !c.shouldReconnect(err) { |
| 169 | + return err |
| 170 | + } |
| 171 | + |
| 172 | + c.log.Debugf("sftp binding error: %s", err) |
| 173 | + c.needsReconnect.Store(true) |
| 174 | + |
| 175 | + rErr := c.doReconnect() |
| 176 | + if rErr != nil { |
| 177 | + c.log.Debugf("sftp binding error: reconnect failed: %s", rErr) |
| 178 | + return errors.Join(err, rErr) |
| 179 | + } |
| 180 | + c.log.Debugf("sftp binding: reconnected to %s", c.address) |
| 181 | + |
| 182 | + c.log.Debugf("sftp binding: retrying operation") |
| 183 | + return c.do(fn) |
| 184 | +} |
| 185 | + |
| 186 | +func (c *Client) do(fn func() error) error { |
| 187 | + c.lock.RLock() |
| 188 | + defer c.lock.RUnlock() |
| 189 | + return fn() |
| 190 | +} |
| 191 | + |
| 192 | +func (c *Client) doReconnect() error { |
| 193 | + c.lock.Lock() |
| 194 | + defer c.lock.Unlock() |
| 195 | + |
| 196 | + c.log.Debugf("sftp binding: reconnecting to %s", c.address) |
| 197 | + |
| 198 | + if !c.needsReconnect.Load() { |
| 199 | + return nil |
| 200 | + } |
| 201 | + |
| 202 | + pErr := c.ping() |
| 203 | + if pErr == nil { |
| 204 | + c.needsReconnect.Store(false) |
| 205 | + return nil |
| 206 | + } |
| 207 | + |
| 208 | + sshClient, err := newSSHClient(c.address, c.config) |
| 209 | + if err != nil { |
| 210 | + return err |
| 211 | + } |
| 212 | + |
| 213 | + newSftpClient, err := sftpClient.NewClient(sshClient) |
| 214 | + if err != nil { |
| 215 | + _ = sshClient.Close() |
| 216 | + return fmt.Errorf("sftp binding error: error create sftp client: %w", err) |
| 217 | + } |
| 218 | + |
| 219 | + if c.sftpClient != nil { |
| 220 | + _ = c.sftpClient.Close() |
| 221 | + } |
| 222 | + if c.sshClient != nil { |
| 223 | + _ = c.sshClient.Close() |
| 224 | + } |
| 225 | + |
| 226 | + c.sftpClient = newSftpClient |
| 227 | + c.sshClient = sshClient |
| 228 | + |
| 229 | + c.needsReconnect.Store(false) |
| 230 | + return nil |
| 231 | +} |
| 232 | + |
| 233 | +func newSSHClient(address string, config *ssh.ClientConfig) (*ssh.Client, error) { |
| 234 | + sshClient, err := ssh.Dial("tcp", address, config) |
| 235 | + if err != nil { |
| 236 | + return nil, fmt.Errorf("sftp binding error: error dialing ssh server: %w", err) |
| 237 | + } |
| 238 | + return sshClient, nil |
| 239 | +} |
| 240 | + |
| 241 | +// shouldReconnect returns true if the error looks like a transport-level failure |
| 242 | +func (c *Client) shouldReconnect(err error) bool { |
| 243 | + if err == nil { |
| 244 | + return false |
| 245 | + } |
| 246 | + |
| 247 | + // SFTP status errors that are logical, not connectivity (avoid reconnect) |
| 248 | + if errors.Is(err, sftpClient.ErrSSHFxPermissionDenied) || |
| 249 | + errors.Is(err, sftpClient.ErrSSHFxNoSuchFile) || |
| 250 | + errors.Is(err, sftpClient.ErrSSHFxOpUnsupported) { |
| 251 | + return false |
| 252 | + } |
| 253 | + |
| 254 | + return true |
| 255 | +} |
0 commit comments