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
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-07-29 06:53:22 +00:00
|
|
|
"strings"
|
2019-07-26 09:34:44 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-12-03 15:23:33 +00:00
|
|
|
"github.com/spf13/viper"
|
2020-05-06 17:36:34 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-12-23 15:12:30 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-05-06 17:36:34 +00:00
|
|
|
|
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"
|
2021-04-18 10:31:06 +00:00
|
|
|
"github.com/drakkan/sftpgo/utils"
|
2020-12-23 15:12:30 +00:00
|
|
|
"github.com/drakkan/sftpgo/webdavd"
|
2019-07-26 09:34:44 +00:00
|
|
|
)
|
|
|
|
|
2019-08-07 20:46:13 +00:00
|
|
|
const (
|
|
|
|
tempConfigName = "temp"
|
|
|
|
)
|
|
|
|
|
2020-12-03 15:23:33 +00:00
|
|
|
func reset() {
|
|
|
|
viper.Reset()
|
|
|
|
config.Init()
|
|
|
|
}
|
|
|
|
|
2019-07-26 09:34:44 +00:00
|
|
|
func TestLoadConfigTest(t *testing.T) {
|
2020-12-03 15:23:33 +00:00
|
|
|
reset()
|
|
|
|
|
2019-07-26 09:34:44 +00:00
|
|
|
configDir := ".."
|
2020-12-03 15:23:33 +00:00
|
|
|
err := config.LoadConfig(configDir, "")
|
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)
|
2020-12-03 16:16:35 +00:00
|
|
|
err = config.LoadConfig(configDir, confName)
|
2020-10-30 17:58:57 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(configFilePath, []byte("{invalid json}"), os.ModePerm)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-03 16:16:35 +00:00
|
|
|
err = config.LoadConfig(configDir, confName)
|
2020-10-30 17:58:57 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(configFilePath, []byte("{\"sftpd\": {\"bind_port\": \"a\"}}"), os.ModePerm)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-03 16:16:35 +00:00
|
|
|
err = config.LoadConfig(configDir, confName)
|
|
|
|
assert.Error(t, err)
|
2020-05-02 13:47:23 +00:00
|
|
|
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
|
|
|
|
2021-01-03 16:57:07 +00:00
|
|
|
func TestLoadConfigFileNotFound(t *testing.T) {
|
|
|
|
reset()
|
|
|
|
|
|
|
|
viper.SetConfigName("configfile")
|
|
|
|
err := config.LoadConfig(os.TempDir(), "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2019-07-29 06:53:22 +00:00
|
|
|
func TestEmptyBanner(t *testing.T) {
|
2020-12-03 15:23:33 +00:00
|
|
|
reset()
|
|
|
|
|
2019-07-29 06:53:22 +00:00
|
|
|
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-12-03 15:23:33 +00:00
|
|
|
err := config.LoadConfig(configDir, "")
|
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)
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(configFilePath, jsonConf, os.ModePerm)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-03 16:16:35 +00:00
|
|
|
err = config.LoadConfig(configDir, confName)
|
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)
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(configFilePath, jsonConf, os.ModePerm)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-03 16:16:35 +00:00
|
|
|
err = config.LoadConfig(configDir, confName)
|
2020-07-29 19:56:56 +00:00
|
|
|
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) {
|
2020-12-03 15:23:33 +00:00
|
|
|
reset()
|
|
|
|
|
2019-08-04 07:37:58 +00:00
|
|
|
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-12-03 15:23:33 +00:00
|
|
|
err := config.LoadConfig(configDir, "")
|
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)
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(configFilePath, jsonConf, os.ModePerm)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-03 16:16:35 +00:00
|
|
|
err = config.LoadConfig(configDir, confName)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 0, config.GetCommonConfig().UploadMode)
|
2020-05-02 13:47:23 +00:00
|
|
|
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) {
|
2020-12-03 15:23:33 +00:00
|
|
|
reset()
|
|
|
|
|
2020-01-06 20:42:41 +00:00
|
|
|
configDir := ".."
|
|
|
|
confName := tempConfigName + ".json"
|
|
|
|
configFilePath := filepath.Join(configDir, confName)
|
2020-12-03 15:23:33 +00:00
|
|
|
err := config.LoadConfig(configDir, "")
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-01-06 20:42:41 +00:00
|
|
|
providerConf := config.GetProviderConf()
|
2021-02-28 11:10:40 +00:00
|
|
|
providerConf.ExternalAuthScope = 100
|
2020-01-06 20:42:41 +00:00
|
|
|
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)
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(configFilePath, jsonConf, os.ModePerm)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-03 16:16:35 +00:00
|
|
|
err = config.LoadConfig(configDir, confName)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 0, config.GetProviderConf().ExternalAuthScope)
|
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-01-06 20:42:41 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 18:04:00 +00:00
|
|
|
func TestInvalidCredentialsPath(t *testing.T) {
|
2020-12-03 15:23:33 +00:00
|
|
|
reset()
|
|
|
|
|
2020-01-31 18:04:00 +00:00
|
|
|
configDir := ".."
|
|
|
|
confName := tempConfigName + ".json"
|
|
|
|
configFilePath := filepath.Join(configDir, confName)
|
2020-12-03 15:23:33 +00:00
|
|
|
err := config.LoadConfig(configDir, "")
|
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)
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(configFilePath, jsonConf, os.ModePerm)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-03 16:16:35 +00:00
|
|
|
err = config.LoadConfig(configDir, confName)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "credentials", config.GetProviderConf().CredentialsPath)
|
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-01-31 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 23:02:06 +00:00
|
|
|
func TestInvalidProxyProtocol(t *testing.T) {
|
2020-12-03 15:23:33 +00:00
|
|
|
reset()
|
|
|
|
|
2020-02-28 23:02:06 +00:00
|
|
|
configDir := ".."
|
|
|
|
confName := tempConfigName + ".json"
|
|
|
|
configFilePath := filepath.Join(configDir, confName)
|
2020-12-03 15:23:33 +00:00
|
|
|
err := config.LoadConfig(configDir, "")
|
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)
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(configFilePath, jsonConf, os.ModePerm)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-03 16:16:35 +00:00
|
|
|
err = config.LoadConfig(configDir, confName)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 0, config.GetCommonConfig().ProxyProtocol)
|
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-02-28 23:02:06 +00:00
|
|
|
}
|
|
|
|
|
2020-03-03 08:09:58 +00:00
|
|
|
func TestInvalidUsersBaseDir(t *testing.T) {
|
2020-12-03 15:23:33 +00:00
|
|
|
reset()
|
|
|
|
|
2020-03-03 08:09:58 +00:00
|
|
|
configDir := ".."
|
|
|
|
confName := tempConfigName + ".json"
|
|
|
|
configFilePath := filepath.Join(configDir, confName)
|
2020-12-03 15:23:33 +00:00
|
|
|
err := config.LoadConfig(configDir, "")
|
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)
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(configFilePath, jsonConf, os.ModePerm)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-03 16:16:35 +00:00
|
|
|
err = config.LoadConfig(configDir, confName)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Empty(t, config.GetProviderConf().UsersBaseDir)
|
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-03-03 08:09:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-24 16:50:35 +00:00
|
|
|
func TestSetGetConfig(t *testing.T) {
|
2020-12-03 15:23:33 +00:00
|
|
|
reset()
|
|
|
|
|
2019-10-24 16:50:35 +00:00
|
|
|
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()
|
2021-01-19 17:59:41 +00:00
|
|
|
httpdConf.Bindings = append(httpdConf.Bindings, httpd.Binding{Address: "0.0.0.0"})
|
2019-10-24 16:50:35 +00:00
|
|
|
config.SetHTTPDConfig(httpdConf)
|
2021-01-19 17:59:41 +00:00
|
|
|
assert.Equal(t, httpdConf.Bindings[0].Address, config.GetHTTPDConfig().Bindings[0].Address)
|
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)
|
2020-11-30 20:46:34 +00:00
|
|
|
kmsConf := config.GetKMSConfig()
|
|
|
|
kmsConf.Secrets.MasterKeyPath = "apath"
|
|
|
|
kmsConf.Secrets.URL = "aurl"
|
|
|
|
config.SetKMSConfig(kmsConf)
|
|
|
|
assert.Equal(t, kmsConf.Secrets.MasterKeyPath, config.GetKMSConfig().Secrets.MasterKeyPath)
|
|
|
|
assert.Equal(t, kmsConf.Secrets.URL, config.GetKMSConfig().Secrets.URL)
|
2020-12-18 08:01:19 +00:00
|
|
|
telemetryConf := config.GetTelemetryConfig()
|
|
|
|
telemetryConf.BindPort = 10001
|
|
|
|
telemetryConf.BindAddress = "0.0.0.0"
|
|
|
|
config.SetTelemetryConfig(telemetryConf)
|
|
|
|
assert.Equal(t, telemetryConf.BindPort, config.GetTelemetryConfig().BindPort)
|
|
|
|
assert.Equal(t, telemetryConf.BindAddress, config.GetTelemetryConfig().BindAddress)
|
2019-10-24 16:50:35 +00:00
|
|
|
}
|
2020-10-30 17:58:57 +00:00
|
|
|
|
2020-11-24 12:44:57 +00:00
|
|
|
func TestServiceToStart(t *testing.T) {
|
2020-12-03 15:23:33 +00:00
|
|
|
reset()
|
|
|
|
|
2020-11-24 12:44:57 +00:00
|
|
|
configDir := ".."
|
2020-12-03 15:23:33 +00:00
|
|
|
err := config.LoadConfig(configDir, "")
|
2020-11-24 12:44:57 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, config.HasServicesToStart())
|
|
|
|
sftpdConf := config.GetSFTPDConfig()
|
2020-12-23 15:12:30 +00:00
|
|
|
sftpdConf.Bindings[0].Port = 0
|
2020-11-24 12:44:57 +00:00
|
|
|
config.SetSFTPDConfig(sftpdConf)
|
|
|
|
assert.False(t, config.HasServicesToStart())
|
|
|
|
ftpdConf := config.GetFTPDConfig()
|
2020-12-23 15:12:30 +00:00
|
|
|
ftpdConf.Bindings[0].Port = 2121
|
2020-11-24 12:44:57 +00:00
|
|
|
config.SetFTPDConfig(ftpdConf)
|
|
|
|
assert.True(t, config.HasServicesToStart())
|
2020-12-23 15:12:30 +00:00
|
|
|
ftpdConf.Bindings[0].Port = 0
|
2020-11-24 12:44:57 +00:00
|
|
|
config.SetFTPDConfig(ftpdConf)
|
|
|
|
webdavdConf := config.GetWebDAVDConfig()
|
2020-12-23 15:12:30 +00:00
|
|
|
webdavdConf.Bindings[0].Port = 9000
|
2020-11-24 12:44:57 +00:00
|
|
|
config.SetWebDAVDConfig(webdavdConf)
|
|
|
|
assert.True(t, config.HasServicesToStart())
|
2020-12-23 15:12:30 +00:00
|
|
|
webdavdConf.Bindings[0].Port = 0
|
2020-11-24 12:44:57 +00:00
|
|
|
config.SetWebDAVDConfig(webdavdConf)
|
|
|
|
assert.False(t, config.HasServicesToStart())
|
2020-12-23 15:12:30 +00:00
|
|
|
sftpdConf.Bindings[0].Port = 2022
|
2020-11-24 12:44:57 +00:00
|
|
|
config.SetSFTPDConfig(sftpdConf)
|
|
|
|
assert.True(t, config.HasServicesToStart())
|
|
|
|
}
|
|
|
|
|
2020-12-23 15:12:30 +00:00
|
|
|
func TestSFTPDBindingsCompatibility(t *testing.T) {
|
|
|
|
reset()
|
|
|
|
|
|
|
|
configDir := ".."
|
|
|
|
confName := tempConfigName + ".json"
|
|
|
|
configFilePath := filepath.Join(configDir, confName)
|
|
|
|
err := config.LoadConfig(configDir, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
sftpdConf := config.GetSFTPDConfig()
|
|
|
|
require.Len(t, sftpdConf.Bindings, 1)
|
|
|
|
sftpdConf.Bindings = nil
|
|
|
|
sftpdConf.BindPort = 9022 //nolint:staticcheck
|
|
|
|
sftpdConf.BindAddress = "127.0.0.1" //nolint:staticcheck
|
|
|
|
c := make(map[string]sftpd.Configuration)
|
|
|
|
c["sftpd"] = sftpdConf
|
|
|
|
jsonConf, err := json.Marshal(c)
|
|
|
|
assert.NoError(t, err)
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(configFilePath, jsonConf, os.ModePerm)
|
2020-12-23 15:12:30 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = config.LoadConfig(configDir, confName)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
sftpdConf = config.GetSFTPDConfig()
|
2021-02-06 08:53:31 +00:00
|
|
|
// the default binding should be replaced with the deprecated configuration
|
2020-12-23 15:12:30 +00:00
|
|
|
require.Len(t, sftpdConf.Bindings, 1)
|
2021-02-06 08:53:31 +00:00
|
|
|
require.Equal(t, 9022, sftpdConf.Bindings[0].Port)
|
|
|
|
require.Equal(t, "127.0.0.1", sftpdConf.Bindings[0].Address)
|
2020-12-23 15:12:30 +00:00
|
|
|
require.True(t, sftpdConf.Bindings[0].ApplyProxyConfig)
|
|
|
|
|
|
|
|
err = config.LoadConfig(configDir, confName)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
sftpdConf = config.GetSFTPDConfig()
|
|
|
|
require.Len(t, sftpdConf.Bindings, 1)
|
|
|
|
require.Equal(t, 9022, sftpdConf.Bindings[0].Port)
|
|
|
|
require.Equal(t, "127.0.0.1", sftpdConf.Bindings[0].Address)
|
|
|
|
require.True(t, sftpdConf.Bindings[0].ApplyProxyConfig)
|
|
|
|
err = os.Remove(configFilePath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFTPDBindingsCompatibility(t *testing.T) {
|
|
|
|
reset()
|
|
|
|
|
|
|
|
configDir := ".."
|
|
|
|
confName := tempConfigName + ".json"
|
|
|
|
configFilePath := filepath.Join(configDir, confName)
|
|
|
|
err := config.LoadConfig(configDir, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
ftpdConf := config.GetFTPDConfig()
|
|
|
|
require.Len(t, ftpdConf.Bindings, 1)
|
|
|
|
ftpdConf.Bindings = nil
|
|
|
|
ftpdConf.BindPort = 9022 //nolint:staticcheck
|
|
|
|
ftpdConf.BindAddress = "127.1.0.1" //nolint:staticcheck
|
|
|
|
ftpdConf.ForcePassiveIP = "127.1.1.1" //nolint:staticcheck
|
|
|
|
ftpdConf.TLSMode = 2 //nolint:staticcheck
|
|
|
|
c := make(map[string]ftpd.Configuration)
|
|
|
|
c["ftpd"] = ftpdConf
|
|
|
|
jsonConf, err := json.Marshal(c)
|
|
|
|
assert.NoError(t, err)
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(configFilePath, jsonConf, os.ModePerm)
|
2020-12-23 15:12:30 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = config.LoadConfig(configDir, confName)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
ftpdConf = config.GetFTPDConfig()
|
2021-02-06 08:53:31 +00:00
|
|
|
// the default binding should be replaced with the deprecated configuration
|
2020-12-23 15:12:30 +00:00
|
|
|
require.Len(t, ftpdConf.Bindings, 1)
|
|
|
|
require.Equal(t, 9022, ftpdConf.Bindings[0].Port)
|
|
|
|
require.Equal(t, "127.1.0.1", ftpdConf.Bindings[0].Address)
|
|
|
|
require.True(t, ftpdConf.Bindings[0].ApplyProxyConfig)
|
|
|
|
require.Equal(t, 2, ftpdConf.Bindings[0].TLSMode)
|
|
|
|
require.Equal(t, "127.1.1.1", ftpdConf.Bindings[0].ForcePassiveIP)
|
|
|
|
err = os.Remove(configFilePath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebDAVDBindingsCompatibility(t *testing.T) {
|
|
|
|
reset()
|
|
|
|
|
|
|
|
configDir := ".."
|
|
|
|
confName := tempConfigName + ".json"
|
|
|
|
configFilePath := filepath.Join(configDir, confName)
|
|
|
|
err := config.LoadConfig(configDir, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
webdavConf := config.GetWebDAVDConfig()
|
|
|
|
require.Len(t, webdavConf.Bindings, 1)
|
|
|
|
webdavConf.Bindings = nil
|
|
|
|
webdavConf.BindPort = 9080 //nolint:staticcheck
|
|
|
|
webdavConf.BindAddress = "127.0.0.1" //nolint:staticcheck
|
|
|
|
c := make(map[string]webdavd.Configuration)
|
|
|
|
c["webdavd"] = webdavConf
|
|
|
|
jsonConf, err := json.Marshal(c)
|
|
|
|
assert.NoError(t, err)
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(configFilePath, jsonConf, os.ModePerm)
|
2020-12-23 15:12:30 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = config.LoadConfig(configDir, confName)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
webdavConf = config.GetWebDAVDConfig()
|
2021-02-06 08:53:31 +00:00
|
|
|
// the default binding should be replaced with the deprecated configuration
|
2020-12-23 15:12:30 +00:00
|
|
|
require.Len(t, webdavConf.Bindings, 1)
|
|
|
|
require.Equal(t, 9080, webdavConf.Bindings[0].Port)
|
|
|
|
require.Equal(t, "127.0.0.1", webdavConf.Bindings[0].Address)
|
|
|
|
require.False(t, webdavConf.Bindings[0].EnableHTTPS)
|
|
|
|
err = os.Remove(configFilePath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-01-19 17:59:41 +00:00
|
|
|
func TestHTTPDBindingsCompatibility(t *testing.T) {
|
|
|
|
reset()
|
|
|
|
|
|
|
|
configDir := ".."
|
|
|
|
confName := tempConfigName + ".json"
|
|
|
|
configFilePath := filepath.Join(configDir, confName)
|
|
|
|
err := config.LoadConfig(configDir, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
httpdConf := config.GetHTTPDConfig()
|
|
|
|
require.Len(t, httpdConf.Bindings, 1)
|
|
|
|
httpdConf.Bindings = nil
|
|
|
|
httpdConf.BindPort = 9080 //nolint:staticcheck
|
|
|
|
httpdConf.BindAddress = "127.1.1.1" //nolint:staticcheck
|
|
|
|
c := make(map[string]httpd.Conf)
|
|
|
|
c["httpd"] = httpdConf
|
|
|
|
jsonConf, err := json.Marshal(c)
|
|
|
|
assert.NoError(t, err)
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(configFilePath, jsonConf, os.ModePerm)
|
2021-01-19 17:59:41 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = config.LoadConfig(configDir, confName)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
httpdConf = config.GetHTTPDConfig()
|
2021-02-06 08:53:31 +00:00
|
|
|
// the default binding should be replaced with the deprecated configuration
|
2021-01-19 17:59:41 +00:00
|
|
|
require.Len(t, httpdConf.Bindings, 1)
|
|
|
|
require.Equal(t, 9080, httpdConf.Bindings[0].Port)
|
|
|
|
require.Equal(t, "127.1.1.1", httpdConf.Bindings[0].Address)
|
|
|
|
require.False(t, httpdConf.Bindings[0].EnableHTTPS)
|
|
|
|
require.True(t, httpdConf.Bindings[0].EnableWebAdmin)
|
|
|
|
err = os.Remove(configFilePath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-04-18 10:31:06 +00:00
|
|
|
func TestRateLimitersFromEnv(t *testing.T) {
|
|
|
|
reset()
|
|
|
|
|
|
|
|
os.Setenv("SFTPGO_COMMON__RATE_LIMITERS__0__AVERAGE", "100")
|
|
|
|
os.Setenv("SFTPGO_COMMON__RATE_LIMITERS__0__PERIOD", "2000")
|
|
|
|
os.Setenv("SFTPGO_COMMON__RATE_LIMITERS__0__BURST", "10")
|
|
|
|
os.Setenv("SFTPGO_COMMON__RATE_LIMITERS__0__TYPE", "2")
|
|
|
|
os.Setenv("SFTPGO_COMMON__RATE_LIMITERS__0__PROTOCOLS", "SSH, FTP")
|
|
|
|
os.Setenv("SFTPGO_COMMON__RATE_LIMITERS__0__GENERATE_DEFENDER_EVENTS", "1")
|
|
|
|
os.Setenv("SFTPGO_COMMON__RATE_LIMITERS__0__ENTRIES_SOFT_LIMIT", "50")
|
|
|
|
os.Setenv("SFTPGO_COMMON__RATE_LIMITERS__0__ENTRIES_HARD_LIMIT", "100")
|
|
|
|
os.Setenv("SFTPGO_COMMON__RATE_LIMITERS__8__AVERAGE", "50")
|
|
|
|
t.Cleanup(func() {
|
|
|
|
os.Unsetenv("SFTPGO_COMMON__RATE_LIMITERS__0__AVERAGE")
|
|
|
|
os.Unsetenv("SFTPGO_COMMON__RATE_LIMITERS__0__PERIOD")
|
|
|
|
os.Unsetenv("SFTPGO_COMMON__RATE_LIMITERS__0__BURST")
|
|
|
|
os.Unsetenv("SFTPGO_COMMON__RATE_LIMITERS__0__TYPE")
|
|
|
|
os.Unsetenv("SFTPGO_COMMON__RATE_LIMITERS__0__PROTOCOLS")
|
|
|
|
os.Unsetenv("SFTPGO_COMMON__RATE_LIMITERS__0__GENERATE_DEFENDER_EVENTS")
|
|
|
|
os.Unsetenv("SFTPGO_COMMON__RATE_LIMITERS__0__ENTRIES_SOFT_LIMIT")
|
|
|
|
os.Unsetenv("SFTPGO_COMMON__RATE_LIMITERS__0__ENTRIES_HARD_LIMIT")
|
|
|
|
os.Unsetenv("SFTPGO_COMMON__RATE_LIMITERS__8__AVERAGE")
|
|
|
|
})
|
|
|
|
|
|
|
|
configDir := ".."
|
|
|
|
err := config.LoadConfig(configDir, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
limiters := config.GetCommonConfig().RateLimitersConfig
|
|
|
|
require.Len(t, limiters, 2)
|
|
|
|
require.Equal(t, int64(100), limiters[0].Average)
|
|
|
|
require.Equal(t, int64(2000), limiters[0].Period)
|
|
|
|
require.Equal(t, 10, limiters[0].Burst)
|
|
|
|
require.Equal(t, 2, limiters[0].Type)
|
|
|
|
protocols := limiters[0].Protocols
|
|
|
|
require.Len(t, protocols, 2)
|
|
|
|
require.True(t, utils.IsStringInSlice(common.ProtocolFTP, protocols))
|
|
|
|
require.True(t, utils.IsStringInSlice(common.ProtocolSSH, protocols))
|
|
|
|
require.True(t, limiters[0].GenerateDefenderEvents)
|
|
|
|
require.Equal(t, 50, limiters[0].EntriesSoftLimit)
|
|
|
|
require.Equal(t, 100, limiters[0].EntriesHardLimit)
|
|
|
|
require.Equal(t, int64(50), limiters[1].Average)
|
|
|
|
// we check the default values here
|
|
|
|
require.Equal(t, int64(1000), limiters[1].Period)
|
|
|
|
require.Equal(t, 1, limiters[1].Burst)
|
|
|
|
require.Equal(t, 2, limiters[1].Type)
|
|
|
|
protocols = limiters[1].Protocols
|
2021-04-19 06:14:04 +00:00
|
|
|
require.Len(t, protocols, 4)
|
2021-04-18 10:31:06 +00:00
|
|
|
require.True(t, utils.IsStringInSlice(common.ProtocolFTP, protocols))
|
|
|
|
require.True(t, utils.IsStringInSlice(common.ProtocolSSH, protocols))
|
|
|
|
require.True(t, utils.IsStringInSlice(common.ProtocolWebDAV, protocols))
|
2021-04-19 06:14:04 +00:00
|
|
|
require.True(t, utils.IsStringInSlice(common.ProtocolHTTP, protocols))
|
2021-04-18 10:31:06 +00:00
|
|
|
require.False(t, limiters[1].GenerateDefenderEvents)
|
|
|
|
require.Equal(t, 100, limiters[1].EntriesSoftLimit)
|
|
|
|
require.Equal(t, 150, limiters[1].EntriesHardLimit)
|
|
|
|
}
|
|
|
|
|
2020-12-23 15:12:30 +00:00
|
|
|
func TestSFTPDBindingsFromEnv(t *testing.T) {
|
|
|
|
reset()
|
|
|
|
|
|
|
|
os.Setenv("SFTPGO_SFTPD__BINDINGS__0__ADDRESS", "127.0.0.1")
|
|
|
|
os.Setenv("SFTPGO_SFTPD__BINDINGS__0__PORT", "2200")
|
|
|
|
os.Setenv("SFTPGO_SFTPD__BINDINGS__0__APPLY_PROXY_CONFIG", "false")
|
|
|
|
os.Setenv("SFTPGO_SFTPD__BINDINGS__3__ADDRESS", "127.0.1.1")
|
|
|
|
os.Setenv("SFTPGO_SFTPD__BINDINGS__3__PORT", "2203")
|
|
|
|
t.Cleanup(func() {
|
|
|
|
os.Unsetenv("SFTPGO_SFTPD__BINDINGS__0__ADDRESS")
|
|
|
|
os.Unsetenv("SFTPGO_SFTPD__BINDINGS__0__PORT")
|
|
|
|
os.Unsetenv("SFTPGO_SFTPD__BINDINGS__0__APPLY_PROXY_CONFIG")
|
|
|
|
os.Unsetenv("SFTPGO_SFTPD__BINDINGS__3__ADDRESS")
|
|
|
|
os.Unsetenv("SFTPGO_SFTPD__BINDINGS__3__PORT")
|
|
|
|
})
|
|
|
|
|
|
|
|
configDir := ".."
|
|
|
|
err := config.LoadConfig(configDir, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
bindings := config.GetSFTPDConfig().Bindings
|
|
|
|
require.Len(t, bindings, 2)
|
|
|
|
require.Equal(t, 2200, bindings[0].Port)
|
|
|
|
require.Equal(t, "127.0.0.1", bindings[0].Address)
|
|
|
|
require.False(t, bindings[0].ApplyProxyConfig)
|
|
|
|
require.Equal(t, 2203, bindings[1].Port)
|
|
|
|
require.Equal(t, "127.0.1.1", bindings[1].Address)
|
2021-04-18 10:31:06 +00:00
|
|
|
require.True(t, bindings[1].ApplyProxyConfig) // default value
|
2020-12-23 15:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFTPDBindingsFromEnv(t *testing.T) {
|
|
|
|
reset()
|
|
|
|
|
|
|
|
os.Setenv("SFTPGO_FTPD__BINDINGS__0__ADDRESS", "127.0.0.1")
|
|
|
|
os.Setenv("SFTPGO_FTPD__BINDINGS__0__PORT", "2200")
|
|
|
|
os.Setenv("SFTPGO_FTPD__BINDINGS__0__APPLY_PROXY_CONFIG", "f")
|
|
|
|
os.Setenv("SFTPGO_FTPD__BINDINGS__0__TLS_MODE", "2")
|
|
|
|
os.Setenv("SFTPGO_FTPD__BINDINGS__0__FORCE_PASSIVE_IP", "127.0.1.2")
|
2021-02-18 19:17:16 +00:00
|
|
|
os.Setenv("SFTPGO_FTPD__BINDINGS__0__TLS_CIPHER_SUITES", "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256")
|
2020-12-23 15:12:30 +00:00
|
|
|
os.Setenv("SFTPGO_FTPD__BINDINGS__9__ADDRESS", "127.0.1.1")
|
|
|
|
os.Setenv("SFTPGO_FTPD__BINDINGS__9__PORT", "2203")
|
|
|
|
os.Setenv("SFTPGO_FTPD__BINDINGS__9__TLS_MODE", "1")
|
|
|
|
os.Setenv("SFTPGO_FTPD__BINDINGS__9__FORCE_PASSIVE_IP", "127.0.1.1")
|
2021-02-28 11:10:40 +00:00
|
|
|
os.Setenv("SFTPGO_FTPD__BINDINGS__9__CLIENT_AUTH_TYPE", "2")
|
2020-12-23 15:12:30 +00:00
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
os.Unsetenv("SFTPGO_FTPD__BINDINGS__0__ADDRESS")
|
|
|
|
os.Unsetenv("SFTPGO_FTPD__BINDINGS__0__PORT")
|
|
|
|
os.Unsetenv("SFTPGO_FTPD__BINDINGS__0__APPLY_PROXY_CONFIG")
|
|
|
|
os.Unsetenv("SFTPGO_FTPD__BINDINGS__0__TLS_MODE")
|
|
|
|
os.Unsetenv("SFTPGO_FTPD__BINDINGS__0__FORCE_PASSIVE_IP")
|
2021-02-18 19:17:16 +00:00
|
|
|
os.Unsetenv("SFTPGO_FTPD__BINDINGS__0__TLS_CIPHER_SUITES")
|
2020-12-23 15:12:30 +00:00
|
|
|
os.Unsetenv("SFTPGO_FTPD__BINDINGS__9__ADDRESS")
|
|
|
|
os.Unsetenv("SFTPGO_FTPD__BINDINGS__9__PORT")
|
|
|
|
os.Unsetenv("SFTPGO_FTPD__BINDINGS__9__TLS_MODE")
|
|
|
|
os.Unsetenv("SFTPGO_FTPD__BINDINGS__9__FORCE_PASSIVE_IP")
|
2020-12-29 08:20:09 +00:00
|
|
|
os.Unsetenv("SFTPGO_FTPD__BINDINGS__9__CLIENT_AUTH_TYPE")
|
2020-12-23 15:12:30 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
configDir := ".."
|
|
|
|
err := config.LoadConfig(configDir, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
bindings := config.GetFTPDConfig().Bindings
|
|
|
|
require.Len(t, bindings, 2)
|
|
|
|
require.Equal(t, 2200, bindings[0].Port)
|
|
|
|
require.Equal(t, "127.0.0.1", bindings[0].Address)
|
|
|
|
require.False(t, bindings[0].ApplyProxyConfig)
|
2020-12-29 08:20:09 +00:00
|
|
|
require.Equal(t, 2, bindings[0].TLSMode)
|
|
|
|
require.Equal(t, "127.0.1.2", bindings[0].ForcePassiveIP)
|
|
|
|
require.Equal(t, 0, bindings[0].ClientAuthType)
|
2021-02-18 19:17:16 +00:00
|
|
|
require.Len(t, bindings[0].TLSCipherSuites, 2)
|
|
|
|
require.Equal(t, "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256", bindings[0].TLSCipherSuites[0])
|
|
|
|
require.Equal(t, "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", bindings[0].TLSCipherSuites[1])
|
2020-12-23 15:12:30 +00:00
|
|
|
require.Equal(t, 2203, bindings[1].Port)
|
|
|
|
require.Equal(t, "127.0.1.1", bindings[1].Address)
|
2021-04-18 10:31:06 +00:00
|
|
|
require.True(t, bindings[1].ApplyProxyConfig) // default value
|
2020-12-29 08:20:09 +00:00
|
|
|
require.Equal(t, 1, bindings[1].TLSMode)
|
|
|
|
require.Equal(t, "127.0.1.1", bindings[1].ForcePassiveIP)
|
2021-02-28 11:10:40 +00:00
|
|
|
require.Equal(t, 2, bindings[1].ClientAuthType)
|
2021-02-18 19:17:16 +00:00
|
|
|
require.Nil(t, bindings[1].TLSCipherSuites)
|
2020-12-23 15:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebDAVBindingsFromEnv(t *testing.T) {
|
|
|
|
reset()
|
|
|
|
|
|
|
|
os.Setenv("SFTPGO_WEBDAVD__BINDINGS__1__ADDRESS", "127.0.0.1")
|
|
|
|
os.Setenv("SFTPGO_WEBDAVD__BINDINGS__1__PORT", "8000")
|
|
|
|
os.Setenv("SFTPGO_WEBDAVD__BINDINGS__1__ENABLE_HTTPS", "0")
|
2021-02-18 19:17:16 +00:00
|
|
|
os.Setenv("SFTPGO_WEBDAVD__BINDINGS__1__TLS_CIPHER_SUITES", "TLS_RSA_WITH_AES_128_CBC_SHA ")
|
2021-05-11 04:54:06 +00:00
|
|
|
os.Setenv("SFTPGO_WEBDAVD__BINDINGS__1__PROXY_ALLOWED", "192.168.10.1")
|
2020-12-23 15:12:30 +00:00
|
|
|
os.Setenv("SFTPGO_WEBDAVD__BINDINGS__2__ADDRESS", "127.0.1.1")
|
|
|
|
os.Setenv("SFTPGO_WEBDAVD__BINDINGS__2__PORT", "9000")
|
|
|
|
os.Setenv("SFTPGO_WEBDAVD__BINDINGS__2__ENABLE_HTTPS", "1")
|
2020-12-28 18:48:23 +00:00
|
|
|
os.Setenv("SFTPGO_WEBDAVD__BINDINGS__2__CLIENT_AUTH_TYPE", "1")
|
2021-03-07 16:10:45 +00:00
|
|
|
os.Setenv("SFTPGO_WEBDAVD__BINDINGS__2__PREFIX", "/dav2")
|
2020-12-23 15:12:30 +00:00
|
|
|
t.Cleanup(func() {
|
|
|
|
os.Unsetenv("SFTPGO_WEBDAVD__BINDINGS__1__ADDRESS")
|
|
|
|
os.Unsetenv("SFTPGO_WEBDAVD__BINDINGS__1__PORT")
|
|
|
|
os.Unsetenv("SFTPGO_WEBDAVD__BINDINGS__1__ENABLE_HTTPS")
|
2021-02-18 19:17:16 +00:00
|
|
|
os.Unsetenv("SFTPGO_WEBDAVD__BINDINGS__1__TLS_CIPHER_SUITES")
|
2021-05-11 04:54:06 +00:00
|
|
|
os.Unsetenv("SFTPGO_WEBDAVD__BINDINGS__1__PROXY_ALLOWED")
|
2020-12-23 15:12:30 +00:00
|
|
|
os.Unsetenv("SFTPGO_WEBDAVD__BINDINGS__2__ADDRESS")
|
|
|
|
os.Unsetenv("SFTPGO_WEBDAVD__BINDINGS__2__PORT")
|
|
|
|
os.Unsetenv("SFTPGO_WEBDAVD__BINDINGS__2__ENABLE_HTTPS")
|
2020-12-28 18:48:23 +00:00
|
|
|
os.Unsetenv("SFTPGO_WEBDAVD__BINDINGS__2__CLIENT_AUTH_TYPE")
|
2021-03-07 16:10:45 +00:00
|
|
|
os.Unsetenv("SFTPGO_WEBDAVD__BINDINGS__2__PREFIX")
|
2020-12-23 15:12:30 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
configDir := ".."
|
|
|
|
err := config.LoadConfig(configDir, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
bindings := config.GetWebDAVDConfig().Bindings
|
|
|
|
require.Len(t, bindings, 3)
|
|
|
|
require.Equal(t, 0, bindings[0].Port)
|
|
|
|
require.Empty(t, bindings[0].Address)
|
|
|
|
require.False(t, bindings[0].EnableHTTPS)
|
2021-02-18 19:17:16 +00:00
|
|
|
require.Len(t, bindings[0].TLSCipherSuites, 0)
|
2021-03-07 16:10:45 +00:00
|
|
|
require.Empty(t, bindings[0].Prefix)
|
2020-12-23 15:12:30 +00:00
|
|
|
require.Equal(t, 8000, bindings[1].Port)
|
|
|
|
require.Equal(t, "127.0.0.1", bindings[1].Address)
|
|
|
|
require.False(t, bindings[1].EnableHTTPS)
|
2020-12-29 08:20:09 +00:00
|
|
|
require.Equal(t, 0, bindings[1].ClientAuthType)
|
2021-02-18 19:17:16 +00:00
|
|
|
require.Len(t, bindings[1].TLSCipherSuites, 1)
|
|
|
|
require.Equal(t, "TLS_RSA_WITH_AES_128_CBC_SHA", bindings[1].TLSCipherSuites[0])
|
2021-05-11 04:54:06 +00:00
|
|
|
require.Equal(t, "192.168.10.1", bindings[1].ProxyAllowed[0])
|
2021-03-07 16:10:45 +00:00
|
|
|
require.Empty(t, bindings[1].Prefix)
|
2020-12-23 15:12:30 +00:00
|
|
|
require.Equal(t, 9000, bindings[2].Port)
|
|
|
|
require.Equal(t, "127.0.1.1", bindings[2].Address)
|
|
|
|
require.True(t, bindings[2].EnableHTTPS)
|
2020-12-28 18:48:23 +00:00
|
|
|
require.Equal(t, 1, bindings[2].ClientAuthType)
|
2021-02-18 19:17:16 +00:00
|
|
|
require.Nil(t, bindings[2].TLSCipherSuites)
|
2021-03-07 16:10:45 +00:00
|
|
|
require.Equal(t, "/dav2", bindings[2].Prefix)
|
2020-12-23 15:12:30 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 17:59:41 +00:00
|
|
|
func TestHTTPDBindingsFromEnv(t *testing.T) {
|
|
|
|
reset()
|
|
|
|
|
|
|
|
sockPath := filepath.Clean(os.TempDir())
|
|
|
|
|
|
|
|
os.Setenv("SFTPGO_HTTPD__BINDINGS__0__ADDRESS", sockPath)
|
|
|
|
os.Setenv("SFTPGO_HTTPD__BINDINGS__0__PORT", "0")
|
2021-02-18 19:17:16 +00:00
|
|
|
os.Setenv("SFTPGO_HTTPD__BINDINGS__0__TLS_CIPHER_SUITES", " TLS_AES_128_GCM_SHA256")
|
2021-01-19 17:59:41 +00:00
|
|
|
os.Setenv("SFTPGO_HTTPD__BINDINGS__1__ADDRESS", "127.0.0.1")
|
|
|
|
os.Setenv("SFTPGO_HTTPD__BINDINGS__1__PORT", "8000")
|
|
|
|
os.Setenv("SFTPGO_HTTPD__BINDINGS__1__ENABLE_HTTPS", "0")
|
|
|
|
os.Setenv("SFTPGO_HTTPD__BINDINGS__1__ENABLE_WEB_ADMIN", "1")
|
2021-05-06 19:35:43 +00:00
|
|
|
os.Setenv("SFTPGO_HTTPD__BINDINGS__1__ENABLE_WEB_CLIENT", "1")
|
2021-01-19 17:59:41 +00:00
|
|
|
os.Setenv("SFTPGO_HTTPD__BINDINGS__2__ADDRESS", "127.0.1.1")
|
|
|
|
os.Setenv("SFTPGO_HTTPD__BINDINGS__2__PORT", "9000")
|
|
|
|
os.Setenv("SFTPGO_HTTPD__BINDINGS__2__ENABLE_WEB_ADMIN", "0")
|
2021-05-06 19:35:43 +00:00
|
|
|
os.Setenv("SFTPGO_HTTPD__BINDINGS__2__ENABLE_WEB_CLIENT", "0")
|
2021-01-19 17:59:41 +00:00
|
|
|
os.Setenv("SFTPGO_HTTPD__BINDINGS__2__ENABLE_HTTPS", "1")
|
|
|
|
os.Setenv("SFTPGO_HTTPD__BINDINGS__2__CLIENT_AUTH_TYPE", "1")
|
2021-02-18 19:17:16 +00:00
|
|
|
os.Setenv("SFTPGO_HTTPD__BINDINGS__2__TLS_CIPHER_SUITES", " TLS_AES_256_GCM_SHA384 , TLS_CHACHA20_POLY1305_SHA256")
|
2021-05-11 04:54:06 +00:00
|
|
|
os.Setenv("SFTPGO_HTTPD__BINDINGS__2__PROXY_ALLOWED", " 192.168.9.1 , 172.16.25.0/24")
|
2021-01-19 17:59:41 +00:00
|
|
|
t.Cleanup(func() {
|
|
|
|
os.Unsetenv("SFTPGO_HTTPD__BINDINGS__0__ADDRESS")
|
|
|
|
os.Unsetenv("SFTPGO_HTTPD__BINDINGS__0__PORT")
|
2021-02-18 19:17:16 +00:00
|
|
|
os.Unsetenv("SFTPGO_HTTPD__BINDINGS__0__TLS_CIPHER_SUITES")
|
2021-01-19 17:59:41 +00:00
|
|
|
os.Unsetenv("SFTPGO_HTTPD__BINDINGS__1__ADDRESS")
|
|
|
|
os.Unsetenv("SFTPGO_HTTPD__BINDINGS__1__PORT")
|
|
|
|
os.Unsetenv("SFTPGO_HTTPD__BINDINGS__1__ENABLE_HTTPS")
|
|
|
|
os.Unsetenv("SFTPGO_HTTPD__BINDINGS__1__ENABLE_WEB_ADMIN")
|
2021-05-06 19:35:43 +00:00
|
|
|
os.Unsetenv("SFTPGO_HTTPD__BINDINGS__1__ENABLE_WEB_CLIENT")
|
2021-01-19 17:59:41 +00:00
|
|
|
os.Unsetenv("SFTPGO_HTTPD__BINDINGS__2__ADDRESS")
|
|
|
|
os.Unsetenv("SFTPGO_HTTPD__BINDINGS__2__PORT")
|
|
|
|
os.Unsetenv("SFTPGO_HTTPD__BINDINGS__2__ENABLE_HTTPS")
|
|
|
|
os.Unsetenv("SFTPGO_HTTPD__BINDINGS__2__ENABLE_WEB_ADMIN")
|
2021-05-06 19:35:43 +00:00
|
|
|
os.Unsetenv("SFTPGO_HTTPD__BINDINGS__2__ENABLE_WEB_CLIENT")
|
2021-01-19 17:59:41 +00:00
|
|
|
os.Unsetenv("SFTPGO_HTTPD__BINDINGS__2__CLIENT_AUTH_TYPE")
|
2021-02-18 19:17:16 +00:00
|
|
|
os.Unsetenv("SFTPGO_HTTPD__BINDINGS__2__TLS_CIPHER_SUITES")
|
2021-05-11 04:54:06 +00:00
|
|
|
os.Unsetenv("SFTPGO_HTTPD__BINDINGS__2__PROXY_ALLOWED")
|
2021-01-19 17:59:41 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
configDir := ".."
|
|
|
|
err := config.LoadConfig(configDir, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
bindings := config.GetHTTPDConfig().Bindings
|
|
|
|
require.Len(t, bindings, 3)
|
|
|
|
require.Equal(t, 0, bindings[0].Port)
|
|
|
|
require.Equal(t, sockPath, bindings[0].Address)
|
|
|
|
require.False(t, bindings[0].EnableHTTPS)
|
|
|
|
require.True(t, bindings[0].EnableWebAdmin)
|
2021-05-06 19:35:43 +00:00
|
|
|
require.True(t, bindings[0].EnableWebClient)
|
2021-02-18 19:17:16 +00:00
|
|
|
require.Len(t, bindings[0].TLSCipherSuites, 1)
|
|
|
|
require.Equal(t, "TLS_AES_128_GCM_SHA256", bindings[0].TLSCipherSuites[0])
|
2021-01-19 17:59:41 +00:00
|
|
|
require.Equal(t, 8000, bindings[1].Port)
|
|
|
|
require.Equal(t, "127.0.0.1", bindings[1].Address)
|
|
|
|
require.False(t, bindings[1].EnableHTTPS)
|
|
|
|
require.True(t, bindings[1].EnableWebAdmin)
|
2021-05-06 19:35:43 +00:00
|
|
|
require.True(t, bindings[1].EnableWebClient)
|
2021-02-18 19:17:16 +00:00
|
|
|
require.Nil(t, bindings[1].TLSCipherSuites)
|
2021-01-19 17:59:41 +00:00
|
|
|
|
|
|
|
require.Equal(t, 9000, bindings[2].Port)
|
|
|
|
require.Equal(t, "127.0.1.1", bindings[2].Address)
|
|
|
|
require.True(t, bindings[2].EnableHTTPS)
|
|
|
|
require.False(t, bindings[2].EnableWebAdmin)
|
2021-05-06 19:35:43 +00:00
|
|
|
require.False(t, bindings[2].EnableWebClient)
|
2021-01-19 17:59:41 +00:00
|
|
|
require.Equal(t, 1, bindings[2].ClientAuthType)
|
2021-02-18 19:17:16 +00:00
|
|
|
require.Len(t, bindings[2].TLSCipherSuites, 2)
|
|
|
|
require.Equal(t, "TLS_AES_256_GCM_SHA384", bindings[2].TLSCipherSuites[0])
|
|
|
|
require.Equal(t, "TLS_CHACHA20_POLY1305_SHA256", bindings[2].TLSCipherSuites[1])
|
2021-05-11 04:54:06 +00:00
|
|
|
require.Len(t, bindings[2].ProxyAllowed, 2)
|
|
|
|
require.Equal(t, "192.168.9.1", bindings[2].ProxyAllowed[0])
|
|
|
|
require.Equal(t, "172.16.25.0/24", bindings[2].ProxyAllowed[1])
|
2021-01-19 17:59:41 +00:00
|
|
|
}
|
|
|
|
|
2021-02-13 13:41:37 +00:00
|
|
|
func TestHTTPClientCertificatesFromEnv(t *testing.T) {
|
|
|
|
reset()
|
|
|
|
|
|
|
|
configDir := ".."
|
|
|
|
confName := tempConfigName + ".json"
|
|
|
|
configFilePath := filepath.Join(configDir, confName)
|
|
|
|
err := config.LoadConfig(configDir, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
httpConf := config.GetHTTPConfig()
|
|
|
|
httpConf.Certificates = append(httpConf.Certificates, httpclient.TLSKeyPair{
|
|
|
|
Cert: "cert",
|
|
|
|
Key: "key",
|
|
|
|
})
|
|
|
|
c := make(map[string]httpclient.Config)
|
|
|
|
c["http"] = httpConf
|
|
|
|
jsonConf, err := json.Marshal(c)
|
|
|
|
require.NoError(t, err)
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(configFilePath, jsonConf, os.ModePerm)
|
2021-02-13 13:41:37 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
err = config.LoadConfig(configDir, confName)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, config.GetHTTPConfig().Certificates, 1)
|
|
|
|
require.Equal(t, "cert", config.GetHTTPConfig().Certificates[0].Cert)
|
|
|
|
require.Equal(t, "key", config.GetHTTPConfig().Certificates[0].Key)
|
|
|
|
|
|
|
|
os.Setenv("SFTPGO_HTTP__CERTIFICATES__0__CERT", "cert0")
|
|
|
|
os.Setenv("SFTPGO_HTTP__CERTIFICATES__0__KEY", "key0")
|
|
|
|
os.Setenv("SFTPGO_HTTP__CERTIFICATES__8__CERT", "cert8")
|
|
|
|
os.Setenv("SFTPGO_HTTP__CERTIFICATES__9__CERT", "cert9")
|
|
|
|
os.Setenv("SFTPGO_HTTP__CERTIFICATES__9__KEY", "key9")
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
os.Unsetenv("SFTPGO_HTTP__CERTIFICATES__0__CERT")
|
|
|
|
os.Unsetenv("SFTPGO_HTTP__CERTIFICATES__0__KEY")
|
|
|
|
os.Unsetenv("SFTPGO_HTTP__CERTIFICATES__8__CERT")
|
|
|
|
os.Unsetenv("SFTPGO_HTTP__CERTIFICATES__9__CERT")
|
|
|
|
os.Unsetenv("SFTPGO_HTTP__CERTIFICATES__9__KEY")
|
|
|
|
})
|
|
|
|
|
|
|
|
err = config.LoadConfig(configDir, confName)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, config.GetHTTPConfig().Certificates, 2)
|
|
|
|
require.Equal(t, "cert0", config.GetHTTPConfig().Certificates[0].Cert)
|
|
|
|
require.Equal(t, "key0", config.GetHTTPConfig().Certificates[0].Key)
|
|
|
|
require.Equal(t, "cert9", config.GetHTTPConfig().Certificates[1].Cert)
|
|
|
|
require.Equal(t, "key9", config.GetHTTPConfig().Certificates[1].Key)
|
|
|
|
|
|
|
|
err = os.Remove(configFilePath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
config.Init()
|
|
|
|
|
|
|
|
err = config.LoadConfig(configDir, "")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, config.GetHTTPConfig().Certificates, 2)
|
|
|
|
require.Equal(t, "cert0", config.GetHTTPConfig().Certificates[0].Cert)
|
|
|
|
require.Equal(t, "key0", config.GetHTTPConfig().Certificates[0].Key)
|
|
|
|
require.Equal(t, "cert9", config.GetHTTPConfig().Certificates[1].Cert)
|
|
|
|
require.Equal(t, "key9", config.GetHTTPConfig().Certificates[1].Key)
|
|
|
|
}
|
|
|
|
|
2020-10-30 17:58:57 +00:00
|
|
|
func TestConfigFromEnv(t *testing.T) {
|
2020-12-03 15:23:33 +00:00
|
|
|
reset()
|
|
|
|
|
2020-12-23 15:12:30 +00:00
|
|
|
os.Setenv("SFTPGO_SFTPD__BINDINGS__0__ADDRESS", "127.0.0.1")
|
|
|
|
os.Setenv("SFTPGO_WEBDAVD__BINDINGS__0__PORT", "12000")
|
2020-10-30 17:58:57 +00:00
|
|
|
os.Setenv("SFTPGO_DATA_PROVIDER__PASSWORD_HASHING__ARGON2_OPTIONS__ITERATIONS", "41")
|
|
|
|
os.Setenv("SFTPGO_DATA_PROVIDER__POOL_SIZE", "10")
|
|
|
|
os.Setenv("SFTPGO_DATA_PROVIDER__ACTIONS__EXECUTE_ON", "add")
|
2020-11-30 20:46:34 +00:00
|
|
|
os.Setenv("SFTPGO_KMS__SECRETS__URL", "local")
|
|
|
|
os.Setenv("SFTPGO_KMS__SECRETS__MASTER_KEY_PATH", "path")
|
2021-02-18 19:17:16 +00:00
|
|
|
os.Setenv("SFTPGO_TELEMETRY__TLS_CIPHER_SUITES", "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA")
|
2020-10-30 17:58:57 +00:00
|
|
|
t.Cleanup(func() {
|
2020-12-23 15:12:30 +00:00
|
|
|
os.Unsetenv("SFTPGO_SFTPD__BINDINGS__0__ADDRESS")
|
|
|
|
os.Unsetenv("SFTPGO_WEBDAVD__BINDINGS__0__PORT")
|
2020-10-30 17:58:57 +00:00
|
|
|
os.Unsetenv("SFTPGO_DATA_PROVIDER__PASSWORD_HASHING__ARGON2_OPTIONS__ITERATIONS")
|
|
|
|
os.Unsetenv("SFTPGO_DATA_PROVIDER__POOL_SIZE")
|
|
|
|
os.Unsetenv("SFTPGO_DATA_PROVIDER__ACTIONS__EXECUTE_ON")
|
2020-11-30 20:46:34 +00:00
|
|
|
os.Unsetenv("SFTPGO_KMS__SECRETS__URL")
|
|
|
|
os.Unsetenv("SFTPGO_KMS__SECRETS__MASTER_KEY_PATH")
|
2021-02-18 19:17:16 +00:00
|
|
|
os.Unsetenv("SFTPGO_TELEMETRY__TLS_CIPHER_SUITES")
|
2020-10-30 17:58:57 +00:00
|
|
|
})
|
|
|
|
err := config.LoadConfig(".", "invalid config")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
sftpdConfig := config.GetSFTPDConfig()
|
2020-12-23 15:12:30 +00:00
|
|
|
assert.Equal(t, "127.0.0.1", sftpdConfig.Bindings[0].Address)
|
|
|
|
assert.Equal(t, 12000, config.GetWebDAVDConfig().Bindings[0].Port)
|
2020-10-30 17:58:57 +00:00
|
|
|
dataProviderConf := config.GetProviderConf()
|
|
|
|
assert.Equal(t, uint32(41), dataProviderConf.PasswordHashing.Argon2Options.Iterations)
|
|
|
|
assert.Equal(t, 10, dataProviderConf.PoolSize)
|
|
|
|
assert.Len(t, dataProviderConf.Actions.ExecuteOn, 1)
|
|
|
|
assert.Contains(t, dataProviderConf.Actions.ExecuteOn, "add")
|
2020-11-30 20:46:34 +00:00
|
|
|
kmsConfig := config.GetKMSConfig()
|
|
|
|
assert.Equal(t, "local", kmsConfig.Secrets.URL)
|
|
|
|
assert.Equal(t, "path", kmsConfig.Secrets.MasterKeyPath)
|
2021-02-18 19:17:16 +00:00
|
|
|
telemetryConfig := config.GetTelemetryConfig()
|
|
|
|
assert.Len(t, telemetryConfig.TLSCipherSuites, 2)
|
|
|
|
assert.Equal(t, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", telemetryConfig.TLSCipherSuites[0])
|
|
|
|
assert.Equal(t, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", telemetryConfig.TLSCipherSuites[1])
|
2020-10-30 17:58:57 +00:00
|
|
|
}
|