2019-10-07 16:19:01 +00:00
|
|
|
package httpd
|
2019-07-20 10:26:52 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-01-31 22:26:56 +00:00
|
|
|
"context"
|
2019-07-20 10:26:52 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-02-03 23:08:00 +00:00
|
|
|
"io"
|
2019-07-20 10:26:52 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2019-07-27 18:42:45 +00:00
|
|
|
"net/url"
|
2019-12-27 22:12:44 +00:00
|
|
|
"os"
|
2019-08-05 13:17:43 +00:00
|
|
|
"path"
|
2020-02-23 10:30:26 +00:00
|
|
|
"path/filepath"
|
2019-07-20 10:26:52 +00:00
|
|
|
"strconv"
|
2019-08-05 13:17:43 +00:00
|
|
|
"strings"
|
2019-07-20 10:26:52 +00:00
|
|
|
|
2020-05-06 17:36:34 +00:00
|
|
|
"github.com/go-chi/render"
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
"github.com/drakkan/sftpgo/common"
|
2019-07-20 10:26:52 +00:00
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
2020-04-26 21:29:09 +00:00
|
|
|
"github.com/drakkan/sftpgo/httpclient"
|
2020-11-30 20:46:34 +00:00
|
|
|
"github.com/drakkan/sftpgo/kms"
|
2019-07-20 10:26:52 +00:00
|
|
|
"github.com/drakkan/sftpgo/utils"
|
2020-06-19 15:08:51 +00:00
|
|
|
"github.com/drakkan/sftpgo/version"
|
2020-06-07 21:30:18 +00:00
|
|
|
"github.com/drakkan/sftpgo/vfs"
|
2019-07-20 10:26:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-02-03 23:08:00 +00:00
|
|
|
httpBaseURL = "http://127.0.0.1:8080"
|
|
|
|
authUsername = ""
|
|
|
|
authPassword = ""
|
2019-07-20 10:26:52 +00:00
|
|
|
)
|
|
|
|
|
2020-02-03 23:08:00 +00:00
|
|
|
// SetBaseURLAndCredentials sets the base url and the optional credentials to use for HTTP requests.
|
|
|
|
// Default URL is "http://127.0.0.1:8080" with empty credentials
|
|
|
|
func SetBaseURLAndCredentials(url, username, password string) {
|
2019-07-26 09:34:44 +00:00
|
|
|
httpBaseURL = url
|
2020-02-03 23:08:00 +00:00
|
|
|
authUsername = username
|
|
|
|
authPassword = password
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 23:08:00 +00:00
|
|
|
func sendHTTPRequest(method, url string, body io.Reader, contentType string) (*http.Response, error) {
|
|
|
|
req, err := http.NewRequest(method, url, body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-18 15:04:42 +00:00
|
|
|
if contentType != "" {
|
2020-02-03 23:08:00 +00:00
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
}
|
2020-12-18 15:04:42 +00:00
|
|
|
if authUsername != "" || authPassword != "" {
|
2020-02-03 23:08:00 +00:00
|
|
|
req.SetBasicAuth(authUsername, authPassword)
|
|
|
|
}
|
2020-04-26 21:29:09 +00:00
|
|
|
return httpclient.GetHTTPClient().Do(req)
|
2020-02-03 23:08:00 +00:00
|
|
|
}
|
|
|
|
|
2019-08-05 13:17:43 +00:00
|
|
|
func buildURLRelativeToBase(paths ...string) string {
|
|
|
|
// we need to use path.Join and not filepath.Join
|
|
|
|
// since filepath.Join will use backslash separator on Windows
|
|
|
|
p := path.Join(paths...)
|
|
|
|
return fmt.Sprintf("%s/%s", strings.TrimRight(httpBaseURL, "/"), strings.TrimLeft(p, "/"))
|
|
|
|
}
|
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
func sendAPIResponse(w http.ResponseWriter, r *http.Request, err error, message string, code int) {
|
|
|
|
var errorString string
|
|
|
|
if err != nil {
|
|
|
|
errorString = err.Error()
|
|
|
|
}
|
|
|
|
resp := apiResponse{
|
2020-09-08 07:45:21 +00:00
|
|
|
Error: errorString,
|
|
|
|
Message: message,
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
2020-01-31 22:26:56 +00:00
|
|
|
ctx := context.WithValue(r.Context(), render.StatusCtxKey, code)
|
|
|
|
render.JSON(w, r.WithContext(ctx), resp)
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getRespStatus(err error) int {
|
|
|
|
if _, ok := err.(*dataprovider.ValidationError); ok {
|
|
|
|
return http.StatusBadRequest
|
|
|
|
}
|
|
|
|
if _, ok := err.(*dataprovider.MethodDisabledError); ok {
|
|
|
|
return http.StatusForbidden
|
|
|
|
}
|
2020-06-20 10:38:04 +00:00
|
|
|
if _, ok := err.(*dataprovider.RecordNotFoundError); ok {
|
|
|
|
return http.StatusNotFound
|
|
|
|
}
|
2019-12-27 22:12:44 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return http.StatusBadRequest
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
return http.StatusInternalServerError
|
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// AddUser adds a new user and checks the received HTTP Status code against expectedStatusCode.
|
2019-08-04 19:37:31 +00:00
|
|
|
func AddUser(user dataprovider.User, expectedStatusCode int) (dataprovider.User, []byte, error) {
|
2019-07-20 10:26:52 +00:00
|
|
|
var newUser dataprovider.User
|
2019-08-04 19:37:31 +00:00
|
|
|
var body []byte
|
2020-06-07 21:30:18 +00:00
|
|
|
userAsJSON, _ := json.Marshal(user)
|
2020-02-03 23:08:00 +00:00
|
|
|
resp, err := sendHTTPRequest(http.MethodPost, buildURLRelativeToBase(userPath), bytes.NewBuffer(userAsJSON),
|
|
|
|
"application/json")
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2019-08-04 19:37:31 +00:00
|
|
|
return newUser, body, err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2019-08-04 19:37:31 +00:00
|
|
|
err = checkResponse(resp.StatusCode, expectedStatusCode)
|
2019-07-20 10:26:52 +00:00
|
|
|
if expectedStatusCode != http.StatusOK {
|
2019-08-04 19:37:31 +00:00
|
|
|
body, _ = getResponseBody(resp)
|
|
|
|
return newUser, body, err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
err = render.DecodeJSON(resp.Body, &newUser)
|
2019-08-04 19:37:31 +00:00
|
|
|
} else {
|
|
|
|
body, _ = getResponseBody(resp)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
if err == nil {
|
2019-12-30 17:37:50 +00:00
|
|
|
err = checkUser(&user, &newUser)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-08-04 19:37:31 +00:00
|
|
|
return newUser, body, err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// UpdateUser updates an existing user and checks the received HTTP Status code against expectedStatusCode.
|
2020-09-01 14:10:26 +00:00
|
|
|
func UpdateUser(user dataprovider.User, expectedStatusCode int, disconnect string) (dataprovider.User, []byte, error) {
|
2019-07-20 10:26:52 +00:00
|
|
|
var newUser dataprovider.User
|
2019-08-04 19:37:31 +00:00
|
|
|
var body []byte
|
2020-09-01 14:10:26 +00:00
|
|
|
url, err := addDisconnectQueryParam(buildURLRelativeToBase(userPath, strconv.FormatInt(user.ID, 10)), disconnect)
|
|
|
|
if err != nil {
|
|
|
|
return user, body, err
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
userAsJSON, _ := json.Marshal(user)
|
2020-09-01 14:10:26 +00:00
|
|
|
resp, err := sendHTTPRequest(http.MethodPut, url.String(), bytes.NewBuffer(userAsJSON), "application/json")
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2019-08-04 19:37:31 +00:00
|
|
|
return user, body, err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2019-08-04 19:37:31 +00:00
|
|
|
body, _ = getResponseBody(resp)
|
|
|
|
err = checkResponse(resp.StatusCode, expectedStatusCode)
|
2019-07-20 10:26:52 +00:00
|
|
|
if expectedStatusCode != http.StatusOK {
|
2019-08-04 19:37:31 +00:00
|
|
|
return newUser, body, err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
if err == nil {
|
2019-08-04 19:37:31 +00:00
|
|
|
newUser, body, err = GetUserByID(user.ID, expectedStatusCode)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
if err == nil {
|
2019-12-30 17:37:50 +00:00
|
|
|
err = checkUser(&user, &newUser)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-08-04 19:37:31 +00:00
|
|
|
return newUser, body, err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// RemoveUser removes an existing user and checks the received HTTP Status code against expectedStatusCode.
|
2019-08-04 19:37:31 +00:00
|
|
|
func RemoveUser(user dataprovider.User, expectedStatusCode int) ([]byte, error) {
|
|
|
|
var body []byte
|
2020-02-03 23:08:00 +00:00
|
|
|
resp, err := sendHTTPRequest(http.MethodDelete, buildURLRelativeToBase(userPath, strconv.FormatInt(user.ID, 10)), nil, "")
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2019-08-04 19:37:31 +00:00
|
|
|
return body, err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2019-08-04 19:37:31 +00:00
|
|
|
body, _ = getResponseBody(resp)
|
|
|
|
return body, checkResponse(resp.StatusCode, expectedStatusCode)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 22:26:22 +00:00
|
|
|
// GetUserByID gets a user by database id and checks the received HTTP Status code against expectedStatusCode.
|
2019-08-04 19:37:31 +00:00
|
|
|
func GetUserByID(userID int64, expectedStatusCode int) (dataprovider.User, []byte, error) {
|
2019-07-20 10:26:52 +00:00
|
|
|
var user dataprovider.User
|
2019-08-04 19:37:31 +00:00
|
|
|
var body []byte
|
2020-02-03 23:08:00 +00:00
|
|
|
resp, err := sendHTTPRequest(http.MethodGet, buildURLRelativeToBase(userPath, strconv.FormatInt(userID, 10)), nil, "")
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2019-08-04 19:37:31 +00:00
|
|
|
return user, body, err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2019-08-04 19:37:31 +00:00
|
|
|
err = checkResponse(resp.StatusCode, expectedStatusCode)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err == nil && expectedStatusCode == http.StatusOK {
|
|
|
|
err = render.DecodeJSON(resp.Body, &user)
|
2019-08-04 19:37:31 +00:00
|
|
|
} else {
|
|
|
|
body, _ = getResponseBody(resp)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-08-04 19:37:31 +00:00
|
|
|
return user, body, err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
// GetUsers returns a list of users and checks the received HTTP Status code against expectedStatusCode.
|
2019-07-30 18:51:29 +00:00
|
|
|
// The number of results can be limited specifying a limit.
|
|
|
|
// Some results can be skipped specifying an offset.
|
2020-03-27 22:26:22 +00:00
|
|
|
// The results can be filtered specifying a username, the username filter is an exact match
|
2020-06-07 21:30:18 +00:00
|
|
|
func GetUsers(limit, offset int64, username string, expectedStatusCode int) ([]dataprovider.User, []byte, error) {
|
2019-07-20 10:26:52 +00:00
|
|
|
var users []dataprovider.User
|
2019-08-04 19:37:31 +00:00
|
|
|
var body []byte
|
2020-06-07 21:30:18 +00:00
|
|
|
url, err := addLimitAndOffsetQueryParams(buildURLRelativeToBase(userPath), limit, offset)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2019-08-04 19:37:31 +00:00
|
|
|
return users, body, err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
if len(username) > 0 {
|
2020-06-07 21:30:18 +00:00
|
|
|
q := url.Query()
|
2019-07-20 10:26:52 +00:00
|
|
|
q.Add("username", username)
|
2020-06-07 21:30:18 +00:00
|
|
|
url.RawQuery = q.Encode()
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2020-02-03 23:08:00 +00:00
|
|
|
resp, err := sendHTTPRequest(http.MethodGet, url.String(), nil, "")
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2019-08-04 19:37:31 +00:00
|
|
|
return users, body, err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2019-08-04 19:37:31 +00:00
|
|
|
err = checkResponse(resp.StatusCode, expectedStatusCode)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err == nil && expectedStatusCode == http.StatusOK {
|
|
|
|
err = render.DecodeJSON(resp.Body, &users)
|
2019-08-04 19:37:31 +00:00
|
|
|
} else {
|
|
|
|
body, _ = getResponseBody(resp)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-08-04 19:37:31 +00:00
|
|
|
return users, body, err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
// GetQuotaScans gets active quota scans for users and checks the received HTTP Status code against expectedStatusCode.
|
2020-07-24 21:39:38 +00:00
|
|
|
func GetQuotaScans(expectedStatusCode int) ([]common.ActiveQuotaScan, []byte, error) {
|
|
|
|
var quotaScans []common.ActiveQuotaScan
|
2019-08-04 19:37:31 +00:00
|
|
|
var body []byte
|
2020-02-03 23:08:00 +00:00
|
|
|
resp, err := sendHTTPRequest(http.MethodGet, buildURLRelativeToBase(quotaScanPath), nil, "")
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2019-08-04 19:37:31 +00:00
|
|
|
return quotaScans, body, err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2019-08-04 19:37:31 +00:00
|
|
|
err = checkResponse(resp.StatusCode, expectedStatusCode)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err == nil && expectedStatusCode == http.StatusOK {
|
|
|
|
err = render.DecodeJSON(resp.Body, "aScans)
|
2019-08-04 19:37:31 +00:00
|
|
|
} else {
|
|
|
|
body, _ = getResponseBody(resp)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-08-04 19:37:31 +00:00
|
|
|
return quotaScans, body, err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2020-06-20 10:38:04 +00:00
|
|
|
// StartQuotaScan starts a new quota scan for the given user and checks the received HTTP Status code against expectedStatusCode.
|
2019-08-04 19:37:31 +00:00
|
|
|
func StartQuotaScan(user dataprovider.User, expectedStatusCode int) ([]byte, error) {
|
|
|
|
var body []byte
|
2020-06-07 21:30:18 +00:00
|
|
|
userAsJSON, _ := json.Marshal(user)
|
2020-02-03 23:08:00 +00:00
|
|
|
resp, err := sendHTTPRequest(http.MethodPost, buildURLRelativeToBase(quotaScanPath), bytes.NewBuffer(userAsJSON), "")
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2019-08-04 19:37:31 +00:00
|
|
|
return body, err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2019-08-04 19:37:31 +00:00
|
|
|
body, _ = getResponseBody(resp)
|
|
|
|
return body, checkResponse(resp.StatusCode, expectedStatusCode)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2020-06-20 10:38:04 +00:00
|
|
|
// UpdateQuotaUsage updates the user used quota limits and checks the received HTTP Status code against expectedStatusCode.
|
|
|
|
func UpdateQuotaUsage(user dataprovider.User, mode string, expectedStatusCode int) ([]byte, error) {
|
|
|
|
var body []byte
|
|
|
|
userAsJSON, _ := json.Marshal(user)
|
|
|
|
url, err := addModeQueryParam(buildURLRelativeToBase(updateUsedQuotaPath), mode)
|
|
|
|
if err != nil {
|
|
|
|
return body, err
|
|
|
|
}
|
|
|
|
resp, err := sendHTTPRequest(http.MethodPut, url.String(), bytes.NewBuffer(userAsJSON), "")
|
|
|
|
if err != nil {
|
|
|
|
return body, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, _ = getResponseBody(resp)
|
|
|
|
return body, checkResponse(resp.StatusCode, expectedStatusCode)
|
|
|
|
}
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
// GetConnections returns status and stats for active SFTP/SCP connections
|
2020-07-24 21:39:38 +00:00
|
|
|
func GetConnections(expectedStatusCode int) ([]common.ConnectionStatus, []byte, error) {
|
|
|
|
var connections []common.ConnectionStatus
|
2019-08-04 19:37:31 +00:00
|
|
|
var body []byte
|
2020-02-03 23:08:00 +00:00
|
|
|
resp, err := sendHTTPRequest(http.MethodGet, buildURLRelativeToBase(activeConnectionsPath), nil, "")
|
2019-08-03 11:19:00 +00:00
|
|
|
if err != nil {
|
2019-08-04 19:37:31 +00:00
|
|
|
return connections, body, err
|
2019-08-03 11:19:00 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2019-08-04 19:37:31 +00:00
|
|
|
err = checkResponse(resp.StatusCode, expectedStatusCode)
|
2019-08-03 11:19:00 +00:00
|
|
|
if err == nil && expectedStatusCode == http.StatusOK {
|
|
|
|
err = render.DecodeJSON(resp.Body, &connections)
|
2019-08-04 19:37:31 +00:00
|
|
|
} else {
|
|
|
|
body, _ = getResponseBody(resp)
|
2019-08-03 11:19:00 +00:00
|
|
|
}
|
2019-08-04 19:37:31 +00:00
|
|
|
return connections, body, err
|
2019-08-03 11:19:00 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
// CloseConnection closes an active connection identified by connectionID
|
|
|
|
func CloseConnection(connectionID string, expectedStatusCode int) ([]byte, error) {
|
2019-08-04 19:37:31 +00:00
|
|
|
var body []byte
|
2020-02-03 23:08:00 +00:00
|
|
|
resp, err := sendHTTPRequest(http.MethodDelete, buildURLRelativeToBase(activeConnectionsPath, connectionID), nil, "")
|
2019-08-03 11:19:00 +00:00
|
|
|
if err != nil {
|
2019-08-04 19:37:31 +00:00
|
|
|
return body, err
|
2019-08-03 11:19:00 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2019-08-04 19:37:31 +00:00
|
|
|
err = checkResponse(resp.StatusCode, expectedStatusCode)
|
|
|
|
body, _ = getResponseBody(resp)
|
|
|
|
return body, err
|
2019-08-03 11:19:00 +00:00
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
// AddFolder adds a new folder and checks the received HTTP Status code against expectedStatusCode
|
|
|
|
func AddFolder(folder vfs.BaseVirtualFolder, expectedStatusCode int) (vfs.BaseVirtualFolder, []byte, error) {
|
|
|
|
var newFolder vfs.BaseVirtualFolder
|
|
|
|
var body []byte
|
|
|
|
folderAsJSON, _ := json.Marshal(folder)
|
|
|
|
resp, err := sendHTTPRequest(http.MethodPost, buildURLRelativeToBase(folderPath), bytes.NewBuffer(folderAsJSON),
|
|
|
|
"application/json")
|
|
|
|
if err != nil {
|
|
|
|
return newFolder, body, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
err = checkResponse(resp.StatusCode, expectedStatusCode)
|
|
|
|
if expectedStatusCode != http.StatusOK {
|
|
|
|
body, _ = getResponseBody(resp)
|
|
|
|
return newFolder, body, err
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
err = render.DecodeJSON(resp.Body, &newFolder)
|
|
|
|
} else {
|
|
|
|
body, _ = getResponseBody(resp)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
err = checkFolder(&folder, &newFolder)
|
|
|
|
}
|
|
|
|
return newFolder, body, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveFolder removes an existing user and checks the received HTTP Status code against expectedStatusCode.
|
|
|
|
func RemoveFolder(folder vfs.BaseVirtualFolder, expectedStatusCode int) ([]byte, error) {
|
|
|
|
var body []byte
|
|
|
|
baseURL := buildURLRelativeToBase(folderPath)
|
|
|
|
url, err := url.Parse(baseURL)
|
|
|
|
if err != nil {
|
|
|
|
return body, err
|
|
|
|
}
|
|
|
|
q := url.Query()
|
|
|
|
q.Add("folder_path", folder.MappedPath)
|
|
|
|
url.RawQuery = q.Encode()
|
|
|
|
resp, err := sendHTTPRequest(http.MethodDelete, url.String(), nil, "")
|
|
|
|
if err != nil {
|
|
|
|
return body, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, _ = getResponseBody(resp)
|
|
|
|
return body, checkResponse(resp.StatusCode, expectedStatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFolders returns a list of folders and checks the received HTTP Status code against expectedStatusCode.
|
|
|
|
// The number of results can be limited specifying a limit.
|
|
|
|
// Some results can be skipped specifying an offset.
|
|
|
|
// The results can be filtered specifying a folder path, the folder path filter is an exact match
|
|
|
|
func GetFolders(limit int64, offset int64, mappedPath string, expectedStatusCode int) ([]vfs.BaseVirtualFolder, []byte, error) {
|
|
|
|
var folders []vfs.BaseVirtualFolder
|
|
|
|
var body []byte
|
|
|
|
url, err := addLimitAndOffsetQueryParams(buildURLRelativeToBase(folderPath), limit, offset)
|
|
|
|
if err != nil {
|
|
|
|
return folders, body, err
|
|
|
|
}
|
|
|
|
if len(mappedPath) > 0 {
|
|
|
|
q := url.Query()
|
|
|
|
q.Add("folder_path", mappedPath)
|
|
|
|
url.RawQuery = q.Encode()
|
|
|
|
}
|
|
|
|
resp, err := sendHTTPRequest(http.MethodGet, url.String(), nil, "")
|
|
|
|
if err != nil {
|
|
|
|
return folders, body, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
err = checkResponse(resp.StatusCode, expectedStatusCode)
|
|
|
|
if err == nil && expectedStatusCode == http.StatusOK {
|
|
|
|
err = render.DecodeJSON(resp.Body, &folders)
|
|
|
|
} else {
|
|
|
|
body, _ = getResponseBody(resp)
|
|
|
|
}
|
|
|
|
return folders, body, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFoldersQuotaScans gets active quota scans for folders and checks the received HTTP Status code against expectedStatusCode.
|
2020-07-24 21:39:38 +00:00
|
|
|
func GetFoldersQuotaScans(expectedStatusCode int) ([]common.ActiveVirtualFolderQuotaScan, []byte, error) {
|
|
|
|
var quotaScans []common.ActiveVirtualFolderQuotaScan
|
2020-06-07 21:30:18 +00:00
|
|
|
var body []byte
|
|
|
|
resp, err := sendHTTPRequest(http.MethodGet, buildURLRelativeToBase(quotaScanVFolderPath), nil, "")
|
|
|
|
if err != nil {
|
|
|
|
return quotaScans, body, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
err = checkResponse(resp.StatusCode, expectedStatusCode)
|
|
|
|
if err == nil && expectedStatusCode == http.StatusOK {
|
|
|
|
err = render.DecodeJSON(resp.Body, "aScans)
|
|
|
|
} else {
|
|
|
|
body, _ = getResponseBody(resp)
|
|
|
|
}
|
|
|
|
return quotaScans, body, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartFolderQuotaScan start a new quota scan for the given folder and checks the received HTTP Status code against expectedStatusCode.
|
|
|
|
func StartFolderQuotaScan(folder vfs.BaseVirtualFolder, expectedStatusCode int) ([]byte, error) {
|
|
|
|
var body []byte
|
|
|
|
folderAsJSON, _ := json.Marshal(folder)
|
|
|
|
resp, err := sendHTTPRequest(http.MethodPost, buildURLRelativeToBase(quotaScanVFolderPath), bytes.NewBuffer(folderAsJSON), "")
|
|
|
|
if err != nil {
|
|
|
|
return body, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, _ = getResponseBody(resp)
|
|
|
|
return body, checkResponse(resp.StatusCode, expectedStatusCode)
|
|
|
|
}
|
|
|
|
|
2020-06-20 10:38:04 +00:00
|
|
|
// UpdateFolderQuotaUsage updates the folder used quota limits and checks the received HTTP Status code against expectedStatusCode.
|
|
|
|
func UpdateFolderQuotaUsage(folder vfs.BaseVirtualFolder, mode string, expectedStatusCode int) ([]byte, error) {
|
|
|
|
var body []byte
|
|
|
|
folderAsJSON, _ := json.Marshal(folder)
|
|
|
|
url, err := addModeQueryParam(buildURLRelativeToBase(updateFolderUsedQuotaPath), mode)
|
|
|
|
if err != nil {
|
|
|
|
return body, err
|
|
|
|
}
|
|
|
|
resp, err := sendHTTPRequest(http.MethodPut, url.String(), bytes.NewBuffer(folderAsJSON), "")
|
|
|
|
if err != nil {
|
|
|
|
return body, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, _ = getResponseBody(resp)
|
|
|
|
return body, checkResponse(resp.StatusCode, expectedStatusCode)
|
|
|
|
}
|
|
|
|
|
2019-08-08 08:01:33 +00:00
|
|
|
// GetVersion returns version details
|
2020-06-19 15:08:51 +00:00
|
|
|
func GetVersion(expectedStatusCode int) (version.Info, []byte, error) {
|
|
|
|
var appVersion version.Info
|
2019-08-08 08:01:33 +00:00
|
|
|
var body []byte
|
2020-02-03 23:08:00 +00:00
|
|
|
resp, err := sendHTTPRequest(http.MethodGet, buildURLRelativeToBase(versionPath), nil, "")
|
2019-08-08 08:01:33 +00:00
|
|
|
if err != nil {
|
2020-06-19 15:08:51 +00:00
|
|
|
return appVersion, body, err
|
2019-08-08 08:01:33 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
err = checkResponse(resp.StatusCode, expectedStatusCode)
|
|
|
|
if err == nil && expectedStatusCode == http.StatusOK {
|
2020-06-19 15:08:51 +00:00
|
|
|
err = render.DecodeJSON(resp.Body, &appVersion)
|
2019-08-08 08:01:33 +00:00
|
|
|
} else {
|
|
|
|
body, _ = getResponseBody(resp)
|
|
|
|
}
|
2020-06-19 15:08:51 +00:00
|
|
|
return appVersion, body, err
|
2019-08-08 08:01:33 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 10:18:34 +00:00
|
|
|
// GetStatus returns the server status
|
|
|
|
func GetStatus(expectedStatusCode int) (ServicesStatus, []byte, error) {
|
|
|
|
var response ServicesStatus
|
2019-11-14 17:48:01 +00:00
|
|
|
var body []byte
|
2020-12-08 10:18:34 +00:00
|
|
|
resp, err := sendHTTPRequest(http.MethodGet, buildURLRelativeToBase(serverStatusPath), nil, "")
|
2019-11-14 17:48:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return response, body, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
err = checkResponse(resp.StatusCode, expectedStatusCode)
|
2020-12-08 10:18:34 +00:00
|
|
|
if err == nil && (expectedStatusCode == http.StatusOK) {
|
2019-11-14 17:48:01 +00:00
|
|
|
err = render.DecodeJSON(resp.Body, &response)
|
|
|
|
} else {
|
|
|
|
body, _ = getResponseBody(resp)
|
|
|
|
}
|
|
|
|
return response, body, err
|
|
|
|
}
|
|
|
|
|
2019-12-27 22:12:44 +00:00
|
|
|
// Dumpdata requests a backup to outputFile.
|
|
|
|
// outputFile is relative to the configured backups_path
|
2020-02-02 21:20:39 +00:00
|
|
|
func Dumpdata(outputFile, indent string, expectedStatusCode int) (map[string]interface{}, []byte, error) {
|
2019-12-27 22:12:44 +00:00
|
|
|
var response map[string]interface{}
|
|
|
|
var body []byte
|
|
|
|
url, err := url.Parse(buildURLRelativeToBase(dumpDataPath))
|
|
|
|
if err != nil {
|
|
|
|
return response, body, err
|
|
|
|
}
|
|
|
|
q := url.Query()
|
|
|
|
q.Add("output_file", outputFile)
|
2020-02-02 21:20:39 +00:00
|
|
|
if len(indent) > 0 {
|
|
|
|
q.Add("indent", indent)
|
|
|
|
}
|
2019-12-27 22:12:44 +00:00
|
|
|
url.RawQuery = q.Encode()
|
2020-02-03 23:08:00 +00:00
|
|
|
resp, err := sendHTTPRequest(http.MethodGet, url.String(), nil, "")
|
2019-12-27 22:12:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return response, body, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
err = checkResponse(resp.StatusCode, expectedStatusCode)
|
|
|
|
if err == nil && expectedStatusCode == http.StatusOK {
|
|
|
|
err = render.DecodeJSON(resp.Body, &response)
|
|
|
|
} else {
|
|
|
|
body, _ = getResponseBody(resp)
|
|
|
|
}
|
|
|
|
return response, body, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loaddata restores a backup.
|
|
|
|
// New users are added, existing users are updated. Users will be restored one by one and the restore is stopped if a
|
|
|
|
// user cannot be added/updated, so it could happen a partial restore
|
2020-01-31 18:10:45 +00:00
|
|
|
func Loaddata(inputFile, scanQuota, mode string, expectedStatusCode int) (map[string]interface{}, []byte, error) {
|
2019-12-27 22:12:44 +00:00
|
|
|
var response map[string]interface{}
|
|
|
|
var body []byte
|
|
|
|
url, err := url.Parse(buildURLRelativeToBase(loadDataPath))
|
|
|
|
if err != nil {
|
|
|
|
return response, body, err
|
|
|
|
}
|
|
|
|
q := url.Query()
|
|
|
|
q.Add("input_file", inputFile)
|
|
|
|
if len(scanQuota) > 0 {
|
|
|
|
q.Add("scan_quota", scanQuota)
|
|
|
|
}
|
2020-01-31 18:10:45 +00:00
|
|
|
if len(mode) > 0 {
|
|
|
|
q.Add("mode", mode)
|
|
|
|
}
|
2019-12-27 22:12:44 +00:00
|
|
|
url.RawQuery = q.Encode()
|
2020-02-03 23:08:00 +00:00
|
|
|
resp, err := sendHTTPRequest(http.MethodGet, url.String(), nil, "")
|
2019-12-27 22:12:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return response, body, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
err = checkResponse(resp.StatusCode, expectedStatusCode)
|
|
|
|
if err == nil && expectedStatusCode == http.StatusOK {
|
|
|
|
err = render.DecodeJSON(resp.Body, &response)
|
|
|
|
} else {
|
|
|
|
body, _ = getResponseBody(resp)
|
|
|
|
}
|
|
|
|
return response, body, err
|
|
|
|
}
|
|
|
|
|
2019-08-04 19:37:31 +00:00
|
|
|
func checkResponse(actual int, expected int) error {
|
2019-07-20 10:26:52 +00:00
|
|
|
if expected != actual {
|
|
|
|
return fmt.Errorf("wrong status code: got %v want %v", actual, expected)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-04 19:37:31 +00:00
|
|
|
func getResponseBody(resp *http.Response) ([]byte, error) {
|
|
|
|
return ioutil.ReadAll(resp.Body)
|
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
func checkFolder(expected *vfs.BaseVirtualFolder, actual *vfs.BaseVirtualFolder) error {
|
|
|
|
if expected.ID <= 0 {
|
|
|
|
if actual.ID <= 0 {
|
|
|
|
return errors.New("actual folder ID must be > 0")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if actual.ID != expected.ID {
|
|
|
|
return errors.New("folder ID mismatch")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if expected.MappedPath != actual.MappedPath {
|
|
|
|
return errors.New("mapped path mismatch")
|
|
|
|
}
|
|
|
|
if expected.LastQuotaUpdate != actual.LastQuotaUpdate {
|
|
|
|
return errors.New("last quota update mismatch")
|
|
|
|
}
|
|
|
|
if expected.UsedQuotaSize != actual.UsedQuotaSize {
|
|
|
|
return errors.New("used quota size mismatch")
|
|
|
|
}
|
|
|
|
if expected.UsedQuotaFiles != actual.UsedQuotaFiles {
|
|
|
|
return errors.New("used quota files mismatch")
|
|
|
|
}
|
|
|
|
if len(expected.Users) != len(actual.Users) {
|
|
|
|
return errors.New("folder users mismatch")
|
|
|
|
}
|
|
|
|
for _, u := range actual.Users {
|
|
|
|
if !utils.IsStringInSlice(u, expected.Users) {
|
|
|
|
return errors.New("folder users mismatch")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-30 17:37:50 +00:00
|
|
|
func checkUser(expected *dataprovider.User, actual *dataprovider.User) error {
|
2020-12-12 09:31:09 +00:00
|
|
|
if actual.Password != "" {
|
2019-07-20 10:26:52 +00:00
|
|
|
return errors.New("User password must not be visible")
|
|
|
|
}
|
|
|
|
if expected.ID <= 0 {
|
|
|
|
if actual.ID <= 0 {
|
|
|
|
return errors.New("actual user ID must be > 0")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if actual.ID != expected.ID {
|
|
|
|
return errors.New("user ID mismatch")
|
|
|
|
}
|
|
|
|
}
|
2019-12-30 17:37:50 +00:00
|
|
|
if len(expected.Permissions) != len(actual.Permissions) {
|
|
|
|
return errors.New("Permissions mismatch")
|
|
|
|
}
|
2019-12-25 17:20:19 +00:00
|
|
|
for dir, perms := range expected.Permissions {
|
|
|
|
if actualPerms, ok := actual.Permissions[dir]; ok {
|
|
|
|
for _, v := range actualPerms {
|
|
|
|
if !utils.IsStringInSlice(v, perms) {
|
|
|
|
return errors.New("Permissions contents mismatch")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return errors.New("Permissions directories mismatch")
|
2019-07-20 22:19:17 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-30 17:37:50 +00:00
|
|
|
if err := compareUserFilters(expected, actual); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-19 06:41:05 +00:00
|
|
|
if err := compareUserFsConfig(expected, actual); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-23 10:30:26 +00:00
|
|
|
if err := compareUserVirtualFolders(expected, actual); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-20 22:19:17 +00:00
|
|
|
return compareEqualsUserFields(expected, actual)
|
|
|
|
}
|
|
|
|
|
2020-02-23 10:30:26 +00:00
|
|
|
func compareUserVirtualFolders(expected *dataprovider.User, actual *dataprovider.User) error {
|
|
|
|
if len(actual.VirtualFolders) != len(expected.VirtualFolders) {
|
|
|
|
return errors.New("Virtual folders mismatch")
|
|
|
|
}
|
|
|
|
for _, v := range actual.VirtualFolders {
|
|
|
|
found := false
|
|
|
|
for _, v1 := range expected.VirtualFolders {
|
|
|
|
if path.Clean(v.VirtualPath) == path.Clean(v1.VirtualPath) &&
|
|
|
|
filepath.Clean(v.MappedPath) == filepath.Clean(v1.MappedPath) {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return errors.New("Virtual folders mismatch")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-19 06:41:05 +00:00
|
|
|
func compareUserFsConfig(expected *dataprovider.User, actual *dataprovider.User) error {
|
|
|
|
if expected.FsConfig.Provider != actual.FsConfig.Provider {
|
|
|
|
return errors.New("Fs provider mismatch")
|
|
|
|
}
|
2020-03-13 16:28:55 +00:00
|
|
|
if err := compareS3Config(expected, actual); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := compareGCSConfig(expected, actual); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-25 07:18:48 +00:00
|
|
|
if err := compareAzBlobConfig(expected, actual); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-05 12:48:13 +00:00
|
|
|
if err := checkEncryptedSecret(expected.FsConfig.CryptConfig.Passphrase, actual.FsConfig.CryptConfig.Passphrase); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-12 09:31:09 +00:00
|
|
|
if err := compareSFTPFsConfig(expected, actual); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-13 16:28:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func compareS3Config(expected *dataprovider.User, actual *dataprovider.User) error {
|
2020-01-19 06:41:05 +00:00
|
|
|
if expected.FsConfig.S3Config.Bucket != actual.FsConfig.S3Config.Bucket {
|
|
|
|
return errors.New("S3 bucket mismatch")
|
|
|
|
}
|
|
|
|
if expected.FsConfig.S3Config.Region != actual.FsConfig.S3Config.Region {
|
|
|
|
return errors.New("S3 region mismatch")
|
|
|
|
}
|
|
|
|
if expected.FsConfig.S3Config.AccessKey != actual.FsConfig.S3Config.AccessKey {
|
|
|
|
return errors.New("S3 access key mismatch")
|
|
|
|
}
|
2020-10-25 07:18:48 +00:00
|
|
|
if err := checkEncryptedSecret(expected.FsConfig.S3Config.AccessSecret, actual.FsConfig.S3Config.AccessSecret); err != nil {
|
|
|
|
return fmt.Errorf("S3 access secret mismatch: %v", err)
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
if expected.FsConfig.S3Config.Endpoint != actual.FsConfig.S3Config.Endpoint {
|
|
|
|
return errors.New("S3 endpoint mismatch")
|
|
|
|
}
|
|
|
|
if expected.FsConfig.S3Config.StorageClass != actual.FsConfig.S3Config.StorageClass {
|
|
|
|
return errors.New("S3 storage class mismatch")
|
|
|
|
}
|
2020-03-13 16:28:55 +00:00
|
|
|
if expected.FsConfig.S3Config.UploadPartSize != actual.FsConfig.S3Config.UploadPartSize {
|
2020-03-13 18:13:58 +00:00
|
|
|
return errors.New("S3 upload part size mismatch")
|
|
|
|
}
|
|
|
|
if expected.FsConfig.S3Config.UploadConcurrency != actual.FsConfig.S3Config.UploadConcurrency {
|
|
|
|
return errors.New("S3 upload concurrency mismatch")
|
2020-03-13 16:28:55 +00:00
|
|
|
}
|
2020-01-19 22:23:09 +00:00
|
|
|
if expected.FsConfig.S3Config.KeyPrefix != actual.FsConfig.S3Config.KeyPrefix &&
|
|
|
|
expected.FsConfig.S3Config.KeyPrefix+"/" != actual.FsConfig.S3Config.KeyPrefix {
|
|
|
|
return errors.New("S3 key prefix mismatch")
|
|
|
|
}
|
2020-03-13 16:28:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func compareGCSConfig(expected *dataprovider.User, actual *dataprovider.User) error {
|
2020-01-31 18:04:00 +00:00
|
|
|
if expected.FsConfig.GCSConfig.Bucket != actual.FsConfig.GCSConfig.Bucket {
|
|
|
|
return errors.New("GCS bucket mismatch")
|
|
|
|
}
|
|
|
|
if expected.FsConfig.GCSConfig.StorageClass != actual.FsConfig.GCSConfig.StorageClass {
|
|
|
|
return errors.New("GCS storage class mismatch")
|
|
|
|
}
|
|
|
|
if expected.FsConfig.GCSConfig.KeyPrefix != actual.FsConfig.GCSConfig.KeyPrefix &&
|
|
|
|
expected.FsConfig.GCSConfig.KeyPrefix+"/" != actual.FsConfig.GCSConfig.KeyPrefix {
|
|
|
|
return errors.New("GCS key prefix mismatch")
|
|
|
|
}
|
2020-02-19 08:41:15 +00:00
|
|
|
if expected.FsConfig.GCSConfig.AutomaticCredentials != actual.FsConfig.GCSConfig.AutomaticCredentials {
|
|
|
|
return errors.New("GCS automatic credentials mismatch")
|
|
|
|
}
|
2020-01-19 06:41:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-12 09:31:09 +00:00
|
|
|
func compareSFTPFsConfig(expected *dataprovider.User, actual *dataprovider.User) error {
|
|
|
|
if expected.FsConfig.SFTPConfig.Endpoint != actual.FsConfig.SFTPConfig.Endpoint {
|
|
|
|
return errors.New("SFTPFs endpoint mismatch")
|
|
|
|
}
|
|
|
|
if expected.FsConfig.SFTPConfig.Username != actual.FsConfig.SFTPConfig.Username {
|
|
|
|
return errors.New("SFTPFs username mismatch")
|
|
|
|
}
|
|
|
|
if err := checkEncryptedSecret(expected.FsConfig.SFTPConfig.Password, actual.FsConfig.SFTPConfig.Password); err != nil {
|
|
|
|
return fmt.Errorf("SFTPFs password mismatch: %v", err)
|
|
|
|
}
|
|
|
|
if err := checkEncryptedSecret(expected.FsConfig.SFTPConfig.PrivateKey, actual.FsConfig.SFTPConfig.PrivateKey); err != nil {
|
|
|
|
return fmt.Errorf("SFTPFs private key mismatch: %v", err)
|
|
|
|
}
|
|
|
|
if expected.FsConfig.SFTPConfig.Prefix != actual.FsConfig.SFTPConfig.Prefix {
|
|
|
|
if expected.FsConfig.SFTPConfig.Prefix != "" && actual.FsConfig.SFTPConfig.Prefix != "/" {
|
|
|
|
return errors.New("SFTPFs prefix mismatch")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(expected.FsConfig.SFTPConfig.Fingerprints) != len(actual.FsConfig.SFTPConfig.Fingerprints) {
|
|
|
|
return errors.New("SFTPFs fingerprints mismatch")
|
|
|
|
}
|
|
|
|
for _, value := range actual.FsConfig.SFTPConfig.Fingerprints {
|
|
|
|
if !utils.IsStringInSlice(value, expected.FsConfig.SFTPConfig.Fingerprints) {
|
|
|
|
return errors.New("SFTPFs fingerprints mismatch")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-25 07:18:48 +00:00
|
|
|
func compareAzBlobConfig(expected *dataprovider.User, actual *dataprovider.User) error {
|
|
|
|
if expected.FsConfig.AzBlobConfig.Container != actual.FsConfig.AzBlobConfig.Container {
|
|
|
|
return errors.New("Azure Blob container mismatch")
|
|
|
|
}
|
|
|
|
if expected.FsConfig.AzBlobConfig.AccountName != actual.FsConfig.AzBlobConfig.AccountName {
|
|
|
|
return errors.New("Azure Blob account name mismatch")
|
|
|
|
}
|
|
|
|
if err := checkEncryptedSecret(expected.FsConfig.AzBlobConfig.AccountKey, actual.FsConfig.AzBlobConfig.AccountKey); err != nil {
|
|
|
|
return fmt.Errorf("Azure Blob account key mismatch: %v", err)
|
|
|
|
}
|
|
|
|
if expected.FsConfig.AzBlobConfig.Endpoint != actual.FsConfig.AzBlobConfig.Endpoint {
|
|
|
|
return errors.New("Azure Blob endpoint mismatch")
|
|
|
|
}
|
|
|
|
if expected.FsConfig.AzBlobConfig.SASURL != actual.FsConfig.AzBlobConfig.SASURL {
|
|
|
|
return errors.New("Azure Blob SASL URL mismatch")
|
|
|
|
}
|
|
|
|
if expected.FsConfig.AzBlobConfig.UploadPartSize != actual.FsConfig.AzBlobConfig.UploadPartSize {
|
|
|
|
return errors.New("Azure Blob upload part size mismatch")
|
|
|
|
}
|
|
|
|
if expected.FsConfig.AzBlobConfig.UploadConcurrency != actual.FsConfig.AzBlobConfig.UploadConcurrency {
|
|
|
|
return errors.New("Azure Blob upload concurrency mismatch")
|
|
|
|
}
|
|
|
|
if expected.FsConfig.AzBlobConfig.KeyPrefix != actual.FsConfig.AzBlobConfig.KeyPrefix &&
|
|
|
|
expected.FsConfig.AzBlobConfig.KeyPrefix+"/" != actual.FsConfig.AzBlobConfig.KeyPrefix {
|
|
|
|
return errors.New("Azure Blob key prefix mismatch")
|
|
|
|
}
|
|
|
|
if expected.FsConfig.AzBlobConfig.UseEmulator != actual.FsConfig.AzBlobConfig.UseEmulator {
|
|
|
|
return errors.New("Azure Blob use emulator mismatch")
|
|
|
|
}
|
2020-11-25 21:26:34 +00:00
|
|
|
if expected.FsConfig.AzBlobConfig.AccessTier != actual.FsConfig.AzBlobConfig.AccessTier {
|
|
|
|
return errors.New("Azure Blob access tier mismatch")
|
|
|
|
}
|
2020-10-25 07:18:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-30 20:46:34 +00:00
|
|
|
func areSecretEquals(expected, actual *kms.Secret) bool {
|
|
|
|
if expected == nil && actual == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if expected != nil && expected.IsEmpty() && actual == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if actual != nil && actual.IsEmpty() && expected == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkEncryptedSecret(expected, actual *kms.Secret) error {
|
|
|
|
if areSecretEquals(expected, actual) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if expected == nil && actual != nil && !actual.IsEmpty() {
|
|
|
|
return errors.New("secret mismatch")
|
|
|
|
}
|
|
|
|
if actual == nil && expected != nil && !expected.IsEmpty() {
|
|
|
|
return errors.New("secret mismatch")
|
|
|
|
}
|
2020-11-22 20:53:04 +00:00
|
|
|
if expected.IsPlain() && actual.IsEncrypted() {
|
2020-11-30 20:46:34 +00:00
|
|
|
if actual.GetPayload() == "" {
|
2020-11-22 20:53:04 +00:00
|
|
|
return errors.New("invalid secret payload")
|
|
|
|
}
|
2020-11-30 20:46:34 +00:00
|
|
|
if actual.GetAdditionalData() != "" {
|
2020-11-22 20:53:04 +00:00
|
|
|
return errors.New("invalid secret additional data")
|
|
|
|
}
|
2020-11-30 20:46:34 +00:00
|
|
|
if actual.GetKey() != "" {
|
2020-11-22 20:53:04 +00:00
|
|
|
return errors.New("invalid secret key")
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-11-30 20:46:34 +00:00
|
|
|
if expected.GetStatus() != actual.GetStatus() || expected.GetPayload() != actual.GetPayload() {
|
2020-10-25 07:18:48 +00:00
|
|
|
return errors.New("secret mismatch")
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-30 17:37:50 +00:00
|
|
|
func compareUserFilters(expected *dataprovider.User, actual *dataprovider.User) error {
|
|
|
|
if len(expected.Filters.AllowedIP) != len(actual.Filters.AllowedIP) {
|
|
|
|
return errors.New("AllowedIP mismatch")
|
|
|
|
}
|
|
|
|
if len(expected.Filters.DeniedIP) != len(actual.Filters.DeniedIP) {
|
|
|
|
return errors.New("DeniedIP mismatch")
|
|
|
|
}
|
2020-02-19 21:39:30 +00:00
|
|
|
if len(expected.Filters.DeniedLoginMethods) != len(actual.Filters.DeniedLoginMethods) {
|
|
|
|
return errors.New("Denied login methods mismatch")
|
|
|
|
}
|
2020-08-17 10:49:20 +00:00
|
|
|
if len(expected.Filters.DeniedProtocols) != len(actual.Filters.DeniedProtocols) {
|
|
|
|
return errors.New("Denied protocols mismatch")
|
|
|
|
}
|
2020-08-16 18:17:02 +00:00
|
|
|
if expected.Filters.MaxUploadFileSize != actual.Filters.MaxUploadFileSize {
|
|
|
|
return errors.New("Max upload file size mismatch")
|
|
|
|
}
|
2019-12-30 17:37:50 +00:00
|
|
|
for _, IPMask := range expected.Filters.AllowedIP {
|
|
|
|
if !utils.IsStringInSlice(IPMask, actual.Filters.AllowedIP) {
|
|
|
|
return errors.New("AllowedIP contents mismatch")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, IPMask := range expected.Filters.DeniedIP {
|
|
|
|
if !utils.IsStringInSlice(IPMask, actual.Filters.DeniedIP) {
|
|
|
|
return errors.New("DeniedIP contents mismatch")
|
|
|
|
}
|
|
|
|
}
|
2020-02-19 21:39:30 +00:00
|
|
|
for _, method := range expected.Filters.DeniedLoginMethods {
|
|
|
|
if !utils.IsStringInSlice(method, actual.Filters.DeniedLoginMethods) {
|
|
|
|
return errors.New("Denied login methods contents mismatch")
|
|
|
|
}
|
|
|
|
}
|
2020-08-17 10:49:20 +00:00
|
|
|
for _, protocol := range expected.Filters.DeniedProtocols {
|
|
|
|
if !utils.IsStringInSlice(protocol, actual.Filters.DeniedProtocols) {
|
|
|
|
return errors.New("Denied protocols contents mismatch")
|
|
|
|
}
|
|
|
|
}
|
2020-03-01 21:10:29 +00:00
|
|
|
if err := compareUserFileExtensionsFilters(expected, actual); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-15 21:04:48 +00:00
|
|
|
return compareUserFilePatternsFilters(expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkFilterMatch(expected []string, actual []string) bool {
|
|
|
|
if len(expected) != len(actual) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, e := range expected {
|
|
|
|
if !utils.IsStringInSlice(strings.ToLower(e), actual) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func compareUserFilePatternsFilters(expected *dataprovider.User, actual *dataprovider.User) error {
|
|
|
|
if len(expected.Filters.FilePatterns) != len(actual.Filters.FilePatterns) {
|
|
|
|
return errors.New("file patterns mismatch")
|
|
|
|
}
|
|
|
|
for _, f := range expected.Filters.FilePatterns {
|
|
|
|
found := false
|
|
|
|
for _, f1 := range actual.Filters.FilePatterns {
|
|
|
|
if path.Clean(f.Path) == path.Clean(f1.Path) {
|
|
|
|
if !checkFilterMatch(f.AllowedPatterns, f1.AllowedPatterns) ||
|
|
|
|
!checkFilterMatch(f.DeniedPatterns, f1.DeniedPatterns) {
|
|
|
|
return errors.New("file patterns contents mismatch")
|
|
|
|
}
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return errors.New("file patterns contents mismatch")
|
|
|
|
}
|
|
|
|
}
|
2020-03-01 21:10:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func compareUserFileExtensionsFilters(expected *dataprovider.User, actual *dataprovider.User) error {
|
|
|
|
if len(expected.Filters.FileExtensions) != len(actual.Filters.FileExtensions) {
|
|
|
|
return errors.New("file extensions mismatch")
|
|
|
|
}
|
|
|
|
for _, f := range expected.Filters.FileExtensions {
|
|
|
|
found := false
|
|
|
|
for _, f1 := range actual.Filters.FileExtensions {
|
|
|
|
if path.Clean(f.Path) == path.Clean(f1.Path) {
|
2020-11-15 21:04:48 +00:00
|
|
|
if !checkFilterMatch(f.AllowedExtensions, f1.AllowedExtensions) ||
|
|
|
|
!checkFilterMatch(f.DeniedExtensions, f1.DeniedExtensions) {
|
2020-03-01 21:10:29 +00:00
|
|
|
return errors.New("file extensions contents mismatch")
|
|
|
|
}
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return errors.New("file extensions contents mismatch")
|
|
|
|
}
|
|
|
|
}
|
2019-12-30 17:37:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func compareEqualsUserFields(expected *dataprovider.User, actual *dataprovider.User) error {
|
2019-07-20 10:26:52 +00:00
|
|
|
if expected.Username != actual.Username {
|
|
|
|
return errors.New("Username mismatch")
|
|
|
|
}
|
|
|
|
if expected.HomeDir != actual.HomeDir {
|
|
|
|
return errors.New("HomeDir mismatch")
|
|
|
|
}
|
|
|
|
if expected.UID != actual.UID {
|
|
|
|
return errors.New("UID mismatch")
|
|
|
|
}
|
|
|
|
if expected.GID != actual.GID {
|
|
|
|
return errors.New("GID mismatch")
|
|
|
|
}
|
|
|
|
if expected.MaxSessions != actual.MaxSessions {
|
|
|
|
return errors.New("MaxSessions mismatch")
|
|
|
|
}
|
|
|
|
if expected.QuotaSize != actual.QuotaSize {
|
|
|
|
return errors.New("QuotaSize mismatch")
|
|
|
|
}
|
|
|
|
if expected.QuotaFiles != actual.QuotaFiles {
|
|
|
|
return errors.New("QuotaFiles mismatch")
|
|
|
|
}
|
|
|
|
if len(expected.Permissions) != len(actual.Permissions) {
|
|
|
|
return errors.New("Permissions mismatch")
|
|
|
|
}
|
|
|
|
if expected.UploadBandwidth != actual.UploadBandwidth {
|
|
|
|
return errors.New("UploadBandwidth mismatch")
|
|
|
|
}
|
|
|
|
if expected.DownloadBandwidth != actual.DownloadBandwidth {
|
|
|
|
return errors.New("DownloadBandwidth mismatch")
|
|
|
|
}
|
2019-11-13 10:36:21 +00:00
|
|
|
if expected.Status != actual.Status {
|
|
|
|
return errors.New("Status mismatch")
|
|
|
|
}
|
|
|
|
if expected.ExpirationDate != actual.ExpirationDate {
|
|
|
|
return errors.New("ExpirationDate mismatch")
|
|
|
|
}
|
2020-11-25 21:26:34 +00:00
|
|
|
if expected.AdditionalInfo != actual.AdditionalInfo {
|
|
|
|
return errors.New("AdditionalInfo mismatch")
|
|
|
|
}
|
2019-07-20 10:26:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
|
|
|
|
func addLimitAndOffsetQueryParams(rawurl string, limit, offset int64) (*url.URL, error) {
|
|
|
|
url, err := url.Parse(rawurl)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
q := url.Query()
|
|
|
|
if limit > 0 {
|
|
|
|
q.Add("limit", strconv.FormatInt(limit, 10))
|
|
|
|
}
|
|
|
|
if offset > 0 {
|
|
|
|
q.Add("offset", strconv.FormatInt(offset, 10))
|
|
|
|
}
|
|
|
|
url.RawQuery = q.Encode()
|
|
|
|
return url, err
|
|
|
|
}
|
2020-06-20 10:38:04 +00:00
|
|
|
|
|
|
|
func addModeQueryParam(rawurl, mode string) (*url.URL, error) {
|
|
|
|
url, err := url.Parse(rawurl)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
q := url.Query()
|
|
|
|
if len(mode) > 0 {
|
|
|
|
q.Add("mode", mode)
|
|
|
|
}
|
|
|
|
url.RawQuery = q.Encode()
|
|
|
|
return url, err
|
|
|
|
}
|
2020-09-01 14:10:26 +00:00
|
|
|
|
|
|
|
func addDisconnectQueryParam(rawurl, disconnect string) (*url.URL, error) {
|
|
|
|
url, err := url.Parse(rawurl)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
q := url.Query()
|
|
|
|
if len(disconnect) > 0 {
|
|
|
|
q.Add("disconnect", disconnect)
|
|
|
|
}
|
|
|
|
url.RawQuery = q.Encode()
|
|
|
|
return url, err
|
|
|
|
}
|