From 954c36c0a23b97576bc9e11acf3fef078359b37f Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Sat, 10 Aug 2024 15:57:05 +0200 Subject: [PATCH] add fs providers hook Signed-off-by: Nicola Murino --- internal/dataprovider/user.go | 3 +++ internal/httpd/webadmin.go | 1 + internal/vfs/filesystem.go | 23 +++++++++++++++++++++-- internal/vfs/folder.go | 3 +++ templates/webadmin/fsconfig.html | 12 ++++++------ 5 files changed, 34 insertions(+), 8 deletions(-) diff --git a/internal/dataprovider/user.go b/internal/dataprovider/user.go index cf3d5b87..707ecd83 100644 --- a/internal/dataprovider/user.go +++ b/internal/dataprovider/user.go @@ -157,6 +157,9 @@ func (u *User) GetFilesystem(connectionID string) (fs vfs.Fs, err error) { } func (u *User) getRootFs(connectionID string) (fs vfs.Fs, err error) { + if vfs.IsFsDisabled(u.FsConfig.Provider) { + return nil, fmt.Errorf("filesystem provider %d is disabled", u.FsConfig.Provider) + } switch u.FsConfig.Provider { case sdk.S3FilesystemProvider: return vfs.NewS3Fs(connectionID, u.GetHomeDir(), "", u.FsConfig.S3Config) diff --git a/internal/httpd/webadmin.go b/internal/httpd/webadmin.go index 878dafe1..46fd833a 100644 --- a/internal/httpd/webadmin.go +++ b/internal/httpd/webadmin.go @@ -514,6 +514,7 @@ func loadAdminTemplates(templatesPath string) { fsBaseTpl := template.New("fsBaseTemplate").Funcs(template.FuncMap{ "HumanizeBytes": util.ByteCountSI, + "IsFsDisabled": vfs.IsFsDisabled, }) usersTmpl := util.LoadTemplate(nil, usersPaths...) userTmpl := util.LoadTemplate(fsBaseTpl, userPaths...) diff --git a/internal/vfs/filesystem.go b/internal/vfs/filesystem.go index 95b3df40..cf013097 100644 --- a/internal/vfs/filesystem.go +++ b/internal/vfs/filesystem.go @@ -16,10 +16,17 @@ package vfs import ( "os" + "slices" "github.com/sftpgo/sdk" "github.com/drakkan/sftpgo/v2/internal/kms" + "github.com/drakkan/sftpgo/v2/internal/util" +) + +var ( + disabledFilesystemProviders []sdk.FilesystemProvider + loadDisabledProvidersFn func() ) // Filesystem defines filesystem details @@ -232,8 +239,7 @@ func (f *Filesystem) Validate(additionalData string) error { f.CryptConfig = CryptFsConfig{} f.SFTPConfig = SFTPFsConfig{} return nil - default: - f.Provider = sdk.LocalFilesystemProvider + case sdk.LocalFilesystemProvider: f.S3Config = S3FsConfig{} f.GCSConfig = GCSFsConfig{} f.AzBlobConfig = AzBlobFsConfig{} @@ -241,6 +247,11 @@ func (f *Filesystem) Validate(additionalData string) error { f.SFTPConfig = SFTPFsConfig{} f.HTTPConfig = HTTPFsConfig{} return validateOSFsConfig(&f.OSConfig) + default: + return util.NewI18nError( + util.NewValidationError("invalid filesystem provider"), + util.I18nErrorFsValidation, + ) } } @@ -390,3 +401,11 @@ func (f *Filesystem) GetACopy() Filesystem { } return fs } + +// IsFsDisabled returns if a filesystem provider is disabled +func IsFsDisabled(provider sdk.FilesystemProvider) bool { + if loadDisabledProvidersFn != nil { + loadDisabledProvidersFn() + } + return slices.Contains(disabledFilesystemProviders, provider) +} diff --git a/internal/vfs/folder.go b/internal/vfs/folder.go index b85d82ff..4b6169ee 100644 --- a/internal/vfs/folder.go +++ b/internal/vfs/folder.go @@ -140,6 +140,9 @@ type VirtualFolder struct { // GetFilesystem returns the filesystem for this folder func (v *VirtualFolder) GetFilesystem(connectionID string, forbiddenSelfUsers []string) (Fs, error) { + if IsFsDisabled(v.FsConfig.Provider) { + return nil, fmt.Errorf("filesystem provider %d is disabled", v.FsConfig.Provider) + } switch v.FsConfig.Provider { case sdk.S3FilesystemProvider: return NewS3Fs(connectionID, v.MappedPath, v.VirtualPath, v.FsConfig.S3Config) diff --git a/templates/webadmin/fsconfig.html b/templates/webadmin/fsconfig.html index 271eac3e..50bb5060 100644 --- a/templates/webadmin/fsconfig.html +++ b/templates/webadmin/fsconfig.html @@ -24,12 +24,12 @@ explicit grant from the SFTPGo Team (support@sftpgo.com).