serve sftp: convert options to new style

This commit is contained in:
Nick Craig-Wood 2024-07-03 19:29:54 +01:00
parent 28ba4b832d
commit eff2497633
3 changed files with 45 additions and 24 deletions

View File

@ -133,7 +133,7 @@ func (s *server) serve() (err error) {
var authorizedKeysMap map[string]struct{} var authorizedKeysMap map[string]struct{}
// ensure the user isn't trying to use conflicting flags // ensure the user isn't trying to use conflicting flags
if proxyflags.Opt.AuthProxy != "" && s.opt.AuthorizedKeys != "" && s.opt.AuthorizedKeys != DefaultOpt.AuthorizedKeys { if proxyflags.Opt.AuthProxy != "" && s.opt.AuthorizedKeys != "" && s.opt.AuthorizedKeys != Opt.AuthorizedKeys {
return errors.New("--auth-proxy and --authorized-keys cannot be used at the same time") return errors.New("--auth-proxy and --authorized-keys cannot be used at the same time")
} }
@ -142,7 +142,7 @@ func (s *server) serve() (err error) {
authKeysFile := env.ShellExpand(s.opt.AuthorizedKeys) authKeysFile := env.ShellExpand(s.opt.AuthorizedKeys)
authorizedKeysMap, err = loadAuthorizedKeys(authKeysFile) authorizedKeysMap, err = loadAuthorizedKeys(authKeysFile)
// If user set the flag away from the default then report an error // If user set the flag away from the default then report an error
if err != nil && s.opt.AuthorizedKeys != DefaultOpt.AuthorizedKeys { if err != nil && s.opt.AuthorizedKeys != Opt.AuthorizedKeys {
return err return err
} }
fs.Logf(nil, "Loaded %d authorized keys from %q", len(authorizedKeysMap), authKeysFile) fs.Logf(nil, "Loaded %d authorized keys from %q", len(authorizedKeysMap), authKeysFile)

View File

@ -11,7 +11,6 @@ import (
"github.com/rclone/rclone/cmd/serve/proxy/proxyflags" "github.com/rclone/rclone/cmd/serve/proxy/proxyflags"
"github.com/rclone/rclone/fs" "github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config/flags" "github.com/rclone/rclone/fs/config/flags"
"github.com/rclone/rclone/fs/rc"
"github.com/rclone/rclone/lib/systemd" "github.com/rclone/rclone/lib/systemd"
"github.com/rclone/rclone/vfs" "github.com/rclone/rclone/vfs"
"github.com/rclone/rclone/vfs/vfsflags" "github.com/rclone/rclone/vfs/vfsflags"
@ -19,36 +18,58 @@ import (
"github.com/spf13/pflag" "github.com/spf13/pflag"
) )
// OptionsInfo descripts the Options in use
var OptionsInfo = fs.Options{{
Name: "addr",
Default: "localhost:2022",
Help: "IPaddress:Port or :Port to bind server to",
}, {
Name: "key",
Default: []string{},
Help: "SSH private host key file (Can be multi-valued, leave blank to auto generate)",
}, {
Name: "authorized_keys",
Default: "~/.ssh/authorized_keys",
Help: "Authorized keys file",
}, {
Name: "user",
Default: "",
Help: "User name for authentication",
}, {
Name: "pass",
Default: "",
Help: "Password for authentication",
}, {
Name: "no_auth",
Default: false,
Help: "Allow connections with no authentication if set",
}, {
Name: "stdio",
Default: false,
Help: "Run an sftp server on stdin/stdout",
}}
// Options contains options for the http Server // Options contains options for the http Server
type Options struct { type Options struct {
ListenAddr string // Port to listen on ListenAddr string `config:"addr"` // Port to listen on
HostKeys []string // Paths to private host keys HostKeys []string `config:"key"` // Paths to private host keys
AuthorizedKeys string // Path to authorized keys file AuthorizedKeys string `config:"authorized_keys"` // Path to authorized keys file
User string // single username User string `config:"user"` // single username
Pass string // password for user Pass string `config:"pass"` // password for user
NoAuth bool // allow no authentication on connections NoAuth bool `config:"no_auth"` // allow no authentication on connections
Stdio bool // serve on stdio Stdio bool `config:"stdio"` // serve on stdio
} }
// DefaultOpt is the default values used for Options func init() {
var DefaultOpt = Options{ fs.RegisterGlobalOptions(fs.OptionsInfo{Name: "sftp", Opt: &Opt, Options: OptionsInfo})
ListenAddr: "localhost:2022",
AuthorizedKeys: "~/.ssh/authorized_keys",
} }
// Opt is options set by command line flags // Opt is options set by command line flags
var Opt = DefaultOpt var Opt Options
// AddFlags adds flags for the sftp // AddFlags adds flags for the sftp
func AddFlags(flagSet *pflag.FlagSet, Opt *Options) { func AddFlags(flagSet *pflag.FlagSet, Opt *Options) {
rc.AddOption("sftp", &Opt) flags.AddFlagsFromOptions(flagSet, "", OptionsInfo)
flags.StringVarP(flagSet, &Opt.ListenAddr, "addr", "", Opt.ListenAddr, "IPaddress:Port or :Port to bind server to", "")
flags.StringArrayVarP(flagSet, &Opt.HostKeys, "key", "", Opt.HostKeys, "SSH private host key file (Can be multi-valued, leave blank to auto generate)", "")
flags.StringVarP(flagSet, &Opt.AuthorizedKeys, "authorized-keys", "", Opt.AuthorizedKeys, "Authorized keys file", "")
flags.StringVarP(flagSet, &Opt.User, "user", "", Opt.User, "User name for authentication", "")
flags.StringVarP(flagSet, &Opt.Pass, "pass", "", Opt.Pass, "Password for authentication", "")
flags.BoolVarP(flagSet, &Opt.NoAuth, "no-auth", "", Opt.NoAuth, "Allow connections with no authentication if set", "")
flags.BoolVarP(flagSet, &Opt.Stdio, "stdio", "", Opt.Stdio, "Run an sftp server on stdin/stdout", "")
} }
func init() { func init() {

View File

@ -40,7 +40,7 @@ var (
func TestSftp(t *testing.T) { func TestSftp(t *testing.T) {
// Configure and start the server // Configure and start the server
start := func(f fs.Fs) (configmap.Simple, func()) { start := func(f fs.Fs) (configmap.Simple, func()) {
opt := DefaultOpt opt := Opt
opt.ListenAddr = testBindAddress opt.ListenAddr = testBindAddress
opt.User = testUser opt.User = testUser
opt.Pass = testPass opt.Pass = testPass