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"
|
|
|
|
|
2019-07-26 09:34:44 +00:00
|
|
|
"github.com/drakkan/sftpgo/config"
|
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
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"
|
|
|
|
)
|
|
|
|
|
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)
|
|
|
|
err = ioutil.WriteFile(configFilePath, []byte("{invalid json}"), 0666)
|
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 = ioutil.WriteFile(configFilePath, []byte("{\"sftpd\": {\"bind_port\": \"a\"}}"), 0666)
|
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-05-02 13:47:23 +00:00
|
|
|
err = ioutil.WriteFile(configFilePath, jsonConf, 0666)
|
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)
|
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)
|
2019-08-04 07:37:58 +00:00
|
|
|
sftpdConf := config.GetSFTPDConfig()
|
|
|
|
sftpdConf.UploadMode = 10
|
|
|
|
c := make(map[string]sftpd.Configuration)
|
|
|
|
c["sftpd"] = sftpdConf
|
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-05-02 13:47:23 +00:00
|
|
|
err = ioutil.WriteFile(configFilePath, jsonConf, 0666)
|
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-05-02 13:47:23 +00:00
|
|
|
err = ioutil.WriteFile(configFilePath, jsonConf, 0666)
|
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-05-02 13:47:23 +00:00
|
|
|
err = ioutil.WriteFile(configFilePath, jsonConf, 0666)
|
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-02-28 23:02:06 +00:00
|
|
|
sftpdConf := config.GetSFTPDConfig()
|
|
|
|
sftpdConf.ProxyProtocol = 10
|
|
|
|
c := make(map[string]sftpd.Configuration)
|
|
|
|
c["sftpd"] = sftpdConf
|
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-05-02 13:47:23 +00:00
|
|
|
err = ioutil.WriteFile(configFilePath, jsonConf, 0666)
|
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-05-02 13:47:23 +00:00
|
|
|
err = ioutil.WriteFile(configFilePath, jsonConf, 0666)
|
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-04-01 21:25:23 +00:00
|
|
|
func TestHookCompatibity(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-04-01 21:25:23 +00:00
|
|
|
providerConf := config.GetProviderConf()
|
2020-05-06 17:36:34 +00:00
|
|
|
providerConf.ExternalAuthProgram = "ext_auth_program" //nolint:staticcheck
|
|
|
|
providerConf.PreLoginProgram = "pre_login_program" //nolint:staticcheck
|
2020-04-01 21:25:23 +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)
|
2020-05-02 13:47:23 +00:00
|
|
|
err = ioutil.WriteFile(configFilePath, jsonConf, 0666)
|
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)
|
2020-04-01 21:25:23 +00:00
|
|
|
providerConf = config.GetProviderConf()
|
2020-05-02 13:47:23 +00:00
|
|
|
assert.Equal(t, "ext_auth_program", providerConf.ExternalAuthHook)
|
|
|
|
assert.Equal(t, "pre_login_program", providerConf.PreLoginHook)
|
|
|
|
err = os.Remove(configFilePath)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-04-01 21:25:23 +00:00
|
|
|
sftpdConf := config.GetSFTPDConfig()
|
2020-05-06 17:36:34 +00:00
|
|
|
sftpdConf.KeyboardInteractiveProgram = "key_int_program" //nolint:staticcheck
|
2020-04-01 21:25:23 +00:00
|
|
|
cnf := make(map[string]sftpd.Configuration)
|
|
|
|
cnf["sftpd"] = sftpdConf
|
2020-05-02 13:47:23 +00:00
|
|
|
jsonConf, err = json.Marshal(cnf)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-04-01 21:25:23 +00:00
|
|
|
err = ioutil.WriteFile(configFilePath, jsonConf, 0666)
|
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)
|
2020-04-01 21:25:23 +00:00
|
|
|
sftpdConf = config.GetSFTPDConfig()
|
2020-05-02 13:47:23 +00:00
|
|
|
assert.Equal(t, "key_int_program", sftpdConf.KeyboardInteractiveHook)
|
|
|
|
err = os.Remove(configFilePath)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-04-01 21:25:23 +00:00
|
|
|
}
|
|
|
|
|
2019-10-24 16:50:35 +00:00
|
|
|
func TestSetGetConfig(t *testing.T) {
|
|
|
|
sftpdConf := config.GetSFTPDConfig()
|
|
|
|
sftpdConf.IdleTimeout = 3
|
|
|
|
config.SetSFTPDConfig(sftpdConf)
|
2020-05-02 13:47:23 +00:00
|
|
|
assert.Equal(t, sftpdConf.IdleTimeout, config.GetSFTPDConfig().IdleTimeout)
|
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)
|
2019-10-24 16:50:35 +00:00
|
|
|
}
|