| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464 |
- package sftpd_test
- import (
- "crypto/rand"
- "fmt"
- "io"
- "io/ioutil"
- "net"
- "net/http"
- "os"
- "path/filepath"
- "runtime"
- "testing"
- "time"
- _ "github.com/go-sql-driver/mysql"
- _ "github.com/lib/pq"
- _ "github.com/mattn/go-sqlite3"
- "golang.org/x/crypto/ssh"
- "github.com/drakkan/sftpgo/api"
- "github.com/drakkan/sftpgo/config"
- "github.com/drakkan/sftpgo/dataprovider"
- "github.com/drakkan/sftpgo/logger"
- "github.com/drakkan/sftpgo/sftpd"
- "github.com/pkg/sftp"
- "github.com/rs/zerolog"
- )
- const (
- logSender = "sftpdTesting"
- sftpServerAddr = "127.0.0.1:2022"
- defaultUsername = "test_user_sftp"
- defaultPassword = "test_password"
- testPubKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC03jj0D+djk7pxIf/0OhrxrchJTRZklofJ1NoIu4752S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NK/+KolVepe
- A6vcl/iKaG4U7su166nxLST06M2EgcSVsFJHpKn5+WAXC+X0Gx8kNjWIIb3GpiChdc0xZD
- mq02xZthVJrTCVw/e7gfDoB2QRsNV8HwAAAMEAzsCghZVp+0YsYg9oOrw4tEqcbEXEMhwY
- 0jW8JNL8Spr1Ibp5Dw6bRSk5azARjmJtnMJhJ3oeHfF0eoISqcNuQXGndGQbVM9YzzAzc1
- NbbCNsVroqKlChT5wyPNGS+phi2bPARBno7WSDvshTZ7dAVEP2c9MJW0XwoSevwKlhgSdt
- RLFFQ/5nclJSdzPBOmQouC0OBcMFSrYtMeknJ4VvueVvve5HcHFaEsaMc7ABAGaLYaBQOm
- iixITGvaNZh/tjAAAACW5pY29sYUBwMQE=
- -----END OPENSSH PRIVATE KEY-----`
- configDir = ".."
- )
- var (
- allPerms = []string{dataprovider.PermAny}
- homeBasePath string
- )
- func TestMain(m *testing.M) {
- logfilePath := filepath.Join(configDir, "sftpgo_sftpd_test.log")
- logger.InitLogger(logfilePath, 5, 1, 28, false, zerolog.DebugLevel)
- config.LoadConfig(configDir, "")
- providerConf := config.GetProviderConf()
- err := dataprovider.Initialize(providerConf, configDir)
- if err != nil {
- logger.Warn(logSender, "error initializing data provider: %v", err)
- os.Exit(1)
- }
- dataProvider := dataprovider.GetProvider()
- sftpdConf := config.GetSFTPDConfig()
- httpdConf := config.GetHTTPDConfig()
- router := api.GetHTTPRouter()
- sftpdConf.BindPort = 2022
- // we run the test cases with UploadMode atomic. The non atomic code path
- // simply does not execute some code so if it works in atomic mode will
- // work in non atomic mode too
- sftpdConf.UploadMode = 1
- if runtime.GOOS == "windows" {
- homeBasePath = "C:\\"
- } else {
- homeBasePath = "/tmp"
- sftpdConf.Actions.ExecuteOn = []string{"download", "upload", "rename", "delete"}
- sftpdConf.Actions.Command = "/usr/bin/true"
- sftpdConf.Actions.HTTPNotificationURL = "http://127.0.0.1:8080/"
- }
- sftpd.SetDataProvider(dataProvider)
- api.SetDataProvider(dataProvider)
- go func() {
- logger.Debug(logSender, "initializing SFTP server with config %+v", sftpdConf)
- if err := sftpdConf.Initialize(configDir); err != nil {
- logger.Error(logSender, "could not start SFTP server: %v", err)
- }
- }()
- go func() {
- logger.Debug(logSender, "initializing HTTP server with config %+v", httpdConf)
- s := &http.Server{
- Addr: fmt.Sprintf("%s:%d", httpdConf.BindAddress, httpdConf.BindPort),
- Handler: router,
- ReadTimeout: 300 * time.Second,
- WriteTimeout: 300 * time.Second,
- MaxHeaderBytes: 1 << 20, // 1MB
- }
- if err := s.ListenAndServe(); err != nil {
- logger.Error(logSender, "could not start HTTP server: %v", err)
- }
- }()
- waitTCPListening(fmt.Sprintf("%s:%d", sftpdConf.BindAddress, sftpdConf.BindPort))
- waitTCPListening(fmt.Sprintf("%s:%d", httpdConf.BindAddress, httpdConf.BindPort))
- exitCode := m.Run()
- os.Remove(logfilePath)
- os.Exit(exitCode)
- }
- func TestInitialization(t *testing.T) {
- config.LoadConfig(configDir, "")
- sftpdConf := config.GetSFTPDConfig()
- sftpdConf.Umask = "invalid umask"
- sftpdConf.BindPort = 2022
- err := sftpdConf.Initialize(configDir)
- if err == nil {
- t.Errorf("Inizialize must fail, a SFTP server should be already running")
- }
- }
- func TestBasicSFTPHandling(t *testing.T) {
- usePubKey := false
- u := getTestUser(usePubKey)
- u.QuotaSize = 6553600
- user, _, err := api.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- expectedQuotaSize := user.UsedQuotaSize + testFileSize
- expectedQuotaFiles := user.UsedQuotaFiles + 1
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- localDownloadPath := filepath.Join(homeBasePath, "test_download.dat")
- err = sftpDownloadFile(testFileName, localDownloadPath, testFileSize, client)
- if err != nil {
- t.Errorf("file download error: %v", err)
- }
- user, _, err = api.GetUserByID(user.ID, http.StatusOK)
- if err != nil {
- t.Errorf("error getting user: %v", err)
- }
- if expectedQuotaFiles != user.UsedQuotaFiles {
- t.Errorf("quota files does not match, expected: %v, actual: %v", expectedQuotaFiles, user.UsedQuotaFiles)
- }
- if expectedQuotaSize != user.UsedQuotaSize {
- t.Errorf("quota size does not match, expected: %v, actual: %v", expectedQuotaSize, user.UsedQuotaSize)
- }
- err = client.Remove(testFileName)
- if err != nil {
- t.Errorf("error removing uploaded file: %v", err)
- }
- _, err = client.Lstat(testFileName)
- if err == nil {
- t.Errorf("stat for deleted file must not succeed")
- }
- user, _, err = api.GetUserByID(user.ID, http.StatusOK)
- if err != nil {
- t.Errorf("error getting user: %v", err)
- }
- if (expectedQuotaFiles - 1) != user.UsedQuotaFiles {
- t.Errorf("quota files does not match after delete, expected: %v, actual: %v", expectedQuotaFiles-1, user.UsedQuotaFiles)
- }
- if (expectedQuotaSize - testFileSize) != user.UsedQuotaSize {
- t.Errorf("quota size does not match, expected: %v, actual: %v", expectedQuotaSize-testFileSize, user.UsedQuotaSize)
- }
- }
- _, err = api.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestDirCommands(t *testing.T) {
- usePubKey := false
- user, _, err := api.AddUser(getTestUser(usePubKey), http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- // remove the home dir to test auto creation
- _, err = os.Stat(user.HomeDir)
- if err == nil {
- os.RemoveAll(user.HomeDir)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- err = client.Mkdir("test")
- if err != nil {
- t.Errorf("error mkdir: %v", err)
- }
- err = client.Rename("test", "test1")
- if err != nil {
- t.Errorf("error rename: %v", err)
- }
- err = client.Remove("test1")
- if err != nil {
- t.Errorf("error rmdir: %v", err)
- }
- err = client.Mkdir("/test/test1")
- if err != nil {
- t.Errorf("error mkdir all: %v", err)
- }
- testFileName := "/test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, filepath.Join("/test", testFileName), testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- // internally client.Remove will call RemoveDirectory on failure
- // the first remove will fail since test directory is not empty
- // the RemoveDirectory called internally by client.Remove will succeed
- err = client.Remove("/test")
- if err != nil {
- t.Errorf("error rmdir all: %v", err)
- }
- _, err = client.Lstat("/test")
- if err == nil {
- t.Errorf("stat for deleted dir must not succeed")
- }
- err = client.Remove("/test")
- if err == nil {
- t.Errorf("remove missing path must fail")
- }
- }
- _, err = api.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestSymlink(t *testing.T) {
- usePubKey := false
- user, _, err := api.AddUser(getTestUser(usePubKey), http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- err = client.Symlink(testFileName, testFileName+".link")
- if err != nil {
- t.Errorf("error creating symlink: %v", err)
- }
- err = client.Symlink(testFileName, testFileName+".link")
- if err == nil {
- t.Errorf("creating a symlink to an existing one must fail")
- }
- err = client.Remove(testFileName + ".link")
- if err != nil {
- t.Errorf("error removing symlink: %v", err)
- }
- err = client.Remove(testFileName)
- if err != nil {
- t.Errorf("error removing uploaded file: %v", err)
- }
- }
- _, err = api.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestStat(t *testing.T) {
- usePubKey := false
- user, _, err := api.AddUser(getTestUser(usePubKey), http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- fi, err := client.Lstat(testFileName)
- if err != nil {
- t.Errorf("stat error: %v", err)
- }
- err = client.Chown(testFileName, 1000, 1000)
- if err != nil {
- t.Errorf("chown error: %v", err)
- }
- err = client.Chmod(testFileName, 0600)
- if err != nil {
- t.Errorf("chmod error: %v", err)
- }
- newFi, err := client.Lstat(testFileName)
- if err != nil {
- t.Errorf("stat error: %v", err)
- }
- if fi.Mode().Perm() != newFi.Mode().Perm() {
- t.Errorf("stat must remain unchanged")
- }
- _, err = client.ReadLink(testFileName)
- if err == nil {
- t.Errorf("readlink is not supported and must fail")
- }
- err = client.Remove(testFileName)
- if err != nil {
- t.Errorf("error removing uploaded file: %v", err)
- }
- }
- _, err = api.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- // basic tests to verify virtual chroot, should be improved to cover more cases ...
- func TestEscapeHomeDir(t *testing.T) {
- usePubKey := true
- user, _, err := api.AddUser(getTestUser(usePubKey), http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- _, err := client.Getwd()
- if err != nil {
- t.Errorf("unable to get working dir: %v", err)
- }
- testDir := "testDir"
- linkPath := filepath.Join(homeBasePath, defaultUsername, testDir)
- err = os.Symlink(homeBasePath, linkPath)
- if err != nil {
- t.Errorf("error making local symlink: %v", err)
- }
- _, err = client.ReadDir(testDir)
- if err == nil {
- t.Errorf("reading a symbolic link outside home dir should not succeeded")
- }
- os.Remove(linkPath)
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- remoteDestPath := filepath.Join("..", "..", testFileName)
- err = sftpUploadFile(testFilePath, remoteDestPath, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- _, err = client.Lstat(testFileName)
- if err != nil {
- t.Errorf("file stat error: %v the file was created outside the user dir!", err)
- }
- err = client.Remove(testFileName)
- if err != nil {
- t.Errorf("error removing uploaded file: %v", err)
- }
- linkPath = filepath.Join(homeBasePath, defaultUsername, testFileName)
- err = os.Symlink(homeBasePath, linkPath)
- if err != nil {
- t.Errorf("error making local symlink: %v", err)
- }
- err = sftpDownloadFile(testFileName, testFilePath, 0, client)
- if err == nil {
- t.Errorf("download file outside home dir must fail")
- }
- err = sftpUploadFile(testFilePath, remoteDestPath, testFileSize, client)
- if err == nil {
- t.Errorf("overwrite a file outside home dir must fail")
- }
- err = client.Chmod(remoteDestPath, 0644)
- if err == nil {
- t.Errorf("setstat on a file outside home dir must fail")
- }
- os.Remove(linkPath)
- }
- _, err = api.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestHomeSpecialChars(t *testing.T) {
- usePubKey := true
- u := getTestUser(usePubKey)
- u.HomeDir = filepath.Join(homeBasePath, "abc açà#&%lk")
- user, _, err := api.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- _, err := client.Getwd()
- if err != nil {
- t.Errorf("unable to get working dir: %v", err)
- }
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- files, err := client.ReadDir(".")
- if err != nil {
- t.Errorf("unable to read remote dir: %v", err)
- }
- if len(files) < 1 {
- t.Errorf("expected at least 1 file in this dir")
- }
- err = client.Remove(testFileName)
- if err != nil {
- t.Errorf("error removing uploaded file: %v", err)
- }
- }
- _, err = api.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestLogin(t *testing.T) {
- u := getTestUser(false)
- u.PublicKeys = []string{testPubKey}
- user, _, err := api.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, false)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- _, err := client.Getwd()
- if err != nil {
- t.Errorf("sftp client with valid password must work")
- }
- }
- client, err = getSftpClient(user, true)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- _, err := client.Getwd()
- if err != nil {
- t.Errorf("sftp client with valid public key must work")
- }
- }
- user.Password = "invalid password"
- client, err = getSftpClient(user, false)
- if err == nil {
- t.Errorf("login with invalid password must fail")
- defer client.Close()
- }
- // testPubKey1 is not authorized
- user.PublicKeys = []string{testPubKey1}
- user.Password = ""
- _, _, err = api.UpdateUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to update user: %v", err)
- }
- client, err = getSftpClient(user, true)
- if err == nil {
- t.Errorf("login with invalid public key must fail")
- defer client.Close()
- }
- // login a user with multiple public keys, only the second one is valid
- user.PublicKeys = []string{testPubKey1, testPubKey}
- user.Password = ""
- _, _, err = api.UpdateUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to update user: %v", err)
- }
- client, err = getSftpClient(user, true)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- _, err := client.Getwd()
- if err != nil {
- t.Errorf("sftp client with multiple public key must work if at least one public key is valid")
- }
- }
- _, err = api.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestLoginAfterUserUpdateEmptyPwd(t *testing.T) {
- usePubKey := false
- user, _, err := api.AddUser(getTestUser(usePubKey), http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- user.Password = ""
- user.PublicKeys = []string{}
- // password and public key should remain unchanged
- _, _, err = api.UpdateUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to update user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- _, err := client.Getwd()
- if err != nil {
- t.Errorf("unable to get working dir: %v", err)
- }
- _, err = client.ReadDir(".")
- if err != nil {
- t.Errorf("unable to read remote dir: %v", err)
- }
- }
- _, err = api.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestLoginAfterUserUpdateEmptyPubKey(t *testing.T) {
- usePubKey := true
- user, _, err := api.AddUser(getTestUser(usePubKey), http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- user.Password = ""
- user.PublicKeys = []string{}
- // password and public key should remain unchanged
- _, _, err = api.UpdateUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to update user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- _, err := client.Getwd()
- if err != nil {
- t.Errorf("unable to get working dir: %v", err)
- }
- _, err = client.ReadDir(".")
- if err != nil {
- t.Errorf("unable to read remote dir: %v", err)
- }
- }
- _, err = api.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestMaxSessions(t *testing.T) {
- usePubKey := false
- u := getTestUser(usePubKey)
- u.MaxSessions = 1
- user, _, err := api.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- _, err := client.Getwd()
- if err != nil {
- t.Errorf("unable to get working dir: %v", err)
- }
- _, err = client.ReadDir(".")
- if err != nil {
- t.Errorf("unable to read remote dir: %v", err)
- }
- _, err = getSftpClient(user, usePubKey)
- if err == nil {
- t.Errorf("max sessions exceeded, new login should not succeed")
- }
- }
- _, err = api.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestQuotaFileReplace(t *testing.T) {
- usePubKey := false
- u := getTestUser(usePubKey)
- u.QuotaFiles = 1000
- user, _, err := api.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- testFileSize := int64(65535)
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- expectedQuotaSize := user.UsedQuotaSize + testFileSize
- expectedQuotaFiles := user.UsedQuotaFiles + 1
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- user, _, err = api.GetUserByID(user.ID, http.StatusOK)
- if err != nil {
- t.Errorf("error getting user: %v", err)
- }
- // now replace the same file, the quota must not change
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- user, _, err = api.GetUserByID(user.ID, http.StatusOK)
- if err != nil {
- t.Errorf("error getting user: %v", err)
- }
- if expectedQuotaFiles != user.UsedQuotaFiles {
- t.Errorf("quota files does not match, expected: %v, actual: %v", expectedQuotaFiles, user.UsedQuotaFiles)
- }
- if expectedQuotaSize != user.UsedQuotaSize {
- t.Errorf("quota size does not match, expected: %v, actual: %v", expectedQuotaSize, user.UsedQuotaSize)
- }
- }
- // now set a quota size restriction and upload the same fail, upload should fail for space limit exceeded
- user.QuotaSize = testFileSize - 1
- user, _, err = api.UpdateUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("error updating user: %v", err)
- }
- client, err = getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err == nil {
- t.Errorf("quota size exceeded, file upload must fail")
- }
- err = client.Remove(testFileName)
- if err != nil {
- t.Errorf("error removing uploaded file: %v", err)
- }
- }
- _, err = api.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestQuotaScan(t *testing.T) {
- usePubKey := false
- user, _, err := api.AddUser(getTestUser(usePubKey), http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- testFileSize := int64(65535)
- expectedQuotaSize := user.UsedQuotaSize + testFileSize
- expectedQuotaFiles := user.UsedQuotaFiles + 1
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- }
- _, err = api.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- // create user with the same home dir, so there is at least an untracked file
- user, _, err = api.AddUser(getTestUser(usePubKey), http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- _, err = api.StartQuotaScan(user, http.StatusCreated)
- if err != nil {
- t.Errorf("error starting quota scan: %v", err)
- }
- scans, _, err := api.GetQuotaScans(http.StatusOK)
- if err != nil {
- t.Errorf("error getting active quota scans: %v", err)
- }
- for len(scans) > 0 {
- scans, _, err = api.GetQuotaScans(http.StatusOK)
- if err != nil {
- t.Errorf("error getting active quota scans: %v", err)
- break
- }
- }
- user, _, err = api.GetUserByID(user.ID, http.StatusOK)
- if err != nil {
- t.Errorf("error getting user: %v", err)
- }
- if expectedQuotaFiles != user.UsedQuotaFiles {
- t.Errorf("quota files does not match after scan, expected: %v, actual: %v", expectedQuotaFiles, user.UsedQuotaFiles)
- }
- if expectedQuotaSize != user.UsedQuotaSize {
- t.Errorf("quota size does not match after scan, expected: %v, actual: %v", expectedQuotaSize, user.UsedQuotaSize)
- }
- _, err = api.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestMultipleQuotaScans(t *testing.T) {
- if !sftpd.AddQuotaScan(defaultUsername) {
- t.Errorf("add quota failed")
- }
- if sftpd.AddQuotaScan(defaultUsername) {
- t.Errorf("add quota must fail if another scan is already active")
- }
- sftpd.RemoveQuotaScan(defaultPassword)
- }
- func TestQuotaSize(t *testing.T) {
- usePubKey := false
- testFileSize := int64(65535)
- u := getTestUser(usePubKey)
- u.QuotaFiles = 1
- u.QuotaSize = testFileSize - 1
- user, _, err := api.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName+".quota", testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName+".quota.1", testFileSize, client)
- if err == nil {
- t.Errorf("user is over quota file upload must fail")
- }
- err = client.Remove(testFileName + ".quota")
- if err != nil {
- t.Errorf("error removing uploaded file: %v", err)
- }
- }
- _, err = api.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestBandwidthAndConnections(t *testing.T) {
- usePubKey := false
- testFileSize := int64(131072)
- u := getTestUser(usePubKey)
- u.UploadBandwidth = 30
- u.DownloadBandwidth = 25
- wantedUploadElapsed := 1000 * (testFileSize / 1000) / u.UploadBandwidth
- wantedDownloadElapsed := 1000 * (testFileSize / 1000) / u.DownloadBandwidth
- // 100 ms tolerance
- wantedUploadElapsed -= 100
- wantedDownloadElapsed -= 100
- user, _, err := api.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- startTime := time.Now()
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- elapsed := time.Since(startTime).Nanoseconds() / 1000000
- if elapsed < (wantedUploadElapsed) {
- t.Errorf("upload bandwidth throttling not respected, elapsed: %v, wanted: %v", elapsed, wantedUploadElapsed)
- }
- startTime = time.Now()
- localDownloadPath := filepath.Join(homeBasePath, "test_download.dat")
- c := sftpDownloadNonBlocking(testFileName, localDownloadPath, testFileSize, client)
- waitForActiveTransfer()
- // wait some additional arbitrary time to wait for transfer activity to happen
- // it is need to reach all the code in CheckIdleConnections
- time.Sleep(100 * time.Millisecond)
- sftpd.CheckIdleConnections()
- err = <-c
- if err != nil {
- t.Errorf("file download error: %v", err)
- }
- elapsed = time.Since(startTime).Nanoseconds() / 1000000
- if elapsed < (wantedDownloadElapsed) {
- t.Errorf("download bandwidth throttling not respected, elapsed: %v, wanted: %v", elapsed, wantedDownloadElapsed)
- }
- // test disconnection
- c = sftpUploadNonBlocking(testFilePath, testFileName+"_partial", testFileSize, client)
- waitForActiveTransfer()
- time.Sleep(100 * time.Millisecond)
- sftpd.CheckIdleConnections()
- stats := sftpd.GetConnectionsStats()
- for _, stat := range stats {
- sftpd.CloseActiveConnection(stat.ConnectionID)
- }
- err = <-c
- if err == nil {
- t.Errorf("connection closed upload must fail")
- }
- }
- _, err = api.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestMissingFile(t *testing.T) {
- usePubKey := false
- u := getTestUser(usePubKey)
- user, _, err := api.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- localDownloadPath := filepath.Join(homeBasePath, "test_download.dat")
- err = sftpDownloadFile("missing_file", localDownloadPath, 0, client)
- if err == nil {
- t.Errorf("download missing file must fail")
- }
- }
- _, err = api.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestOverwriteDirWithFile(t *testing.T) {
- usePubKey := false
- u := getTestUser(usePubKey)
- user, _, err := api.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- testFileSize := int64(65535)
- testFileName := "test_file.dat"
- testDirName := "test_dir"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = client.Mkdir(testDirName)
- if err != nil {
- t.Errorf("mkdir error: %v", err)
- }
- err = sftpUploadFile(testFilePath, testDirName, testFileSize, client)
- if err == nil {
- t.Errorf("copying a file over an existing dir must fail")
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- err = client.Rename(testFileName, testDirName)
- if err == nil {
- t.Errorf("rename a file over an existing dir must fail")
- }
- err = client.RemoveDirectory(testDirName)
- if err != nil {
- t.Errorf("dir remove error: %v", err)
- }
- err = client.Remove(testFileName)
- if err != nil {
- t.Errorf("error removing uploaded file: %v", err)
- }
- }
- _, err = api.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestPermList(t *testing.T) {
- usePubKey := true
- u := getTestUser(usePubKey)
- u.Permissions = []string{dataprovider.PermDownload, dataprovider.PermUpload, dataprovider.PermDelete, dataprovider.PermRename,
- dataprovider.PermCreateDirs, dataprovider.PermCreateSymlinks}
- user, _, err := api.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- _, err = client.ReadDir(".")
- if err == nil {
- t.Errorf("read remote dir without permission should not succeed")
- }
- _, err = client.Stat("test_file")
- if err == nil {
- t.Errorf("stat remote file without permission should not succeed")
- }
- }
- _, err = api.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestPermDownload(t *testing.T) {
- usePubKey := true
- u := getTestUser(usePubKey)
- u.Permissions = []string{dataprovider.PermListItems, dataprovider.PermUpload, dataprovider.PermDelete, dataprovider.PermRename,
- dataprovider.PermCreateDirs, dataprovider.PermCreateSymlinks}
- user, _, err := api.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- localDownloadPath := filepath.Join(homeBasePath, "test_download.dat")
- err = sftpDownloadFile(testFileName, localDownloadPath, testFileSize, client)
- if err == nil {
- t.Errorf("file download without permission should not succeed")
- }
- err = client.Remove(testFileName)
- if err != nil {
- t.Errorf("error removing uploaded file: %v", err)
- }
- }
- _, err = api.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestPermUpload(t *testing.T) {
- usePubKey := false
- u := getTestUser(usePubKey)
- u.Permissions = []string{dataprovider.PermListItems, dataprovider.PermDownload, dataprovider.PermDelete, dataprovider.PermRename,
- dataprovider.PermCreateDirs, dataprovider.PermCreateSymlinks}
- user, _, err := api.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err == nil {
- t.Errorf("file upload without permission should not succeed")
- }
- }
- _, err = api.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestPermDelete(t *testing.T) {
- usePubKey := false
- u := getTestUser(usePubKey)
- u.Permissions = []string{dataprovider.PermListItems, dataprovider.PermDownload, dataprovider.PermUpload, dataprovider.PermRename,
- dataprovider.PermCreateDirs, dataprovider.PermCreateSymlinks}
- user, _, err := api.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- err = client.Remove(testFileName)
- if err == nil {
- t.Errorf("delete without permission should not succeed")
- }
- }
- _, err = api.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestPermRename(t *testing.T) {
- usePubKey := false
- u := getTestUser(usePubKey)
- u.Permissions = []string{dataprovider.PermListItems, dataprovider.PermDownload, dataprovider.PermUpload, dataprovider.PermDelete,
- dataprovider.PermCreateDirs, dataprovider.PermCreateSymlinks}
- user, _, err := api.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- err = client.Rename(testFileName, testFileName+".rename")
- if err == nil {
- t.Errorf("rename without permission should not succeed")
- }
- err = client.Remove(testFileName)
- if err != nil {
- t.Errorf("error removing uploaded file: %v", err)
- }
- }
- _, err = api.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestPermCreateDirs(t *testing.T) {
- usePubKey := false
- u := getTestUser(usePubKey)
- u.Permissions = []string{dataprovider.PermListItems, dataprovider.PermDownload, dataprovider.PermUpload, dataprovider.PermDelete,
- dataprovider.PermRename, dataprovider.PermCreateSymlinks}
- user, _, err := api.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- err = client.Mkdir("testdir")
- if err == nil {
- t.Errorf("mkdir without permission should not succeed")
- }
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, "/dir/subdir/test_file.dat", testFileSize, client)
- if err == nil {
- t.Errorf("mkdir without permission should not succeed")
- }
- }
- _, err = api.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestPermSymlink(t *testing.T) {
- usePubKey := false
- u := getTestUser(usePubKey)
- u.Permissions = []string{dataprovider.PermListItems, dataprovider.PermDownload, dataprovider.PermUpload, dataprovider.PermDelete,
- dataprovider.PermRename, dataprovider.PermCreateDirs}
- user, _, err := api.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- err = client.Symlink(testFilePath, testFilePath+".symlink")
- if err == nil {
- t.Errorf("symlink without permission should not succeed")
- }
- err = client.Remove(testFileName)
- if err != nil {
- t.Errorf("error removing uploaded file: %v", err)
- }
- }
- _, err = api.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestSSHConnection(t *testing.T) {
- usePubKey := false
- user, _, err := api.AddUser(getTestUser(usePubKey), http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- err = doSSH(user, usePubKey)
- if err == nil {
- t.Errorf("ssh connection must fail: %v", err)
- }
- _, err = api.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func waitTCPListening(address string) {
- for {
- conn, err := net.Dial("tcp", address)
- if err != nil {
- logger.WarnToConsole("tcp server %v not listening: %v\n", address, err)
- time.Sleep(100 * time.Millisecond)
- continue
- }
- logger.InfoToConsole("tcp server %v now listening\n", address)
- defer conn.Close()
- break
- }
- }
- func getTestUser(usePubKey bool) dataprovider.User {
- user := dataprovider.User{
- Username: defaultUsername,
- Password: defaultPassword,
- HomeDir: filepath.Join(homeBasePath, defaultUsername),
- Permissions: allPerms,
- }
- if usePubKey {
- user.PublicKeys = []string{testPubKey}
- user.Password = ""
- }
- return user
- }
- func doSSH(user dataprovider.User, usePubKey bool) error {
- var sshSession *ssh.Session
- config := &ssh.ClientConfig{
- User: defaultUsername,
- HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
- return nil
- },
- }
- if usePubKey {
- key, err := ssh.ParsePrivateKey([]byte(testPrivateKey))
- if err != nil {
- return err
- }
- config.Auth = []ssh.AuthMethod{ssh.PublicKeys(key)}
- } else {
- config.Auth = []ssh.AuthMethod{ssh.Password(defaultPassword)}
- }
- conn, err := ssh.Dial("tcp", sftpServerAddr, config)
- if err != nil {
- return err
- }
- defer conn.Close()
- sshSession, err = conn.NewSession()
- if err != nil {
- return err
- }
- _, err = sshSession.CombinedOutput("ls")
- return err
- }
- func getSftpClient(user dataprovider.User, usePubKey bool) (*sftp.Client, error) {
- var sftpClient *sftp.Client
- config := &ssh.ClientConfig{
- User: user.Username,
- HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
- return nil
- },
- }
- if usePubKey {
- key, err := ssh.ParsePrivateKey([]byte(testPrivateKey))
- if err != nil {
- return nil, err
- }
- config.Auth = []ssh.AuthMethod{ssh.PublicKeys(key)}
- } else {
- if len(user.Password) > 0 {
- config.Auth = []ssh.AuthMethod{ssh.Password(user.Password)}
- } else {
- config.Auth = []ssh.AuthMethod{ssh.Password(defaultPassword)}
- }
- }
- conn, err := ssh.Dial("tcp", sftpServerAddr, config)
- if err != nil {
- return sftpClient, err
- }
- sftpClient, err = sftp.NewClient(conn)
- return sftpClient, err
- }
- func createTestFile(path string, size int64) error {
- content := make([]byte, size)
- _, err := rand.Read(content)
- if err != nil {
- return err
- }
- return ioutil.WriteFile(path, content, 0666)
- }
- func sftpUploadFile(localSourcePath string, remoteDestPath string, expectedSize int64, client *sftp.Client) error {
- srcFile, err := os.Open(localSourcePath)
- if err != nil {
- return err
- }
- defer srcFile.Close()
- destFile, err := client.Create(remoteDestPath)
- if err != nil {
- return err
- }
- _, err = io.Copy(destFile, srcFile)
- if err != nil {
- destFile.Close()
- return err
- }
- // we need to close the file to trigger the close method on server
- // we cannot defer closing or Lstat will fail for upload atomic mode
- destFile.Close()
- if expectedSize > 0 {
- fi, err := client.Lstat(remoteDestPath)
- if err != nil {
- return err
- }
- if fi.Size() != expectedSize {
- return fmt.Errorf("uploaded file size does not match, actual: %v, expected: %v", fi.Size(), expectedSize)
- }
- }
- return err
- }
- func sftpDownloadFile(remoteSourcePath string, localDestPath string, expectedSize int64, client *sftp.Client) error {
- downloadDest, err := os.Create(localDestPath)
- if err != nil {
- return err
- }
- defer downloadDest.Close()
- sftpSrcFile, err := client.Open(remoteSourcePath)
- if err != nil {
- return err
- }
- defer sftpSrcFile.Close()
- _, err = io.Copy(downloadDest, sftpSrcFile)
- if err != nil {
- return err
- }
- err = downloadDest.Sync()
- if err != nil {
- return err
- }
- if expectedSize > 0 {
- fi, err := downloadDest.Stat()
- if err != nil {
- return err
- }
- if fi.Size() != expectedSize {
- return fmt.Errorf("downloaded file size does not match, actual: %v, expected: %v", fi.Size(), expectedSize)
- }
- }
- return err
- }
- func sftpUploadNonBlocking(localSourcePath string, remoteDestPath string, expectedSize int64, client *sftp.Client) <-chan error {
- c := make(chan error)
- go func() {
- c <- sftpUploadFile(localSourcePath, remoteDestPath, expectedSize, client)
- }()
- return c
- }
- func sftpDownloadNonBlocking(remoteSourcePath string, localDestPath string, expectedSize int64, client *sftp.Client) <-chan error {
- c := make(chan error)
- go func() {
- c <- sftpDownloadFile(remoteSourcePath, localDestPath, expectedSize, client)
- }()
- return c
- }
- func waitForActiveTransfer() {
- stats := sftpd.GetConnectionsStats()
- for len(stats) < 1 {
- stats = sftpd.GetConnectionsStats()
- }
- activeTransferFound := false
- for !activeTransferFound {
- stats = sftpd.GetConnectionsStats()
- if len(stats) == 0 {
- break
- }
- for _, stat := range stats {
- if len(stat.Transfers) > 0 {
- activeTransferFound = true
- }
- }
- }
- }
|