auth proxy: convert options to new style

This commit is contained in:
Nick Craig-Wood 2025-03-28 13:58:36 +00:00
parent 028316ba5d
commit 6054c4e49d
13 changed files with 43 additions and 40 deletions

View File

@ -123,7 +123,7 @@ You can set a single username and password with the --user and --pass flags.
}, },
Run: func(command *cobra.Command, args []string) { Run: func(command *cobra.Command, args []string) {
var f fs.Fs var f fs.Fs
if proxyflags.Opt.AuthProxy == "" { if proxy.Opt.AuthProxy == "" {
cmd.CheckArgs(1, 1, command, args) cmd.CheckArgs(1, 1, command, args)
f = cmd.NewFsSrc(args) f = cmd.NewFsSrc(args)
} else { } else {
@ -174,8 +174,8 @@ func newServer(ctx context.Context, f fs.Fs, opt *Options) (*driver, error) {
ctx: ctx, ctx: ctx,
opt: *opt, opt: *opt,
} }
if proxyflags.Opt.AuthProxy != "" { if proxy.Opt.AuthProxy != "" {
d.proxy = proxy.New(ctx, &proxyflags.Opt, &vfscommon.Opt) d.proxy = proxy.New(ctx, &proxy.Opt, &vfscommon.Opt)
d.userPass = make(map[string]string, 16) d.userPass = make(map[string]string, 16)
} else { } else {
d.globalVFS = vfs.New(f, &vfscommon.Opt) d.globalVFS = vfs.New(f, &vfscommon.Opt)

View File

@ -83,7 +83,7 @@ control the stats printing.
}, },
Run: func(command *cobra.Command, args []string) { Run: func(command *cobra.Command, args []string) {
var f fs.Fs var f fs.Fs
if proxyflags.Opt.AuthProxy == "" { if proxy.Opt.AuthProxy == "" {
cmd.CheckArgs(1, 1, command, args) cmd.CheckArgs(1, 1, command, args)
f = cmd.NewFsSrc(args) f = cmd.NewFsSrc(args)
} else { } else {
@ -145,8 +145,8 @@ func run(ctx context.Context, f fs.Fs, opt Options) (s *HTTP, err error) {
opt: opt, opt: opt,
} }
if proxyflags.Opt.AuthProxy != "" { if proxy.Opt.AuthProxy != "" {
s.proxy = proxy.New(ctx, &proxyflags.Opt, &vfscommon.Opt) s.proxy = proxy.New(ctx, &proxy.Opt, &vfscommon.Opt)
// override auth // override auth
s.opt.Auth.CustomAuthFn = s.auth s.opt.Auth.CustomAuthFn = s.auth
} else { } else {

View File

@ -12,7 +12,7 @@ import (
"time" "time"
_ "github.com/rclone/rclone/backend/local" _ "github.com/rclone/rclone/backend/local"
"github.com/rclone/rclone/cmd/serve/proxy/proxyflags" "github.com/rclone/rclone/cmd/serve/proxy"
"github.com/rclone/rclone/fs" "github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/filter" "github.com/rclone/rclone/fs/filter"
libhttp "github.com/rclone/rclone/lib/http" libhttp "github.com/rclone/rclone/lib/http"
@ -39,7 +39,7 @@ func start(ctx context.Context, t *testing.T, f fs.Fs) (s *HTTP, testURL string)
}, },
} }
opts.HTTP.ListenAddr = []string{testBindAddress} opts.HTTP.ListenAddr = []string{testBindAddress}
if proxyflags.Opt.AuthProxy == "" { if proxy.Opt.AuthProxy == "" {
opts.Auth.BasicUser = testUser opts.Auth.BasicUser = testUser
opts.Auth.BasicPass = testPass opts.Auth.BasicPass = testPass
} }
@ -110,9 +110,9 @@ func testGET(t *testing.T, useProxy bool) {
cmd := "go run " + prog + " " + files cmd := "go run " + prog + " " + files
// FIXME this is untidy setting a global variable! // FIXME this is untidy setting a global variable!
proxyflags.Opt.AuthProxy = cmd proxy.Opt.AuthProxy = cmd
defer func() { defer func() {
proxyflags.Opt.AuthProxy = "" proxy.Opt.AuthProxy = ""
}() }()
f = nil f = nil

View File

@ -106,14 +106,23 @@ backend that rclone supports.
`, "|", "`") `, "|", "`")
// OptionsInfo descripts the Options in use
var OptionsInfo = fs.Options{{
Name: "auth_proxy",
Default: "",
Help: "A program to use to create the backend from the auth",
}}
// Options is options for creating the proxy // Options is options for creating the proxy
type Options struct { type Options struct {
AuthProxy string AuthProxy string `config:"auth_proxy"`
} }
// DefaultOpt is the default values uses for Opt // Opt is the default options
var DefaultOpt = Options{ var Opt Options
AuthProxy: "",
func init() {
fs.RegisterGlobalOptions(fs.OptionsInfo{Name: "proxy", Opt: &Opt, Options: OptionsInfo})
} }
// Proxy represents a proxy to turn auth requests into a VFS // Proxy represents a proxy to turn auth requests into a VFS

View File

@ -20,7 +20,7 @@ import (
) )
func TestRun(t *testing.T) { func TestRun(t *testing.T) {
opt := DefaultOpt opt := Opt
cmd := "go run proxy_code.go" cmd := "go run proxy_code.go"
opt.AuthProxy = cmd opt.AuthProxy = cmd
p := New(context.Background(), &opt, &vfscommon.Opt) p := New(context.Background(), &opt, &vfscommon.Opt)

View File

@ -7,12 +7,7 @@ import (
"github.com/spf13/pflag" "github.com/spf13/pflag"
) )
// Options set by command line flags
var (
Opt = proxy.DefaultOpt
)
// AddFlags adds the non filing system specific flags to the command // AddFlags adds the non filing system specific flags to the command
func AddFlags(flagSet *pflag.FlagSet) { func AddFlags(flagSet *pflag.FlagSet) {
flags.StringVarP(flagSet, &Opt.AuthProxy, "auth-proxy", "", Opt.AuthProxy, "A program to use to create the backend from the auth", "") flags.AddFlagsFromOptions(flagSet, "", proxy.OptionsInfo)
} }

View File

@ -7,6 +7,7 @@ import (
"github.com/rclone/rclone/cmd" "github.com/rclone/rclone/cmd"
"github.com/rclone/rclone/cmd/serve" "github.com/rclone/rclone/cmd/serve"
"github.com/rclone/rclone/cmd/serve/proxy"
"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"
@ -65,7 +66,7 @@ var Command = &cobra.Command{
Long: help() + httplib.AuthHelp(flagPrefix) + httplib.Help(flagPrefix) + vfs.Help(), Long: help() + httplib.AuthHelp(flagPrefix) + httplib.Help(flagPrefix) + vfs.Help(),
RunE: func(command *cobra.Command, args []string) error { RunE: func(command *cobra.Command, args []string) error {
var f fs.Fs var f fs.Fs
if proxyflags.Opt.AuthProxy == "" { if proxy.Opt.AuthProxy == "" {
cmd.CheckArgs(1, 1, command, args) cmd.CheckArgs(1, 1, command, args)
f = cmd.NewFsSrc(args) f = cmd.NewFsSrc(args)
} else { } else {

View File

@ -18,7 +18,7 @@ import (
"github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials" "github.com/minio/minio-go/v7/pkg/credentials"
_ "github.com/rclone/rclone/backend/local" _ "github.com/rclone/rclone/backend/local"
"github.com/rclone/rclone/cmd/serve/proxy/proxyflags" "github.com/rclone/rclone/cmd/serve/proxy"
"github.com/rclone/rclone/cmd/serve/servetest" "github.com/rclone/rclone/cmd/serve/servetest"
"github.com/rclone/rclone/fs" "github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config/configmap" "github.com/rclone/rclone/fs/config/configmap"
@ -173,9 +173,9 @@ func testListBuckets(t *testing.T, cases []TestCase, useProxy bool) {
cmd := "go run " + prog + " " + files cmd := "go run " + prog + " " + files
// FIXME: this is untidy setting a global variable! // FIXME: this is untidy setting a global variable!
proxyflags.Opt.AuthProxy = cmd proxy.Opt.AuthProxy = cmd
defer func() { defer func() {
proxyflags.Opt.AuthProxy = "" proxy.Opt.AuthProxy = ""
}() }()
f = nil f = nil

View File

@ -15,7 +15,6 @@ import (
"github.com/rclone/gofakes3" "github.com/rclone/gofakes3"
"github.com/rclone/gofakes3/signature" "github.com/rclone/gofakes3/signature"
"github.com/rclone/rclone/cmd/serve/proxy" "github.com/rclone/rclone/cmd/serve/proxy"
"github.com/rclone/rclone/cmd/serve/proxy/proxyflags"
"github.com/rclone/rclone/fs" "github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/hash" "github.com/rclone/rclone/fs/hash"
httplib "github.com/rclone/rclone/lib/http" httplib "github.com/rclone/rclone/lib/http"
@ -80,8 +79,8 @@ func newServer(ctx context.Context, f fs.Fs, opt *Options) (s *Server, err error
w.handler = http.NewServeMux() w.handler = http.NewServeMux()
w.handler = w.faker.Server() w.handler = w.faker.Server()
if proxyflags.Opt.AuthProxy != "" { if proxy.Opt.AuthProxy != "" {
w.proxy = proxy.New(ctx, &proxyflags.Opt, &vfscommon.Opt) w.proxy = proxy.New(ctx, &proxy.Opt, &vfscommon.Opt)
// proxy auth middleware // proxy auth middleware
w.handler = proxyAuthMiddleware(w.handler, w) w.handler = proxyAuthMiddleware(w.handler, w)
w.handler = authPairMiddleware(w.handler, w) w.handler = authPairMiddleware(w.handler, w)

View File

@ -13,7 +13,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/rclone/rclone/cmd/serve/proxy/proxyflags" "github.com/rclone/rclone/cmd/serve/proxy"
"github.com/rclone/rclone/fs" "github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config/configmap" "github.com/rclone/rclone/fs/config/configmap"
"github.com/rclone/rclone/fstest" "github.com/rclone/rclone/fstest"
@ -50,9 +50,9 @@ func run(t *testing.T, name string, start StartFn, useProxy bool) {
cmd := "go run " + prog + " " + fremote.Root() cmd := "go run " + prog + " " + fremote.Root()
// FIXME this is untidy setting a global variable! // FIXME this is untidy setting a global variable!
proxyflags.Opt.AuthProxy = cmd proxy.Opt.AuthProxy = cmd
defer func() { defer func() {
proxyflags.Opt.AuthProxy = "" proxy.Opt.AuthProxy = ""
}() }()
} }
config, cleanup := start(f) config, cleanup := start(f)

View File

@ -22,7 +22,6 @@ import (
"strings" "strings"
"github.com/rclone/rclone/cmd/serve/proxy" "github.com/rclone/rclone/cmd/serve/proxy"
"github.com/rclone/rclone/cmd/serve/proxy/proxyflags"
"github.com/rclone/rclone/fs" "github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config" "github.com/rclone/rclone/fs/config"
"github.com/rclone/rclone/lib/env" "github.com/rclone/rclone/lib/env"
@ -52,8 +51,8 @@ func newServer(ctx context.Context, f fs.Fs, opt *Options) *server {
opt: *opt, opt: *opt,
waitChan: make(chan struct{}), waitChan: make(chan struct{}),
} }
if proxyflags.Opt.AuthProxy != "" { if proxy.Opt.AuthProxy != "" {
s.proxy = proxy.New(ctx, &proxyflags.Opt, &vfscommon.Opt) s.proxy = proxy.New(ctx, &proxy.Opt, &vfscommon.Opt)
} else { } else {
s.vfs = vfs.New(f, &vfscommon.Opt) s.vfs = vfs.New(f, &vfscommon.Opt)
} }
@ -134,12 +133,12 @@ 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 != Opt.AuthorizedKeys { if proxy.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")
} }
// Load the authorized keys // Load the authorized keys
if s.opt.AuthorizedKeys != "" && proxyflags.Opt.AuthProxy == "" { if s.opt.AuthorizedKeys != "" && proxy.Opt.AuthProxy == "" {
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

View File

@ -154,7 +154,7 @@ provided by OpenSSH in this case.
}, },
Run: func(command *cobra.Command, args []string) { Run: func(command *cobra.Command, args []string) {
var f fs.Fs var f fs.Fs
if proxyflags.Opt.AuthProxy == "" { if proxy.Opt.AuthProxy == "" {
cmd.CheckArgs(1, 1, command, args) cmd.CheckArgs(1, 1, command, args)
f = cmd.NewFsSrc(args) f = cmd.NewFsSrc(args)
} else { } else {

View File

@ -137,7 +137,7 @@ done by the permissions on the socket.
}, },
RunE: func(command *cobra.Command, args []string) error { RunE: func(command *cobra.Command, args []string) error {
var f fs.Fs var f fs.Fs
if proxyflags.Opt.AuthProxy == "" { if proxy.Opt.AuthProxy == "" {
cmd.CheckArgs(1, 1, command, args) cmd.CheckArgs(1, 1, command, args)
f = cmd.NewFsSrc(args) f = cmd.NewFsSrc(args)
} else { } else {
@ -204,8 +204,8 @@ func newWebDAV(ctx context.Context, f fs.Fs, opt *Options) (w *WebDAV, err error
ctx: ctx, ctx: ctx,
opt: *opt, opt: *opt,
} }
if proxyflags.Opt.AuthProxy != "" { if proxy.Opt.AuthProxy != "" {
w.proxy = proxy.New(ctx, &proxyflags.Opt, &vfscommon.Opt) w.proxy = proxy.New(ctx, &proxy.Opt, &vfscommon.Opt)
// override auth // override auth
w.opt.Auth.CustomAuthFn = w.auth w.opt.Auth.CustomAuthFn = w.auth
} else { } else {