2022-07-17 18:16:00 +00:00
|
|
|
// Copyright (C) 2019-2022 Nicola Murino
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2021-10-10 11:08:05 +00:00
|
|
|
package dataprovider
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-01-06 10:54:43 +00:00
|
|
|
"github.com/sftpgo/sdk/plugin/notifier"
|
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/command"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/httpclient"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/logger"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/plugin"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/util"
|
2021-10-10 11:08:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// ActionExecutorSelf is used as username for self action, for example a user/admin that updates itself
|
|
|
|
ActionExecutorSelf = "__self__"
|
|
|
|
// ActionExecutorSystem is used as username for actions with no explicit executor associated, for example
|
|
|
|
// adding/updating a user/admin by loading initial data
|
|
|
|
ActionExecutorSystem = "__system__"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-07-11 06:17:36 +00:00
|
|
|
actionObjectUser = "user"
|
2022-07-24 06:10:23 +00:00
|
|
|
actionObjectFolder = "folder"
|
2022-07-11 06:17:36 +00:00
|
|
|
actionObjectGroup = "group"
|
|
|
|
actionObjectAdmin = "admin"
|
|
|
|
actionObjectAPIKey = "api_key"
|
|
|
|
actionObjectShare = "share"
|
|
|
|
actionObjectEventAction = "event_action"
|
|
|
|
actionObjectEventRule = "event_rule"
|
2021-10-10 11:08:05 +00:00
|
|
|
)
|
|
|
|
|
2022-08-10 16:41:59 +00:00
|
|
|
var (
|
|
|
|
actionsConcurrencyGuard = make(chan struct{}, 100)
|
2022-09-13 16:04:27 +00:00
|
|
|
reservedUsers = []string{ActionExecutorSelf, ActionExecutorSystem}
|
2022-08-10 16:41:59 +00:00
|
|
|
)
|
|
|
|
|
2021-10-10 11:08:05 +00:00
|
|
|
func executeAction(operation, executor, ip, objectType, objectName string, object plugin.Renderer) {
|
2022-01-02 14:16:35 +00:00
|
|
|
if plugin.Handler.HasNotifiers() {
|
|
|
|
plugin.Handler.NotifyProviderEvent(¬ifier.ProviderEvent{
|
|
|
|
Action: operation,
|
|
|
|
Username: executor,
|
|
|
|
ObjectType: objectType,
|
|
|
|
ObjectName: objectName,
|
|
|
|
IP: ip,
|
|
|
|
Timestamp: time.Now().UnixNano(),
|
|
|
|
}, object)
|
|
|
|
}
|
2022-08-01 16:48:54 +00:00
|
|
|
if fnHandleRuleForProviderEvent != nil {
|
|
|
|
fnHandleRuleForProviderEvent(operation, executor, ip, objectType, objectName, object)
|
2022-07-11 06:17:36 +00:00
|
|
|
}
|
2021-10-10 11:08:05 +00:00
|
|
|
if config.Actions.Hook == "" {
|
|
|
|
return
|
|
|
|
}
|
2022-05-19 17:49:51 +00:00
|
|
|
if !util.Contains(config.Actions.ExecuteOn, operation) ||
|
|
|
|
!util.Contains(config.Actions.ExecuteFor, objectType) {
|
2021-10-10 11:08:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2022-08-10 16:41:59 +00:00
|
|
|
actionsConcurrencyGuard <- struct{}{}
|
|
|
|
defer func() {
|
|
|
|
<-actionsConcurrencyGuard
|
|
|
|
}()
|
|
|
|
|
2021-10-10 11:08:05 +00:00
|
|
|
dataAsJSON, err := object.RenderAsJSON(operation != operationDelete)
|
|
|
|
if err != nil {
|
2021-12-16 18:53:00 +00:00
|
|
|
providerLog(logger.LevelError, "unable to serialize user as JSON for operation %#v: %v", operation, err)
|
2021-10-10 11:08:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(config.Actions.Hook, "http") {
|
|
|
|
var url *url.URL
|
|
|
|
url, err := url.Parse(config.Actions.Hook)
|
|
|
|
if err != nil {
|
2021-12-16 18:53:00 +00:00
|
|
|
providerLog(logger.LevelError, "Invalid http_notification_url %#v for operation %#v: %v",
|
|
|
|
config.Actions.Hook, operation, err)
|
2021-10-10 11:08:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
q := url.Query()
|
|
|
|
q.Add("action", operation)
|
|
|
|
q.Add("username", executor)
|
|
|
|
q.Add("ip", ip)
|
|
|
|
q.Add("object_type", objectType)
|
|
|
|
q.Add("object_name", objectName)
|
2022-07-11 06:17:36 +00:00
|
|
|
q.Add("timestamp", fmt.Sprintf("%d", time.Now().UnixNano()))
|
2021-10-10 11:08:05 +00:00
|
|
|
url.RawQuery = q.Encode()
|
|
|
|
startTime := time.Now()
|
|
|
|
resp, err := httpclient.RetryablePost(url.String(), "application/json", bytes.NewBuffer(dataAsJSON))
|
|
|
|
respCode := 0
|
|
|
|
if err == nil {
|
|
|
|
respCode = resp.StatusCode
|
|
|
|
resp.Body.Close()
|
|
|
|
}
|
|
|
|
providerLog(logger.LevelDebug, "notified operation %#v to URL: %v status code: %v, elapsed: %v err: %v",
|
|
|
|
operation, url.Redacted(), respCode, time.Since(startTime), err)
|
|
|
|
} else {
|
|
|
|
executeNotificationCommand(operation, executor, ip, objectType, objectName, dataAsJSON) //nolint:errcheck // the error is used in test cases only
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func executeNotificationCommand(operation, executor, ip, objectType, objectName string, objectAsJSON []byte) error {
|
|
|
|
if !filepath.IsAbs(config.Actions.Hook) {
|
|
|
|
err := fmt.Errorf("invalid notification command %#v", config.Actions.Hook)
|
|
|
|
logger.Warn(logSender, "", "unable to execute notification command: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-20 11:58:44 +00:00
|
|
|
timeout, env, args := command.GetConfig(config.Actions.Hook, command.HookProviderActions)
|
2022-05-20 17:30:54 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
2021-10-10 11:08:05 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2022-09-20 11:58:44 +00:00
|
|
|
cmd := exec.CommandContext(ctx, config.Actions.Hook, args...)
|
2022-05-20 17:30:54 +00:00
|
|
|
cmd.Env = append(env,
|
2022-07-11 06:17:36 +00:00
|
|
|
fmt.Sprintf("SFTPGO_PROVIDER_ACTION=%vs", operation),
|
|
|
|
fmt.Sprintf("SFTPGO_PROVIDER_OBJECT_TYPE=%s", objectType),
|
|
|
|
fmt.Sprintf("SFTPGO_PROVIDER_OBJECT_NAME=%s", objectName),
|
|
|
|
fmt.Sprintf("SFTPGO_PROVIDER_USERNAME=%s", executor),
|
|
|
|
fmt.Sprintf("SFTPGO_PROVIDER_IP=%s", ip),
|
|
|
|
fmt.Sprintf("SFTPGO_PROVIDER_TIMESTAMP=%d", util.GetTimeAsMsSinceEpoch(time.Now())),
|
|
|
|
fmt.Sprintf("SFTPGO_PROVIDER_OBJECT=%s", string(objectAsJSON)))
|
2021-10-10 11:08:05 +00:00
|
|
|
|
|
|
|
startTime := time.Now()
|
|
|
|
err := cmd.Run()
|
|
|
|
providerLog(logger.LevelDebug, "executed command %#v, elapsed: %v, error: %v", config.Actions.Hook,
|
|
|
|
time.Since(startTime), err)
|
|
|
|
return err
|
|
|
|
}
|