2022-07-17 18:16:00 +00:00
|
|
|
// Copyright (C) 2019-2022 Nicola Murino
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2021-09-25 10:20:31 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2021-10-02 20:25:41 +00:00
|
|
|
"errors"
|
2021-09-25 10:20:31 +00:00
|
|
|
"fmt"
|
2021-10-03 13:17:49 +00:00
|
|
|
"os/exec"
|
|
|
|
"runtime"
|
2021-09-25 10:20:31 +00:00
|
|
|
"testing"
|
2021-10-02 20:25:41 +00:00
|
|
|
"time"
|
2021-09-25 10:20:31 +00:00
|
|
|
|
2022-01-06 10:54:43 +00:00
|
|
|
"github.com/sftpgo/sdk"
|
2021-09-25 10:20:31 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/smtp"
|
2021-09-25 10:20:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRetentionValidation(t *testing.T) {
|
|
|
|
check := RetentionCheck{}
|
2022-08-04 19:50:38 +00:00
|
|
|
check.Folders = []dataprovider.FolderRetention{
|
2021-09-25 10:20:31 +00:00
|
|
|
{
|
|
|
|
Path: "/",
|
|
|
|
Retention: -1,
|
|
|
|
},
|
|
|
|
}
|
2022-08-04 19:50:38 +00:00
|
|
|
err := check.Validate()
|
2021-09-25 10:20:31 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "invalid folder retention")
|
|
|
|
|
2022-08-04 19:50:38 +00:00
|
|
|
check.Folders = []dataprovider.FolderRetention{
|
2021-09-25 10:20:31 +00:00
|
|
|
{
|
|
|
|
Path: "/ab/..",
|
|
|
|
Retention: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = check.Validate()
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "nothing to delete")
|
|
|
|
assert.Equal(t, "/", check.Folders[0].Path)
|
|
|
|
|
2022-08-04 19:50:38 +00:00
|
|
|
check.Folders = append(check.Folders, dataprovider.FolderRetention{
|
2021-09-25 10:20:31 +00:00
|
|
|
Path: "/../..",
|
|
|
|
Retention: 24,
|
|
|
|
})
|
|
|
|
err = check.Validate()
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), `duplicated folder path "/"`)
|
|
|
|
|
2022-08-04 19:50:38 +00:00
|
|
|
check.Folders = []dataprovider.FolderRetention{
|
2021-09-25 10:20:31 +00:00
|
|
|
{
|
|
|
|
Path: "/dir1",
|
|
|
|
Retention: 48,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Path: "/dir2",
|
|
|
|
Retention: 96,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = check.Validate()
|
|
|
|
assert.NoError(t, err)
|
2021-10-03 13:17:49 +00:00
|
|
|
assert.Len(t, check.Notifications, 0)
|
2021-10-02 20:25:41 +00:00
|
|
|
assert.Empty(t, check.Email)
|
|
|
|
|
2021-10-03 13:17:49 +00:00
|
|
|
check.Notifications = []RetentionCheckNotification{RetentionCheckNotificationEmail}
|
2021-10-02 20:25:41 +00:00
|
|
|
err = check.Validate()
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "you must configure an SMTP server")
|
|
|
|
|
|
|
|
smtpCfg := smtp.Config{
|
|
|
|
Host: "mail.example.com",
|
|
|
|
Port: 25,
|
|
|
|
TemplatesPath: "templates",
|
|
|
|
}
|
2022-07-24 14:18:54 +00:00
|
|
|
err = smtpCfg.Initialize(configDir)
|
2021-10-02 20:25:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = check.Validate()
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "you must add a valid email address")
|
|
|
|
|
|
|
|
check.Email = "admin@example.com"
|
|
|
|
err = check.Validate()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
smtpCfg = smtp.Config{}
|
2022-07-24 14:18:54 +00:00
|
|
|
err = smtpCfg.Initialize(configDir)
|
2021-10-02 20:25:41 +00:00
|
|
|
require.NoError(t, err)
|
2021-10-03 13:17:49 +00:00
|
|
|
|
|
|
|
check.Notifications = []RetentionCheckNotification{RetentionCheckNotificationHook}
|
|
|
|
err = check.Validate()
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "data_retention_hook")
|
|
|
|
|
|
|
|
check.Notifications = []string{"not valid"}
|
|
|
|
err = check.Validate()
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "invalid notification")
|
2021-10-02 20:25:41 +00:00
|
|
|
}
|
|
|
|
|
2021-10-03 13:17:49 +00:00
|
|
|
func TestRetentionEmailNotifications(t *testing.T) {
|
2021-10-02 20:25:41 +00:00
|
|
|
smtpCfg := smtp.Config{
|
|
|
|
Host: "127.0.0.1",
|
|
|
|
Port: 2525,
|
|
|
|
TemplatesPath: "templates",
|
|
|
|
}
|
2022-07-24 14:18:54 +00:00
|
|
|
err := smtpCfg.Initialize(configDir)
|
2021-10-02 20:25:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
user := dataprovider.User{
|
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "user1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
user.Permissions = make(map[string][]string)
|
|
|
|
user.Permissions["/"] = []string{dataprovider.PermAny}
|
|
|
|
check := RetentionCheck{
|
2021-10-03 13:17:49 +00:00
|
|
|
Notifications: []RetentionCheckNotification{RetentionCheckNotificationEmail},
|
|
|
|
Email: "notification@example.com",
|
2022-09-06 17:09:23 +00:00
|
|
|
results: []folderRetentionCheckResult{
|
2021-10-02 20:25:41 +00:00
|
|
|
{
|
|
|
|
Path: "/",
|
|
|
|
Retention: 24,
|
|
|
|
DeletedFiles: 10,
|
|
|
|
DeletedSize: 32657,
|
|
|
|
Elapsed: 10 * time.Second,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
conn := NewBaseConnection("", "", "", "", user)
|
2021-10-03 08:22:47 +00:00
|
|
|
conn.SetProtocol(ProtocolDataRetention)
|
|
|
|
conn.ID = fmt.Sprintf("data_retention_%v", user.Username)
|
2021-10-02 20:25:41 +00:00
|
|
|
check.conn = conn
|
2021-10-03 13:17:49 +00:00
|
|
|
check.sendNotifications(1*time.Second, nil)
|
2022-09-28 16:37:32 +00:00
|
|
|
err = check.sendEmailNotification(nil)
|
2021-10-02 20:25:41 +00:00
|
|
|
assert.NoError(t, err)
|
2022-09-28 16:37:32 +00:00
|
|
|
err = check.sendEmailNotification(errors.New("test error"))
|
2021-10-02 20:25:41 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-09-28 16:37:32 +00:00
|
|
|
check.results = nil
|
|
|
|
err = check.sendEmailNotification(nil)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "no data retention report available")
|
|
|
|
}
|
|
|
|
|
2021-10-02 20:25:41 +00:00
|
|
|
smtpCfg.Port = 2626
|
2022-07-24 14:18:54 +00:00
|
|
|
err = smtpCfg.Initialize(configDir)
|
2021-10-02 20:25:41 +00:00
|
|
|
require.NoError(t, err)
|
2022-09-28 16:37:32 +00:00
|
|
|
err = check.sendEmailNotification(nil)
|
2021-10-02 20:25:41 +00:00
|
|
|
assert.Error(t, err)
|
2022-09-28 16:37:32 +00:00
|
|
|
check.results = []folderRetentionCheckResult{
|
|
|
|
{
|
|
|
|
Path: "/",
|
|
|
|
Retention: 24,
|
|
|
|
DeletedFiles: 20,
|
|
|
|
DeletedSize: 456789,
|
|
|
|
Elapsed: 12 * time.Second,
|
|
|
|
},
|
|
|
|
}
|
2021-10-02 20:25:41 +00:00
|
|
|
|
|
|
|
smtpCfg = smtp.Config{}
|
2022-07-24 14:18:54 +00:00
|
|
|
err = smtpCfg.Initialize(configDir)
|
2021-10-02 20:25:41 +00:00
|
|
|
require.NoError(t, err)
|
2022-09-28 16:37:32 +00:00
|
|
|
err = check.sendEmailNotification(nil)
|
2021-10-03 13:17:49 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRetentionHookNotifications(t *testing.T) {
|
|
|
|
dataRetentionHook := Config.DataRetentionHook
|
|
|
|
|
|
|
|
Config.DataRetentionHook = fmt.Sprintf("http://%v", httpAddr)
|
|
|
|
user := dataprovider.User{
|
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "user2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
user.Permissions = make(map[string][]string)
|
|
|
|
user.Permissions["/"] = []string{dataprovider.PermAny}
|
|
|
|
check := RetentionCheck{
|
|
|
|
Notifications: []RetentionCheckNotification{RetentionCheckNotificationHook},
|
2022-09-06 17:09:23 +00:00
|
|
|
results: []folderRetentionCheckResult{
|
2021-10-03 13:17:49 +00:00
|
|
|
{
|
|
|
|
Path: "/",
|
|
|
|
Retention: 24,
|
|
|
|
DeletedFiles: 10,
|
|
|
|
DeletedSize: 32657,
|
|
|
|
Elapsed: 10 * time.Second,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
conn := NewBaseConnection("", "", "", "", user)
|
|
|
|
conn.SetProtocol(ProtocolDataRetention)
|
|
|
|
conn.ID = fmt.Sprintf("data_retention_%v", user.Username)
|
|
|
|
check.conn = conn
|
|
|
|
check.sendNotifications(1*time.Second, nil)
|
|
|
|
err := check.sendHookNotification(1*time.Second, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
Config.DataRetentionHook = fmt.Sprintf("http://%v/404", httpAddr)
|
|
|
|
err = check.sendHookNotification(1*time.Second, nil)
|
|
|
|
assert.ErrorIs(t, err, errUnexpectedHTTResponse)
|
|
|
|
|
|
|
|
Config.DataRetentionHook = "http://foo\x7f.com/retention"
|
|
|
|
err = check.sendHookNotification(1*time.Second, err)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
Config.DataRetentionHook = "relativepath"
|
|
|
|
err = check.sendHookNotification(1*time.Second, err)
|
2021-10-02 20:25:41 +00:00
|
|
|
assert.Error(t, err)
|
2021-10-03 13:17:49 +00:00
|
|
|
|
|
|
|
if runtime.GOOS != osWindows {
|
|
|
|
hookCmd, err := exec.LookPath("true")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
Config.DataRetentionHook = hookCmd
|
|
|
|
err = check.sendHookNotification(1*time.Second, err)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
Config.DataRetentionHook = dataRetentionHook
|
2021-09-25 10:20:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRetentionPermissionsAndGetFolder(t *testing.T) {
|
|
|
|
user := dataprovider.User{
|
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "user1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
user.Permissions = make(map[string][]string)
|
|
|
|
user.Permissions["/"] = []string{dataprovider.PermListItems, dataprovider.PermDelete}
|
|
|
|
user.Permissions["/dir1"] = []string{dataprovider.PermListItems}
|
|
|
|
user.Permissions["/dir2/sub1"] = []string{dataprovider.PermCreateDirs}
|
|
|
|
user.Permissions["/dir2/sub2"] = []string{dataprovider.PermDelete}
|
|
|
|
|
|
|
|
check := RetentionCheck{
|
2022-08-04 19:50:38 +00:00
|
|
|
Folders: []dataprovider.FolderRetention{
|
2021-09-25 10:20:31 +00:00
|
|
|
{
|
|
|
|
Path: "/dir2",
|
|
|
|
Retention: 24 * 7,
|
|
|
|
IgnoreUserPermissions: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Path: "/dir3",
|
|
|
|
Retention: 24 * 7,
|
|
|
|
IgnoreUserPermissions: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Path: "/dir2/sub1/sub",
|
|
|
|
Retention: 24,
|
|
|
|
IgnoreUserPermissions: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := NewBaseConnection("", "", "", "", user)
|
2021-10-03 08:22:47 +00:00
|
|
|
conn.SetProtocol(ProtocolDataRetention)
|
|
|
|
conn.ID = fmt.Sprintf("data_retention_%v", user.Username)
|
2021-09-25 10:20:31 +00:00
|
|
|
check.conn = conn
|
|
|
|
check.updateUserPermissions()
|
|
|
|
assert.Equal(t, []string{dataprovider.PermListItems, dataprovider.PermDelete}, conn.User.Permissions["/"])
|
|
|
|
assert.Equal(t, []string{dataprovider.PermListItems}, conn.User.Permissions["/dir1"])
|
|
|
|
assert.Equal(t, []string{dataprovider.PermAny}, conn.User.Permissions["/dir2"])
|
|
|
|
assert.Equal(t, []string{dataprovider.PermAny}, conn.User.Permissions["/dir2/sub1/sub"])
|
|
|
|
assert.Equal(t, []string{dataprovider.PermCreateDirs}, conn.User.Permissions["/dir2/sub1"])
|
|
|
|
assert.Equal(t, []string{dataprovider.PermDelete}, conn.User.Permissions["/dir2/sub2"])
|
|
|
|
|
|
|
|
_, err := check.getFolderRetention("/")
|
|
|
|
assert.Error(t, err)
|
|
|
|
folder, err := check.getFolderRetention("/dir3")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "/dir3", folder.Path)
|
|
|
|
folder, err = check.getFolderRetention("/dir2/sub3")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "/dir2", folder.Path)
|
|
|
|
folder, err = check.getFolderRetention("/dir2/sub2")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "/dir2", folder.Path)
|
|
|
|
folder, err = check.getFolderRetention("/dir2/sub1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "/dir2", folder.Path)
|
|
|
|
folder, err = check.getFolderRetention("/dir2/sub1/sub/sub")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "/dir2/sub1/sub", folder.Path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRetentionCheckAddRemove(t *testing.T) {
|
|
|
|
username := "username"
|
|
|
|
user := dataprovider.User{
|
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: username,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
user.Permissions = make(map[string][]string)
|
|
|
|
user.Permissions["/"] = []string{dataprovider.PermAny}
|
|
|
|
check := RetentionCheck{
|
2022-08-04 19:50:38 +00:00
|
|
|
Folders: []dataprovider.FolderRetention{
|
2021-09-25 10:20:31 +00:00
|
|
|
{
|
|
|
|
Path: "/",
|
|
|
|
Retention: 48,
|
|
|
|
},
|
|
|
|
},
|
2021-10-03 13:17:49 +00:00
|
|
|
Notifications: []RetentionCheckNotification{RetentionCheckNotificationHook},
|
2021-09-25 10:20:31 +00:00
|
|
|
}
|
|
|
|
assert.NotNil(t, RetentionChecks.Add(check, &user))
|
|
|
|
checks := RetentionChecks.Get()
|
|
|
|
require.Len(t, checks, 1)
|
|
|
|
assert.Equal(t, username, checks[0].Username)
|
|
|
|
assert.Greater(t, checks[0].StartTime, int64(0))
|
|
|
|
require.Len(t, checks[0].Folders, 1)
|
|
|
|
assert.Equal(t, check.Folders[0].Path, checks[0].Folders[0].Path)
|
|
|
|
assert.Equal(t, check.Folders[0].Retention, checks[0].Folders[0].Retention)
|
2021-10-03 13:17:49 +00:00
|
|
|
require.Len(t, checks[0].Notifications, 1)
|
|
|
|
assert.Equal(t, RetentionCheckNotificationHook, checks[0].Notifications[0])
|
2021-09-25 10:20:31 +00:00
|
|
|
|
|
|
|
assert.Nil(t, RetentionChecks.Add(check, &user))
|
|
|
|
assert.True(t, RetentionChecks.remove(username))
|
|
|
|
require.Len(t, RetentionChecks.Get(), 0)
|
|
|
|
assert.False(t, RetentionChecks.remove(username))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCleanupErrors(t *testing.T) {
|
|
|
|
user := dataprovider.User{
|
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "u",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
user.Permissions = make(map[string][]string)
|
|
|
|
user.Permissions["/"] = []string{dataprovider.PermAny}
|
|
|
|
check := &RetentionCheck{
|
2022-08-04 19:50:38 +00:00
|
|
|
Folders: []dataprovider.FolderRetention{
|
2021-09-25 10:20:31 +00:00
|
|
|
{
|
|
|
|
Path: "/path",
|
|
|
|
Retention: 48,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
check = RetentionChecks.Add(*check, &user)
|
|
|
|
require.NotNil(t, check)
|
|
|
|
|
|
|
|
err := check.removeFile("missing file", nil)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
err = check.cleanupFolder("/")
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
assert.True(t, RetentionChecks.remove(user.Username))
|
|
|
|
}
|