forked from TrueCloudLab/rclone
sftp: add --sftp-set-env option to set environment variables
Fixes #6094
This commit is contained in:
parent
78120d40d9
commit
60d87185e1
1 changed files with 73 additions and 27 deletions
|
@ -302,6 +302,27 @@ cost of using more memory.
|
||||||
`,
|
`,
|
||||||
Default: 64,
|
Default: 64,
|
||||||
Advanced: true,
|
Advanced: true,
|
||||||
|
}, {
|
||||||
|
Name: "set_env",
|
||||||
|
Default: fs.SpaceSepList{},
|
||||||
|
Help: `Environment variables to pass to sftp and commands
|
||||||
|
|
||||||
|
Set environment variables in the form:
|
||||||
|
|
||||||
|
VAR=value
|
||||||
|
|
||||||
|
to be passed to the sftp client and to any commands run (eg md5sum).
|
||||||
|
|
||||||
|
Pass multiple variables space separated, eg
|
||||||
|
|
||||||
|
VAR1=value VAR2=value
|
||||||
|
|
||||||
|
and pass variables with spaces in in quotes, eg
|
||||||
|
|
||||||
|
"VAR3=value with space" "VAR4=value with space" VAR5=nospacehere
|
||||||
|
|
||||||
|
`,
|
||||||
|
Advanced: true,
|
||||||
}},
|
}},
|
||||||
}
|
}
|
||||||
fs.Register(fsi)
|
fs.Register(fsi)
|
||||||
|
@ -309,33 +330,34 @@ cost of using more memory.
|
||||||
|
|
||||||
// Options defines the configuration for this backend
|
// Options defines the configuration for this backend
|
||||||
type Options struct {
|
type Options struct {
|
||||||
Host string `config:"host"`
|
Host string `config:"host"`
|
||||||
User string `config:"user"`
|
User string `config:"user"`
|
||||||
Port string `config:"port"`
|
Port string `config:"port"`
|
||||||
Pass string `config:"pass"`
|
Pass string `config:"pass"`
|
||||||
KeyPem string `config:"key_pem"`
|
KeyPem string `config:"key_pem"`
|
||||||
KeyFile string `config:"key_file"`
|
KeyFile string `config:"key_file"`
|
||||||
KeyFilePass string `config:"key_file_pass"`
|
KeyFilePass string `config:"key_file_pass"`
|
||||||
PubKeyFile string `config:"pubkey_file"`
|
PubKeyFile string `config:"pubkey_file"`
|
||||||
KnownHostsFile string `config:"known_hosts_file"`
|
KnownHostsFile string `config:"known_hosts_file"`
|
||||||
KeyUseAgent bool `config:"key_use_agent"`
|
KeyUseAgent bool `config:"key_use_agent"`
|
||||||
UseInsecureCipher bool `config:"use_insecure_cipher"`
|
UseInsecureCipher bool `config:"use_insecure_cipher"`
|
||||||
DisableHashCheck bool `config:"disable_hashcheck"`
|
DisableHashCheck bool `config:"disable_hashcheck"`
|
||||||
AskPassword bool `config:"ask_password"`
|
AskPassword bool `config:"ask_password"`
|
||||||
PathOverride string `config:"path_override"`
|
PathOverride string `config:"path_override"`
|
||||||
SetModTime bool `config:"set_modtime"`
|
SetModTime bool `config:"set_modtime"`
|
||||||
ShellType string `config:"shell_type"`
|
ShellType string `config:"shell_type"`
|
||||||
Md5sumCommand string `config:"md5sum_command"`
|
Md5sumCommand string `config:"md5sum_command"`
|
||||||
Sha1sumCommand string `config:"sha1sum_command"`
|
Sha1sumCommand string `config:"sha1sum_command"`
|
||||||
SkipLinks bool `config:"skip_links"`
|
SkipLinks bool `config:"skip_links"`
|
||||||
Subsystem string `config:"subsystem"`
|
Subsystem string `config:"subsystem"`
|
||||||
ServerCommand string `config:"server_command"`
|
ServerCommand string `config:"server_command"`
|
||||||
UseFstat bool `config:"use_fstat"`
|
UseFstat bool `config:"use_fstat"`
|
||||||
DisableConcurrentReads bool `config:"disable_concurrent_reads"`
|
DisableConcurrentReads bool `config:"disable_concurrent_reads"`
|
||||||
DisableConcurrentWrites bool `config:"disable_concurrent_writes"`
|
DisableConcurrentWrites bool `config:"disable_concurrent_writes"`
|
||||||
IdleTimeout fs.Duration `config:"idle_timeout"`
|
IdleTimeout fs.Duration `config:"idle_timeout"`
|
||||||
ChunkSize fs.SizeSuffix `config:"chunk_size"`
|
ChunkSize fs.SizeSuffix `config:"chunk_size"`
|
||||||
Concurrency int `config:"concurrency"`
|
Concurrency int `config:"concurrency"`
|
||||||
|
SetEnv fs.SpaceSepList `config:"set_env"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fs stores the interface to the remote SFTP files
|
// Fs stores the interface to the remote SFTP files
|
||||||
|
@ -483,6 +505,22 @@ func (f *Fs) sftpConnection(ctx context.Context) (c *conn, err error) {
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set any environment variables on the ssh.Session
|
||||||
|
func (f *Fs) setEnv(s *ssh.Session) error {
|
||||||
|
for _, env := range f.opt.SetEnv {
|
||||||
|
equal := strings.IndexRune(env, '=')
|
||||||
|
if equal < 0 {
|
||||||
|
return fmt.Errorf("no = found in env var %q", env)
|
||||||
|
}
|
||||||
|
// fs.Debugf(f, "Setting env %q = %q", env[:equal], env[equal+1:])
|
||||||
|
err := s.Setenv(env[:equal], env[equal+1:])
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Failed to set env var %q: %w", env[:equal], err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Creates a new SFTP client on conn, using the specified subsystem
|
// Creates a new SFTP client on conn, using the specified subsystem
|
||||||
// or sftp server, and zero or more option functions
|
// or sftp server, and zero or more option functions
|
||||||
func (f *Fs) newSftpClient(conn *ssh.Client, opts ...sftp.ClientOption) (*sftp.Client, error) {
|
func (f *Fs) newSftpClient(conn *ssh.Client, opts ...sftp.ClientOption) (*sftp.Client, error) {
|
||||||
|
@ -490,6 +528,10 @@ func (f *Fs) newSftpClient(conn *ssh.Client, opts ...sftp.ClientOption) (*sftp.C
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
err = f.setEnv(s)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
pw, err := s.StdinPipe()
|
pw, err := s.StdinPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -1243,6 +1285,10 @@ func (f *Fs) run(ctx context.Context, cmd string) ([]byte, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("run: get SFTP session: %w", err)
|
return nil, fmt.Errorf("run: get SFTP session: %w", err)
|
||||||
}
|
}
|
||||||
|
err = f.setEnv(session)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
_ = session.Close()
|
_ = session.Close()
|
||||||
}()
|
}()
|
||||||
|
|
Loading…
Add table
Reference in a new issue