You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
7.5 KiB

Send AuditLog via new middleware fn for admin APIs (#17632) A new middleware function is added for admin handlers, including options for modifying certain behaviors. This admin middleware: - sets the handler context via reflection in the request and sends AuditLog - checks for object API availability (skipping it if a flag is passed) - enables gzip compression (skipping it if a flag is passed) - enables header tracing (adding body tracing if a flag is passed) While the new function is a middleware, due to the flags used for conditional behavior modification, which is used in each route registration call. To try to ensure that no regressions are introduced, the following changes were done mechanically mostly with `sed` and regexp: - Remove defer logger.AuditLog in admin handlers - Replace newContext() calls with r.Context() - Update admin routes registration calls Bonus: remove unused NetSpeedtestHandler Since the new adminMiddleware function checks for object layer presence by default, we need to pass the `noObjLayerFlag` explicitly to admin handlers that should work even when it is not available. The following admin handlers do not require it: - ServerInfoHandler - StartProfilingHandler - DownloadProfilingHandler - ProfileHandler - SiteReplicationDevNull - SiteReplicationNetPerf - TraceHandler For these handlers adminMiddleware does not check for the object layer presence (disabled by passing the `noObjLayerFlag`), and for all other handlers, the pre-check ensures that the handler is not called when the object layer is not available - the client would get a ErrServerNotInitialized and can retry later. This `noObjLayerFlag` is added based on existing behavior for these handlers only.
2 years ago
Send AuditLog via new middleware fn for admin APIs (#17632) A new middleware function is added for admin handlers, including options for modifying certain behaviors. This admin middleware: - sets the handler context via reflection in the request and sends AuditLog - checks for object API availability (skipping it if a flag is passed) - enables gzip compression (skipping it if a flag is passed) - enables header tracing (adding body tracing if a flag is passed) While the new function is a middleware, due to the flags used for conditional behavior modification, which is used in each route registration call. To try to ensure that no regressions are introduced, the following changes were done mechanically mostly with `sed` and regexp: - Remove defer logger.AuditLog in admin handlers - Replace newContext() calls with r.Context() - Update admin routes registration calls Bonus: remove unused NetSpeedtestHandler Since the new adminMiddleware function checks for object layer presence by default, we need to pass the `noObjLayerFlag` explicitly to admin handlers that should work even when it is not available. The following admin handlers do not require it: - ServerInfoHandler - StartProfilingHandler - DownloadProfilingHandler - ProfileHandler - SiteReplicationDevNull - SiteReplicationNetPerf - TraceHandler For these handlers adminMiddleware does not check for the object layer presence (disabled by passing the `noObjLayerFlag`), and for all other handlers, the pre-check ensures that the handler is not called when the object layer is not available - the client would get a ErrServerNotInitialized and can retry later. This `noObjLayerFlag` is added based on existing behavior for these handlers only.
2 years ago
Send AuditLog via new middleware fn for admin APIs (#17632) A new middleware function is added for admin handlers, including options for modifying certain behaviors. This admin middleware: - sets the handler context via reflection in the request and sends AuditLog - checks for object API availability (skipping it if a flag is passed) - enables gzip compression (skipping it if a flag is passed) - enables header tracing (adding body tracing if a flag is passed) While the new function is a middleware, due to the flags used for conditional behavior modification, which is used in each route registration call. To try to ensure that no regressions are introduced, the following changes were done mechanically mostly with `sed` and regexp: - Remove defer logger.AuditLog in admin handlers - Replace newContext() calls with r.Context() - Update admin routes registration calls Bonus: remove unused NetSpeedtestHandler Since the new adminMiddleware function checks for object layer presence by default, we need to pass the `noObjLayerFlag` explicitly to admin handlers that should work even when it is not available. The following admin handlers do not require it: - ServerInfoHandler - StartProfilingHandler - DownloadProfilingHandler - ProfileHandler - SiteReplicationDevNull - SiteReplicationNetPerf - TraceHandler For these handlers adminMiddleware does not check for the object layer presence (disabled by passing the `noObjLayerFlag`), and for all other handlers, the pre-check ensures that the handler is not called when the object layer is not available - the client would get a ErrServerNotInitialized and can retry later. This `noObjLayerFlag` is added based on existing behavior for these handlers only.
2 years ago
Send AuditLog via new middleware fn for admin APIs (#17632) A new middleware function is added for admin handlers, including options for modifying certain behaviors. This admin middleware: - sets the handler context via reflection in the request and sends AuditLog - checks for object API availability (skipping it if a flag is passed) - enables gzip compression (skipping it if a flag is passed) - enables header tracing (adding body tracing if a flag is passed) While the new function is a middleware, due to the flags used for conditional behavior modification, which is used in each route registration call. To try to ensure that no regressions are introduced, the following changes were done mechanically mostly with `sed` and regexp: - Remove defer logger.AuditLog in admin handlers - Replace newContext() calls with r.Context() - Update admin routes registration calls Bonus: remove unused NetSpeedtestHandler Since the new adminMiddleware function checks for object layer presence by default, we need to pass the `noObjLayerFlag` explicitly to admin handlers that should work even when it is not available. The following admin handlers do not require it: - ServerInfoHandler - StartProfilingHandler - DownloadProfilingHandler - ProfileHandler - SiteReplicationDevNull - SiteReplicationNetPerf - TraceHandler For these handlers adminMiddleware does not check for the object layer presence (disabled by passing the `noObjLayerFlag`), and for all other handlers, the pre-check ensures that the handler is not called when the object layer is not available - the client would get a ErrServerNotInitialized and can retry later. This `noObjLayerFlag` is added based on existing behavior for these handlers only.
2 years ago
Send AuditLog via new middleware fn for admin APIs (#17632) A new middleware function is added for admin handlers, including options for modifying certain behaviors. This admin middleware: - sets the handler context via reflection in the request and sends AuditLog - checks for object API availability (skipping it if a flag is passed) - enables gzip compression (skipping it if a flag is passed) - enables header tracing (adding body tracing if a flag is passed) While the new function is a middleware, due to the flags used for conditional behavior modification, which is used in each route registration call. To try to ensure that no regressions are introduced, the following changes were done mechanically mostly with `sed` and regexp: - Remove defer logger.AuditLog in admin handlers - Replace newContext() calls with r.Context() - Update admin routes registration calls Bonus: remove unused NetSpeedtestHandler Since the new adminMiddleware function checks for object layer presence by default, we need to pass the `noObjLayerFlag` explicitly to admin handlers that should work even when it is not available. The following admin handlers do not require it: - ServerInfoHandler - StartProfilingHandler - DownloadProfilingHandler - ProfileHandler - SiteReplicationDevNull - SiteReplicationNetPerf - TraceHandler For these handlers adminMiddleware does not check for the object layer presence (disabled by passing the `noObjLayerFlag`), and for all other handlers, the pre-check ensures that the handler is not called when the object layer is not available - the client would get a ErrServerNotInitialized and can retry later. This `noObjLayerFlag` is added based on existing behavior for these handlers only.
2 years ago
Send AuditLog via new middleware fn for admin APIs (#17632) A new middleware function is added for admin handlers, including options for modifying certain behaviors. This admin middleware: - sets the handler context via reflection in the request and sends AuditLog - checks for object API availability (skipping it if a flag is passed) - enables gzip compression (skipping it if a flag is passed) - enables header tracing (adding body tracing if a flag is passed) While the new function is a middleware, due to the flags used for conditional behavior modification, which is used in each route registration call. To try to ensure that no regressions are introduced, the following changes were done mechanically mostly with `sed` and regexp: - Remove defer logger.AuditLog in admin handlers - Replace newContext() calls with r.Context() - Update admin routes registration calls Bonus: remove unused NetSpeedtestHandler Since the new adminMiddleware function checks for object layer presence by default, we need to pass the `noObjLayerFlag` explicitly to admin handlers that should work even when it is not available. The following admin handlers do not require it: - ServerInfoHandler - StartProfilingHandler - DownloadProfilingHandler - ProfileHandler - SiteReplicationDevNull - SiteReplicationNetPerf - TraceHandler For these handlers adminMiddleware does not check for the object layer presence (disabled by passing the `noObjLayerFlag`), and for all other handlers, the pre-check ensures that the handler is not called when the object layer is not available - the client would get a ErrServerNotInitialized and can retry later. This `noObjLayerFlag` is added based on existing behavior for these handlers only.
2 years ago
  1. // Copyright (c) 2015-2021 MinIO, Inc.
  2. //
  3. // This file is part of MinIO Object Storage stack
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. package cmd
  18. import (
  19. "encoding/json"
  20. "io"
  21. "net/http"
  22. "strconv"
  23. jsoniter "github.com/json-iterator/go"
  24. "github.com/minio/madmin-go/v3"
  25. "github.com/minio/minio/internal/config/storageclass"
  26. "github.com/minio/mux"
  27. "github.com/minio/pkg/v3/policy"
  28. )
  29. var (
  30. // error returned when remote tier already exists
  31. errTierAlreadyExists = AdminError{
  32. Code: "XMinioAdminTierAlreadyExists",
  33. Message: "Specified remote tier already exists",
  34. StatusCode: http.StatusConflict,
  35. }
  36. // error returned when remote tier is not found
  37. errTierNotFound = AdminError{
  38. Code: "XMinioAdminTierNotFound",
  39. Message: "Specified remote tier was not found",
  40. StatusCode: http.StatusNotFound,
  41. }
  42. // error returned when remote tier name is not in uppercase
  43. errTierNameNotUppercase = AdminError{
  44. Code: "XMinioAdminTierNameNotUpperCase",
  45. Message: "Tier name must be in uppercase",
  46. StatusCode: http.StatusBadRequest,
  47. }
  48. // error returned when remote tier bucket is not found
  49. errTierBucketNotFound = AdminError{
  50. Code: "XMinioAdminTierBucketNotFound",
  51. Message: "Remote tier bucket not found",
  52. StatusCode: http.StatusBadRequest,
  53. }
  54. // error returned when remote tier credentials are invalid.
  55. errTierInvalidCredentials = AdminError{
  56. Code: "XMinioAdminTierInvalidCredentials",
  57. Message: "Invalid remote tier credentials",
  58. StatusCode: http.StatusBadRequest,
  59. }
  60. // error returned when reserved internal names are used.
  61. errTierReservedName = AdminError{
  62. Code: "XMinioAdminTierReserved",
  63. Message: "Cannot use reserved tier name",
  64. StatusCode: http.StatusBadRequest,
  65. }
  66. )
  67. func (api adminAPIHandlers) AddTierHandler(w http.ResponseWriter, r *http.Request) {
  68. ctx := r.Context()
  69. objAPI, cred := validateAdminReq(ctx, w, r, policy.SetTierAction)
  70. if objAPI == nil {
  71. return
  72. }
  73. password := cred.SecretKey
  74. reqBytes, err := madmin.DecryptData(password, io.LimitReader(r.Body, r.ContentLength))
  75. if err != nil {
  76. writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErrWithErr(ErrAdminConfigBadJSON, err), r.URL)
  77. return
  78. }
  79. var cfg madmin.TierConfig
  80. json := jsoniter.ConfigCompatibleWithStandardLibrary
  81. if err := json.Unmarshal(reqBytes, &cfg); err != nil {
  82. writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
  83. return
  84. }
  85. var ignoreInUse bool
  86. if forceStr := r.Form.Get("force"); forceStr != "" {
  87. ignoreInUse, _ = strconv.ParseBool(forceStr)
  88. }
  89. // Disallow remote tiers with internal storage class names
  90. switch cfg.Name {
  91. case storageclass.STANDARD, storageclass.RRS:
  92. writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, errTierReservedName), r.URL)
  93. return
  94. }
  95. // Refresh from the disk in case we had missed notifications about edits from peers.
  96. if err := globalTierConfigMgr.Reload(ctx, objAPI); err != nil {
  97. writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
  98. return
  99. }
  100. err = globalTierConfigMgr.Add(ctx, cfg, ignoreInUse)
  101. if err != nil {
  102. writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
  103. return
  104. }
  105. err = globalTierConfigMgr.Save(ctx, objAPI)
  106. if err != nil {
  107. writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
  108. return
  109. }
  110. globalNotificationSys.LoadTransitionTierConfig(ctx)
  111. writeSuccessNoContent(w)
  112. }
  113. func (api adminAPIHandlers) ListTierHandler(w http.ResponseWriter, r *http.Request) {
  114. ctx := r.Context()
  115. objAPI, _ := validateAdminReq(ctx, w, r, policy.ListTierAction)
  116. if objAPI == nil {
  117. return
  118. }
  119. tiers := globalTierConfigMgr.ListTiers()
  120. data, err := json.Marshal(tiers)
  121. if err != nil {
  122. writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
  123. return
  124. }
  125. w.Header().Set(tierCfgRefreshAtHdr, globalTierConfigMgr.refreshedAt().String())
  126. writeSuccessResponseJSON(w, data)
  127. }
  128. func (api adminAPIHandlers) EditTierHandler(w http.ResponseWriter, r *http.Request) {
  129. ctx := r.Context()
  130. objAPI, cred := validateAdminReq(ctx, w, r, policy.SetTierAction)
  131. if objAPI == nil {
  132. return
  133. }
  134. vars := mux.Vars(r)
  135. scName := vars["tier"]
  136. password := cred.SecretKey
  137. reqBytes, err := madmin.DecryptData(password, io.LimitReader(r.Body, r.ContentLength))
  138. if err != nil {
  139. writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErrWithErr(ErrAdminConfigBadJSON, err), r.URL)
  140. return
  141. }
  142. var creds madmin.TierCreds
  143. json := jsoniter.ConfigCompatibleWithStandardLibrary
  144. if err := json.Unmarshal(reqBytes, &creds); err != nil {
  145. writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
  146. return
  147. }
  148. // Refresh from the disk in case we had missed notifications about edits from peers.
  149. if err := globalTierConfigMgr.Reload(ctx, objAPI); err != nil {
  150. writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
  151. return
  152. }
  153. if err := globalTierConfigMgr.Edit(ctx, scName, creds); err != nil {
  154. writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
  155. return
  156. }
  157. if err := globalTierConfigMgr.Save(ctx, objAPI); err != nil {
  158. writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
  159. return
  160. }
  161. globalNotificationSys.LoadTransitionTierConfig(ctx)
  162. writeSuccessNoContent(w)
  163. }
  164. func (api adminAPIHandlers) RemoveTierHandler(w http.ResponseWriter, r *http.Request) {
  165. ctx := r.Context()
  166. objAPI, _ := validateAdminReq(ctx, w, r, policy.SetTierAction)
  167. if objAPI == nil {
  168. return
  169. }
  170. vars := mux.Vars(r)
  171. tier := vars["tier"]
  172. force := r.Form.Get("force") == "true"
  173. if err := globalTierConfigMgr.Reload(ctx, objAPI); err != nil {
  174. writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
  175. return
  176. }
  177. if err := globalTierConfigMgr.Remove(ctx, tier, force); err != nil {
  178. writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
  179. return
  180. }
  181. if err := globalTierConfigMgr.Save(ctx, objAPI); err != nil {
  182. writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
  183. return
  184. }
  185. globalNotificationSys.LoadTransitionTierConfig(ctx)
  186. writeSuccessNoContent(w)
  187. }
  188. func (api adminAPIHandlers) VerifyTierHandler(w http.ResponseWriter, r *http.Request) {
  189. ctx := r.Context()
  190. objAPI, _ := validateAdminReq(ctx, w, r, policy.ListTierAction)
  191. if objAPI == nil {
  192. return
  193. }
  194. vars := mux.Vars(r)
  195. tier := vars["tier"]
  196. if err := globalTierConfigMgr.Verify(ctx, tier); err != nil {
  197. writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
  198. return
  199. }
  200. writeSuccessNoContent(w)
  201. }
  202. func (api adminAPIHandlers) TierStatsHandler(w http.ResponseWriter, r *http.Request) {
  203. ctx := r.Context()
  204. objAPI, _ := validateAdminReq(ctx, w, r, policy.ListTierAction)
  205. if objAPI == nil {
  206. return
  207. }
  208. dui, err := loadDataUsageFromBackend(ctx, objAPI)
  209. if err != nil {
  210. writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
  211. return
  212. }
  213. tierStats := dui.tierStats()
  214. dailyStats := globalNotificationSys.GetLastDayTierStats(ctx)
  215. tierStats = dailyStats.addToTierInfo(tierStats)
  216. data, err := json.Marshal(tierStats)
  217. if err != nil {
  218. writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
  219. return
  220. }
  221. writeSuccessResponseJSON(w, data)
  222. }