2019-10-07 16:19:01 +00:00
|
|
|
package httpd
|
2019-07-20 10:26:52 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"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"
|
2019-07-20 10:26:52 +00:00
|
|
|
"strconv"
|
2019-08-05 13:17:43 +00:00
|
|
|
"strings"
|
2019-07-27 18:42:45 +00:00
|
|
|
"time"
|
2019-07-20 10:26:52 +00:00
|
|
|
|
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/sftpd"
|
|
|
|
"github.com/drakkan/sftpgo/utils"
|
|
|
|
"github.com/go-chi/render"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-08-04 19:37:31 +00:00
|
|
|
httpBaseURL = "http://127.0.0.1:8080"
|
2019-07-20 10:26:52 +00:00
|
|
|
)
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// SetBaseURL sets the base url to use for HTTP requests, default is "http://127.0.0.1:8080"
|
2019-07-26 09:34:44 +00:00
|
|
|
func SetBaseURL(url string) {
|
|
|
|
httpBaseURL = url
|
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// gets an HTTP Client with a timeout
|
2019-07-27 18:42:45 +00:00
|
|
|
func getHTTPClient() *http.Client {
|
|
|
|
return &http.Client{
|
|
|
|
Timeout: 15 * time.Second,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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{
|
|
|
|
Error: errorString,
|
|
|
|
Message: message,
|
|
|
|
HTTPStatus: code,
|
|
|
|
}
|
|
|
|
if code != http.StatusOK {
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
w.WriteHeader(code)
|
|
|
|
}
|
|
|
|
render.JSON(w, r, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRespStatus(err error) int {
|
|
|
|
if _, ok := err.(*dataprovider.ValidationError); ok {
|
|
|
|
return http.StatusBadRequest
|
|
|
|
}
|
|
|
|
if _, ok := err.(*dataprovider.MethodDisabledError); ok {
|
|
|
|
return http.StatusForbidden
|
|
|
|
}
|
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
|
2019-07-20 10:26:52 +00:00
|
|
|
userAsJSON, err := json.Marshal(user)
|
|
|
|
if err != nil {
|
2019-08-04 19:37:31 +00:00
|
|
|
return newUser, body, err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-08-05 13:17:43 +00:00
|
|
|
resp, err := getHTTPClient().Post(buildURLRelativeToBase(userPath), "application/json", bytes.NewBuffer(userAsJSON))
|
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.
|
2019-08-04 19:37:31 +00:00
|
|
|
func UpdateUser(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
|
2019-07-20 10:26:52 +00:00
|
|
|
userAsJSON, err := json.Marshal(user)
|
|
|
|
if err != nil {
|
2019-08-04 19:37:31 +00:00
|
|
|
return user, body, err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-08-05 13:17:43 +00:00
|
|
|
req, err := http.NewRequest(http.MethodPut, buildURLRelativeToBase(userPath, strconv.FormatInt(user.ID, 10)),
|
|
|
|
bytes.NewBuffer(userAsJSON))
|
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
|
|
|
}
|
2019-07-27 18:42:45 +00:00
|
|
|
resp, err := getHTTPClient().Do(req)
|
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
|
2019-08-05 13:17:43 +00:00
|
|
|
req, err := http.NewRequest(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
|
|
|
}
|
2019-07-27 18:42:45 +00:00
|
|
|
resp, err := getHTTPClient().Do(req)
|
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
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// GetUserByID gets an 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
|
2019-08-05 13:17:43 +00:00
|
|
|
resp, err := getHTTPClient().Get(buildURLRelativeToBase(userPath, strconv.FormatInt(userID, 10)))
|
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
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// GetUsers allows to get a list of users 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 an username, the username filter is an exact match
|
2019-08-04 19:37:31 +00:00
|
|
|
func GetUsers(limit int64, 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
|
2019-08-05 13:17:43 +00:00
|
|
|
url, err := url.Parse(buildURLRelativeToBase(userPath))
|
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
|
|
|
}
|
2019-07-27 18:42:45 +00:00
|
|
|
q := url.Query()
|
2019-07-20 10:26:52 +00:00
|
|
|
if limit > 0 {
|
|
|
|
q.Add("limit", strconv.FormatInt(limit, 10))
|
|
|
|
}
|
|
|
|
if offset > 0 {
|
|
|
|
q.Add("offset", strconv.FormatInt(offset, 10))
|
|
|
|
}
|
|
|
|
if len(username) > 0 {
|
|
|
|
q.Add("username", username)
|
|
|
|
}
|
2019-07-27 18:42:45 +00:00
|
|
|
url.RawQuery = q.Encode()
|
|
|
|
resp, err := getHTTPClient().Get(url.String())
|
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
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// GetQuotaScans gets active quota scans and checks the received HTTP Status code against expectedStatusCode.
|
2019-08-04 19:37:31 +00:00
|
|
|
func GetQuotaScans(expectedStatusCode int) ([]sftpd.ActiveQuotaScan, []byte, error) {
|
2019-07-20 10:26:52 +00:00
|
|
|
var quotaScans []sftpd.ActiveQuotaScan
|
2019-08-04 19:37:31 +00:00
|
|
|
var body []byte
|
2019-08-05 13:17:43 +00:00
|
|
|
resp, err := getHTTPClient().Get(buildURLRelativeToBase(quotaScanPath))
|
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
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// StartQuotaScan start 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
|
2019-07-20 10:26:52 +00:00
|
|
|
userAsJSON, err := json.Marshal(user)
|
|
|
|
if err != nil {
|
2019-08-04 19:37:31 +00:00
|
|
|
return body, err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-08-05 13:17:43 +00:00
|
|
|
resp, err := getHTTPClient().Post(buildURLRelativeToBase(quotaScanPath), "application/json", 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
|
|
|
}
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
// GetConnections returns status and stats for active SFTP/SCP connections
|
|
|
|
func GetConnections(expectedStatusCode int) ([]sftpd.ConnectionStatus, []byte, error) {
|
2019-08-03 11:19:00 +00:00
|
|
|
var connections []sftpd.ConnectionStatus
|
2019-08-04 19:37:31 +00:00
|
|
|
var body []byte
|
2019-08-05 13:17:43 +00:00
|
|
|
resp, err := getHTTPClient().Get(buildURLRelativeToBase(activeConnectionsPath))
|
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
|
2019-08-05 13:17:43 +00:00
|
|
|
req, err := http.NewRequest(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
|
|
|
}
|
|
|
|
resp, err := getHTTPClient().Do(req)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-08-08 08:01:33 +00:00
|
|
|
// GetVersion returns version details
|
|
|
|
func GetVersion(expectedStatusCode int) (utils.VersionInfo, []byte, error) {
|
|
|
|
var version utils.VersionInfo
|
|
|
|
var body []byte
|
|
|
|
resp, err := getHTTPClient().Get(buildURLRelativeToBase(versionPath))
|
|
|
|
if err != nil {
|
|
|
|
return version, body, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
err = checkResponse(resp.StatusCode, expectedStatusCode)
|
|
|
|
if err == nil && expectedStatusCode == http.StatusOK {
|
|
|
|
err = render.DecodeJSON(resp.Body, &version)
|
|
|
|
} else {
|
|
|
|
body, _ = getResponseBody(resp)
|
|
|
|
}
|
|
|
|
return version, body, err
|
|
|
|
}
|
|
|
|
|
2019-11-14 17:48:01 +00:00
|
|
|
// GetProviderStatus returns provider status
|
|
|
|
func GetProviderStatus(expectedStatusCode int) (map[string]interface{}, []byte, error) {
|
|
|
|
var response map[string]interface{}
|
|
|
|
var body []byte
|
|
|
|
resp, err := getHTTPClient().Get(buildURLRelativeToBase(providerStatusPath))
|
|
|
|
if err != nil {
|
|
|
|
return response, body, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
err = checkResponse(resp.StatusCode, expectedStatusCode)
|
|
|
|
if err == nil && (expectedStatusCode == http.StatusOK || expectedStatusCode == http.StatusInternalServerError) {
|
|
|
|
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
|
|
|
|
func Dumpdata(outputFile string, expectedStatusCode int) (map[string]interface{}, []byte, error) {
|
|
|
|
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)
|
|
|
|
url.RawQuery = q.Encode()
|
|
|
|
resp, err := getHTTPClient().Get(url.String())
|
|
|
|
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
|
|
|
|
func Loaddata(inputFile, scanQuota string, expectedStatusCode int) (map[string]interface{}, []byte, error) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
url.RawQuery = q.Encode()
|
|
|
|
resp, err := getHTTPClient().Get(url.String())
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-12-30 17:37:50 +00:00
|
|
|
func checkUser(expected *dataprovider.User, actual *dataprovider.User) error {
|
2019-07-20 10:26:52 +00:00
|
|
|
if len(actual.Password) > 0 {
|
|
|
|
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
|
|
|
|
}
|
2019-12-30 17:37:50 +00:00
|
|
|
|
2019-07-20 22:19:17 +00:00
|
|
|
return compareEqualsUserFields(expected, actual)
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
if err := checkS3AccessSecret(expected.FsConfig.S3Config.AccessSecret, actual.FsConfig.S3Config.AccessSecret); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
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-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-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-01-19 06:41:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkS3AccessSecret(expectedAccessSecret, actualAccessSecret string) error {
|
|
|
|
if len(expectedAccessSecret) > 0 {
|
|
|
|
vals := strings.Split(expectedAccessSecret, "$")
|
|
|
|
if strings.HasPrefix(expectedAccessSecret, "$aes$") && len(vals) == 4 {
|
|
|
|
expectedAccessSecret = utils.RemoveDecryptionKey(expectedAccessSecret)
|
|
|
|
if expectedAccessSecret != actualAccessSecret {
|
|
|
|
return fmt.Errorf("S3 access secret mismatch, expected: %v", expectedAccessSecret)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// here we check that actualAccessSecret is aes encrypted without the nonce
|
|
|
|
parts := strings.Split(actualAccessSecret, "$")
|
|
|
|
if !strings.HasPrefix(actualAccessSecret, "$aes$") || len(parts) != 3 {
|
|
|
|
return errors.New("Invalid S3 access secret")
|
|
|
|
}
|
|
|
|
if len(parts) == len(vals) {
|
|
|
|
if expectedAccessSecret != actualAccessSecret {
|
|
|
|
return errors.New("S3 encrypted access secret mismatch")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if expectedAccessSecret != actualAccessSecret {
|
|
|
|
return errors.New("S3 access secret mismatch")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
2019-07-20 10:26:52 +00:00
|
|
|
return nil
|
|
|
|
}
|