sftpgo/httpd/api_events.go

143 lines
4.2 KiB
Go
Raw Normal View History

2021-10-23 13:47:21 +00:00
package httpd
import (
"fmt"
"net/http"
"strconv"
"github.com/sftpgo/sdk/plugin/eventsearcher"
2021-10-23 13:47:21 +00:00
"github.com/drakkan/sftpgo/v2/dataprovider"
"github.com/drakkan/sftpgo/v2/plugin"
2021-10-23 13:47:21 +00:00
"github.com/drakkan/sftpgo/v2/util"
)
func getCommonSearchParamsFromRequest(r *http.Request) (eventsearcher.CommonSearchParams, error) {
c := eventsearcher.CommonSearchParams{}
2021-10-23 13:47:21 +00:00
c.Limit = 100
if _, ok := r.URL.Query()["limit"]; ok {
limit, err := strconv.Atoi(r.URL.Query().Get("limit"))
if err != nil {
return c, util.NewValidationError(fmt.Sprintf("invalid limit: %v", err))
2021-10-23 13:47:21 +00:00
}
if limit < 1 || limit > 1000 {
return c, util.NewValidationError(fmt.Sprintf("limit is out of the 1-1000 range: %v", limit))
2021-10-23 13:47:21 +00:00
}
c.Limit = limit
}
if _, ok := r.URL.Query()["order"]; ok {
order := r.URL.Query().Get("order")
if order != dataprovider.OrderASC && order != dataprovider.OrderDESC {
return c, util.NewValidationError(fmt.Sprintf("invalid order %#v", order))
2021-10-23 13:47:21 +00:00
}
if order == dataprovider.OrderASC {
c.Order = 1
}
}
if _, ok := r.URL.Query()["start_timestamp"]; ok {
ts, err := strconv.ParseInt(r.URL.Query().Get("start_timestamp"), 10, 64)
if err != nil {
return c, util.NewValidationError(fmt.Sprintf("invalid start_timestamp: %v", err))
2021-10-23 13:47:21 +00:00
}
c.StartTimestamp = ts
}
if _, ok := r.URL.Query()["end_timestamp"]; ok {
ts, err := strconv.ParseInt(r.URL.Query().Get("end_timestamp"), 10, 64)
if err != nil {
return c, util.NewValidationError(fmt.Sprintf("invalid end_timestamp: %v", err))
2021-10-23 13:47:21 +00:00
}
c.EndTimestamp = ts
}
c.Actions = getCommaSeparatedQueryParam(r, "actions")
c.Username = r.URL.Query().Get("username")
c.IP = r.URL.Query().Get("ip")
c.InstanceIDs = getCommaSeparatedQueryParam(r, "instance_ids")
c.ExcludeIDs = getCommaSeparatedQueryParam(r, "exclude_ids")
return c, nil
2021-10-23 13:47:21 +00:00
}
func getFsSearchParamsFromRequest(r *http.Request) (eventsearcher.FsEventSearch, error) {
var err error
s := eventsearcher.FsEventSearch{}
s.CommonSearchParams, err = getCommonSearchParamsFromRequest(r)
if err != nil {
return s, err
}
s.FsProvider = -1
if _, ok := r.URL.Query()["fs_provider"]; ok {
provider := r.URL.Query().Get("fs_provider")
val, err := strconv.Atoi(provider)
if err != nil {
return s, util.NewValidationError(fmt.Sprintf("invalid fs_provider: %v", provider))
}
s.FsProvider = val
2021-10-23 13:47:21 +00:00
}
s.IP = r.URL.Query().Get("ip")
s.SSHCmd = r.URL.Query().Get("ssh_cmd")
s.Bucket = r.URL.Query().Get("bucket")
s.Endpoint = r.URL.Query().Get("endpoint")
2021-10-23 13:47:21 +00:00
s.Protocols = getCommaSeparatedQueryParam(r, "protocols")
statuses := getCommaSeparatedQueryParam(r, "statuses")
for _, status := range statuses {
val, err := strconv.Atoi(status)
if err != nil {
return s, util.NewValidationError(fmt.Sprintf("invalid status: %v", status))
2021-10-23 13:47:21 +00:00
}
s.Statuses = append(s.Statuses, int32(val))
}
return s, nil
2021-10-23 13:47:21 +00:00
}
func getProviderSearchParamsFromRequest(r *http.Request) (eventsearcher.ProviderEventSearch, error) {
var err error
s := eventsearcher.ProviderEventSearch{}
s.CommonSearchParams, err = getCommonSearchParamsFromRequest(r)
if err != nil {
return s, err
2021-10-23 13:47:21 +00:00
}
s.ObjectName = r.URL.Query().Get("object_name")
s.ObjectTypes = getCommaSeparatedQueryParam(r, "object_types")
return s, nil
2021-10-23 13:47:21 +00:00
}
func searchFsEvents(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
filters, err := getFsSearchParamsFromRequest(r)
2021-10-23 13:47:21 +00:00
if err != nil {
sendAPIResponse(w, r, err, "", getRespStatus(err))
return
}
data, _, _, err := plugin.Handler.SearchFsEvents(&filters)
2021-10-23 13:47:21 +00:00
if err != nil {
sendAPIResponse(w, r, err, "", getRespStatus(err))
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write(data) //nolint:errcheck
}
func searchProviderEvents(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
filters, err := getProviderSearchParamsFromRequest(r)
2021-10-23 13:47:21 +00:00
if err != nil {
sendAPIResponse(w, r, err, "", getRespStatus(err))
return
}
data, _, _, err := plugin.Handler.SearchProviderEvents(&filters)
2021-10-23 13:47:21 +00:00
if err != nil {
sendAPIResponse(w, r, err, "", getRespStatus(err))
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write(data) //nolint:errcheck
}