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/>.
|
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
package httpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-12-23 12:08:04 +00:00
|
|
|
"fmt"
|
2022-07-11 06:17:36 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/go-chi/render"
|
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/util"
|
2022-07-11 06:17:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func getEventActions(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
limit, offset, order, err := getSearchFilters(w, r)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
actions, err := dataprovider.GetEventActions(limit, offset, order, false)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
render.JSON(w, r, actions)
|
|
|
|
}
|
|
|
|
|
|
|
|
func renderEventAction(w http.ResponseWriter, r *http.Request, name string, status int) {
|
|
|
|
action, err := dataprovider.EventActionExists(name)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
action.PrepareForRendering()
|
|
|
|
if status != http.StatusOK {
|
|
|
|
ctx := context.WithValue(r.Context(), render.StatusCtxKey, status)
|
|
|
|
render.JSON(w, r.WithContext(ctx), action)
|
|
|
|
} else {
|
|
|
|
render.JSON(w, r, action)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getEventActionByName(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
name := getURLParam(r, "name")
|
|
|
|
renderEventAction(w, r, name, http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func addEventAction(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var action dataprovider.BaseEventAction
|
|
|
|
err = render.DecodeJSON(r.Body, &action)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2022-12-23 12:08:04 +00:00
|
|
|
ipAddr := util.GetIPFromRemoteAddress(r.RemoteAddr)
|
|
|
|
err = dataprovider.AddEventAction(&action, claims.Username, ipAddr, claims.Role)
|
2022-07-11 06:17:36 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
2022-12-23 12:08:04 +00:00
|
|
|
w.Header().Add("Location", fmt.Sprintf("%s/%s", eventActionsPath, action.Name))
|
2022-07-11 06:17:36 +00:00
|
|
|
renderEventAction(w, r, action.Name, http.StatusCreated)
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateEventAction(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
name := getURLParam(r, "name")
|
|
|
|
action, err := dataprovider.EventActionExists(name)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-23 08:36:20 +00:00
|
|
|
var updatedAction dataprovider.BaseEventAction
|
|
|
|
err = render.DecodeJSON(r.Body, &updatedAction)
|
2022-07-11 06:17:36 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2022-12-23 08:36:20 +00:00
|
|
|
updatedAction.ID = action.ID
|
|
|
|
updatedAction.Name = action.Name
|
|
|
|
updatedAction.Options.SetEmptySecretsIfNil()
|
|
|
|
|
|
|
|
switch updatedAction.Type {
|
2022-07-11 06:17:36 +00:00
|
|
|
case dataprovider.ActionTypeHTTP:
|
2022-12-23 08:36:20 +00:00
|
|
|
if updatedAction.Options.HTTPConfig.Password.IsNotPlainAndNotEmpty() {
|
|
|
|
updatedAction.Options.HTTPConfig.Password = action.Options.HTTPConfig.Password
|
2022-07-11 06:17:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-23 08:36:20 +00:00
|
|
|
err = dataprovider.UpdateEventAction(&updatedAction, claims.Username, util.GetIPFromRemoteAddress(r.RemoteAddr), claims.Role)
|
2022-07-11 06:17:36 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
2022-12-23 08:36:20 +00:00
|
|
|
sendAPIResponse(w, r, nil, "Event action updated", http.StatusOK)
|
2022-07-11 06:17:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func deleteEventAction(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
name := getURLParam(r, "name")
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.DeleteEventAction(name, claims.Username, util.GetIPFromRemoteAddress(r.RemoteAddr), claims.Role)
|
2022-07-11 06:17:36 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sendAPIResponse(w, r, err, "Event action deleted", http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getEventRules(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
limit, offset, order, err := getSearchFilters(w, r)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rules, err := dataprovider.GetEventRules(limit, offset, order)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
render.JSON(w, r, rules)
|
|
|
|
}
|
|
|
|
|
|
|
|
func renderEventRule(w http.ResponseWriter, r *http.Request, name string, status int) {
|
|
|
|
rule, err := dataprovider.EventRuleExists(name)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rule.PrepareForRendering()
|
|
|
|
if status != http.StatusOK {
|
|
|
|
ctx := context.WithValue(r.Context(), render.StatusCtxKey, status)
|
|
|
|
render.JSON(w, r.WithContext(ctx), rule)
|
|
|
|
} else {
|
|
|
|
render.JSON(w, r, rule)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getEventRuleByName(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
name := getURLParam(r, "name")
|
|
|
|
renderEventRule(w, r, name, http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func addEventRule(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var rule dataprovider.EventRule
|
|
|
|
err = render.DecodeJSON(r.Body, &rule)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2022-12-23 12:08:04 +00:00
|
|
|
ipAddr := util.GetIPFromRemoteAddress(r.RemoteAddr)
|
|
|
|
if err := dataprovider.AddEventRule(&rule, claims.Username, ipAddr, claims.Role); err != nil {
|
2022-07-11 06:17:36 +00:00
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
2022-12-23 12:08:04 +00:00
|
|
|
w.Header().Add("Location", fmt.Sprintf("%s/%s", eventRulesPath, rule.Name))
|
2022-07-11 06:17:36 +00:00
|
|
|
renderEventRule(w, r, rule.Name, http.StatusCreated)
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateEventRule(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-23 08:36:20 +00:00
|
|
|
rule, err := dataprovider.EventRuleExists(getURLParam(r, "name"))
|
2022-07-11 06:17:36 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-23 08:36:20 +00:00
|
|
|
var updatedRule dataprovider.EventRule
|
|
|
|
err = render.DecodeJSON(r.Body, &updatedRule)
|
2022-07-11 06:17:36 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2022-12-23 08:36:20 +00:00
|
|
|
updatedRule.ID = rule.ID
|
|
|
|
updatedRule.Name = rule.Name
|
2022-07-11 06:17:36 +00:00
|
|
|
|
2022-12-23 08:36:20 +00:00
|
|
|
err = dataprovider.UpdateEventRule(&updatedRule, claims.Username, util.GetIPFromRemoteAddress(r.RemoteAddr), claims.Role)
|
2022-07-11 06:17:36 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sendAPIResponse(w, r, nil, "Event rules updated", http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteEventRule(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
name := getURLParam(r, "name")
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.DeleteEventRule(name, claims.Username, util.GetIPFromRemoteAddress(r.RemoteAddr), claims.Role)
|
2022-07-11 06:17:36 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sendAPIResponse(w, r, err, "Event rule deleted", http.StatusOK)
|
|
|
|
}
|