api_user.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package httpd
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "net/http"
  7. "strconv"
  8. "github.com/go-chi/render"
  9. "github.com/drakkan/sftpgo/common"
  10. "github.com/drakkan/sftpgo/dataprovider"
  11. "github.com/drakkan/sftpgo/kms"
  12. "github.com/drakkan/sftpgo/vfs"
  13. )
  14. func getUsers(w http.ResponseWriter, r *http.Request) {
  15. limit, offset, order, err := getSearchFilters(w, r)
  16. if err != nil {
  17. return
  18. }
  19. users, err := dataprovider.GetUsers(limit, offset, order)
  20. if err == nil {
  21. render.JSON(w, r, users)
  22. } else {
  23. sendAPIResponse(w, r, err, "", http.StatusInternalServerError)
  24. }
  25. }
  26. func getUserByUsername(w http.ResponseWriter, r *http.Request) {
  27. username := getURLParam(r, "username")
  28. renderUser(w, r, username, http.StatusOK)
  29. }
  30. func renderUser(w http.ResponseWriter, r *http.Request, username string, status int) {
  31. user, err := dataprovider.UserExists(username)
  32. if err != nil {
  33. sendAPIResponse(w, r, err, "", getRespStatus(err))
  34. return
  35. }
  36. user.PrepareForRendering()
  37. if status != http.StatusOK {
  38. ctx := context.WithValue(r.Context(), render.StatusCtxKey, status)
  39. render.JSON(w, r.WithContext(ctx), user)
  40. } else {
  41. render.JSON(w, r, user)
  42. }
  43. }
  44. func addUser(w http.ResponseWriter, r *http.Request) {
  45. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  46. var user dataprovider.User
  47. err := render.DecodeJSON(r.Body, &user)
  48. if err != nil {
  49. sendAPIResponse(w, r, err, "", http.StatusBadRequest)
  50. return
  51. }
  52. user.SetEmptySecretsIfNil()
  53. switch user.FsConfig.Provider {
  54. case vfs.S3FilesystemProvider:
  55. if user.FsConfig.S3Config.AccessSecret.IsRedacted() {
  56. sendAPIResponse(w, r, errors.New("invalid access_secret"), "", http.StatusBadRequest)
  57. return
  58. }
  59. case vfs.GCSFilesystemProvider:
  60. if user.FsConfig.GCSConfig.Credentials.IsRedacted() {
  61. sendAPIResponse(w, r, errors.New("invalid credentials"), "", http.StatusBadRequest)
  62. return
  63. }
  64. case vfs.AzureBlobFilesystemProvider:
  65. if user.FsConfig.AzBlobConfig.AccountKey.IsRedacted() {
  66. sendAPIResponse(w, r, errors.New("invalid account_key"), "", http.StatusBadRequest)
  67. return
  68. }
  69. case vfs.CryptedFilesystemProvider:
  70. if user.FsConfig.CryptConfig.Passphrase.IsRedacted() {
  71. sendAPIResponse(w, r, errors.New("invalid passphrase"), "", http.StatusBadRequest)
  72. return
  73. }
  74. case vfs.SFTPFilesystemProvider:
  75. if user.FsConfig.SFTPConfig.Password.IsRedacted() {
  76. sendAPIResponse(w, r, errors.New("invalid SFTP password"), "", http.StatusBadRequest)
  77. return
  78. }
  79. if user.FsConfig.SFTPConfig.PrivateKey.IsRedacted() {
  80. sendAPIResponse(w, r, errors.New("invalid SFTP private key"), "", http.StatusBadRequest)
  81. return
  82. }
  83. }
  84. err = dataprovider.AddUser(&user)
  85. if err != nil {
  86. sendAPIResponse(w, r, err, "", getRespStatus(err))
  87. return
  88. }
  89. renderUser(w, r, user.Username, http.StatusCreated)
  90. }
  91. func updateUser(w http.ResponseWriter, r *http.Request) {
  92. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  93. var err error
  94. username := getURLParam(r, "username")
  95. disconnect := 0
  96. if _, ok := r.URL.Query()["disconnect"]; ok {
  97. disconnect, err = strconv.Atoi(r.URL.Query().Get("disconnect"))
  98. if err != nil {
  99. err = fmt.Errorf("invalid disconnect parameter: %v", err)
  100. sendAPIResponse(w, r, err, "", http.StatusBadRequest)
  101. return
  102. }
  103. }
  104. user, err := dataprovider.UserExists(username)
  105. if err != nil {
  106. sendAPIResponse(w, r, err, "", getRespStatus(err))
  107. return
  108. }
  109. userID := user.ID
  110. currentPermissions := user.Permissions
  111. currentS3AccessSecret := user.FsConfig.S3Config.AccessSecret
  112. currentAzAccountKey := user.FsConfig.AzBlobConfig.AccountKey
  113. currentGCSCredentials := user.FsConfig.GCSConfig.Credentials
  114. currentCryptoPassphrase := user.FsConfig.CryptConfig.Passphrase
  115. currentSFTPPassword := user.FsConfig.SFTPConfig.Password
  116. currentSFTPKey := user.FsConfig.SFTPConfig.PrivateKey
  117. user.Permissions = make(map[string][]string)
  118. user.FsConfig.S3Config = vfs.S3FsConfig{}
  119. user.FsConfig.AzBlobConfig = vfs.AzBlobFsConfig{}
  120. user.FsConfig.GCSConfig = vfs.GCSFsConfig{}
  121. user.FsConfig.CryptConfig = vfs.CryptFsConfig{}
  122. user.FsConfig.SFTPConfig = vfs.SFTPFsConfig{}
  123. user.VirtualFolders = nil
  124. err = render.DecodeJSON(r.Body, &user)
  125. if err != nil {
  126. sendAPIResponse(w, r, err, "", http.StatusBadRequest)
  127. return
  128. }
  129. user.ID = userID
  130. user.Username = username
  131. user.SetEmptySecretsIfNil()
  132. // we use new Permissions if passed otherwise the old ones
  133. if len(user.Permissions) == 0 {
  134. user.Permissions = currentPermissions
  135. }
  136. updateEncryptedSecrets(&user.FsConfig, currentS3AccessSecret, currentAzAccountKey, currentGCSCredentials, currentCryptoPassphrase,
  137. currentSFTPPassword, currentSFTPKey)
  138. err = dataprovider.UpdateUser(&user)
  139. if err != nil {
  140. sendAPIResponse(w, r, err, "", getRespStatus(err))
  141. return
  142. }
  143. sendAPIResponse(w, r, err, "User updated", http.StatusOK)
  144. if disconnect == 1 {
  145. disconnectUser(user.Username)
  146. }
  147. }
  148. func deleteUser(w http.ResponseWriter, r *http.Request) {
  149. username := getURLParam(r, "username")
  150. err := dataprovider.DeleteUser(username)
  151. if err != nil {
  152. sendAPIResponse(w, r, err, "", getRespStatus(err))
  153. return
  154. }
  155. sendAPIResponse(w, r, err, "User deleted", http.StatusOK)
  156. disconnectUser(username)
  157. }
  158. func disconnectUser(username string) {
  159. for _, stat := range common.Connections.GetStats() {
  160. if stat.Username == username {
  161. common.Connections.Close(stat.ConnectionID)
  162. }
  163. }
  164. }
  165. func updateEncryptedSecrets(fsConfig *vfs.Filesystem, currentS3AccessSecret, currentAzAccountKey,
  166. currentGCSCredentials, currentCryptoPassphrase, currentSFTPPassword, currentSFTPKey *kms.Secret) {
  167. // we use the new access secret if plain or empty, otherwise the old value
  168. switch fsConfig.Provider {
  169. case vfs.S3FilesystemProvider:
  170. if fsConfig.S3Config.AccessSecret.IsNotPlainAndNotEmpty() {
  171. fsConfig.S3Config.AccessSecret = currentS3AccessSecret
  172. }
  173. case vfs.AzureBlobFilesystemProvider:
  174. if fsConfig.AzBlobConfig.AccountKey.IsNotPlainAndNotEmpty() {
  175. fsConfig.AzBlobConfig.AccountKey = currentAzAccountKey
  176. }
  177. case vfs.GCSFilesystemProvider:
  178. if fsConfig.GCSConfig.Credentials.IsNotPlainAndNotEmpty() {
  179. fsConfig.GCSConfig.Credentials = currentGCSCredentials
  180. }
  181. case vfs.CryptedFilesystemProvider:
  182. if fsConfig.CryptConfig.Passphrase.IsNotPlainAndNotEmpty() {
  183. fsConfig.CryptConfig.Passphrase = currentCryptoPassphrase
  184. }
  185. case vfs.SFTPFilesystemProvider:
  186. if fsConfig.SFTPConfig.Password.IsNotPlainAndNotEmpty() {
  187. fsConfig.SFTPConfig.Password = currentSFTPPassword
  188. }
  189. if fsConfig.SFTPConfig.PrivateKey.IsNotPlainAndNotEmpty() {
  190. fsConfig.SFTPConfig.PrivateKey = currentSFTPKey
  191. }
  192. }
  193. }