mirror of
https://github.com/drakkan/sftpgo.git
synced 2025-12-07 06:40:54 +03:00
eventmanager: add support for file/directory compression
Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -28,6 +29,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/klauspost/compress/zip"
|
||||
"github.com/sftpgo/sdk"
|
||||
sdkkms "github.com/sftpgo/sdk/kms"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -333,6 +335,8 @@ func TestEventManagerErrors(t *testing.T) {
|
||||
assert.Error(t, err)
|
||||
err = executeExistFsRuleAction(nil, nil, dataprovider.ConditionOptions{}, &EventParams{})
|
||||
assert.Error(t, err)
|
||||
err = executeCompressFsRuleAction(dataprovider.EventActionFsCompress{}, nil, dataprovider.ConditionOptions{}, &EventParams{})
|
||||
assert.Error(t, err)
|
||||
|
||||
groupName := "agroup"
|
||||
err = executeQuotaResetForUser(dataprovider.User{
|
||||
@@ -398,6 +402,15 @@ func TestEventManagerErrors(t *testing.T) {
|
||||
},
|
||||
})
|
||||
assert.Error(t, err)
|
||||
err = executeCompressFsActionForUser(dataprovider.EventActionFsCompress{}, nil, dataprovider.User{
|
||||
Groups: []sdk.GroupMapping{
|
||||
{
|
||||
Name: groupName,
|
||||
Type: sdk.GroupTypePrimary,
|
||||
},
|
||||
},
|
||||
})
|
||||
assert.Error(t, err)
|
||||
_, err = getMailAttachments(dataprovider.User{
|
||||
Groups: []sdk.GroupMapping{
|
||||
{
|
||||
@@ -576,9 +589,8 @@ func TestEventRuleActions(t *testing.T) {
|
||||
},
|
||||
}
|
||||
err = executeRuleAction(action, params, dataprovider.ConditionOptions{})
|
||||
if assert.Error(t, err) {
|
||||
assert.Contains(t, err.Error(), "error getting user")
|
||||
}
|
||||
assert.Contains(t, getErrorString(err), "error getting user")
|
||||
|
||||
action.Options.HTTPConfig.Parts = nil
|
||||
action.Options.HTTPConfig.Body = "{{ObjectData}}"
|
||||
// test disk and transfer quota reset
|
||||
@@ -656,9 +668,8 @@ func TestEventRuleActions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
})
|
||||
if assert.Error(t, err) {
|
||||
assert.Contains(t, err.Error(), "no user quota reset executed")
|
||||
}
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, getErrorString(err), "no user quota reset executed")
|
||||
|
||||
action = dataprovider.BaseEventAction{
|
||||
Type: dataprovider.ActionTypeMetadataCheck,
|
||||
@@ -671,9 +682,8 @@ func TestEventRuleActions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
})
|
||||
if assert.Error(t, err) {
|
||||
assert.Contains(t, err.Error(), "no metadata check executed")
|
||||
}
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, getErrorString(err), "no metadata check executed")
|
||||
|
||||
err = executeRuleAction(action, &EventParams{}, dataprovider.ConditionOptions{
|
||||
Names: []dataprovider.ConditionPattern{
|
||||
@@ -784,9 +794,9 @@ func TestEventRuleActions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
})
|
||||
if assert.Error(t, err) {
|
||||
assert.Contains(t, err.Error(), "no retention check executed")
|
||||
}
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, getErrorString(err), "no retention check executed")
|
||||
|
||||
// test file exists action
|
||||
action = dataprovider.BaseEventAction{
|
||||
Type: dataprovider.ActionTypeFilesystem,
|
||||
@@ -804,9 +814,9 @@ func TestEventRuleActions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
})
|
||||
if assert.Error(t, err) {
|
||||
assert.Contains(t, err.Error(), "no existence check executed")
|
||||
}
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, getErrorString(err), "no existence check executed")
|
||||
|
||||
err = executeRuleAction(action, &EventParams{}, dataprovider.ConditionOptions{
|
||||
Names: []dataprovider.ConditionPattern{
|
||||
{
|
||||
@@ -852,9 +862,9 @@ func TestEventRuleActions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
})
|
||||
if assert.Error(t, err) {
|
||||
assert.Contains(t, err.Error(), "no transfer quota reset executed")
|
||||
}
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, getErrorString(err), "no transfer quota reset executed")
|
||||
|
||||
action.Type = dataprovider.ActionTypeFilesystem
|
||||
action.Options = dataprovider.BaseEventActionOptions{
|
||||
FsConfig: dataprovider.EventActionFilesystemConfig{
|
||||
@@ -874,9 +884,9 @@ func TestEventRuleActions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
})
|
||||
if assert.Error(t, err) {
|
||||
assert.Contains(t, err.Error(), "no rename executed")
|
||||
}
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, getErrorString(err), "no rename executed")
|
||||
|
||||
action.Options = dataprovider.BaseEventActionOptions{
|
||||
FsConfig: dataprovider.EventActionFilesystemConfig{
|
||||
Type: dataprovider.FilesystemActionDelete,
|
||||
@@ -890,9 +900,9 @@ func TestEventRuleActions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
})
|
||||
if assert.Error(t, err) {
|
||||
assert.Contains(t, err.Error(), "no delete executed")
|
||||
}
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, getErrorString(err), "no delete executed")
|
||||
|
||||
action.Options = dataprovider.BaseEventActionOptions{
|
||||
FsConfig: dataprovider.EventActionFilesystemConfig{
|
||||
Type: dataprovider.FilesystemActionMkdirs,
|
||||
@@ -906,9 +916,37 @@ func TestEventRuleActions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
})
|
||||
if assert.Error(t, err) {
|
||||
assert.Contains(t, err.Error(), "no mkdir executed")
|
||||
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}}"},
|
||||
},
|
||||
},
|
||||
}
|
||||
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")
|
||||
|
||||
err = dataprovider.DeleteUser(username1, "", "")
|
||||
assert.NoError(t, err)
|
||||
@@ -1164,6 +1202,10 @@ func TestFilesystemActionErrors(t *testing.T) {
|
||||
assert.Error(t, err)
|
||||
err = executeExistFsActionForUser(nil, testReplacer, user)
|
||||
assert.Error(t, err)
|
||||
err = executeCompressFsActionForUser(dataprovider.EventActionFsCompress{}, testReplacer, user)
|
||||
assert.Error(t, err)
|
||||
_, _, _, _, err = getFileWriter(conn, "/path.txt") //nolint:dogsled
|
||||
assert.Error(t, err)
|
||||
err = executeEmailRuleAction(dataprovider.EventActionEmailConfig{
|
||||
Recipients: []string{"test@example.net"},
|
||||
Subject: "subject",
|
||||
@@ -1230,7 +1272,7 @@ func TestFilesystemActionErrors(t *testing.T) {
|
||||
err := os.MkdirAll(dirPath, os.ModePerm)
|
||||
assert.NoError(t, err)
|
||||
filePath := filepath.Join(dirPath, "f.dat")
|
||||
err = os.WriteFile(filePath, nil, 0666)
|
||||
err = os.WriteFile(filePath, []byte("test file content"), 0666)
|
||||
assert.NoError(t, err)
|
||||
err = os.Chmod(dirPath, 0001)
|
||||
assert.NoError(t, err)
|
||||
@@ -1290,6 +1332,16 @@ func TestFilesystemActionErrors(t *testing.T) {
|
||||
|
||||
err = os.Chmod(dirPath, os.ModePerm)
|
||||
assert.NoError(t, err)
|
||||
|
||||
conn = NewBaseConnection("", protocolEventAction, "", "", user)
|
||||
wr := &zipWriterWrapper{
|
||||
Name: "test.zip",
|
||||
Writer: zip.NewWriter(bytes.NewBuffer(nil)),
|
||||
Entries: map[string]bool{},
|
||||
}
|
||||
err = addZipEntry(wr, conn, "/adir/sub/f.dat", "/adir/sub/sub")
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, getErrorString(err), "is outside base dir")
|
||||
}
|
||||
|
||||
err = dataprovider.DeleteUser(username, "", "")
|
||||
@@ -1545,3 +1597,10 @@ func TestWriteHTTPPartsError(t *testing.T) {
|
||||
}, nil, nil, nil, &EventParams{})
|
||||
assert.ErrorIs(t, err, io.ErrUnexpectedEOF)
|
||||
}
|
||||
|
||||
func getErrorString(err error) string {
|
||||
if err == nil {
|
||||
return ""
|
||||
}
|
||||
return err.Error()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user