2023-01-03 09:18:30 +00:00
|
|
|
// Copyright (C) 2019-2023 Nicola Murino
|
2022-08-01 16:48:54 +00:00
|
|
|
//
|
|
|
|
// 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
|
2023-01-03 09:18:30 +00:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2022-08-01 16:48:54 +00:00
|
|
|
|
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2022-10-10 16:53:58 +00:00
|
|
|
"bytes"
|
2022-08-22 17:04:17 +00:00
|
|
|
"crypto/rand"
|
2022-08-01 16:48:54 +00:00
|
|
|
"fmt"
|
2022-09-03 14:29:07 +00:00
|
|
|
"io"
|
|
|
|
"mime/multipart"
|
2022-08-01 16:48:54 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2022-08-10 16:41:59 +00:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2022-08-01 16:48:54 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-10-10 16:53:58 +00:00
|
|
|
"github.com/klauspost/compress/zip"
|
2023-01-15 14:28:31 +00:00
|
|
|
"github.com/rs/xid"
|
2022-08-01 16:48:54 +00:00
|
|
|
"github.com/sftpgo/sdk"
|
|
|
|
sdkkms "github.com/sftpgo/sdk/kms"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2022-09-06 17:09:23 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-08-01 16:48:54 +00:00
|
|
|
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/kms"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/util"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/vfs"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestEventRuleMatch(t *testing.T) {
|
2022-12-03 16:47:43 +00:00
|
|
|
role := "role1"
|
2022-08-01 16:48:54 +00:00
|
|
|
conditions := dataprovider.EventConditions{
|
|
|
|
ProviderEvents: []string{"add", "update"},
|
|
|
|
Options: dataprovider.ConditionOptions{
|
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: "user1",
|
|
|
|
InverseMatch: true,
|
|
|
|
},
|
|
|
|
},
|
2022-12-03 16:47:43 +00:00
|
|
|
RoleNames: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: role,
|
|
|
|
},
|
|
|
|
},
|
2022-08-01 16:48:54 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
res := eventManager.checkProviderEventMatch(conditions, EventParams{
|
|
|
|
Name: "user1",
|
2022-12-03 16:47:43 +00:00
|
|
|
Role: role,
|
2022-08-01 16:48:54 +00:00
|
|
|
Event: "add",
|
|
|
|
})
|
|
|
|
assert.False(t, res)
|
|
|
|
res = eventManager.checkProviderEventMatch(conditions, EventParams{
|
|
|
|
Name: "user2",
|
2022-12-03 16:47:43 +00:00
|
|
|
Role: role,
|
2022-08-01 16:48:54 +00:00
|
|
|
Event: "update",
|
|
|
|
})
|
|
|
|
assert.True(t, res)
|
|
|
|
res = eventManager.checkProviderEventMatch(conditions, EventParams{
|
|
|
|
Name: "user2",
|
2022-12-03 16:47:43 +00:00
|
|
|
Role: role,
|
2022-08-01 16:48:54 +00:00
|
|
|
Event: "delete",
|
|
|
|
})
|
|
|
|
assert.False(t, res)
|
|
|
|
conditions.Options.ProviderObjects = []string{"api_key"}
|
|
|
|
res = eventManager.checkProviderEventMatch(conditions, EventParams{
|
|
|
|
Name: "user2",
|
|
|
|
Event: "update",
|
2022-12-03 16:47:43 +00:00
|
|
|
Role: role,
|
2022-08-01 16:48:54 +00:00
|
|
|
ObjectType: "share",
|
|
|
|
})
|
|
|
|
assert.False(t, res)
|
|
|
|
res = eventManager.checkProviderEventMatch(conditions, EventParams{
|
|
|
|
Name: "user2",
|
|
|
|
Event: "update",
|
2022-12-03 16:47:43 +00:00
|
|
|
Role: role,
|
2022-08-01 16:48:54 +00:00
|
|
|
ObjectType: "api_key",
|
|
|
|
})
|
|
|
|
assert.True(t, res)
|
2022-12-03 16:47:43 +00:00
|
|
|
res = eventManager.checkProviderEventMatch(conditions, EventParams{
|
|
|
|
Name: "user2",
|
|
|
|
Event: "update",
|
|
|
|
Role: role + "1",
|
|
|
|
ObjectType: "api_key",
|
|
|
|
})
|
|
|
|
assert.False(t, res)
|
2022-08-01 16:48:54 +00:00
|
|
|
// now test fs events
|
|
|
|
conditions = dataprovider.EventConditions{
|
|
|
|
FsEvents: []string{operationUpload, operationDownload},
|
|
|
|
Options: dataprovider.ConditionOptions{
|
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: "user*",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Pattern: "tester*",
|
|
|
|
},
|
|
|
|
},
|
2022-12-03 16:47:43 +00:00
|
|
|
RoleNames: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: role,
|
|
|
|
InverseMatch: true,
|
|
|
|
},
|
|
|
|
},
|
2022-08-01 16:48:54 +00:00
|
|
|
FsPaths: []dataprovider.ConditionPattern{
|
|
|
|
{
|
2023-01-02 14:59:00 +00:00
|
|
|
Pattern: "/**/*.txt",
|
2022-08-01 16:48:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Protocols: []string{ProtocolSFTP},
|
|
|
|
MinFileSize: 10,
|
|
|
|
MaxFileSize: 30,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
params := EventParams{
|
|
|
|
Name: "tester4",
|
|
|
|
Event: operationDelete,
|
|
|
|
VirtualPath: "/path.txt",
|
|
|
|
Protocol: ProtocolSFTP,
|
|
|
|
ObjectName: "path.txt",
|
|
|
|
FileSize: 20,
|
|
|
|
}
|
|
|
|
res = eventManager.checkFsEventMatch(conditions, params)
|
|
|
|
assert.False(t, res)
|
|
|
|
params.Event = operationDownload
|
|
|
|
res = eventManager.checkFsEventMatch(conditions, params)
|
|
|
|
assert.True(t, res)
|
2022-12-03 16:47:43 +00:00
|
|
|
params.Role = role
|
|
|
|
res = eventManager.checkFsEventMatch(conditions, params)
|
|
|
|
assert.False(t, res)
|
|
|
|
params.Role = ""
|
2022-08-01 16:48:54 +00:00
|
|
|
params.Name = "name"
|
|
|
|
res = eventManager.checkFsEventMatch(conditions, params)
|
|
|
|
assert.False(t, res)
|
|
|
|
params.Name = "user5"
|
|
|
|
res = eventManager.checkFsEventMatch(conditions, params)
|
|
|
|
assert.True(t, res)
|
|
|
|
params.VirtualPath = "/sub/f.jpg"
|
|
|
|
params.ObjectName = path.Base(params.VirtualPath)
|
|
|
|
res = eventManager.checkFsEventMatch(conditions, params)
|
|
|
|
assert.False(t, res)
|
|
|
|
params.VirtualPath = "/sub/f.txt"
|
|
|
|
params.ObjectName = path.Base(params.VirtualPath)
|
|
|
|
res = eventManager.checkFsEventMatch(conditions, params)
|
|
|
|
assert.True(t, res)
|
|
|
|
params.Protocol = ProtocolHTTP
|
|
|
|
res = eventManager.checkFsEventMatch(conditions, params)
|
|
|
|
assert.False(t, res)
|
|
|
|
params.Protocol = ProtocolSFTP
|
|
|
|
params.FileSize = 5
|
|
|
|
res = eventManager.checkFsEventMatch(conditions, params)
|
|
|
|
assert.False(t, res)
|
|
|
|
params.FileSize = 50
|
|
|
|
res = eventManager.checkFsEventMatch(conditions, params)
|
|
|
|
assert.False(t, res)
|
|
|
|
params.FileSize = 25
|
|
|
|
res = eventManager.checkFsEventMatch(conditions, params)
|
|
|
|
assert.True(t, res)
|
|
|
|
// bad pattern
|
|
|
|
conditions.Options.Names = []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: "[-]",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
res = eventManager.checkFsEventMatch(conditions, params)
|
|
|
|
assert.False(t, res)
|
2022-09-04 15:48:09 +00:00
|
|
|
// check fs events with group name filters
|
|
|
|
conditions = dataprovider.EventConditions{
|
|
|
|
FsEvents: []string{operationUpload, operationDownload},
|
|
|
|
Options: dataprovider.ConditionOptions{
|
|
|
|
GroupNames: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: "group*",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Pattern: "testgroup*",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
params = EventParams{
|
|
|
|
Name: "user1",
|
|
|
|
Event: operationUpload,
|
|
|
|
}
|
|
|
|
res = eventManager.checkFsEventMatch(conditions, params)
|
|
|
|
assert.False(t, res)
|
|
|
|
params.Groups = []sdk.GroupMapping{
|
|
|
|
{
|
|
|
|
Name: "g1",
|
|
|
|
Type: sdk.GroupTypePrimary,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "g2",
|
|
|
|
Type: sdk.GroupTypeSecondary,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
res = eventManager.checkFsEventMatch(conditions, params)
|
|
|
|
assert.False(t, res)
|
|
|
|
params.Groups = []sdk.GroupMapping{
|
|
|
|
{
|
|
|
|
Name: "testgroup2",
|
|
|
|
Type: sdk.GroupTypePrimary,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "g2",
|
|
|
|
Type: sdk.GroupTypeSecondary,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
res = eventManager.checkFsEventMatch(conditions, params)
|
|
|
|
assert.True(t, res)
|
2022-12-03 16:47:43 +00:00
|
|
|
// check user conditions
|
|
|
|
user := dataprovider.User{}
|
|
|
|
user.Username = "u1"
|
|
|
|
res = checkUserConditionOptions(&user, &dataprovider.ConditionOptions{})
|
|
|
|
assert.True(t, res)
|
|
|
|
res = checkUserConditionOptions(&user, &dataprovider.ConditionOptions{
|
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: "user",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.False(t, res)
|
|
|
|
res = checkUserConditionOptions(&user, &dataprovider.ConditionOptions{
|
|
|
|
RoleNames: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: role,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.False(t, res)
|
|
|
|
user.Role = role
|
|
|
|
res = checkUserConditionOptions(&user, &dataprovider.ConditionOptions{
|
|
|
|
RoleNames: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: role,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.True(t, res)
|
|
|
|
res = checkUserConditionOptions(&user, &dataprovider.ConditionOptions{
|
|
|
|
GroupNames: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: "group",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
RoleNames: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: role,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.False(t, res)
|
2022-08-01 16:48:54 +00:00
|
|
|
}
|
|
|
|
|
2023-01-02 14:59:00 +00:00
|
|
|
func TestDoubleStarMatching(t *testing.T) {
|
|
|
|
c := dataprovider.ConditionPattern{
|
|
|
|
Pattern: "/mydir/**",
|
|
|
|
}
|
|
|
|
res := checkEventConditionPattern(c, "/mydir")
|
|
|
|
assert.True(t, res)
|
|
|
|
res = checkEventConditionPattern(c, "/mydirname")
|
|
|
|
assert.False(t, res)
|
|
|
|
res = checkEventConditionPattern(c, "/mydir/sub")
|
|
|
|
assert.True(t, res)
|
|
|
|
res = checkEventConditionPattern(c, "/mydir/sub/dir")
|
|
|
|
assert.True(t, res)
|
|
|
|
|
|
|
|
c.Pattern = "/**/*"
|
|
|
|
res = checkEventConditionPattern(c, "/mydir")
|
|
|
|
assert.True(t, res)
|
|
|
|
res = checkEventConditionPattern(c, "/mydirname")
|
|
|
|
assert.True(t, res)
|
|
|
|
res = checkEventConditionPattern(c, "/mydir/sub/dir/file.txt")
|
|
|
|
assert.True(t, res)
|
|
|
|
|
|
|
|
c.Pattern = "/mydir/**/*.txt"
|
|
|
|
res = checkEventConditionPattern(c, "/mydir")
|
|
|
|
assert.False(t, res)
|
|
|
|
res = checkEventConditionPattern(c, "/mydirname/f.txt")
|
|
|
|
assert.False(t, res)
|
|
|
|
res = checkEventConditionPattern(c, "/mydir/sub")
|
|
|
|
assert.False(t, res)
|
|
|
|
res = checkEventConditionPattern(c, "/mydir/sub/dir")
|
|
|
|
assert.False(t, res)
|
|
|
|
res = checkEventConditionPattern(c, "/mydir/sub/dir/a.txt")
|
|
|
|
assert.True(t, res)
|
|
|
|
}
|
|
|
|
|
2022-08-01 16:48:54 +00:00
|
|
|
func TestEventManager(t *testing.T) {
|
|
|
|
startEventScheduler()
|
|
|
|
action := &dataprovider.BaseEventAction{
|
|
|
|
Name: "test_action",
|
|
|
|
Type: dataprovider.ActionTypeHTTP,
|
|
|
|
Options: dataprovider.BaseEventActionOptions{
|
|
|
|
HTTPConfig: dataprovider.EventActionHTTPConfig{
|
|
|
|
Endpoint: "http://localhost",
|
|
|
|
Timeout: 20,
|
|
|
|
Method: http.MethodGet,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-12-03 10:45:27 +00:00
|
|
|
err := dataprovider.AddEventAction(action, "", "", "")
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
rule := &dataprovider.EventRule{
|
|
|
|
Name: "rule",
|
|
|
|
Trigger: dataprovider.EventTriggerFsEvent,
|
|
|
|
Conditions: dataprovider.EventConditions{
|
|
|
|
FsEvents: []string{operationUpload},
|
|
|
|
},
|
|
|
|
Actions: []dataprovider.EventAction{
|
|
|
|
{
|
|
|
|
BaseEventAction: dataprovider.BaseEventAction{
|
|
|
|
Name: action.Name,
|
|
|
|
},
|
|
|
|
Order: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.AddEventRule(rule, "", "", "")
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
eventManager.RLock()
|
|
|
|
assert.Len(t, eventManager.FsEvents, 1)
|
|
|
|
assert.Len(t, eventManager.ProviderEvents, 0)
|
|
|
|
assert.Len(t, eventManager.Schedules, 0)
|
|
|
|
assert.Len(t, eventManager.schedulesMapping, 0)
|
|
|
|
eventManager.RUnlock()
|
|
|
|
|
|
|
|
rule.Trigger = dataprovider.EventTriggerProviderEvent
|
|
|
|
rule.Conditions = dataprovider.EventConditions{
|
|
|
|
ProviderEvents: []string{"add"},
|
|
|
|
}
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.UpdateEventRule(rule, "", "", "")
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
eventManager.RLock()
|
|
|
|
assert.Len(t, eventManager.FsEvents, 0)
|
|
|
|
assert.Len(t, eventManager.ProviderEvents, 1)
|
|
|
|
assert.Len(t, eventManager.Schedules, 0)
|
|
|
|
assert.Len(t, eventManager.schedulesMapping, 0)
|
|
|
|
eventManager.RUnlock()
|
|
|
|
|
|
|
|
rule.Trigger = dataprovider.EventTriggerSchedule
|
|
|
|
rule.Conditions = dataprovider.EventConditions{
|
|
|
|
Schedules: []dataprovider.Schedule{
|
|
|
|
{
|
|
|
|
Hours: "0",
|
|
|
|
DayOfWeek: "*",
|
|
|
|
DayOfMonth: "*",
|
|
|
|
Month: "*",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
rule.DeletedAt = util.GetTimeAsMsSinceEpoch(time.Now().Add(-12 * time.Hour))
|
|
|
|
eventManager.addUpdateRuleInternal(*rule)
|
|
|
|
|
|
|
|
eventManager.RLock()
|
|
|
|
assert.Len(t, eventManager.FsEvents, 0)
|
|
|
|
assert.Len(t, eventManager.ProviderEvents, 0)
|
|
|
|
assert.Len(t, eventManager.Schedules, 0)
|
|
|
|
assert.Len(t, eventManager.schedulesMapping, 0)
|
|
|
|
eventManager.RUnlock()
|
|
|
|
|
|
|
|
assert.Eventually(t, func() bool {
|
|
|
|
_, err = dataprovider.EventRuleExists(rule.Name)
|
|
|
|
_, ok := err.(*util.RecordNotFoundError)
|
|
|
|
return ok
|
|
|
|
}, 2*time.Second, 100*time.Millisecond)
|
|
|
|
|
|
|
|
rule.DeletedAt = 0
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.AddEventRule(rule, "", "", "")
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
eventManager.RLock()
|
|
|
|
assert.Len(t, eventManager.FsEvents, 0)
|
|
|
|
assert.Len(t, eventManager.ProviderEvents, 0)
|
|
|
|
assert.Len(t, eventManager.Schedules, 1)
|
|
|
|
assert.Len(t, eventManager.schedulesMapping, 1)
|
|
|
|
eventManager.RUnlock()
|
|
|
|
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.DeleteEventRule(rule.Name, "", "", "")
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
eventManager.RLock()
|
|
|
|
assert.Len(t, eventManager.FsEvents, 0)
|
|
|
|
assert.Len(t, eventManager.ProviderEvents, 0)
|
|
|
|
assert.Len(t, eventManager.Schedules, 0)
|
|
|
|
assert.Len(t, eventManager.schedulesMapping, 0)
|
|
|
|
eventManager.RUnlock()
|
|
|
|
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.DeleteEventAction(action.Name, "", "", "")
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
stopEventScheduler()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEventManagerErrors(t *testing.T) {
|
|
|
|
startEventScheduler()
|
|
|
|
providerConf := dataprovider.GetProviderConfig()
|
|
|
|
err := dataprovider.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-08-10 16:41:59 +00:00
|
|
|
params := EventParams{
|
|
|
|
sender: "sender",
|
|
|
|
}
|
|
|
|
_, err = params.getUsers()
|
|
|
|
assert.Error(t, err)
|
|
|
|
_, err = params.getFolders()
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.Error(t, err)
|
2022-08-10 16:41:59 +00:00
|
|
|
|
2022-08-29 17:03:31 +00:00
|
|
|
err = executeUsersQuotaResetRuleAction(dataprovider.ConditionOptions{}, &EventParams{})
|
2022-08-10 16:41:59 +00:00
|
|
|
assert.Error(t, err)
|
2022-08-29 17:03:31 +00:00
|
|
|
err = executeFoldersQuotaResetRuleAction(dataprovider.ConditionOptions{}, &EventParams{})
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.Error(t, err)
|
2022-08-29 17:03:31 +00:00
|
|
|
err = executeTransferQuotaResetRuleAction(dataprovider.ConditionOptions{}, &EventParams{})
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.Error(t, err)
|
2022-09-26 17:00:34 +00:00
|
|
|
err = executeMetadataCheckRuleAction(dataprovider.ConditionOptions{}, &EventParams{})
|
|
|
|
assert.Error(t, err)
|
2022-08-29 17:03:31 +00:00
|
|
|
err = executeDeleteFsRuleAction(nil, nil, dataprovider.ConditionOptions{}, &EventParams{})
|
2022-08-19 13:04:00 +00:00
|
|
|
assert.Error(t, err)
|
2022-08-29 17:03:31 +00:00
|
|
|
err = executeMkdirFsRuleAction(nil, nil, dataprovider.ConditionOptions{}, &EventParams{})
|
2022-08-19 13:04:00 +00:00
|
|
|
assert.Error(t, err)
|
2022-08-29 17:03:31 +00:00
|
|
|
err = executeRenameFsRuleAction(nil, nil, dataprovider.ConditionOptions{}, &EventParams{})
|
2022-08-19 13:04:00 +00:00
|
|
|
assert.Error(t, err)
|
2022-08-29 17:03:31 +00:00
|
|
|
err = executeExistFsRuleAction(nil, nil, dataprovider.ConditionOptions{}, &EventParams{})
|
2022-08-20 12:13:43 +00:00
|
|
|
assert.Error(t, err)
|
2022-12-27 17:51:53 +00:00
|
|
|
err = executeCopyFsRuleAction(nil, nil, dataprovider.ConditionOptions{}, &EventParams{})
|
|
|
|
assert.Error(t, err)
|
2022-10-10 16:53:58 +00:00
|
|
|
err = executeCompressFsRuleAction(dataprovider.EventActionFsCompress{}, nil, dataprovider.ConditionOptions{}, &EventParams{})
|
|
|
|
assert.Error(t, err)
|
2022-12-16 17:51:29 +00:00
|
|
|
err = executePwdExpirationCheckRuleAction(dataprovider.EventActionPasswordExpiration{},
|
|
|
|
dataprovider.ConditionOptions{}, &EventParams{})
|
|
|
|
assert.Error(t, err)
|
2022-08-19 13:04:00 +00:00
|
|
|
|
|
|
|
groupName := "agroup"
|
2022-12-16 17:51:29 +00:00
|
|
|
err = executeQuotaResetForUser(&dataprovider.User{
|
2022-08-04 19:50:38 +00:00
|
|
|
Groups: []sdk.GroupMapping{
|
|
|
|
{
|
2022-08-19 13:04:00 +00:00
|
|
|
Name: groupName,
|
2022-08-04 19:50:38 +00:00
|
|
|
Type: sdk.GroupTypePrimary,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
2022-12-16 17:51:29 +00:00
|
|
|
err = executeMetadataCheckForUser(&dataprovider.User{
|
2022-09-26 17:00:34 +00:00
|
|
|
Groups: []sdk.GroupMapping{
|
|
|
|
{
|
|
|
|
Name: groupName,
|
|
|
|
Type: sdk.GroupTypePrimary,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
2022-08-04 19:50:38 +00:00
|
|
|
err = executeDataRetentionCheckForUser(dataprovider.User{
|
|
|
|
Groups: []sdk.GroupMapping{
|
|
|
|
{
|
2022-08-19 13:04:00 +00:00
|
|
|
Name: groupName,
|
2022-08-04 19:50:38 +00:00
|
|
|
Type: sdk.GroupTypePrimary,
|
|
|
|
},
|
|
|
|
},
|
2022-09-06 17:09:23 +00:00
|
|
|
}, nil, &EventParams{}, "")
|
2022-08-04 19:50:38 +00:00
|
|
|
assert.Error(t, err)
|
2022-08-19 13:04:00 +00:00
|
|
|
err = executeDeleteFsActionForUser(nil, nil, dataprovider.User{
|
|
|
|
Groups: []sdk.GroupMapping{
|
|
|
|
{
|
|
|
|
Name: groupName,
|
|
|
|
Type: sdk.GroupTypePrimary,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = executeMkDirsFsActionForUser(nil, nil, dataprovider.User{
|
|
|
|
Groups: []sdk.GroupMapping{
|
|
|
|
{
|
|
|
|
Name: groupName,
|
|
|
|
Type: sdk.GroupTypePrimary,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = executeRenameFsActionForUser(nil, nil, dataprovider.User{
|
|
|
|
Groups: []sdk.GroupMapping{
|
|
|
|
{
|
|
|
|
Name: groupName,
|
|
|
|
Type: sdk.GroupTypePrimary,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
2022-08-20 12:13:43 +00:00
|
|
|
err = executeExistFsActionForUser(nil, nil, dataprovider.User{
|
|
|
|
Groups: []sdk.GroupMapping{
|
|
|
|
{
|
|
|
|
Name: groupName,
|
|
|
|
Type: sdk.GroupTypePrimary,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
2022-12-27 17:51:53 +00:00
|
|
|
err = executeCopyFsActionForUser(nil, nil, dataprovider.User{
|
|
|
|
Groups: []sdk.GroupMapping{
|
|
|
|
{
|
|
|
|
Name: groupName,
|
|
|
|
Type: sdk.GroupTypePrimary,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
2022-10-10 16:53:58 +00:00
|
|
|
err = executeCompressFsActionForUser(dataprovider.EventActionFsCompress{}, nil, dataprovider.User{
|
|
|
|
Groups: []sdk.GroupMapping{
|
|
|
|
{
|
|
|
|
Name: groupName,
|
|
|
|
Type: sdk.GroupTypePrimary,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
2022-12-16 17:51:29 +00:00
|
|
|
err = executePwdExpirationCheckForUser(&dataprovider.User{
|
|
|
|
Groups: []sdk.GroupMapping{
|
|
|
|
{
|
|
|
|
Name: groupName,
|
|
|
|
Type: sdk.GroupTypePrimary,
|
|
|
|
},
|
|
|
|
}}, dataprovider.EventActionPasswordExpiration{})
|
|
|
|
assert.Error(t, err)
|
2022-08-04 19:50:38 +00:00
|
|
|
|
2022-09-03 14:29:07 +00:00
|
|
|
_, _, err = getHTTPRuleActionBody(dataprovider.EventActionHTTPConfig{
|
|
|
|
Method: http.MethodPost,
|
|
|
|
Parts: []dataprovider.HTTPPart{
|
|
|
|
{
|
|
|
|
Name: "p1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil, nil, dataprovider.User{
|
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "u",
|
|
|
|
},
|
|
|
|
Groups: []sdk.GroupMapping{
|
|
|
|
{
|
|
|
|
Name: groupName,
|
|
|
|
Type: sdk.GroupTypePrimary,
|
|
|
|
},
|
|
|
|
},
|
2022-09-06 17:09:23 +00:00
|
|
|
}, &EventParams{})
|
2022-09-03 14:29:07 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
|
2022-08-04 19:50:38 +00:00
|
|
|
dataRetentionAction := dataprovider.BaseEventAction{
|
|
|
|
Type: dataprovider.ActionTypeDataRetentionCheck,
|
|
|
|
Options: dataprovider.BaseEventActionOptions{
|
|
|
|
RetentionConfig: dataprovider.EventActionDataRetentionConfig{
|
|
|
|
Folders: []dataprovider.FolderRetention{
|
|
|
|
{
|
|
|
|
Path: "/",
|
|
|
|
Retention: 24,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-08-29 17:03:31 +00:00
|
|
|
err = executeRuleAction(dataRetentionAction, &EventParams{}, dataprovider.ConditionOptions{
|
2022-08-04 19:50:38 +00:00
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: "username1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "unable to get users")
|
|
|
|
}
|
2022-08-01 16:48:54 +00:00
|
|
|
|
|
|
|
eventManager.loadRules()
|
|
|
|
|
|
|
|
eventManager.RLock()
|
|
|
|
assert.Len(t, eventManager.FsEvents, 0)
|
|
|
|
assert.Len(t, eventManager.ProviderEvents, 0)
|
|
|
|
assert.Len(t, eventManager.Schedules, 0)
|
|
|
|
eventManager.RUnlock()
|
|
|
|
|
|
|
|
// rule with invalid trigger
|
|
|
|
eventManager.addUpdateRuleInternal(dataprovider.EventRule{
|
|
|
|
Name: "test rule",
|
|
|
|
Trigger: -1,
|
|
|
|
})
|
|
|
|
|
|
|
|
eventManager.RLock()
|
|
|
|
assert.Len(t, eventManager.FsEvents, 0)
|
|
|
|
assert.Len(t, eventManager.ProviderEvents, 0)
|
|
|
|
assert.Len(t, eventManager.Schedules, 0)
|
|
|
|
eventManager.RUnlock()
|
|
|
|
// rule with invalid cronspec
|
|
|
|
eventManager.addUpdateRuleInternal(dataprovider.EventRule{
|
|
|
|
Name: "test rule",
|
|
|
|
Trigger: dataprovider.EventTriggerSchedule,
|
|
|
|
Conditions: dataprovider.EventConditions{
|
|
|
|
Schedules: []dataprovider.Schedule{
|
|
|
|
{
|
|
|
|
Hours: "1000",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
eventManager.RLock()
|
|
|
|
assert.Len(t, eventManager.FsEvents, 0)
|
|
|
|
assert.Len(t, eventManager.ProviderEvents, 0)
|
|
|
|
assert.Len(t, eventManager.Schedules, 0)
|
|
|
|
eventManager.RUnlock()
|
|
|
|
|
|
|
|
err = dataprovider.Initialize(providerConf, configDir, true)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
stopEventScheduler()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEventRuleActions(t *testing.T) {
|
|
|
|
actionName := "test rule action"
|
|
|
|
action := dataprovider.BaseEventAction{
|
|
|
|
Name: actionName,
|
|
|
|
Type: dataprovider.ActionTypeBackup,
|
|
|
|
}
|
2022-08-29 17:03:31 +00:00
|
|
|
err := executeRuleAction(action, &EventParams{}, dataprovider.ConditionOptions{})
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
action.Type = -1
|
2022-08-29 17:03:31 +00:00
|
|
|
err = executeRuleAction(action, &EventParams{}, dataprovider.ConditionOptions{})
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
action = dataprovider.BaseEventAction{
|
|
|
|
Name: actionName,
|
|
|
|
Type: dataprovider.ActionTypeHTTP,
|
|
|
|
Options: dataprovider.BaseEventActionOptions{
|
|
|
|
HTTPConfig: dataprovider.EventActionHTTPConfig{
|
|
|
|
Endpoint: "http://foo\x7f.com/", // invalid URL
|
|
|
|
SkipTLSVerify: true,
|
|
|
|
Body: "{{ObjectData}}",
|
|
|
|
Method: http.MethodPost,
|
|
|
|
QueryParameters: []dataprovider.KeyValue{
|
|
|
|
{
|
|
|
|
Key: "param",
|
|
|
|
Value: "value",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Timeout: 5,
|
|
|
|
Headers: []dataprovider.KeyValue{
|
|
|
|
{
|
|
|
|
Key: "Content-Type",
|
|
|
|
Value: "application/json",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Username: "httpuser",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
action.Options.SetEmptySecretsIfNil()
|
2022-08-29 17:03:31 +00:00
|
|
|
err = executeRuleAction(action, &EventParams{}, dataprovider.ConditionOptions{})
|
2022-08-01 16:48:54 +00:00
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "invalid endpoint")
|
|
|
|
}
|
|
|
|
action.Options.HTTPConfig.Endpoint = fmt.Sprintf("http://%v", httpAddr)
|
2022-08-29 17:03:31 +00:00
|
|
|
params := &EventParams{
|
2022-08-01 16:48:54 +00:00
|
|
|
Name: "a",
|
|
|
|
Object: &dataprovider.User{
|
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "test user",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = executeRuleAction(action, params, dataprovider.ConditionOptions{})
|
|
|
|
assert.NoError(t, err)
|
2022-09-03 14:29:07 +00:00
|
|
|
action.Options.HTTPConfig.Method = http.MethodGet
|
|
|
|
err = executeRuleAction(action, params, dataprovider.ConditionOptions{})
|
|
|
|
assert.NoError(t, err)
|
2022-08-01 16:48:54 +00:00
|
|
|
action.Options.HTTPConfig.Endpoint = fmt.Sprintf("http://%v/404", httpAddr)
|
|
|
|
err = executeRuleAction(action, params, dataprovider.ConditionOptions{})
|
|
|
|
if assert.Error(t, err) {
|
2022-08-29 17:03:31 +00:00
|
|
|
assert.Contains(t, err.Error(), "unexpected status code: 404")
|
2022-08-01 16:48:54 +00:00
|
|
|
}
|
|
|
|
action.Options.HTTPConfig.Endpoint = "http://invalid:1234"
|
|
|
|
err = executeRuleAction(action, params, dataprovider.ConditionOptions{})
|
|
|
|
assert.Error(t, err)
|
|
|
|
action.Options.HTTPConfig.QueryParameters = nil
|
|
|
|
action.Options.HTTPConfig.Endpoint = "http://bar\x7f.com/"
|
|
|
|
err = executeRuleAction(action, params, dataprovider.ConditionOptions{})
|
|
|
|
assert.Error(t, err)
|
|
|
|
action.Options.HTTPConfig.Password = kms.NewSecret(sdkkms.SecretStatusSecretBox, "payload", "key", "data")
|
|
|
|
err = executeRuleAction(action, params, dataprovider.ConditionOptions{})
|
|
|
|
if assert.Error(t, err) {
|
2022-09-03 14:29:07 +00:00
|
|
|
assert.Contains(t, err.Error(), "unable to decrypt HTTP password")
|
|
|
|
}
|
|
|
|
action.Options.HTTPConfig.Password = kms.NewEmptySecret()
|
|
|
|
action.Options.HTTPConfig.Body = ""
|
|
|
|
action.Options.HTTPConfig.Parts = []dataprovider.HTTPPart{
|
|
|
|
{
|
|
|
|
Name: "p1",
|
|
|
|
Filepath: "path",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = executeRuleAction(action, params, dataprovider.ConditionOptions{})
|
2022-10-10 16:53:58 +00:00
|
|
|
assert.Contains(t, getErrorString(err), "error getting user")
|
|
|
|
|
2022-09-03 14:29:07 +00:00
|
|
|
action.Options.HTTPConfig.Parts = nil
|
|
|
|
action.Options.HTTPConfig.Body = "{{ObjectData}}"
|
2022-08-01 16:48:54 +00:00
|
|
|
// test disk and transfer quota reset
|
|
|
|
username1 := "user1"
|
|
|
|
username2 := "user2"
|
|
|
|
user1 := dataprovider.User{
|
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: username1,
|
|
|
|
HomeDir: filepath.Join(os.TempDir(), username1),
|
|
|
|
Status: 1,
|
|
|
|
Permissions: map[string][]string{
|
|
|
|
"/": {dataprovider.PermAny},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
user2 := dataprovider.User{
|
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: username2,
|
|
|
|
HomeDir: filepath.Join(os.TempDir(), username2),
|
|
|
|
Status: 1,
|
|
|
|
Permissions: map[string][]string{
|
|
|
|
"/": {dataprovider.PermAny},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-12-16 17:51:29 +00:00
|
|
|
user2.Filters.PasswordExpiration = 10
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.AddUser(&user1, "", "", "")
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.NoError(t, err)
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.AddUser(&user2, "", "", "")
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-12-16 17:51:29 +00:00
|
|
|
err = executePwdExpirationCheckRuleAction(dataprovider.EventActionPasswordExpiration{
|
|
|
|
Threshold: 20,
|
|
|
|
}, dataprovider.ConditionOptions{
|
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: user2.Username,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, &EventParams{})
|
|
|
|
// smtp not configured
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
2022-08-01 16:48:54 +00:00
|
|
|
action = dataprovider.BaseEventAction{
|
|
|
|
Type: dataprovider.ActionTypeUserQuotaReset,
|
|
|
|
}
|
2022-08-29 17:03:31 +00:00
|
|
|
err = executeRuleAction(action, &EventParams{}, dataprovider.ConditionOptions{
|
2022-08-01 16:48:54 +00:00
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: username1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Error(t, err) // no home dir
|
|
|
|
// create the home dir
|
|
|
|
err = os.MkdirAll(user1.GetHomeDir(), os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.WriteFile(filepath.Join(user1.GetHomeDir(), "file.txt"), []byte("user"), 0666)
|
|
|
|
assert.NoError(t, err)
|
2022-08-29 17:03:31 +00:00
|
|
|
err = executeRuleAction(action, &EventParams{}, dataprovider.ConditionOptions{
|
2022-08-01 16:48:54 +00:00
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: username1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
2022-11-16 18:04:50 +00:00
|
|
|
userGet, err := dataprovider.UserExists(username1, "")
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, userGet.UsedQuotaFiles)
|
|
|
|
assert.Equal(t, int64(4), userGet.UsedQuotaSize)
|
|
|
|
// simulate another quota scan in progress
|
2022-11-16 18:04:50 +00:00
|
|
|
assert.True(t, QuotaScans.AddUserQuotaScan(username1, ""))
|
2022-08-29 17:03:31 +00:00
|
|
|
err = executeRuleAction(action, &EventParams{}, dataprovider.ConditionOptions{
|
2022-08-01 16:48:54 +00:00
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: username1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, QuotaScans.RemoveUserQuotaScan(username1))
|
2022-08-10 16:41:59 +00:00
|
|
|
// non matching pattern
|
2022-08-29 17:03:31 +00:00
|
|
|
err = executeRuleAction(action, &EventParams{}, dataprovider.ConditionOptions{
|
2022-08-10 16:41:59 +00:00
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: "don't match",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2022-10-10 16:53:58 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Contains(t, getErrorString(err), "no user quota reset executed")
|
2022-08-01 16:48:54 +00:00
|
|
|
|
2022-09-26 17:00:34 +00:00
|
|
|
action = dataprovider.BaseEventAction{
|
|
|
|
Type: dataprovider.ActionTypeMetadataCheck,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = executeRuleAction(action, &EventParams{}, dataprovider.ConditionOptions{
|
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: "don't match",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2022-10-10 16:53:58 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Contains(t, getErrorString(err), "no metadata check executed")
|
2022-09-26 17:00:34 +00:00
|
|
|
|
|
|
|
err = executeRuleAction(action, &EventParams{}, dataprovider.ConditionOptions{
|
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: username1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
// simulate another metadata check in progress
|
2022-11-16 18:04:50 +00:00
|
|
|
assert.True(t, ActiveMetadataChecks.Add(username1, ""))
|
2022-09-26 17:00:34 +00:00
|
|
|
err = executeRuleAction(action, &EventParams{}, dataprovider.ConditionOptions{
|
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: username1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, ActiveMetadataChecks.Remove(username1))
|
|
|
|
|
2022-08-04 19:50:38 +00:00
|
|
|
dataRetentionAction := dataprovider.BaseEventAction{
|
|
|
|
Type: dataprovider.ActionTypeDataRetentionCheck,
|
|
|
|
Options: dataprovider.BaseEventActionOptions{
|
|
|
|
RetentionConfig: dataprovider.EventActionDataRetentionConfig{
|
|
|
|
Folders: []dataprovider.FolderRetention{
|
|
|
|
{
|
|
|
|
Path: "",
|
|
|
|
Retention: 24,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-08-29 17:03:31 +00:00
|
|
|
err = executeRuleAction(dataRetentionAction, &EventParams{}, dataprovider.ConditionOptions{
|
2022-08-04 19:50:38 +00:00
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: username1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Error(t, err) // invalid config, no folder path specified
|
|
|
|
retentionDir := "testretention"
|
|
|
|
dataRetentionAction = dataprovider.BaseEventAction{
|
|
|
|
Type: dataprovider.ActionTypeDataRetentionCheck,
|
|
|
|
Options: dataprovider.BaseEventActionOptions{
|
|
|
|
RetentionConfig: dataprovider.EventActionDataRetentionConfig{
|
|
|
|
Folders: []dataprovider.FolderRetention{
|
|
|
|
{
|
|
|
|
Path: path.Join("/", retentionDir),
|
|
|
|
Retention: 24,
|
|
|
|
DeleteEmptyDirs: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// create some test files
|
|
|
|
file1 := filepath.Join(user1.GetHomeDir(), "file1.txt")
|
|
|
|
file2 := filepath.Join(user1.GetHomeDir(), retentionDir, "file2.txt")
|
|
|
|
file3 := filepath.Join(user1.GetHomeDir(), retentionDir, "file3.txt")
|
|
|
|
file4 := filepath.Join(user1.GetHomeDir(), retentionDir, "sub", "file4.txt")
|
|
|
|
|
|
|
|
err = os.MkdirAll(filepath.Dir(file4), os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
for _, f := range []string{file1, file2, file3, file4} {
|
|
|
|
err = os.WriteFile(f, []byte(""), 0666)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
timeBeforeRetention := time.Now().Add(-48 * time.Hour)
|
|
|
|
err = os.Chtimes(file1, timeBeforeRetention, timeBeforeRetention)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Chtimes(file2, timeBeforeRetention, timeBeforeRetention)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Chtimes(file4, timeBeforeRetention, timeBeforeRetention)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-08-29 17:03:31 +00:00
|
|
|
err = executeRuleAction(dataRetentionAction, &EventParams{}, dataprovider.ConditionOptions{
|
2022-08-04 19:50:38 +00:00
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: username1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.FileExists(t, file1)
|
|
|
|
assert.NoFileExists(t, file2)
|
|
|
|
assert.FileExists(t, file3)
|
|
|
|
assert.NoDirExists(t, filepath.Dir(file4))
|
|
|
|
// simulate another check in progress
|
|
|
|
c := RetentionChecks.Add(RetentionCheck{}, &user1)
|
|
|
|
assert.NotNil(t, c)
|
2022-08-29 17:03:31 +00:00
|
|
|
err = executeRuleAction(dataRetentionAction, &EventParams{}, dataprovider.ConditionOptions{
|
2022-08-04 19:50:38 +00:00
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: username1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
|
|
|
RetentionChecks.remove(user1.Username)
|
|
|
|
|
2022-08-29 17:03:31 +00:00
|
|
|
err = executeRuleAction(dataRetentionAction, &EventParams{}, dataprovider.ConditionOptions{
|
2022-08-10 16:41:59 +00:00
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: "no match",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2022-10-10 16:53:58 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Contains(t, getErrorString(err), "no retention check executed")
|
|
|
|
|
2022-08-20 12:13:43 +00:00
|
|
|
// test file exists action
|
|
|
|
action = dataprovider.BaseEventAction{
|
|
|
|
Type: dataprovider.ActionTypeFilesystem,
|
|
|
|
Options: dataprovider.BaseEventActionOptions{
|
|
|
|
FsConfig: dataprovider.EventActionFilesystemConfig{
|
|
|
|
Type: dataprovider.FilesystemActionExist,
|
|
|
|
Exist: []string{"/file1.txt", path.Join("/", retentionDir, "file3.txt")},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-08-29 17:03:31 +00:00
|
|
|
err = executeRuleAction(action, &EventParams{}, dataprovider.ConditionOptions{
|
2022-08-20 12:13:43 +00:00
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: "no match",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2022-10-10 16:53:58 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Contains(t, getErrorString(err), "no existence check executed")
|
|
|
|
|
2022-08-29 17:03:31 +00:00
|
|
|
err = executeRuleAction(action, &EventParams{}, dataprovider.ConditionOptions{
|
2022-08-20 12:13:43 +00:00
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: username1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
action.Options.FsConfig.Exist = []string{"/file1.txt", path.Join("/", retentionDir, "file2.txt")}
|
2022-08-29 17:03:31 +00:00
|
|
|
err = executeRuleAction(action, &EventParams{}, dataprovider.ConditionOptions{
|
2022-08-20 12:13:43 +00:00
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: username1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
2022-08-10 16:41:59 +00:00
|
|
|
|
2022-08-01 16:48:54 +00:00
|
|
|
err = os.RemoveAll(user1.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = dataprovider.UpdateUserTransferQuota(&user1, 100, 100, true)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
action.Type = dataprovider.ActionTypeTransferQuotaReset
|
2022-08-29 17:03:31 +00:00
|
|
|
err = executeRuleAction(action, &EventParams{}, dataprovider.ConditionOptions{
|
2022-08-01 16:48:54 +00:00
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: username1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
2022-11-16 18:04:50 +00:00
|
|
|
userGet, err = dataprovider.UserExists(username1, "")
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(0), userGet.UsedDownloadDataTransfer)
|
|
|
|
assert.Equal(t, int64(0), userGet.UsedUploadDataTransfer)
|
|
|
|
|
2022-08-29 17:03:31 +00:00
|
|
|
err = executeRuleAction(action, &EventParams{}, dataprovider.ConditionOptions{
|
2022-08-10 16:41:59 +00:00
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: "no match",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2022-10-10 16:53:58 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Contains(t, getErrorString(err), "no transfer quota reset executed")
|
|
|
|
|
2022-08-19 13:04:00 +00:00
|
|
|
action.Type = dataprovider.ActionTypeFilesystem
|
|
|
|
action.Options = dataprovider.BaseEventActionOptions{
|
|
|
|
FsConfig: dataprovider.EventActionFilesystemConfig{
|
|
|
|
Type: dataprovider.FilesystemActionRename,
|
|
|
|
Renames: []dataprovider.KeyValue{
|
|
|
|
{
|
|
|
|
Key: "/source",
|
|
|
|
Value: "/target",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-08-29 17:03:31 +00:00
|
|
|
err = executeRuleAction(action, &EventParams{}, dataprovider.ConditionOptions{
|
2022-08-19 13:04:00 +00:00
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: "no match",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2022-10-10 16:53:58 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Contains(t, getErrorString(err), "no rename executed")
|
|
|
|
|
2022-08-19 13:04:00 +00:00
|
|
|
action.Options = dataprovider.BaseEventActionOptions{
|
|
|
|
FsConfig: dataprovider.EventActionFilesystemConfig{
|
|
|
|
Type: dataprovider.FilesystemActionDelete,
|
|
|
|
Deletes: []string{"/dir1"},
|
|
|
|
},
|
|
|
|
}
|
2022-08-29 17:03:31 +00:00
|
|
|
err = executeRuleAction(action, &EventParams{}, dataprovider.ConditionOptions{
|
2022-08-19 13:04:00 +00:00
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: "no match",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2022-10-10 16:53:58 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Contains(t, getErrorString(err), "no delete executed")
|
|
|
|
|
2022-08-19 13:04:00 +00:00
|
|
|
action.Options = dataprovider.BaseEventActionOptions{
|
|
|
|
FsConfig: dataprovider.EventActionFilesystemConfig{
|
|
|
|
Type: dataprovider.FilesystemActionMkdirs,
|
|
|
|
Deletes: []string{"/dir1"},
|
|
|
|
},
|
|
|
|
}
|
2022-08-29 17:03:31 +00:00
|
|
|
err = executeRuleAction(action, &EventParams{}, dataprovider.ConditionOptions{
|
2022-08-19 13:04:00 +00:00
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: "no match",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2022-10-10 16:53:58 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Contains(t, getErrorString(err), "no mkdir executed")
|
|
|
|
|
|
|
|
action.Options = dataprovider.BaseEventActionOptions{
|
|
|
|
FsConfig: dataprovider.EventActionFilesystemConfig{
|
|
|
|
Type: dataprovider.FilesystemActionCompress,
|
|
|
|
Compress: dataprovider.EventActionFsCompress{
|
|
|
|
Name: "test.zip",
|
|
|
|
Paths: []string{"/{{VirtualPath}}"},
|
|
|
|
},
|
|
|
|
},
|
2022-08-19 13:04:00 +00:00
|
|
|
}
|
2022-10-10 16:53:58 +00:00
|
|
|
err = executeRuleAction(action, &EventParams{}, dataprovider.ConditionOptions{
|
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: "no match",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Contains(t, getErrorString(err), "no file/folder compressed")
|
|
|
|
|
|
|
|
err = executeRuleAction(action, &EventParams{}, dataprovider.ConditionOptions{
|
|
|
|
GroupNames: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: "no match",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Contains(t, getErrorString(err), "no file/folder compressed")
|
2022-08-10 16:41:59 +00:00
|
|
|
|
2022-11-16 18:04:50 +00:00
|
|
|
err = dataprovider.DeleteUser(username1, "", "", "")
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.NoError(t, err)
|
2022-11-16 18:04:50 +00:00
|
|
|
err = dataprovider.DeleteUser(username2, "", "", "")
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
// test folder quota reset
|
|
|
|
foldername1 := "f1"
|
|
|
|
foldername2 := "f2"
|
|
|
|
folder1 := vfs.BaseVirtualFolder{
|
|
|
|
Name: foldername1,
|
|
|
|
MappedPath: filepath.Join(os.TempDir(), foldername1),
|
|
|
|
}
|
|
|
|
folder2 := vfs.BaseVirtualFolder{
|
|
|
|
Name: foldername2,
|
|
|
|
MappedPath: filepath.Join(os.TempDir(), foldername2),
|
|
|
|
}
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.AddFolder(&folder1, "", "", "")
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.NoError(t, err)
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.AddFolder(&folder2, "", "", "")
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
action = dataprovider.BaseEventAction{
|
|
|
|
Type: dataprovider.ActionTypeFolderQuotaReset,
|
|
|
|
}
|
2022-08-29 17:03:31 +00:00
|
|
|
err = executeRuleAction(action, &EventParams{}, dataprovider.ConditionOptions{
|
2022-08-01 16:48:54 +00:00
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: foldername1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Error(t, err) // no home dir
|
|
|
|
err = os.MkdirAll(folder1.MappedPath, os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.WriteFile(filepath.Join(folder1.MappedPath, "file.txt"), []byte("folder"), 0666)
|
|
|
|
assert.NoError(t, err)
|
2022-08-29 17:03:31 +00:00
|
|
|
err = executeRuleAction(action, &EventParams{}, dataprovider.ConditionOptions{
|
2022-08-01 16:48:54 +00:00
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: foldername1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
folderGet, err := dataprovider.GetFolderByName(foldername1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, folderGet.UsedQuotaFiles)
|
|
|
|
assert.Equal(t, int64(6), folderGet.UsedQuotaSize)
|
|
|
|
// simulate another quota scan in progress
|
|
|
|
assert.True(t, QuotaScans.AddVFolderQuotaScan(foldername1))
|
2022-08-29 17:03:31 +00:00
|
|
|
err = executeRuleAction(action, &EventParams{}, dataprovider.ConditionOptions{
|
2022-08-01 16:48:54 +00:00
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: foldername1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, QuotaScans.RemoveVFolderQuotaScan(foldername1))
|
|
|
|
|
2022-08-29 17:03:31 +00:00
|
|
|
err = executeRuleAction(action, &EventParams{}, dataprovider.ConditionOptions{
|
2022-08-10 16:41:59 +00:00
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: "no folder match",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "no folder quota reset executed")
|
|
|
|
}
|
|
|
|
|
2022-09-03 14:29:07 +00:00
|
|
|
body, _, err := getHTTPRuleActionBody(dataprovider.EventActionHTTPConfig{
|
|
|
|
Method: http.MethodPost,
|
2022-09-06 17:09:23 +00:00
|
|
|
}, nil, nil, dataprovider.User{}, &EventParams{})
|
2022-09-03 14:29:07 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Nil(t, body)
|
|
|
|
|
2022-08-01 16:48:54 +00:00
|
|
|
err = os.RemoveAll(folder1.MappedPath)
|
|
|
|
assert.NoError(t, err)
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.DeleteFolder(foldername1, "", "", "")
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.NoError(t, err)
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.DeleteFolder(foldername2, "", "", "")
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2022-09-04 15:48:09 +00:00
|
|
|
func TestEventRuleActionsNoGroupMatching(t *testing.T) {
|
|
|
|
username := "test_user_action_group_matching"
|
|
|
|
user := dataprovider.User{
|
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: username,
|
|
|
|
Permissions: map[string][]string{
|
|
|
|
"/": {dataprovider.PermAny},
|
|
|
|
},
|
|
|
|
HomeDir: filepath.Join(os.TempDir(), username),
|
|
|
|
},
|
|
|
|
}
|
2022-12-03 10:45:27 +00:00
|
|
|
err := dataprovider.AddUser(&user, "", "", "")
|
2022-09-04 15:48:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
conditions := dataprovider.ConditionOptions{
|
|
|
|
GroupNames: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: "agroup",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = executeDeleteFsRuleAction(nil, nil, conditions, &EventParams{})
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "no delete executed")
|
|
|
|
}
|
|
|
|
err = executeMkdirFsRuleAction(nil, nil, conditions, &EventParams{})
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "no mkdir executed")
|
|
|
|
}
|
|
|
|
err = executeRenameFsRuleAction(nil, nil, conditions, &EventParams{})
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "no rename executed")
|
|
|
|
}
|
|
|
|
err = executeExistFsRuleAction(nil, nil, conditions, &EventParams{})
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "no existence check executed")
|
|
|
|
}
|
2022-12-27 17:51:53 +00:00
|
|
|
err = executeCopyFsRuleAction(nil, nil, conditions, &EventParams{})
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "no copy executed")
|
|
|
|
}
|
2022-09-04 15:48:09 +00:00
|
|
|
err = executeUsersQuotaResetRuleAction(conditions, &EventParams{})
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "no user quota reset executed")
|
|
|
|
}
|
2022-09-26 17:00:34 +00:00
|
|
|
err = executeMetadataCheckRuleAction(conditions, &EventParams{})
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "no metadata check executed")
|
|
|
|
}
|
2022-09-04 15:48:09 +00:00
|
|
|
err = executeTransferQuotaResetRuleAction(conditions, &EventParams{})
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "no transfer quota reset executed")
|
|
|
|
}
|
2022-09-06 17:09:23 +00:00
|
|
|
err = executeDataRetentionCheckRuleAction(dataprovider.EventActionDataRetentionConfig{}, conditions, &EventParams{}, "")
|
2022-09-04 15:48:09 +00:00
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "no retention check executed")
|
|
|
|
}
|
|
|
|
|
2022-11-16 18:04:50 +00:00
|
|
|
err = dataprovider.DeleteUser(username, "", "", "")
|
2022-09-04 15:48:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2022-08-22 17:04:17 +00:00
|
|
|
func TestGetFileContent(t *testing.T) {
|
|
|
|
username := "test_user_get_file_content"
|
|
|
|
user := dataprovider.User{
|
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: username,
|
|
|
|
Permissions: map[string][]string{
|
|
|
|
"/": {dataprovider.PermAny},
|
|
|
|
},
|
|
|
|
HomeDir: filepath.Join(os.TempDir(), username),
|
|
|
|
},
|
|
|
|
}
|
2022-12-03 10:45:27 +00:00
|
|
|
err := dataprovider.AddUser(&user, "", "", "")
|
2022-08-22 17:04:17 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.MkdirAll(user.GetHomeDir(), os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
fileContent := []byte("test file content")
|
|
|
|
err = os.WriteFile(filepath.Join(user.GetHomeDir(), "file.txt"), fileContent, 0666)
|
|
|
|
assert.NoError(t, err)
|
2023-01-15 14:28:31 +00:00
|
|
|
conn := NewBaseConnection(xid.New().String(), protocolEventAction, "", "", user)
|
2022-08-22 17:04:17 +00:00
|
|
|
replacer := strings.NewReplacer("old", "new")
|
2023-01-15 14:28:31 +00:00
|
|
|
files, err := getMailAttachments(conn, []string{"/file.txt"}, replacer)
|
2022-08-22 17:04:17 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, files, 1) {
|
2023-01-15 14:28:31 +00:00
|
|
|
var b bytes.Buffer
|
|
|
|
_, err = files[0].Writer(&b)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, fileContent, b.Bytes())
|
2022-08-22 17:04:17 +00:00
|
|
|
}
|
|
|
|
// missing file
|
2023-01-15 14:28:31 +00:00
|
|
|
_, err = getMailAttachments(conn, []string{"/file1.txt"}, replacer)
|
2022-08-22 17:04:17 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
// directory
|
2023-01-15 14:28:31 +00:00
|
|
|
_, err = getMailAttachments(conn, []string{"/"}, replacer)
|
2022-08-22 17:04:17 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
// files too large
|
2022-09-06 17:09:23 +00:00
|
|
|
content := make([]byte, maxAttachmentsSize/2+1)
|
2022-08-22 17:04:17 +00:00
|
|
|
_, err = rand.Read(content)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.WriteFile(filepath.Join(user.GetHomeDir(), "file1.txt"), content, 0666)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.WriteFile(filepath.Join(user.GetHomeDir(), "file2.txt"), content, 0666)
|
|
|
|
assert.NoError(t, err)
|
2023-01-15 14:28:31 +00:00
|
|
|
files, err = getMailAttachments(conn, []string{"/file1.txt"}, replacer)
|
2022-08-22 17:04:17 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, files, 1) {
|
2023-01-15 14:28:31 +00:00
|
|
|
var b bytes.Buffer
|
|
|
|
_, err = files[0].Writer(&b)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, content, b.Bytes())
|
2022-08-22 17:04:17 +00:00
|
|
|
}
|
2023-01-15 14:28:31 +00:00
|
|
|
_, err = getMailAttachments(conn, []string{"/file1.txt", "/file2.txt"}, replacer)
|
2022-08-22 17:04:17 +00:00
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "size too large")
|
|
|
|
}
|
|
|
|
// change the filesystem provider
|
|
|
|
user.FsConfig.Provider = sdk.CryptedFilesystemProvider
|
|
|
|
user.FsConfig.CryptConfig.Passphrase = kms.NewPlainSecret("pwd")
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.UpdateUser(&user, "", "", "")
|
2022-08-22 17:04:17 +00:00
|
|
|
assert.NoError(t, err)
|
2023-01-15 14:28:31 +00:00
|
|
|
conn = NewBaseConnection(xid.New().String(), protocolEventAction, "", "", user)
|
2022-08-22 17:04:17 +00:00
|
|
|
// the file is not encrypted so reading the encryption header will fail
|
2023-01-15 14:28:31 +00:00
|
|
|
files, err = getMailAttachments(conn, []string{"/file.txt"}, replacer)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, files, 1) {
|
|
|
|
var b bytes.Buffer
|
|
|
|
_, err = files[0].Writer(&b)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
2022-08-22 17:04:17 +00:00
|
|
|
|
2022-11-16 18:04:50 +00:00
|
|
|
err = dataprovider.DeleteUser(username, "", "", "")
|
2022-08-22 17:04:17 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2022-08-10 16:41:59 +00:00
|
|
|
func TestFilesystemActionErrors(t *testing.T) {
|
2022-08-29 17:03:31 +00:00
|
|
|
err := executeFsRuleAction(dataprovider.EventActionFilesystemConfig{}, dataprovider.ConditionOptions{}, &EventParams{})
|
2022-08-10 16:41:59 +00:00
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "unsupported filesystem action")
|
|
|
|
}
|
|
|
|
username := "test_user_for_actions"
|
|
|
|
testReplacer := strings.NewReplacer("old", "new")
|
|
|
|
user := dataprovider.User{
|
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: username,
|
|
|
|
Permissions: map[string][]string{
|
|
|
|
"/": {dataprovider.PermAny},
|
|
|
|
},
|
|
|
|
HomeDir: filepath.Join(os.TempDir(), username),
|
|
|
|
},
|
|
|
|
FsConfig: vfs.Filesystem{
|
|
|
|
Provider: sdk.SFTPFilesystemProvider,
|
|
|
|
SFTPConfig: vfs.SFTPFsConfig{
|
|
|
|
BaseSFTPFsConfig: sdk.BaseSFTPFsConfig{
|
|
|
|
Endpoint: "127.0.0.1:4022",
|
|
|
|
Username: username,
|
|
|
|
},
|
|
|
|
Password: kms.NewPlainSecret("pwd"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-08-22 17:04:17 +00:00
|
|
|
err = executeEmailRuleAction(dataprovider.EventActionEmailConfig{
|
|
|
|
Recipients: []string{"test@example.net"},
|
|
|
|
Subject: "subject",
|
|
|
|
Body: "body",
|
|
|
|
Attachments: []string{"/file.txt"},
|
2022-08-29 17:03:31 +00:00
|
|
|
}, &EventParams{
|
2022-08-22 17:04:17 +00:00
|
|
|
sender: username,
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
2022-08-10 16:41:59 +00:00
|
|
|
conn := NewBaseConnection("", protocolEventAction, "", "", user)
|
|
|
|
err = executeDeleteFileFsAction(conn, "", nil)
|
|
|
|
assert.Error(t, err)
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.AddUser(&user, "", "", "")
|
2022-08-10 16:41:59 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
// check root fs fails
|
2022-08-19 13:04:00 +00:00
|
|
|
err = executeDeleteFsActionForUser(nil, testReplacer, user)
|
2022-08-10 16:41:59 +00:00
|
|
|
assert.Error(t, err)
|
2022-08-19 13:04:00 +00:00
|
|
|
err = executeMkDirsFsActionForUser(nil, testReplacer, user)
|
2022-08-10 16:41:59 +00:00
|
|
|
assert.Error(t, err)
|
2022-08-19 13:04:00 +00:00
|
|
|
err = executeRenameFsActionForUser(nil, testReplacer, user)
|
2022-08-10 16:41:59 +00:00
|
|
|
assert.Error(t, err)
|
2022-08-20 12:13:43 +00:00
|
|
|
err = executeExistFsActionForUser(nil, testReplacer, user)
|
|
|
|
assert.Error(t, err)
|
2022-12-27 17:51:53 +00:00
|
|
|
err = executeCopyFsActionForUser(nil, testReplacer, user)
|
|
|
|
assert.Error(t, err)
|
2022-10-10 16:53:58 +00:00
|
|
|
err = executeCompressFsActionForUser(dataprovider.EventActionFsCompress{}, testReplacer, user)
|
|
|
|
assert.Error(t, err)
|
2022-12-27 17:51:53 +00:00
|
|
|
_, _, _, _, err = getFileWriter(conn, "/path.txt", -1) //nolint:dogsled
|
2022-10-10 16:53:58 +00:00
|
|
|
assert.Error(t, err)
|
2022-08-22 17:04:17 +00:00
|
|
|
err = executeEmailRuleAction(dataprovider.EventActionEmailConfig{
|
|
|
|
Recipients: []string{"test@example.net"},
|
|
|
|
Subject: "subject",
|
|
|
|
Body: "body",
|
|
|
|
Attachments: []string{"/file1.txt"},
|
2022-08-29 17:03:31 +00:00
|
|
|
}, &EventParams{
|
2022-08-22 17:04:17 +00:00
|
|
|
sender: username,
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
2023-01-15 14:28:31 +00:00
|
|
|
fn := getFileContentFn(NewBaseConnection("", protocolEventAction, "", "", user), "/f.txt", 1234)
|
|
|
|
var b bytes.Buffer
|
|
|
|
_, err = fn(&b)
|
2022-08-22 17:04:17 +00:00
|
|
|
assert.Error(t, err)
|
2022-09-03 14:29:07 +00:00
|
|
|
err = executeHTTPRuleAction(dataprovider.EventActionHTTPConfig{
|
|
|
|
Endpoint: "http://127.0.0.1:9999/",
|
|
|
|
Method: http.MethodPost,
|
|
|
|
Parts: []dataprovider.HTTPPart{
|
|
|
|
{
|
|
|
|
Name: "p1",
|
|
|
|
Filepath: "/filepath",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, &EventParams{
|
|
|
|
sender: username,
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
2022-08-10 16:41:59 +00:00
|
|
|
user.FsConfig.Provider = sdk.LocalFilesystemProvider
|
|
|
|
user.Permissions["/"] = []string{dataprovider.PermUpload}
|
2022-11-16 18:04:50 +00:00
|
|
|
err = dataprovider.DeleteUser(username, "", "", "")
|
2022-08-10 16:41:59 +00:00
|
|
|
assert.NoError(t, err)
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.AddUser(&user, "", "", "")
|
2022-08-10 16:41:59 +00:00
|
|
|
assert.NoError(t, err)
|
2022-08-19 13:04:00 +00:00
|
|
|
err = executeRenameFsActionForUser([]dataprovider.KeyValue{
|
2022-08-10 16:41:59 +00:00
|
|
|
{
|
|
|
|
Key: "/p1",
|
|
|
|
Value: "/p1",
|
|
|
|
},
|
2022-08-19 13:04:00 +00:00
|
|
|
}, testReplacer, user)
|
2022-08-10 16:41:59 +00:00
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "the rename source and target cannot be the same")
|
|
|
|
}
|
2022-08-19 13:04:00 +00:00
|
|
|
err = executeRuleAction(dataprovider.BaseEventAction{
|
|
|
|
Type: dataprovider.ActionTypeFilesystem,
|
|
|
|
Options: dataprovider.BaseEventActionOptions{
|
|
|
|
FsConfig: dataprovider.EventActionFilesystemConfig{
|
|
|
|
Type: dataprovider.FilesystemActionRename,
|
|
|
|
Renames: []dataprovider.KeyValue{
|
|
|
|
{
|
|
|
|
Key: "/p2",
|
|
|
|
Value: "/p2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-08-29 17:03:31 +00:00
|
|
|
}, &EventParams{}, dataprovider.ConditionOptions{
|
2022-08-19 13:04:00 +00:00
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: username,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
2022-08-10 16:41:59 +00:00
|
|
|
|
|
|
|
if runtime.GOOS != osWindows {
|
|
|
|
dirPath := filepath.Join(user.HomeDir, "adir", "sub")
|
|
|
|
err := os.MkdirAll(dirPath, os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
filePath := filepath.Join(dirPath, "f.dat")
|
2022-10-10 16:53:58 +00:00
|
|
|
err = os.WriteFile(filePath, []byte("test file content"), 0666)
|
2022-08-10 16:41:59 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Chmod(dirPath, 0001)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-08-19 13:04:00 +00:00
|
|
|
err = executeDeleteFsActionForUser([]string{"/adir/sub"}, testReplacer, user)
|
2022-08-10 16:41:59 +00:00
|
|
|
assert.Error(t, err)
|
2022-08-19 13:04:00 +00:00
|
|
|
err = executeDeleteFsActionForUser([]string{"/adir/sub/f.dat"}, testReplacer, user)
|
2022-08-10 16:41:59 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
err = os.Chmod(dirPath, 0555)
|
|
|
|
assert.NoError(t, err)
|
2022-08-19 13:04:00 +00:00
|
|
|
err = executeDeleteFsActionForUser([]string{"/adir/sub/f.dat"}, testReplacer, user)
|
2022-08-10 17:33:02 +00:00
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "unable to remove file")
|
|
|
|
}
|
2022-08-19 13:04:00 +00:00
|
|
|
err = executeRuleAction(dataprovider.BaseEventAction{
|
|
|
|
Type: dataprovider.ActionTypeFilesystem,
|
|
|
|
Options: dataprovider.BaseEventActionOptions{
|
|
|
|
FsConfig: dataprovider.EventActionFilesystemConfig{
|
|
|
|
Type: dataprovider.FilesystemActionDelete,
|
|
|
|
Deletes: []string{"/adir/sub/f.dat"},
|
|
|
|
},
|
|
|
|
},
|
2022-08-29 17:03:31 +00:00
|
|
|
}, &EventParams{}, dataprovider.ConditionOptions{
|
2022-08-19 13:04:00 +00:00
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: username,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
2022-08-10 17:33:02 +00:00
|
|
|
|
2022-08-19 13:04:00 +00:00
|
|
|
err = executeMkDirsFsActionForUser([]string{"/adir/sub/sub"}, testReplacer, user)
|
2022-08-10 17:33:02 +00:00
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "unable to create dir")
|
|
|
|
}
|
2022-08-19 13:04:00 +00:00
|
|
|
err = executeMkDirsFsActionForUser([]string{"/adir/sub/sub/sub"}, testReplacer, user)
|
2022-08-10 17:33:02 +00:00
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "unable to check parent dirs")
|
|
|
|
}
|
2022-08-10 16:41:59 +00:00
|
|
|
|
2022-08-19 13:04:00 +00:00
|
|
|
err = executeRuleAction(dataprovider.BaseEventAction{
|
|
|
|
Type: dataprovider.ActionTypeFilesystem,
|
|
|
|
Options: dataprovider.BaseEventActionOptions{
|
|
|
|
FsConfig: dataprovider.EventActionFilesystemConfig{
|
|
|
|
Type: dataprovider.FilesystemActionMkdirs,
|
|
|
|
MkDirs: []string{"/adir/sub/sub1"},
|
|
|
|
},
|
|
|
|
},
|
2022-08-29 17:03:31 +00:00
|
|
|
}, &EventParams{}, dataprovider.ConditionOptions{
|
2022-08-19 13:04:00 +00:00
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: username,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
2022-08-10 16:41:59 +00:00
|
|
|
err = os.Chmod(dirPath, os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
2022-10-10 16:53:58 +00:00
|
|
|
|
|
|
|
conn = NewBaseConnection("", protocolEventAction, "", "", user)
|
|
|
|
wr := &zipWriterWrapper{
|
|
|
|
Name: "test.zip",
|
|
|
|
Writer: zip.NewWriter(bytes.NewBuffer(nil)),
|
|
|
|
Entries: map[string]bool{},
|
|
|
|
}
|
|
|
|
err = addZipEntry(wr, conn, "/adir/sub/f.dat", "/adir/sub/sub")
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Contains(t, getErrorString(err), "is outside base dir")
|
2022-08-10 16:41:59 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 18:04:50 +00:00
|
|
|
err = dataprovider.DeleteUser(username, "", "", "")
|
2022-08-10 16:41:59 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2022-08-01 16:48:54 +00:00
|
|
|
func TestQuotaActionsWithQuotaTrackDisabled(t *testing.T) {
|
|
|
|
oldProviderConf := dataprovider.GetProviderConfig()
|
|
|
|
providerConf := dataprovider.GetProviderConfig()
|
|
|
|
providerConf.TrackQuota = 0
|
|
|
|
err := dataprovider.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = dataprovider.Initialize(providerConf, configDir, true)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
username := "u1"
|
|
|
|
user := dataprovider.User{
|
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: username,
|
|
|
|
HomeDir: filepath.Join(os.TempDir(), username),
|
|
|
|
Status: 1,
|
|
|
|
Permissions: map[string][]string{
|
|
|
|
"/": {dataprovider.PermAny},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
FsConfig: vfs.Filesystem{
|
|
|
|
Provider: sdk.LocalFilesystemProvider,
|
|
|
|
},
|
|
|
|
}
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.AddUser(&user, "", "", "")
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = os.MkdirAll(user.GetHomeDir(), os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = executeRuleAction(dataprovider.BaseEventAction{Type: dataprovider.ActionTypeUserQuotaReset},
|
2022-08-29 17:03:31 +00:00
|
|
|
&EventParams{}, dataprovider.ConditionOptions{
|
2022-08-01 16:48:54 +00:00
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: username,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
err = executeRuleAction(dataprovider.BaseEventAction{Type: dataprovider.ActionTypeTransferQuotaReset},
|
2022-08-29 17:03:31 +00:00
|
|
|
&EventParams{}, dataprovider.ConditionOptions{
|
2022-08-01 16:48:54 +00:00
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: username,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
2022-11-16 18:04:50 +00:00
|
|
|
err = dataprovider.DeleteUser(username, "", "", "")
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
foldername := "f1"
|
|
|
|
folder := vfs.BaseVirtualFolder{
|
|
|
|
Name: foldername,
|
|
|
|
MappedPath: filepath.Join(os.TempDir(), foldername),
|
|
|
|
}
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.AddFolder(&folder, "", "", "")
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.MkdirAll(folder.MappedPath, os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = executeRuleAction(dataprovider.BaseEventAction{Type: dataprovider.ActionTypeFolderQuotaReset},
|
2022-08-29 17:03:31 +00:00
|
|
|
&EventParams{}, dataprovider.ConditionOptions{
|
2022-08-01 16:48:54 +00:00
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: foldername,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
err = os.RemoveAll(folder.MappedPath)
|
|
|
|
assert.NoError(t, err)
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.DeleteFolder(foldername, "", "", "")
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = dataprovider.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = dataprovider.Initialize(oldProviderConf, configDir, true)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestScheduledActions(t *testing.T) {
|
|
|
|
startEventScheduler()
|
|
|
|
backupsPath := filepath.Join(os.TempDir(), "backups")
|
|
|
|
err := os.RemoveAll(backupsPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
action := &dataprovider.BaseEventAction{
|
|
|
|
Name: "action",
|
|
|
|
Type: dataprovider.ActionTypeBackup,
|
|
|
|
}
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.AddEventAction(action, "", "", "")
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
rule := &dataprovider.EventRule{
|
|
|
|
Name: "rule",
|
|
|
|
Trigger: dataprovider.EventTriggerSchedule,
|
|
|
|
Conditions: dataprovider.EventConditions{
|
|
|
|
Schedules: []dataprovider.Schedule{
|
|
|
|
{
|
|
|
|
Hours: "11",
|
|
|
|
DayOfWeek: "*",
|
|
|
|
DayOfMonth: "*",
|
|
|
|
Month: "*",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Actions: []dataprovider.EventAction{
|
|
|
|
{
|
|
|
|
BaseEventAction: dataprovider.BaseEventAction{
|
|
|
|
Name: action.Name,
|
|
|
|
},
|
|
|
|
Order: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
job := eventCronJob{
|
|
|
|
ruleName: rule.Name,
|
|
|
|
}
|
|
|
|
job.Run() // rule not found
|
|
|
|
assert.NoDirExists(t, backupsPath)
|
|
|
|
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.AddEventRule(rule, "", "", "")
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
job.Run()
|
|
|
|
assert.DirExists(t, backupsPath)
|
|
|
|
|
2022-08-22 17:04:17 +00:00
|
|
|
action.Type = dataprovider.ActionTypeEmail
|
|
|
|
action.Options = dataprovider.BaseEventActionOptions{
|
|
|
|
EmailConfig: dataprovider.EventActionEmailConfig{
|
|
|
|
Recipients: []string{"example@example.com"},
|
|
|
|
Subject: "test with attachments",
|
|
|
|
Body: "body",
|
|
|
|
Attachments: []string{"/file1.txt"},
|
|
|
|
},
|
|
|
|
}
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.UpdateEventAction(action, "", "", "")
|
2022-08-22 17:04:17 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
job.Run() // action is not compatible with a scheduled rule
|
|
|
|
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.DeleteEventRule(rule.Name, "", "", "")
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.NoError(t, err)
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.DeleteEventAction(action.Name, "", "", "")
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(backupsPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
stopEventScheduler()
|
|
|
|
}
|
2022-08-29 17:03:31 +00:00
|
|
|
|
|
|
|
func TestEventParamsCopy(t *testing.T) {
|
|
|
|
params := EventParams{
|
2022-09-06 17:09:23 +00:00
|
|
|
Name: "name",
|
|
|
|
Event: "event",
|
|
|
|
Status: 1,
|
|
|
|
errors: []string{"error1"},
|
|
|
|
retentionChecks: []executedRetentionCheck{},
|
2022-08-29 17:03:31 +00:00
|
|
|
}
|
|
|
|
paramsCopy := params.getACopy()
|
|
|
|
assert.Equal(t, params, *paramsCopy)
|
|
|
|
params.Name = "name mod"
|
|
|
|
paramsCopy.Event = "event mod"
|
|
|
|
paramsCopy.Status = 2
|
|
|
|
params.errors = append(params.errors, "error2")
|
|
|
|
paramsCopy.errors = append(paramsCopy.errors, "error3")
|
|
|
|
assert.Equal(t, []string{"error1", "error3"}, paramsCopy.errors)
|
|
|
|
assert.Equal(t, []string{"error1", "error2"}, params.errors)
|
|
|
|
assert.Equal(t, "name mod", params.Name)
|
|
|
|
assert.Equal(t, "name", paramsCopy.Name)
|
|
|
|
assert.Equal(t, "event", params.Event)
|
|
|
|
assert.Equal(t, "event mod", paramsCopy.Event)
|
|
|
|
assert.Equal(t, 1, params.Status)
|
|
|
|
assert.Equal(t, 2, paramsCopy.Status)
|
2022-09-06 17:09:23 +00:00
|
|
|
params = EventParams{
|
|
|
|
retentionChecks: []executedRetentionCheck{
|
|
|
|
{
|
|
|
|
Username: "u",
|
|
|
|
ActionName: "a",
|
|
|
|
Results: []folderRetentionCheckResult{
|
|
|
|
{
|
|
|
|
Path: "p",
|
|
|
|
Retention: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
paramsCopy = params.getACopy()
|
|
|
|
require.Len(t, paramsCopy.retentionChecks, 1)
|
|
|
|
paramsCopy.retentionChecks[0].Username = "u_copy"
|
|
|
|
paramsCopy.retentionChecks[0].ActionName = "a_copy"
|
|
|
|
require.Len(t, paramsCopy.retentionChecks[0].Results, 1)
|
|
|
|
paramsCopy.retentionChecks[0].Results[0].Path = "p_copy"
|
|
|
|
paramsCopy.retentionChecks[0].Results[0].Retention = 2
|
|
|
|
assert.Equal(t, "u", params.retentionChecks[0].Username)
|
|
|
|
assert.Equal(t, "a", params.retentionChecks[0].ActionName)
|
|
|
|
assert.Equal(t, "p", params.retentionChecks[0].Results[0].Path)
|
|
|
|
assert.Equal(t, 1, params.retentionChecks[0].Results[0].Retention)
|
|
|
|
assert.Equal(t, "u_copy", paramsCopy.retentionChecks[0].Username)
|
|
|
|
assert.Equal(t, "a_copy", paramsCopy.retentionChecks[0].ActionName)
|
|
|
|
assert.Equal(t, "p_copy", paramsCopy.retentionChecks[0].Results[0].Path)
|
|
|
|
assert.Equal(t, 2, paramsCopy.retentionChecks[0].Results[0].Retention)
|
2022-08-29 17:03:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEventParamsStatusFromError(t *testing.T) {
|
|
|
|
params := EventParams{Status: 1}
|
|
|
|
params.AddError(os.ErrNotExist)
|
|
|
|
assert.Equal(t, 1, params.Status)
|
|
|
|
|
|
|
|
params = EventParams{Status: 1, updateStatusFromError: true}
|
|
|
|
params.AddError(os.ErrNotExist)
|
|
|
|
assert.Equal(t, 2, params.Status)
|
|
|
|
}
|
2022-09-03 14:29:07 +00:00
|
|
|
|
|
|
|
type testWriter struct {
|
|
|
|
errTest error
|
|
|
|
sentinel string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *testWriter) Write(p []byte) (int, error) {
|
|
|
|
if w.errTest != nil {
|
|
|
|
return 0, w.errTest
|
|
|
|
}
|
|
|
|
if w.sentinel == string(p) {
|
|
|
|
return 0, io.ErrUnexpectedEOF
|
|
|
|
}
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteHTTPPartsError(t *testing.T) {
|
|
|
|
m := multipart.NewWriter(&testWriter{
|
|
|
|
errTest: io.ErrShortWrite,
|
|
|
|
})
|
|
|
|
|
2022-09-06 17:09:23 +00:00
|
|
|
err := writeHTTPPart(m, dataprovider.HTTPPart{}, nil, nil, nil, &EventParams{})
|
2022-09-03 14:29:07 +00:00
|
|
|
assert.ErrorIs(t, err, io.ErrShortWrite)
|
|
|
|
|
|
|
|
body := "test body"
|
|
|
|
m = multipart.NewWriter(&testWriter{sentinel: body})
|
|
|
|
err = writeHTTPPart(m, dataprovider.HTTPPart{
|
|
|
|
Body: body,
|
2022-09-06 17:09:23 +00:00
|
|
|
}, nil, nil, nil, &EventParams{})
|
2022-09-03 14:29:07 +00:00
|
|
|
assert.ErrorIs(t, err, io.ErrUnexpectedEOF)
|
|
|
|
}
|
2022-10-10 16:53:58 +00:00
|
|
|
|
2022-10-16 13:28:47 +00:00
|
|
|
func TestReplacePathsPlaceholders(t *testing.T) {
|
|
|
|
replacer := strings.NewReplacer("{{VirtualPath}}", "/path1")
|
|
|
|
paths := []string{"{{VirtualPath}}", "/path1"}
|
|
|
|
paths = replacePathsPlaceholders(paths, replacer)
|
|
|
|
assert.Equal(t, []string{"/path1"}, paths)
|
|
|
|
paths = []string{"{{VirtualPath}}", "/path2"}
|
|
|
|
paths = replacePathsPlaceholders(paths, replacer)
|
|
|
|
assert.Equal(t, []string{"/path1", "/path2"}, paths)
|
|
|
|
}
|
|
|
|
|
2022-12-31 15:41:32 +00:00
|
|
|
func TestEstimateZipSizeErrors(t *testing.T) {
|
|
|
|
u := dataprovider.User{
|
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "u",
|
|
|
|
HomeDir: filepath.Join(os.TempDir(), "u"),
|
|
|
|
Status: 1,
|
|
|
|
Permissions: map[string][]string{
|
|
|
|
"/": {dataprovider.PermAny},
|
|
|
|
},
|
|
|
|
QuotaSize: 1000,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := dataprovider.AddUser(&u, "", "", "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.MkdirAll(u.GetHomeDir(), os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
conn := NewBaseConnection("", ProtocolFTP, "", "", u)
|
|
|
|
_, err = getSizeForPath(conn, "/missing", vfs.NewFileInfo("missing", true, 0, time.Now(), false))
|
|
|
|
assert.True(t, conn.IsNotExistError(err))
|
|
|
|
if runtime.GOOS != osWindows {
|
|
|
|
err = os.MkdirAll(filepath.Join(u.HomeDir, "d1", "d2", "sub"), os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.WriteFile(filepath.Join(u.HomeDir, "d1", "d2", "sub", "file.txt"), []byte("data"), 0666)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Chmod(filepath.Join(u.HomeDir, "d1", "d2"), 0001)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
size, err := estimateZipSize(conn, "/archive.zip", []string{"/d1"})
|
|
|
|
assert.Error(t, err, "size %d", size)
|
|
|
|
err = os.Chmod(filepath.Join(u.HomeDir, "d1", "d2"), os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
err = dataprovider.DeleteUser(u.Username, "", "", "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(u.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2022-10-10 16:53:58 +00:00
|
|
|
func getErrorString(err error) string {
|
|
|
|
if err == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return err.Error()
|
|
|
|
}
|