sftpgo-mirror/httpd/api_user.go

208 lines
6.4 KiB
Go
Raw Normal View History

package httpd
2019-07-20 10:26:52 +00:00
import (
"context"
2019-07-20 10:26:52 +00:00
"errors"
"fmt"
2019-07-20 10:26:52 +00:00
"net/http"
"strconv"
"github.com/go-chi/render"
"github.com/drakkan/sftpgo/common"
"github.com/drakkan/sftpgo/dataprovider"
2020-11-30 20:46:34 +00:00
"github.com/drakkan/sftpgo/kms"
"github.com/drakkan/sftpgo/vfs"
2019-07-20 10:26:52 +00:00
)
func getUsers(w http.ResponseWriter, r *http.Request) {
limit, offset, order, err := getSearchFilters(w, r)
if err != nil {
return
2019-07-20 10:26:52 +00:00
}
users, err := dataprovider.GetUsers(limit, offset, order)
2019-07-20 10:26:52 +00:00
if err == nil {
render.JSON(w, r, users)
} else {
sendAPIResponse(w, r, err, "", http.StatusInternalServerError)
}
}
func getUserByUsername(w http.ResponseWriter, r *http.Request) {
username := getURLParam(r, "username")
renderUser(w, r, username, http.StatusOK)
}
func renderUser(w http.ResponseWriter, r *http.Request, username string, status int) {
user, err := dataprovider.UserExists(username)
2019-07-20 10:26:52 +00:00
if err != nil {
sendAPIResponse(w, r, err, "", getRespStatus(err))
2019-07-20 10:26:52 +00:00
return
}
user.PrepareForRendering()
if status != http.StatusOK {
ctx := context.WithValue(r.Context(), render.StatusCtxKey, status)
render.JSON(w, r.WithContext(ctx), user)
2019-07-20 10:26:52 +00:00
} else {
render.JSON(w, r, user)
2019-07-20 10:26:52 +00:00
}
}
func addUser(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
2019-07-20 10:26:52 +00:00
var user dataprovider.User
err := render.DecodeJSON(r.Body, &user)
if err != nil {
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
return
}
2020-11-30 20:46:34 +00:00
user.SetEmptySecretsIfNil()
switch user.FsConfig.Provider {
case vfs.S3FilesystemProvider:
if user.FsConfig.S3Config.AccessSecret.IsRedacted() {
sendAPIResponse(w, r, errors.New("invalid access_secret"), "", http.StatusBadRequest)
return
}
case vfs.GCSFilesystemProvider:
if user.FsConfig.GCSConfig.Credentials.IsRedacted() {
sendAPIResponse(w, r, errors.New("invalid credentials"), "", http.StatusBadRequest)
return
}
case vfs.AzureBlobFilesystemProvider:
if user.FsConfig.AzBlobConfig.AccountKey.IsRedacted() {
sendAPIResponse(w, r, errors.New("invalid account_key"), "", http.StatusBadRequest)
return
}
case vfs.CryptedFilesystemProvider:
2020-12-05 12:48:13 +00:00
if user.FsConfig.CryptConfig.Passphrase.IsRedacted() {
sendAPIResponse(w, r, errors.New("invalid passphrase"), "", http.StatusBadRequest)
return
}
case vfs.SFTPFilesystemProvider:
2020-12-12 09:31:09 +00:00
if user.FsConfig.SFTPConfig.Password.IsRedacted() {
sendAPIResponse(w, r, errors.New("invalid SFTP password"), "", http.StatusBadRequest)
return
}
if user.FsConfig.SFTPConfig.PrivateKey.IsRedacted() {
sendAPIResponse(w, r, errors.New("invalid SFTP private key"), "", http.StatusBadRequest)
return
}
}
err = dataprovider.AddUser(&user)
if err != nil {
2019-07-20 10:26:52 +00:00
sendAPIResponse(w, r, err, "", getRespStatus(err))
return
2019-07-20 10:26:52 +00:00
}
renderUser(w, r, user.Username, http.StatusCreated)
2019-07-20 10:26:52 +00:00
}
func updateUser(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
var err error
username := getURLParam(r, "username")
disconnect := 0
if _, ok := r.URL.Query()["disconnect"]; ok {
disconnect, err = strconv.Atoi(r.URL.Query().Get("disconnect"))
if err != nil {
err = fmt.Errorf("invalid disconnect parameter: %v", err)
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
return
}
}
user, err := dataprovider.UserExists(username)
if err != nil {
sendAPIResponse(w, r, err, "", getRespStatus(err))
return
}
userID := user.ID
currentPermissions := user.Permissions
2020-12-12 09:31:09 +00:00
currentS3AccessSecret := user.FsConfig.S3Config.AccessSecret
currentAzAccountKey := user.FsConfig.AzBlobConfig.AccountKey
currentGCSCredentials := user.FsConfig.GCSConfig.Credentials
currentCryptoPassphrase := user.FsConfig.CryptConfig.Passphrase
currentSFTPPassword := user.FsConfig.SFTPConfig.Password
currentSFTPKey := user.FsConfig.SFTPConfig.PrivateKey
2020-12-05 12:48:13 +00:00
user.Permissions = make(map[string][]string)
user.FsConfig.S3Config = vfs.S3FsConfig{}
user.FsConfig.AzBlobConfig = vfs.AzBlobFsConfig{}
user.FsConfig.GCSConfig = vfs.GCSFsConfig{}
2020-12-05 12:48:13 +00:00
user.FsConfig.CryptConfig = vfs.CryptFsConfig{}
2020-12-12 09:31:09 +00:00
user.FsConfig.SFTPConfig = vfs.SFTPFsConfig{}
user.VirtualFolders = nil
2019-07-20 10:26:52 +00:00
err = render.DecodeJSON(r.Body, &user)
if err != nil {
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
return
}
user.ID = userID
user.Username = username
2020-11-30 20:46:34 +00:00
user.SetEmptySecretsIfNil()
// we use new Permissions if passed otherwise the old ones
if len(user.Permissions) == 0 {
user.Permissions = currentPermissions
}
updateEncryptedSecrets(&user.FsConfig, currentS3AccessSecret, currentAzAccountKey, currentGCSCredentials, currentCryptoPassphrase,
2020-12-12 09:31:09 +00:00
currentSFTPPassword, currentSFTPKey)
err = dataprovider.UpdateUser(&user)
2019-07-20 10:26:52 +00:00
if err != nil {
sendAPIResponse(w, r, err, "", getRespStatus(err))
return
}
sendAPIResponse(w, r, err, "User updated", http.StatusOK)
if disconnect == 1 {
disconnectUser(user.Username)
2019-07-20 10:26:52 +00:00
}
}
func deleteUser(w http.ResponseWriter, r *http.Request) {
username := getURLParam(r, "username")
err := dataprovider.DeleteUser(username)
if err != nil {
sendAPIResponse(w, r, err, "", getRespStatus(err))
2019-07-20 10:26:52 +00:00
return
}
sendAPIResponse(w, r, err, "User deleted", http.StatusOK)
disconnectUser(username)
}
func disconnectUser(username string) {
for _, stat := range common.Connections.GetStats() {
if stat.Username == username {
common.Connections.Close(stat.ConnectionID)
}
2019-07-20 10:26:52 +00:00
}
}
2020-10-25 07:18:48 +00:00
func updateEncryptedSecrets(fsConfig *vfs.Filesystem, currentS3AccessSecret, currentAzAccountKey,
2020-12-12 09:31:09 +00:00
currentGCSCredentials, currentCryptoPassphrase, currentSFTPPassword, currentSFTPKey *kms.Secret) {
// we use the new access secret if plain or empty, otherwise the old value
switch fsConfig.Provider {
case vfs.S3FilesystemProvider:
if fsConfig.S3Config.AccessSecret.IsNotPlainAndNotEmpty() {
fsConfig.S3Config.AccessSecret = currentS3AccessSecret
2020-10-25 07:18:48 +00:00
}
case vfs.AzureBlobFilesystemProvider:
if fsConfig.AzBlobConfig.AccountKey.IsNotPlainAndNotEmpty() {
fsConfig.AzBlobConfig.AccountKey = currentAzAccountKey
2020-10-25 07:18:48 +00:00
}
case vfs.GCSFilesystemProvider:
if fsConfig.GCSConfig.Credentials.IsNotPlainAndNotEmpty() {
fsConfig.GCSConfig.Credentials = currentGCSCredentials
}
case vfs.CryptedFilesystemProvider:
if fsConfig.CryptConfig.Passphrase.IsNotPlainAndNotEmpty() {
fsConfig.CryptConfig.Passphrase = currentCryptoPassphrase
2020-12-05 12:48:13 +00:00
}
case vfs.SFTPFilesystemProvider:
if fsConfig.SFTPConfig.Password.IsNotPlainAndNotEmpty() {
fsConfig.SFTPConfig.Password = currentSFTPPassword
2020-12-12 09:31:09 +00:00
}
if fsConfig.SFTPConfig.PrivateKey.IsNotPlainAndNotEmpty() {
fsConfig.SFTPConfig.PrivateKey = currentSFTPKey
2020-12-12 09:31:09 +00:00
}
2020-12-05 12:48:13 +00:00
}
2020-10-25 07:18:48 +00:00
}