api_group.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright (C) 2019-2022 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package httpd
  15. import (
  16. "context"
  17. "net/http"
  18. "github.com/go-chi/render"
  19. "github.com/drakkan/sftpgo/v2/internal/dataprovider"
  20. "github.com/drakkan/sftpgo/v2/internal/util"
  21. "github.com/drakkan/sftpgo/v2/internal/vfs"
  22. )
  23. func getGroups(w http.ResponseWriter, r *http.Request) {
  24. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  25. limit, offset, order, err := getSearchFilters(w, r)
  26. if err != nil {
  27. return
  28. }
  29. groups, err := dataprovider.GetGroups(limit, offset, order, false)
  30. if err != nil {
  31. sendAPIResponse(w, r, err, "", http.StatusInternalServerError)
  32. return
  33. }
  34. render.JSON(w, r, groups)
  35. }
  36. func addGroup(w http.ResponseWriter, r *http.Request) {
  37. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  38. claims, err := getTokenClaims(r)
  39. if err != nil || claims.Username == "" {
  40. sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
  41. return
  42. }
  43. var group dataprovider.Group
  44. err = render.DecodeJSON(r.Body, &group)
  45. if err != nil {
  46. sendAPIResponse(w, r, err, "", http.StatusBadRequest)
  47. return
  48. }
  49. err = dataprovider.AddGroup(&group, claims.Username, util.GetIPFromRemoteAddress(r.RemoteAddr), claims.Role)
  50. if err != nil {
  51. sendAPIResponse(w, r, err, "", getRespStatus(err))
  52. return
  53. }
  54. renderGroup(w, r, group.Name, http.StatusCreated)
  55. }
  56. func updateGroup(w http.ResponseWriter, r *http.Request) {
  57. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  58. claims, err := getTokenClaims(r)
  59. if err != nil || claims.Username == "" {
  60. sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
  61. return
  62. }
  63. name := getURLParam(r, "name")
  64. group, err := dataprovider.GroupExists(name)
  65. if err != nil {
  66. sendAPIResponse(w, r, err, "", getRespStatus(err))
  67. return
  68. }
  69. users := group.Users
  70. groupID := group.ID
  71. name = group.Name
  72. currentS3AccessSecret := group.UserSettings.FsConfig.S3Config.AccessSecret
  73. currentAzAccountKey := group.UserSettings.FsConfig.AzBlobConfig.AccountKey
  74. currentAzSASUrl := group.UserSettings.FsConfig.AzBlobConfig.SASURL
  75. currentGCSCredentials := group.UserSettings.FsConfig.GCSConfig.Credentials
  76. currentCryptoPassphrase := group.UserSettings.FsConfig.CryptConfig.Passphrase
  77. currentSFTPPassword := group.UserSettings.FsConfig.SFTPConfig.Password
  78. currentSFTPKey := group.UserSettings.FsConfig.SFTPConfig.PrivateKey
  79. currentSFTPKeyPassphrase := group.UserSettings.FsConfig.SFTPConfig.KeyPassphrase
  80. currentHTTPPassword := group.UserSettings.FsConfig.HTTPConfig.Password
  81. currentHTTPAPIKey := group.UserSettings.FsConfig.HTTPConfig.APIKey
  82. group.UserSettings.FsConfig.S3Config = vfs.S3FsConfig{}
  83. group.UserSettings.FsConfig.AzBlobConfig = vfs.AzBlobFsConfig{}
  84. group.UserSettings.FsConfig.GCSConfig = vfs.GCSFsConfig{}
  85. group.UserSettings.FsConfig.CryptConfig = vfs.CryptFsConfig{}
  86. group.UserSettings.FsConfig.SFTPConfig = vfs.SFTPFsConfig{}
  87. group.UserSettings.FsConfig.HTTPConfig = vfs.HTTPFsConfig{}
  88. err = render.DecodeJSON(r.Body, &group)
  89. if err != nil {
  90. sendAPIResponse(w, r, err, "", http.StatusBadRequest)
  91. return
  92. }
  93. group.ID = groupID
  94. group.Name = name
  95. group.UserSettings.FsConfig.SetEmptySecretsIfNil()
  96. updateEncryptedSecrets(&group.UserSettings.FsConfig, currentS3AccessSecret, currentAzAccountKey, currentAzSASUrl,
  97. currentGCSCredentials, currentCryptoPassphrase, currentSFTPPassword, currentSFTPKey, currentSFTPKeyPassphrase,
  98. currentHTTPPassword, currentHTTPAPIKey)
  99. err = dataprovider.UpdateGroup(&group, users, claims.Username, util.GetIPFromRemoteAddress(r.RemoteAddr), claims.Role)
  100. if err != nil {
  101. sendAPIResponse(w, r, err, "", getRespStatus(err))
  102. return
  103. }
  104. sendAPIResponse(w, r, nil, "Group updated", http.StatusOK)
  105. }
  106. func renderGroup(w http.ResponseWriter, r *http.Request, name string, status int) {
  107. group, err := dataprovider.GroupExists(name)
  108. if err != nil {
  109. sendAPIResponse(w, r, err, "", getRespStatus(err))
  110. return
  111. }
  112. group.PrepareForRendering()
  113. if status != http.StatusOK {
  114. ctx := context.WithValue(r.Context(), render.StatusCtxKey, status)
  115. render.JSON(w, r.WithContext(ctx), group)
  116. } else {
  117. render.JSON(w, r, group)
  118. }
  119. }
  120. func getGroupByName(w http.ResponseWriter, r *http.Request) {
  121. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  122. name := getURLParam(r, "name")
  123. renderGroup(w, r, name, http.StatusOK)
  124. }
  125. func deleteGroup(w http.ResponseWriter, r *http.Request) {
  126. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  127. claims, err := getTokenClaims(r)
  128. if err != nil || claims.Username == "" {
  129. sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
  130. return
  131. }
  132. name := getURLParam(r, "name")
  133. err = dataprovider.DeleteGroup(name, claims.Username, util.GetIPFromRemoteAddress(r.RemoteAddr), claims.Role)
  134. if err != nil {
  135. sendAPIResponse(w, r, err, "", getRespStatus(err))
  136. return
  137. }
  138. sendAPIResponse(w, r, err, "Group deleted", http.StatusOK)
  139. }