2019-07-26 09:34:44 +00:00
|
|
|
package config_test
|
|
|
|
|
|
|
|
import (
|
2019-07-29 06:53:22 +00:00
|
|
|
"encoding/json"
|
2019-07-26 09:34:44 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-07-29 06:53:22 +00:00
|
|
|
"strings"
|
2019-07-26 09:34:44 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-05-06 17:36:34 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
"github.com/drakkan/sftpgo/common"
|
2019-07-26 09:34:44 +00:00
|
|
|
"github.com/drakkan/sftpgo/config"
|
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
2020-07-29 19:56:56 +00:00
|
|
|
"github.com/drakkan/sftpgo/ftpd"
|
2020-04-26 21:29:09 +00:00
|
|
|
"github.com/drakkan/sftpgo/httpclient"
|
2019-10-07 16:19:01 +00:00
|
|
|
"github.com/drakkan/sftpgo/httpd"
|
2019-07-26 09:34:44 +00:00
|
|
|
"github.com/drakkan/sftpgo/sftpd"
|
2020-05-16 21:26:44 +00:00
|
|
|
"github.com/drakkan/sftpgo/utils"
|
2019-07-26 09:34:44 +00:00
|
|
|
)
|
|
|
|
|
2019-08-07 20:46:13 +00:00
|
|
|
const (
|
|
|
|
tempConfigName = "temp"
|
2020-05-02 13:47:23 +00:00
|
|
|
configName = "sftpgo"
|
2019-08-07 20:46:13 +00:00
|
|
|
)
|
|
|
|
|
2019-07-26 09:34:44 +00:00
|
|
|
func TestLoadConfigTest(t *testing.T) {
|
|
|
|
configDir := ".."
|
2020-05-02 13:47:23 +00:00
|
|
|
err := config.LoadConfig(configDir, configName)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-05-02 13:47:23 +00:00
|
|
|
assert.NotEqual(t, httpd.Conf{}, config.GetHTTPConfig())
|
|
|
|
assert.NotEqual(t, dataprovider.Config{}, config.GetProviderConf())
|
|
|
|
assert.NotEqual(t, sftpd.Configuration{}, config.GetSFTPDConfig())
|
|
|
|
assert.NotEqual(t, httpclient.Config{}, config.GetHTTPConfig())
|
2019-08-07 20:46:13 +00:00
|
|
|
confName := tempConfigName + ".json"
|
|
|
|
configFilePath := filepath.Join(configDir, confName)
|
|
|
|
err = config.LoadConfig(configDir, tempConfigName)
|
2020-05-02 13:47:23 +00:00
|
|
|
assert.NotNil(t, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
err = ioutil.WriteFile(configFilePath, []byte("{invalid json}"), os.ModePerm)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-08-07 20:46:13 +00:00
|
|
|
err = config.LoadConfig(configDir, tempConfigName)
|
2020-05-02 13:47:23 +00:00
|
|
|
assert.NotNil(t, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
err = ioutil.WriteFile(configFilePath, []byte("{\"sftpd\": {\"bind_port\": \"a\"}}"), os.ModePerm)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-08-07 20:46:13 +00:00
|
|
|
err = config.LoadConfig(configDir, tempConfigName)
|
2020-05-02 13:47:23 +00:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
err = os.Remove(configFilePath)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
2019-07-29 06:53:22 +00:00
|
|
|
|
|
|
|
func TestEmptyBanner(t *testing.T) {
|
|
|
|
configDir := ".."
|
2019-08-07 20:46:13 +00:00
|
|
|
confName := tempConfigName + ".json"
|
2019-07-29 06:53:22 +00:00
|
|
|
configFilePath := filepath.Join(configDir, confName)
|
2020-05-02 13:47:23 +00:00
|
|
|
err := config.LoadConfig(configDir, configName)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-29 06:53:22 +00:00
|
|
|
sftpdConf := config.GetSFTPDConfig()
|
|
|
|
sftpdConf.Banner = " "
|
|
|
|
c := make(map[string]sftpd.Configuration)
|
|
|
|
c["sftpd"] = sftpdConf
|
|
|
|
jsonConf, _ := json.Marshal(c)
|
2020-07-29 19:56:56 +00:00
|
|
|
err = ioutil.WriteFile(configFilePath, jsonConf, os.ModePerm)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-05-02 13:47:23 +00:00
|
|
|
err = config.LoadConfig(configDir, tempConfigName)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-29 06:53:22 +00:00
|
|
|
sftpdConf = config.GetSFTPDConfig()
|
2020-05-02 13:47:23 +00:00
|
|
|
assert.NotEmpty(t, strings.TrimSpace(sftpdConf.Banner))
|
|
|
|
err = os.Remove(configFilePath)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
|
|
|
|
ftpdConf := config.GetFTPDConfig()
|
|
|
|
ftpdConf.Banner = " "
|
|
|
|
c1 := make(map[string]ftpd.Configuration)
|
|
|
|
c1["ftpd"] = ftpdConf
|
|
|
|
jsonConf, _ = json.Marshal(c1)
|
|
|
|
err = ioutil.WriteFile(configFilePath, jsonConf, os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = config.LoadConfig(configDir, tempConfigName)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
ftpdConf = config.GetFTPDConfig()
|
|
|
|
assert.NotEmpty(t, strings.TrimSpace(ftpdConf.Banner))
|
|
|
|
err = os.Remove(configFilePath)
|
|
|
|
assert.NoError(t, err)
|
2019-07-29 06:53:22 +00:00
|
|
|
}
|
2019-08-04 07:37:58 +00:00
|
|
|
|
|
|
|
func TestInvalidUploadMode(t *testing.T) {
|
|
|
|
configDir := ".."
|
2019-08-07 20:46:13 +00:00
|
|
|
confName := tempConfigName + ".json"
|
2019-08-04 07:37:58 +00:00
|
|
|
configFilePath := filepath.Join(configDir, confName)
|
2020-05-02 13:47:23 +00:00
|
|
|
err := config.LoadConfig(configDir, configName)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-07-24 21:39:38 +00:00
|
|
|
commonConf := config.GetCommonConfig()
|
|
|
|
commonConf.UploadMode = 10
|
|
|
|
c := make(map[string]common.Configuration)
|
|
|
|
c["common"] = commonConf
|
2020-05-02 13:47:23 +00:00
|
|
|
jsonConf, err := json.Marshal(c)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
err = ioutil.WriteFile(configFilePath, jsonConf, os.ModePerm)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-08-07 20:46:13 +00:00
|
|
|
err = config.LoadConfig(configDir, tempConfigName)
|
2020-05-02 13:47:23 +00:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
err = os.Remove(configFilePath)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-08-04 07:37:58 +00:00
|
|
|
}
|
2019-10-24 16:50:35 +00:00
|
|
|
|
2020-01-06 20:42:41 +00:00
|
|
|
func TestInvalidExternalAuthScope(t *testing.T) {
|
|
|
|
configDir := ".."
|
|
|
|
confName := tempConfigName + ".json"
|
|
|
|
configFilePath := filepath.Join(configDir, confName)
|
2020-05-02 13:47:23 +00:00
|
|
|
err := config.LoadConfig(configDir, configName)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-01-06 20:42:41 +00:00
|
|
|
providerConf := config.GetProviderConf()
|
|
|
|
providerConf.ExternalAuthScope = 10
|
|
|
|
c := make(map[string]dataprovider.Config)
|
|
|
|
c["data_provider"] = providerConf
|
2020-05-02 13:47:23 +00:00
|
|
|
jsonConf, err := json.Marshal(c)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
err = ioutil.WriteFile(configFilePath, jsonConf, os.ModePerm)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-01-06 20:42:41 +00:00
|
|
|
err = config.LoadConfig(configDir, tempConfigName)
|
2020-05-02 13:47:23 +00:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
err = os.Remove(configFilePath)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-01-06 20:42:41 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 18:04:00 +00:00
|
|
|
func TestInvalidCredentialsPath(t *testing.T) {
|
|
|
|
configDir := ".."
|
|
|
|
confName := tempConfigName + ".json"
|
|
|
|
configFilePath := filepath.Join(configDir, confName)
|
2020-05-02 13:47:23 +00:00
|
|
|
err := config.LoadConfig(configDir, configName)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-01-31 18:04:00 +00:00
|
|
|
providerConf := config.GetProviderConf()
|
|
|
|
providerConf.CredentialsPath = ""
|
|
|
|
c := make(map[string]dataprovider.Config)
|
|
|
|
c["data_provider"] = providerConf
|
2020-05-02 13:47:23 +00:00
|
|
|
jsonConf, err := json.Marshal(c)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
err = ioutil.WriteFile(configFilePath, jsonConf, os.ModePerm)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-01-31 18:04:00 +00:00
|
|
|
err = config.LoadConfig(configDir, tempConfigName)
|
2020-05-02 13:47:23 +00:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
err = os.Remove(configFilePath)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-01-31 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 23:02:06 +00:00
|
|
|
func TestInvalidProxyProtocol(t *testing.T) {
|
|
|
|
configDir := ".."
|
|
|
|
confName := tempConfigName + ".json"
|
|
|
|
configFilePath := filepath.Join(configDir, confName)
|
2020-05-02 13:47:23 +00:00
|
|
|
err := config.LoadConfig(configDir, configName)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-07-24 21:39:38 +00:00
|
|
|
commonConf := config.GetCommonConfig()
|
|
|
|
commonConf.ProxyProtocol = 10
|
|
|
|
c := make(map[string]common.Configuration)
|
|
|
|
c["common"] = commonConf
|
2020-05-02 13:47:23 +00:00
|
|
|
jsonConf, err := json.Marshal(c)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
err = ioutil.WriteFile(configFilePath, jsonConf, os.ModePerm)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-02-28 23:02:06 +00:00
|
|
|
err = config.LoadConfig(configDir, tempConfigName)
|
2020-05-02 13:47:23 +00:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
err = os.Remove(configFilePath)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-02-28 23:02:06 +00:00
|
|
|
}
|
|
|
|
|
2020-03-03 08:09:58 +00:00
|
|
|
func TestInvalidUsersBaseDir(t *testing.T) {
|
|
|
|
configDir := ".."
|
|
|
|
confName := tempConfigName + ".json"
|
|
|
|
configFilePath := filepath.Join(configDir, confName)
|
2020-05-02 13:47:23 +00:00
|
|
|
err := config.LoadConfig(configDir, configName)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-03-03 08:09:58 +00:00
|
|
|
providerConf := config.GetProviderConf()
|
|
|
|
providerConf.UsersBaseDir = "."
|
|
|
|
c := make(map[string]dataprovider.Config)
|
|
|
|
c["data_provider"] = providerConf
|
2020-05-02 13:47:23 +00:00
|
|
|
jsonConf, err := json.Marshal(c)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
err = ioutil.WriteFile(configFilePath, jsonConf, os.ModePerm)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-03-03 08:09:58 +00:00
|
|
|
err = config.LoadConfig(configDir, tempConfigName)
|
2020-05-02 13:47:23 +00:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
err = os.Remove(configFilePath)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-03-03 08:09:58 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
func TestCommonParamsCompatibility(t *testing.T) {
|
2020-04-01 21:25:23 +00:00
|
|
|
configDir := ".."
|
|
|
|
confName := tempConfigName + ".json"
|
|
|
|
configFilePath := filepath.Join(configDir, confName)
|
2020-05-02 13:47:23 +00:00
|
|
|
err := config.LoadConfig(configDir, configName)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-04-01 21:25:23 +00:00
|
|
|
sftpdConf := config.GetSFTPDConfig()
|
2020-07-24 21:39:38 +00:00
|
|
|
sftpdConf.IdleTimeout = 21 //nolint:staticcheck
|
|
|
|
sftpdConf.Actions.Hook = "http://hook"
|
|
|
|
sftpdConf.Actions.ExecuteOn = []string{"upload"}
|
|
|
|
sftpdConf.SetstatMode = 1 //nolint:staticcheck
|
|
|
|
sftpdConf.UploadMode = common.UploadModeAtomicWithResume //nolint:staticcheck
|
|
|
|
sftpdConf.ProxyProtocol = 1 //nolint:staticcheck
|
|
|
|
sftpdConf.ProxyAllowed = []string{"192.168.1.1"} //nolint:staticcheck
|
|
|
|
c := make(map[string]sftpd.Configuration)
|
|
|
|
c["sftpd"] = sftpdConf
|
|
|
|
jsonConf, err := json.Marshal(c)
|
2020-05-24 13:29:39 +00:00
|
|
|
assert.NoError(t, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
err = ioutil.WriteFile(configFilePath, jsonConf, os.ModePerm)
|
2020-05-24 13:29:39 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = config.LoadConfig(configDir, tempConfigName)
|
|
|
|
assert.NoError(t, err)
|
2020-07-24 21:39:38 +00:00
|
|
|
commonConf := config.GetCommonConfig()
|
|
|
|
assert.Equal(t, 21, commonConf.IdleTimeout)
|
|
|
|
assert.Equal(t, "http://hook", commonConf.Actions.Hook)
|
|
|
|
assert.Len(t, commonConf.Actions.ExecuteOn, 1)
|
|
|
|
assert.True(t, utils.IsStringInSlice("upload", commonConf.Actions.ExecuteOn))
|
|
|
|
assert.Equal(t, 1, commonConf.SetstatMode)
|
|
|
|
assert.Equal(t, 1, commonConf.ProxyProtocol)
|
|
|
|
assert.Len(t, commonConf.ProxyAllowed, 1)
|
|
|
|
assert.True(t, utils.IsStringInSlice("192.168.1.1", commonConf.ProxyAllowed))
|
2020-05-02 13:47:23 +00:00
|
|
|
err = os.Remove(configFilePath)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-04-01 21:25:23 +00:00
|
|
|
}
|
|
|
|
|
2020-05-16 21:26:44 +00:00
|
|
|
func TestHostKeyCompatibility(t *testing.T) {
|
|
|
|
configDir := ".."
|
|
|
|
confName := tempConfigName + ".json"
|
|
|
|
configFilePath := filepath.Join(configDir, confName)
|
|
|
|
err := config.LoadConfig(configDir, configName)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
sftpdConf := config.GetSFTPDConfig()
|
|
|
|
sftpdConf.Keys = []sftpd.Key{ //nolint:staticcheck
|
|
|
|
{
|
|
|
|
PrivateKey: "rsa",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PrivateKey: "ecdsa",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
c := make(map[string]sftpd.Configuration)
|
|
|
|
c["sftpd"] = sftpdConf
|
|
|
|
jsonConf, err := json.Marshal(c)
|
|
|
|
assert.NoError(t, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
err = ioutil.WriteFile(configFilePath, jsonConf, os.ModePerm)
|
2020-05-16 21:26:44 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = config.LoadConfig(configDir, tempConfigName)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
sftpdConf = config.GetSFTPDConfig()
|
|
|
|
assert.Equal(t, 2, len(sftpdConf.HostKeys))
|
|
|
|
assert.True(t, utils.IsStringInSlice("rsa", sftpdConf.HostKeys))
|
|
|
|
assert.True(t, utils.IsStringInSlice("ecdsa", sftpdConf.HostKeys))
|
|
|
|
err = os.Remove(configFilePath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2019-10-24 16:50:35 +00:00
|
|
|
func TestSetGetConfig(t *testing.T) {
|
|
|
|
sftpdConf := config.GetSFTPDConfig()
|
2020-07-24 21:39:38 +00:00
|
|
|
sftpdConf.MaxAuthTries = 10
|
2019-10-24 16:50:35 +00:00
|
|
|
config.SetSFTPDConfig(sftpdConf)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.Equal(t, sftpdConf.MaxAuthTries, config.GetSFTPDConfig().MaxAuthTries)
|
2019-10-24 16:50:35 +00:00
|
|
|
dataProviderConf := config.GetProviderConf()
|
|
|
|
dataProviderConf.Host = "test host"
|
|
|
|
config.SetProviderConf(dataProviderConf)
|
2020-05-02 13:47:23 +00:00
|
|
|
assert.Equal(t, dataProviderConf.Host, config.GetProviderConf().Host)
|
2019-10-24 16:50:35 +00:00
|
|
|
httpdConf := config.GetHTTPDConfig()
|
|
|
|
httpdConf.BindAddress = "0.0.0.0"
|
|
|
|
config.SetHTTPDConfig(httpdConf)
|
2020-05-02 13:47:23 +00:00
|
|
|
assert.Equal(t, httpdConf.BindAddress, config.GetHTTPDConfig().BindAddress)
|
2020-07-24 21:39:38 +00:00
|
|
|
commonConf := config.GetCommonConfig()
|
|
|
|
commonConf.IdleTimeout = 10
|
|
|
|
config.SetCommonConfig(commonConf)
|
|
|
|
assert.Equal(t, commonConf.IdleTimeout, config.GetCommonConfig().IdleTimeout)
|
2020-07-29 19:56:56 +00:00
|
|
|
ftpdConf := config.GetFTPDConfig()
|
|
|
|
ftpdConf.CertificateFile = "cert"
|
|
|
|
ftpdConf.CertificateKeyFile = "key"
|
|
|
|
config.SetFTPDConfig(ftpdConf)
|
|
|
|
assert.Equal(t, ftpdConf.CertificateFile, config.GetFTPDConfig().CertificateFile)
|
|
|
|
assert.Equal(t, ftpdConf.CertificateKeyFile, config.GetFTPDConfig().CertificateKeyFile)
|
2020-08-11 21:56:10 +00:00
|
|
|
webDavConf := config.GetWebDAVDConfig()
|
|
|
|
webDavConf.CertificateFile = "dav_cert"
|
|
|
|
webDavConf.CertificateKeyFile = "dav_key"
|
|
|
|
config.SetWebDAVDConfig(webDavConf)
|
|
|
|
assert.Equal(t, webDavConf.CertificateFile, config.GetWebDAVDConfig().CertificateFile)
|
|
|
|
assert.Equal(t, webDavConf.CertificateKeyFile, config.GetWebDAVDConfig().CertificateKeyFile)
|
2019-10-24 16:50:35 +00:00
|
|
|
}
|