2024-01-01 10:31:45 +00:00
|
|
|
// Copyright (C) 2019 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"
|
2023-01-25 17:49:03 +00:00
|
|
|
"errors"
|
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"
|
2023-01-22 07:46:58 +00:00
|
|
|
"net/url"
|
2022-08-01 16:48:54 +00:00
|
|
|
"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"
|
2023-03-22 18:02:54 +00:00
|
|
|
conditions := &dataprovider.EventConditions{
|
2022-08-01 16:48:54 +00:00
|
|
|
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
|
|
|
},
|
|
|
|
}
|
2023-03-22 18:02:54 +00:00
|
|
|
res := eventManager.checkProviderEventMatch(conditions, &EventParams{
|
2022-08-01 16:48:54 +00:00
|
|
|
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)
|
2023-03-22 18:02:54 +00:00
|
|
|
res = eventManager.checkProviderEventMatch(conditions, &EventParams{
|
2022-08-01 16:48:54 +00:00
|
|
|
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)
|
2023-03-22 18:02:54 +00:00
|
|
|
res = eventManager.checkProviderEventMatch(conditions, &EventParams{
|
2022-08-01 16:48:54 +00:00
|
|
|
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"}
|
2023-03-22 18:02:54 +00:00
|
|
|
res = eventManager.checkProviderEventMatch(conditions, &EventParams{
|
2022-08-01 16:48:54 +00:00
|
|
|
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)
|
2023-03-22 18:02:54 +00:00
|
|
|
res = eventManager.checkProviderEventMatch(conditions, &EventParams{
|
2022-08-01 16:48:54 +00:00
|
|
|
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)
|
2023-03-22 18:02:54 +00:00
|
|
|
res = eventManager.checkProviderEventMatch(conditions, &EventParams{
|
2022-12-03 16:47:43 +00:00
|
|
|
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
|
2023-03-22 18:02:54 +00:00
|
|
|
conditions = &dataprovider.EventConditions{
|
2022-08-01 16:48:54 +00:00
|
|
|
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,
|
|
|
|
}
|
2023-03-22 18:02:54 +00:00
|
|
|
res = eventManager.checkFsEventMatch(conditions, ¶ms)
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.False(t, res)
|
|
|
|
params.Event = operationDownload
|
2023-03-22 18:02:54 +00:00
|
|
|
res = eventManager.checkFsEventMatch(conditions, ¶ms)
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.True(t, res)
|
2022-12-03 16:47:43 +00:00
|
|
|
params.Role = role
|
2023-03-22 18:02:54 +00:00
|
|
|
res = eventManager.checkFsEventMatch(conditions, ¶ms)
|
2022-12-03 16:47:43 +00:00
|
|
|
assert.False(t, res)
|
|
|
|
params.Role = ""
|
2022-08-01 16:48:54 +00:00
|
|
|
params.Name = "name"
|
2023-03-22 18:02:54 +00:00
|
|
|
res = eventManager.checkFsEventMatch(conditions, ¶ms)
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.False(t, res)
|
|
|
|
params.Name = "user5"
|
2023-03-22 18:02:54 +00:00
|
|
|
res = eventManager.checkFsEventMatch(conditions, ¶ms)
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.True(t, res)
|
|
|
|
params.VirtualPath = "/sub/f.jpg"
|
|
|
|
params.ObjectName = path.Base(params.VirtualPath)
|
2023-03-22 18:02:54 +00:00
|
|
|
res = eventManager.checkFsEventMatch(conditions, ¶ms)
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.False(t, res)
|
|
|
|
params.VirtualPath = "/sub/f.txt"
|
|
|
|
params.ObjectName = path.Base(params.VirtualPath)
|
2023-03-22 18:02:54 +00:00
|
|
|
res = eventManager.checkFsEventMatch(conditions, ¶ms)
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.True(t, res)
|
|
|
|
params.Protocol = ProtocolHTTP
|
2023-03-22 18:02:54 +00:00
|
|
|
res = eventManager.checkFsEventMatch(conditions, ¶ms)
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.False(t, res)
|
|
|
|
params.Protocol = ProtocolSFTP
|
|
|
|
params.FileSize = 5
|
2023-03-22 18:02:54 +00:00
|
|
|
res = eventManager.checkFsEventMatch(conditions, ¶ms)
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.False(t, res)
|
|
|
|
params.FileSize = 50
|
2023-03-22 18:02:54 +00:00
|
|
|
res = eventManager.checkFsEventMatch(conditions, ¶ms)
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.False(t, res)
|
|
|
|
params.FileSize = 25
|
2023-03-22 18:02:54 +00:00
|
|
|
res = eventManager.checkFsEventMatch(conditions, ¶ms)
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.True(t, res)
|
|
|
|
// bad pattern
|
|
|
|
conditions.Options.Names = []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: "[-]",
|
|
|
|
},
|
|
|
|
}
|
2023-03-22 18:02:54 +00:00
|
|
|
res = eventManager.checkFsEventMatch(conditions, ¶ms)
|
2022-08-01 16:48:54 +00:00
|
|
|
assert.False(t, res)
|
2022-09-04 15:48:09 +00:00
|
|
|
// check fs events with group name filters
|
2023-03-22 18:02:54 +00:00
|
|
|
conditions = &dataprovider.EventConditions{
|
2022-09-04 15:48:09 +00:00
|
|
|
FsEvents: []string{operationUpload, operationDownload},
|
|
|
|
Options: dataprovider.ConditionOptions{
|
|
|
|
GroupNames: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: "group*",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Pattern: "testgroup*",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
params = EventParams{
|
|
|
|
Name: "user1",
|
|
|
|
Event: operationUpload,
|
|
|
|
}
|
2023-03-22 18:02:54 +00:00
|
|
|
res = eventManager.checkFsEventMatch(conditions, ¶ms)
|
2022-09-04 15:48:09 +00:00
|
|
|
assert.False(t, res)
|
|
|
|
params.Groups = []sdk.GroupMapping{
|
|
|
|
{
|
|
|
|
Name: "g1",
|
|
|
|
Type: sdk.GroupTypePrimary,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "g2",
|
|
|
|
Type: sdk.GroupTypeSecondary,
|
|
|
|
},
|
|
|
|
}
|
2023-03-22 18:02:54 +00:00
|
|
|
res = eventManager.checkFsEventMatch(conditions, ¶ms)
|
2022-09-04 15:48:09 +00:00
|
|
|
assert.False(t, res)
|
|
|
|
params.Groups = []sdk.GroupMapping{
|
|
|
|
{
|
|
|
|
Name: "testgroup2",
|
|
|
|
Type: sdk.GroupTypePrimary,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "g2",
|
|
|
|
Type: sdk.GroupTypeSecondary,
|
|
|
|
},
|
|
|
|
}
|
2023-03-22 18:02:54 +00:00
|
|
|
res = eventManager.checkFsEventMatch(conditions, ¶ms)
|
2022-09-04 15:48:09 +00:00
|
|
|
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)
|
2023-03-22 18:02:54 +00:00
|
|
|
res = eventManager.checkIPDLoginEventMatch(&dataprovider.EventConditions{
|
|
|
|
IDPLoginEvent: 0,
|
|
|
|
}, &EventParams{
|
|
|
|
Event: IDPLoginAdmin,
|
|
|
|
})
|
|
|
|
assert.True(t, res)
|
|
|
|
res = eventManager.checkIPDLoginEventMatch(&dataprovider.EventConditions{
|
|
|
|
IDPLoginEvent: 2,
|
|
|
|
}, &EventParams{
|
|
|
|
Event: IDPLoginAdmin,
|
|
|
|
})
|
|
|
|
assert.True(t, res)
|
|
|
|
res = eventManager.checkIPDLoginEventMatch(&dataprovider.EventConditions{
|
|
|
|
IDPLoginEvent: 1,
|
|
|
|
}, &EventParams{
|
|
|
|
Event: IDPLoginAdmin,
|
|
|
|
})
|
|
|
|
assert.False(t, res)
|
|
|
|
res = eventManager.checkIPDLoginEventMatch(&dataprovider.EventConditions{
|
|
|
|
IDPLoginEvent: 1,
|
|
|
|
}, &EventParams{
|
|
|
|
Event: IDPLoginUser,
|
|
|
|
})
|
|
|
|
assert.True(t, res)
|
|
|
|
res = eventManager.checkIPDLoginEventMatch(&dataprovider.EventConditions{
|
|
|
|
IDPLoginEvent: 1,
|
|
|
|
}, &EventParams{
|
|
|
|
Name: "user",
|
|
|
|
Event: IDPLoginUser,
|
|
|
|
})
|
|
|
|
assert.True(t, res)
|
|
|
|
res = eventManager.checkIPDLoginEventMatch(&dataprovider.EventConditions{
|
|
|
|
IDPLoginEvent: 1,
|
|
|
|
Options: dataprovider.ConditionOptions{
|
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: "abc",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, &EventParams{
|
|
|
|
Name: "user",
|
|
|
|
Event: IDPLoginUser,
|
|
|
|
})
|
|
|
|
assert.False(t, res)
|
|
|
|
res = eventManager.checkIPDLoginEventMatch(&dataprovider.EventConditions{
|
|
|
|
IDPLoginEvent: 2,
|
|
|
|
}, &EventParams{
|
|
|
|
Name: "user",
|
|
|
|
Event: IDPLoginUser,
|
|
|
|
})
|
|
|
|
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)
|
|
|
|
|
2023-10-28 19:02:14 +00:00
|
|
|
c.Pattern = "/**/*.filepart"
|
|
|
|
res = checkEventConditionPattern(c, "/file.filepart")
|
|
|
|
assert.True(t, res)
|
|
|
|
res = checkEventConditionPattern(c, "/mydir/sub/file.filepart")
|
|
|
|
assert.True(t, res)
|
|
|
|
res = checkEventConditionPattern(c, "/file.txt")
|
|
|
|
assert.False(t, res)
|
|
|
|
res = checkEventConditionPattern(c, "/mydir/file.txt")
|
|
|
|
assert.False(t, res)
|
|
|
|
|
2023-01-02 14:59:00 +00:00
|
|
|
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)
|
2023-07-02 07:49:21 +00:00
|
|
|
|
|
|
|
c.InverseMatch = true
|
|
|
|
assert.True(t, checkEventConditionPattern(c, "/mydir"))
|
|
|
|
assert.True(t, checkEventConditionPattern(c, "/mydirname/f.txt"))
|
|
|
|
assert.True(t, checkEventConditionPattern(c, "/mydir/sub"))
|
|
|
|
assert.True(t, checkEventConditionPattern(c, "/mydir/sub/dir"))
|
|
|
|
assert.False(t, checkEventConditionPattern(c, "/mydir/sub/dir/a.txt"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMutlipleDoubleStarMatching(t *testing.T) {
|
|
|
|
patterns := []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: "/**/*.txt",
|
|
|
|
InverseMatch: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Pattern: "/**/*.tmp",
|
|
|
|
InverseMatch: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.False(t, checkEventConditionPatterns("/mydir", patterns))
|
|
|
|
assert.True(t, checkEventConditionPatterns("/mydir/test.tmp", patterns))
|
|
|
|
assert.True(t, checkEventConditionPatterns("/mydir/test.txt", patterns))
|
|
|
|
assert.False(t, checkEventConditionPatterns("/mydir/test.csv", patterns))
|
|
|
|
assert.False(t, checkEventConditionPatterns("/mydir/sub", patterns))
|
|
|
|
assert.True(t, checkEventConditionPatterns("/mydir/sub/test.tmp", patterns))
|
|
|
|
assert.True(t, checkEventConditionPatterns("/mydir/sub/test.txt", patterns))
|
|
|
|
assert.False(t, checkEventConditionPatterns("/mydir/sub/test.csv", patterns))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultipleDoubleStarMatchingInverse(t *testing.T) {
|
|
|
|
patterns := []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: "/**/*.txt",
|
|
|
|
InverseMatch: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Pattern: "/**/*.tmp",
|
|
|
|
InverseMatch: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.True(t, checkEventConditionPatterns("/mydir", patterns))
|
|
|
|
assert.False(t, checkEventConditionPatterns("/mydir/test.tmp", patterns))
|
|
|
|
assert.False(t, checkEventConditionPatterns("/mydir/test.txt", patterns))
|
|
|
|
assert.True(t, checkEventConditionPatterns("/mydir/test.csv", patterns))
|
|
|
|
assert.True(t, checkEventConditionPatterns("/mydir/sub", patterns))
|
|
|
|
assert.False(t, checkEventConditionPatterns("/mydir/sub/test.tmp", patterns))
|
|
|
|
assert.False(t, checkEventConditionPatterns("/mydir/sub/test.txt", patterns))
|
|
|
|
assert.True(t, checkEventConditionPatterns("/mydir/sub/test.csv", patterns))
|
2023-01-02 14:59:00 +00:00
|
|
|
}
|
|
|
|
|
2023-07-02 08:57:29 +00:00
|
|
|
func TestGroupConditionPatterns(t *testing.T) {
|
|
|
|
group1 := "group1"
|
|
|
|
group2 := "group2"
|
|
|
|
patterns := []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: group1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Pattern: group2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
inversePatterns := []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: group1,
|
|
|
|
InverseMatch: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Pattern: group2,
|
|
|
|
InverseMatch: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
groups := []sdk.GroupMapping{
|
|
|
|
{
|
|
|
|
Name: "group3",
|
|
|
|
Type: sdk.GroupTypePrimary,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.False(t, checkEventGroupConditionPatterns(groups, patterns))
|
|
|
|
assert.True(t, checkEventGroupConditionPatterns(groups, inversePatterns))
|
|
|
|
|
|
|
|
groups = []sdk.GroupMapping{
|
|
|
|
{
|
|
|
|
Name: group1,
|
|
|
|
Type: sdk.GroupTypePrimary,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "group4",
|
|
|
|
Type: sdk.GroupTypePrimary,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.True(t, checkEventGroupConditionPatterns(groups, patterns))
|
|
|
|
assert.False(t, checkEventGroupConditionPatterns(groups, inversePatterns))
|
|
|
|
groups = []sdk.GroupMapping{
|
|
|
|
{
|
|
|
|
Name: group1,
|
|
|
|
Type: sdk.GroupTypePrimary,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.True(t, checkEventGroupConditionPatterns(groups, patterns))
|
|
|
|
assert.False(t, checkEventGroupConditionPatterns(groups, inversePatterns))
|
|
|
|
groups = []sdk.GroupMapping{
|
|
|
|
{
|
|
|
|
Name: "group11",
|
|
|
|
Type: sdk.GroupTypePrimary,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.False(t, checkEventGroupConditionPatterns(groups, patterns))
|
|
|
|
assert.True(t, checkEventGroupConditionPatterns(groups, inversePatterns))
|
|
|
|
}
|
|
|
|
|
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",
|
2023-01-19 17:33:04 +00:00
|
|
|
Status: 1,
|
2022-08-01 16:48:54 +00:00
|
|
|
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)
|
2023-01-25 17:49:03 +00:00
|
|
|
ok := errors.Is(err, util.ErrNotFound)
|
2022-08-01 16:48:54 +00:00
|
|
|
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)
|
2023-02-25 12:06:09 +00:00
|
|
|
err = executeUserExpirationCheckRuleAction(dataprovider.ConditionOptions{}, &EventParams{})
|
|
|
|
assert.Error(t, err)
|
2024-03-04 18:48:10 +00:00
|
|
|
err = executeUserInactivityCheckRuleAction(dataprovider.EventActionUserInactivity{},
|
|
|
|
dataprovider.ConditionOptions{}, &EventParams{}, time.Time{})
|
|
|
|
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)
|
2023-03-22 18:02:54 +00:00
|
|
|
_, err = executeAdminCheckAction(&dataprovider.EventActionIDPAccountCheck{}, &EventParams{})
|
|
|
|
assert.Error(t, err)
|
|
|
|
_, err = executeUserCheckAction(&dataprovider.EventActionIDPAccountCheck{}, &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)
|
|
|
|
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
|
|
|
|
2023-03-22 18:02:54 +00:00
|
|
|
_, _, err = getHTTPRuleActionBody(&dataprovider.EventActionHTTPConfig{
|
2022-09-03 14:29:07 +00:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
2023-03-22 18:02:54 +00:00
|
|
|
}, &EventParams{}, false)
|
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",
|
2023-01-19 17:33:04 +00:00
|
|
|
Status: 1,
|
2022-08-01 16:48:54 +00:00
|
|
|
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",
|
2023-01-19 17:33:04 +00:00
|
|
|
Status: 1,
|
2022-08-01 16:48:54 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2024-10-08 17:15:07 +00:00
|
|
|
func TestDateTimePlaceholder(t *testing.T) {
|
|
|
|
dateTime := time.Now()
|
|
|
|
params := EventParams{
|
|
|
|
Timestamp: dateTime,
|
|
|
|
}
|
|
|
|
replacements := params.getStringReplacements(false, false)
|
|
|
|
r := strings.NewReplacer(replacements...)
|
|
|
|
res := r.Replace("{{DateTime}}")
|
|
|
|
assert.Equal(t, dateTime.UTC().Format(dateTimeMillisFormat), res)
|
|
|
|
}
|
|
|
|
|
2022-08-01 16:48:54 +00:00
|
|
|
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,
|
2023-08-12 16:51:47 +00:00
|
|
|
Body: `"data": "{{ObjectDataString}}"`,
|
2022-08-01 16:48:54 +00:00
|
|
|
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")
|
|
|
|
}
|
2023-01-22 07:46:58 +00:00
|
|
|
action.Options.HTTPConfig.Endpoint = fmt.Sprintf("http://%v", httpAddr)
|
2022-09-03 14:29:07 +00:00
|
|
|
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
|
|
|
|
2023-02-25 12:06:09 +00:00
|
|
|
action = dataprovider.BaseEventAction{
|
|
|
|
Type: dataprovider.ActionTypeUserExpirationCheck,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = executeRuleAction(action, &EventParams{}, dataprovider.ConditionOptions{
|
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: "don't match",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Contains(t, getErrorString(err), "no user expiration check executed")
|
|
|
|
|
|
|
|
err = executeRuleAction(action, &EventParams{}, dataprovider.ConditionOptions{
|
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: username1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.NoError(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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2023-03-22 18:02:54 +00:00
|
|
|
body, _, err := getHTTPRuleActionBody(&dataprovider.EventActionHTTPConfig{
|
2022-09-03 14:29:07 +00:00
|
|
|
Method: http.MethodPost,
|
2023-03-22 18:02:54 +00:00
|
|
|
}, nil, nil, dataprovider.User{}, &EventParams{}, true)
|
2022-09-03 14:29:07 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Nil(t, body)
|
2023-03-22 18:02:54 +00:00
|
|
|
body, _, err = getHTTPRuleActionBody(&dataprovider.EventActionHTTPConfig{
|
|
|
|
Method: http.MethodPost,
|
|
|
|
Body: "test body",
|
|
|
|
}, nil, nil, dataprovider.User{}, &EventParams{}, false)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, body)
|
2022-09-03 14:29:07 +00:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-03-22 18:02:54 +00:00
|
|
|
func TestIDPAccountCheckRule(t *testing.T) {
|
|
|
|
_, _, err := executeIDPAccountCheckRule(dataprovider.EventRule{}, EventParams{})
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "no action executed")
|
|
|
|
}
|
|
|
|
_, _, err = executeIDPAccountCheckRule(dataprovider.EventRule{
|
|
|
|
Actions: []dataprovider.EventAction{
|
|
|
|
{
|
|
|
|
BaseEventAction: dataprovider.BaseEventAction{
|
|
|
|
Name: "n",
|
|
|
|
Type: dataprovider.ActionTypeIDPAccountCheck,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, EventParams{Event: "invalid"})
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "unsupported IDP login event")
|
|
|
|
}
|
|
|
|
// invalid json
|
|
|
|
_, err = executeAdminCheckAction(&dataprovider.EventActionIDPAccountCheck{TemplateAdmin: "{"}, &EventParams{Name: "missing admin"})
|
|
|
|
assert.Error(t, err)
|
|
|
|
_, err = executeUserCheckAction(&dataprovider.EventActionIDPAccountCheck{TemplateUser: "["}, &EventParams{Name: "missing user"})
|
|
|
|
assert.Error(t, err)
|
|
|
|
_, err = executeUserCheckAction(&dataprovider.EventActionIDPAccountCheck{TemplateUser: "{}"}, &EventParams{Name: "invalid user template"})
|
|
|
|
assert.ErrorIs(t, err, util.ErrValidation)
|
|
|
|
username := "u"
|
|
|
|
c := &dataprovider.EventActionIDPAccountCheck{
|
|
|
|
Mode: 1,
|
|
|
|
TemplateUser: `{"username":"` + username + `","status":1,"home_dir":"` + util.JSONEscape(filepath.Join(os.TempDir())) + `","permissions":{"/":["*"]}}`,
|
|
|
|
}
|
|
|
|
params := &EventParams{
|
|
|
|
Name: username,
|
|
|
|
Event: IDPLoginUser,
|
|
|
|
}
|
|
|
|
user, err := executeUserCheckAction(c, params)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, username, user.Username)
|
|
|
|
assert.Equal(t, 1, user.Status)
|
|
|
|
user.Status = 0
|
|
|
|
err = dataprovider.UpdateUser(user, "", "", "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
// the user is not changed
|
|
|
|
user, err = executeUserCheckAction(c, params)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, username, user.Username)
|
|
|
|
assert.Equal(t, 0, user.Status)
|
|
|
|
// change the mode, the user is now updated
|
|
|
|
c.Mode = 0
|
|
|
|
user, err = executeUserCheckAction(c, params)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, username, user.Username)
|
|
|
|
assert.Equal(t, 1, user.Status)
|
2024-08-02 17:24:14 +00:00
|
|
|
assert.Empty(t, user.Password)
|
|
|
|
assert.Len(t, user.PublicKeys, 0)
|
|
|
|
assert.Len(t, user.Filters.TLSCerts, 0)
|
|
|
|
assert.Empty(t, user.Email)
|
|
|
|
assert.Empty(t, user.Description)
|
|
|
|
// Update the profile attribute and make sure they are preserved
|
|
|
|
user.Password = "secret"
|
|
|
|
user.Email = "example@example.com"
|
|
|
|
user.Description = "some desc"
|
|
|
|
user.Filters.TLSCerts = []string{serverCert}
|
|
|
|
user.PublicKeys = []string{"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC03jj0D+djk7pxIf/0OhrxrchJTRZklofJ1NoIu4752Sq02mdXmarMVsqJ1cAjV5LBVy3D1F5U6XW4rppkXeVtd04Pxb09ehtH0pRRPaoHHlALiJt8CoMpbKYMA8b3KXPPriGxgGomvtU2T2RMURSwOZbMtpsugfjYSWenyYX+VORYhylWnSXL961LTyC21ehd6d6QnW9G7E5hYMITMY9TuQZz3bROYzXiTsgN0+g6Hn7exFQp50p45StUMfV/SftCMdCxlxuyGny2CrN/vfjO7xxOo2uv7q1qm10Q46KPWJQv+pgZ/OfL+EDjy07n5QVSKHlbx+2nT4Q0EgOSQaCTYwn3YjtABfIxWwgAFdyj6YlPulCL22qU4MYhDcA6PSBwDdf8hvxBfvsiHdM+JcSHvv8/VeJhk6CmnZxGY0fxBupov27z3yEO8nAg8k+6PaUiW1MSUfuGMF/ktB8LOstXsEPXSszuyXiOv4DaryOXUiSn7bmRqKcEFlJusO6aZP0= nicola@p1"}
|
|
|
|
err = dataprovider.UpdateUser(user, "", "", "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
user, err = executeUserCheckAction(c, params)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, username, user.Username)
|
|
|
|
assert.Equal(t, 1, user.Status)
|
|
|
|
assert.NotEmpty(t, user.Password)
|
|
|
|
assert.Len(t, user.PublicKeys, 1)
|
|
|
|
assert.Len(t, user.Filters.TLSCerts, 1)
|
|
|
|
assert.NotEmpty(t, user.Email)
|
|
|
|
assert.NotEmpty(t, user.Description)
|
2023-03-22 18:02:54 +00:00
|
|
|
|
|
|
|
err = dataprovider.DeleteUser(username, "", "", "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
// check rule consistency
|
|
|
|
r := dataprovider.EventRule{
|
|
|
|
Actions: []dataprovider.EventAction{
|
|
|
|
{
|
|
|
|
BaseEventAction: dataprovider.BaseEventAction{
|
|
|
|
Type: dataprovider.ActionTypeIDPAccountCheck,
|
|
|
|
},
|
|
|
|
Order: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = r.CheckActionsConsistency("")
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "IDP account check action is only supported for IDP login trigger")
|
|
|
|
}
|
|
|
|
r.Trigger = dataprovider.EventTriggerIDPLogin
|
|
|
|
err = r.CheckActionsConsistency("")
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "IDP account check must be a sync action")
|
|
|
|
}
|
|
|
|
r.Actions[0].Options.ExecuteSync = true
|
|
|
|
err = r.CheckActionsConsistency("")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
r.Actions = append(r.Actions, dataprovider.EventAction{
|
|
|
|
BaseEventAction: dataprovider.BaseEventAction{
|
|
|
|
Type: dataprovider.ActionTypeCommand,
|
|
|
|
},
|
|
|
|
Options: dataprovider.EventActionOptions{
|
|
|
|
ExecuteSync: true,
|
|
|
|
},
|
|
|
|
Order: 2,
|
|
|
|
})
|
|
|
|
err = r.CheckActionsConsistency("")
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "IDP account check must be the only sync action")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-25 12:06:09 +00:00
|
|
|
func TestUserExpirationCheck(t *testing.T) {
|
|
|
|
username := "test_user_expiration_check"
|
|
|
|
user := dataprovider.User{
|
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: username,
|
|
|
|
Permissions: map[string][]string{
|
|
|
|
"/": {dataprovider.PermAny},
|
|
|
|
},
|
|
|
|
HomeDir: filepath.Join(os.TempDir(), username),
|
|
|
|
ExpirationDate: util.GetTimeAsMsSinceEpoch(time.Now().Add(-24 * time.Hour)),
|
|
|
|
},
|
|
|
|
}
|
2023-02-25 15:33:39 +00:00
|
|
|
user.Filters.PasswordExpiration = 5
|
2023-02-25 12:06:09 +00:00
|
|
|
err := dataprovider.AddUser(&user, "", "", "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2023-02-25 15:33:39 +00:00
|
|
|
conditions := dataprovider.ConditionOptions{
|
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: username,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = executeUserExpirationCheckRuleAction(conditions, &EventParams{})
|
2023-02-25 12:06:09 +00:00
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "expired users")
|
|
|
|
}
|
2023-02-25 15:33:39 +00:00
|
|
|
// the check will be skipped, the user is expired
|
|
|
|
err = executePwdExpirationCheckRuleAction(dataprovider.EventActionPasswordExpiration{Threshold: 10}, conditions, &EventParams{})
|
|
|
|
assert.NoError(t, err)
|
2023-02-25 12:06:09 +00:00
|
|
|
|
|
|
|
err = dataprovider.DeleteUser(username, "", "", "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
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{},
|
|
|
|
}
|
2024-02-15 19:53:56 +00:00
|
|
|
err = addZipEntry(wr, conn, "/adir/sub/f.dat", "/adir/sub/sub", 0)
|
2022-10-10 16:53:58 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Contains(t, getErrorString(err), "is outside base dir")
|
2022-08-10 16:41:59 +00:00
|
|
|
}
|
|
|
|
|
2024-02-17 11:30:47 +00:00
|
|
|
wr := &zipWriterWrapper{
|
|
|
|
Name: xid.New().String() + ".zip",
|
|
|
|
Writer: zip.NewWriter(bytes.NewBuffer(nil)),
|
|
|
|
Entries: map[string]bool{},
|
|
|
|
}
|
|
|
|
err = addZipEntry(wr, conn, "/p1", "/", 2000)
|
|
|
|
assert.ErrorIs(t, err, util.ErrRecursionTooDeep)
|
|
|
|
|
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",
|
2023-01-19 17:33:04 +00:00
|
|
|
Status: 1,
|
2022-08-01 16:48:54 +00:00
|
|
|
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",
|
2023-12-16 10:39:32 +00:00
|
|
|
Extension: "ext",
|
2022-09-06 17:09:23 +00:00
|
|
|
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)
|
2023-03-22 18:02:54 +00:00
|
|
|
assert.Nil(t, params.IDPCustomFields)
|
|
|
|
params.addIDPCustomFields(nil)
|
|
|
|
assert.Nil(t, params.IDPCustomFields)
|
|
|
|
params.IDPCustomFields = &map[string]string{
|
|
|
|
"field1": "val1",
|
|
|
|
}
|
|
|
|
paramsCopy = params.getACopy()
|
|
|
|
for k, v := range *paramsCopy.IDPCustomFields {
|
|
|
|
assert.Equal(t, "field1", k)
|
|
|
|
assert.Equal(t, "val1", v)
|
|
|
|
}
|
|
|
|
assert.Equal(t, params.IDPCustomFields, paramsCopy.IDPCustomFields)
|
2023-03-23 17:15:07 +00:00
|
|
|
(*paramsCopy.IDPCustomFields)["field1"] = "val2"
|
2023-03-22 18:02:54 +00:00
|
|
|
assert.NotEqual(t, params.IDPCustomFields, paramsCopy.IDPCustomFields)
|
2023-08-12 17:03:47 +00:00
|
|
|
params.Metadata = map[string]string{"key": "value"}
|
|
|
|
paramsCopy = params.getACopy()
|
|
|
|
params.Metadata["key1"] = "value1"
|
|
|
|
require.Equal(t, map[string]string{"key": "value"}, paramsCopy.Metadata)
|
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,
|
|
|
|
})
|
|
|
|
|
2023-03-22 18:02:54 +00:00
|
|
|
err := writeHTTPPart(m, dataprovider.HTTPPart{}, nil, nil, nil, &EventParams{}, false)
|
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,
|
2023-03-22 18:02:54 +00:00
|
|
|
}, nil, nil, nil, &EventParams{}, false)
|
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)
|
2023-04-03 16:53:13 +00:00
|
|
|
_, _, _, _, err = getFileWriter(conn, "/missing/path/file.txt", -1) //nolint:dogsled
|
|
|
|
assert.Error(t, err)
|
2022-12-31 15:41:32 +00:00
|
|
|
_, 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)
|
|
|
|
}
|
|
|
|
|
2023-01-21 14:41:24 +00:00
|
|
|
func TestOnDemandRule(t *testing.T) {
|
|
|
|
a := &dataprovider.BaseEventAction{
|
|
|
|
Name: "a",
|
|
|
|
Type: dataprovider.ActionTypeBackup,
|
|
|
|
Options: dataprovider.BaseEventActionOptions{},
|
|
|
|
}
|
|
|
|
err := dataprovider.AddEventAction(a, "", "", "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
r := &dataprovider.EventRule{
|
|
|
|
Name: "test on demand rule",
|
|
|
|
Status: 1,
|
|
|
|
Trigger: dataprovider.EventTriggerOnDemand,
|
|
|
|
Actions: []dataprovider.EventAction{
|
|
|
|
{
|
|
|
|
BaseEventAction: dataprovider.BaseEventAction{
|
|
|
|
Name: a.Name,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = dataprovider.AddEventRule(r, "", "", "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = RunOnDemandRule(r.Name)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
r.Status = 0
|
|
|
|
err = dataprovider.UpdateEventRule(r, "", "", "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = RunOnDemandRule(r.Name)
|
|
|
|
assert.ErrorIs(t, err, util.ErrValidation)
|
|
|
|
assert.Contains(t, err.Error(), "is inactive")
|
|
|
|
|
|
|
|
r.Status = 1
|
|
|
|
r.Trigger = dataprovider.EventTriggerCertificate
|
|
|
|
err = dataprovider.UpdateEventRule(r, "", "", "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = RunOnDemandRule(r.Name)
|
|
|
|
assert.ErrorIs(t, err, util.ErrValidation)
|
|
|
|
assert.Contains(t, err.Error(), "is not defined as on-demand")
|
|
|
|
|
|
|
|
a1 := &dataprovider.BaseEventAction{
|
|
|
|
Name: "a1",
|
|
|
|
Type: dataprovider.ActionTypeEmail,
|
|
|
|
Options: dataprovider.BaseEventActionOptions{
|
|
|
|
EmailConfig: dataprovider.EventActionEmailConfig{
|
|
|
|
Recipients: []string{"example@example.org"},
|
|
|
|
Subject: "subject",
|
|
|
|
Body: "body",
|
|
|
|
Attachments: []string{"/{{VirtualPath}}"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = dataprovider.AddEventAction(a1, "", "", "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
r.Trigger = dataprovider.EventTriggerOnDemand
|
|
|
|
r.Actions = []dataprovider.EventAction{
|
|
|
|
{
|
|
|
|
BaseEventAction: dataprovider.BaseEventAction{
|
|
|
|
Name: a1.Name,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = dataprovider.UpdateEventRule(r, "", "", "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = RunOnDemandRule(r.Name)
|
|
|
|
assert.ErrorIs(t, err, util.ErrValidation)
|
|
|
|
assert.Contains(t, err.Error(), "incosistent actions")
|
|
|
|
|
|
|
|
err = dataprovider.DeleteEventRule(r.Name, "", "", "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = dataprovider.DeleteEventAction(a.Name, "", "", "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = dataprovider.DeleteEventAction(a1.Name, "", "", "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = RunOnDemandRule(r.Name)
|
|
|
|
assert.ErrorIs(t, err, util.ErrNotFound)
|
|
|
|
}
|
|
|
|
|
2022-10-10 16:53:58 +00:00
|
|
|
func getErrorString(err error) string {
|
|
|
|
if err == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return err.Error()
|
|
|
|
}
|
2023-01-22 07:46:58 +00:00
|
|
|
|
|
|
|
func TestHTTPEndpointWithPlaceholders(t *testing.T) {
|
|
|
|
c := dataprovider.EventActionHTTPConfig{
|
|
|
|
Endpoint: "http://127.0.0.1:8080/base/url/{{Name}}/{{VirtualPath}}/upload",
|
|
|
|
QueryParameters: []dataprovider.KeyValue{
|
|
|
|
{
|
|
|
|
Key: "u",
|
|
|
|
Value: "{{Name}}",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "p",
|
|
|
|
Value: "{{VirtualPath}}",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
name := "uname"
|
|
|
|
vPath := "/a dir/@ file.txt"
|
|
|
|
replacer := strings.NewReplacer("{{Name}}", name, "{{VirtualPath}}", vPath)
|
2023-04-25 18:27:40 +00:00
|
|
|
u, err := getHTTPRuleActionEndpoint(&c, replacer)
|
2023-01-22 07:46:58 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
expected := "http://127.0.0.1:8080/base/url/" + url.PathEscape(name) + "/" + url.PathEscape(vPath) +
|
|
|
|
"/upload?" + "p=" + url.QueryEscape(vPath) + "&u=" + url.QueryEscape(name)
|
|
|
|
assert.Equal(t, expected, u)
|
|
|
|
|
|
|
|
c.Endpoint = "http://127.0.0.1/upload"
|
2023-04-25 18:27:40 +00:00
|
|
|
u, err = getHTTPRuleActionEndpoint(&c, replacer)
|
2023-01-22 07:46:58 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
expected = c.Endpoint + "?p=" + url.QueryEscape(vPath) + "&u=" + url.QueryEscape(name)
|
|
|
|
assert.Equal(t, expected, u)
|
|
|
|
}
|
2023-08-12 16:51:47 +00:00
|
|
|
|
|
|
|
func TestMetadataReplacement(t *testing.T) {
|
|
|
|
params := &EventParams{
|
|
|
|
Metadata: map[string]string{
|
|
|
|
"key": "value",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
replacements := params.getStringReplacements(false, false)
|
|
|
|
replacer := strings.NewReplacer(replacements...)
|
|
|
|
reader, _, err := getHTTPRuleActionBody(&dataprovider.EventActionHTTPConfig{Body: "{{Metadata}} {{MetadataString}}"}, replacer, nil, dataprovider.User{}, params, false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
data, err := io.ReadAll(reader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, `{"key":"value"} {\"key\":\"value\"}`, string(data))
|
|
|
|
}
|
2024-03-04 18:48:10 +00:00
|
|
|
|
|
|
|
func TestUserInactivityCheck(t *testing.T) {
|
|
|
|
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},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
days := user1.InactivityDays(time.Now().Add(10*24*time.Hour + 5*time.Second))
|
|
|
|
assert.Equal(t, 0, days)
|
|
|
|
|
|
|
|
user2.LastLogin = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
err := executeInactivityCheckForUser(&user2, dataprovider.EventActionUserInactivity{
|
|
|
|
DisableThreshold: 10,
|
|
|
|
}, time.Now().Add(12*24*time.Hour))
|
|
|
|
assert.Error(t, err)
|
|
|
|
user2.LastLogin = util.GetTimeAsMsSinceEpoch(time.Now())
|
|
|
|
err = executeInactivityCheckForUser(&user2, dataprovider.EventActionUserInactivity{
|
|
|
|
DeleteThreshold: 10,
|
|
|
|
}, time.Now().Add(12*24*time.Hour))
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
err = dataprovider.AddUser(&user1, "", "", "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = dataprovider.AddUser(&user2, "", "", "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
user1, err = dataprovider.UserExists(username1, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, user1.Status)
|
|
|
|
days = user1.InactivityDays(time.Now().Add(10*24*time.Hour + 5*time.Second))
|
|
|
|
assert.Equal(t, 10, days)
|
|
|
|
days = user1.InactivityDays(time.Now().Add(-10*24*time.Hour + 5*time.Second))
|
|
|
|
assert.Equal(t, -9, days)
|
|
|
|
|
|
|
|
err = executeUserInactivityCheckRuleAction(dataprovider.EventActionUserInactivity{
|
|
|
|
DisableThreshold: 10,
|
|
|
|
}, dataprovider.ConditionOptions{
|
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: "not matching",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, &EventParams{}, time.Now().Add(12*24*time.Hour))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = executeUserInactivityCheckRuleAction(dataprovider.EventActionUserInactivity{
|
|
|
|
DisableThreshold: 10,
|
|
|
|
}, dataprovider.ConditionOptions{
|
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: user1.Username,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, &EventParams{}, time.Now())
|
|
|
|
assert.NoError(t, err) // no action
|
|
|
|
|
|
|
|
err = executeUserInactivityCheckRuleAction(dataprovider.EventActionUserInactivity{
|
|
|
|
DisableThreshold: 10,
|
|
|
|
}, dataprovider.ConditionOptions{
|
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: user1.Username,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, &EventParams{}, time.Now().Add(-12*24*time.Hour))
|
|
|
|
assert.NoError(t, err) // no action
|
|
|
|
|
|
|
|
err = executeUserInactivityCheckRuleAction(dataprovider.EventActionUserInactivity{
|
|
|
|
DisableThreshold: 10,
|
|
|
|
DeleteThreshold: 20,
|
|
|
|
}, dataprovider.ConditionOptions{
|
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: user1.Username,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, &EventParams{}, time.Now().Add(30*24*time.Hour))
|
|
|
|
// both thresholds exceeded, the user will be disabled
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "executed inactivity check actions for users")
|
|
|
|
}
|
|
|
|
user1, err = dataprovider.UserExists(username1, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 0, user1.Status)
|
|
|
|
|
|
|
|
err = executeUserInactivityCheckRuleAction(dataprovider.EventActionUserInactivity{
|
|
|
|
DisableThreshold: 10,
|
|
|
|
}, dataprovider.ConditionOptions{
|
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: user1.Username,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, &EventParams{}, time.Now().Add(30*24*time.Hour))
|
|
|
|
assert.NoError(t, err) // already disabled, no action
|
|
|
|
|
|
|
|
err = executeUserInactivityCheckRuleAction(dataprovider.EventActionUserInactivity{
|
|
|
|
DisableThreshold: 10,
|
|
|
|
DeleteThreshold: 20,
|
|
|
|
}, dataprovider.ConditionOptions{
|
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: user1.Username,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, &EventParams{}, time.Now().Add(-30*24*time.Hour))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = executeUserInactivityCheckRuleAction(dataprovider.EventActionUserInactivity{
|
|
|
|
DisableThreshold: 10,
|
|
|
|
DeleteThreshold: 20,
|
|
|
|
}, dataprovider.ConditionOptions{
|
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: user1.Username,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, &EventParams{}, time.Now())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
user1, err = dataprovider.UserExists(username1, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 0, user1.Status)
|
|
|
|
|
|
|
|
err = executeUserInactivityCheckRuleAction(dataprovider.EventActionUserInactivity{
|
|
|
|
DisableThreshold: 10,
|
|
|
|
DeleteThreshold: 20,
|
|
|
|
}, dataprovider.ConditionOptions{
|
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: user1.Username,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, &EventParams{}, time.Now().Add(30*24*time.Hour)) // the user is disabled, will be now deleted
|
|
|
|
assert.Error(t, err)
|
|
|
|
_, err = dataprovider.UserExists(username1, "")
|
|
|
|
assert.ErrorIs(t, err, util.ErrNotFound)
|
|
|
|
|
|
|
|
err = executeUserInactivityCheckRuleAction(dataprovider.EventActionUserInactivity{
|
|
|
|
DeleteThreshold: 20,
|
|
|
|
}, dataprovider.ConditionOptions{
|
|
|
|
Names: []dataprovider.ConditionPattern{
|
|
|
|
{
|
|
|
|
Pattern: user2.Username,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, &EventParams{}, time.Now().Add(30*24*time.Hour)) // no disable threshold, user deleted
|
|
|
|
assert.Error(t, err)
|
|
|
|
_, err = dataprovider.UserExists(username2, "")
|
|
|
|
assert.ErrorIs(t, err, util.ErrNotFound)
|
|
|
|
|
|
|
|
err = dataprovider.DeleteUser(username1, "", "", "")
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = dataprovider.DeleteUser(username2, "", "", "")
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|