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.

163 lines
5.1 KiB

  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/xml"
  20. "io"
  21. "net/http"
  22. "reflect"
  23. "github.com/minio/minio/internal/event"
  24. "github.com/minio/minio/internal/logger"
  25. "github.com/minio/mux"
  26. "github.com/minio/pkg/v3/policy"
  27. )
  28. const (
  29. bucketNotificationConfig = "notification.xml"
  30. )
  31. // GetBucketNotificationHandler - This HTTP handler returns event notification configuration
  32. // as per http://docs.aws.amazon.com/AmazonS3/latest/dev/NotificationHowTo.html.
  33. // It returns empty configuration if its not set.
  34. func (api objectAPIHandlers) GetBucketNotificationHandler(w http.ResponseWriter, r *http.Request) {
  35. ctx := newContext(r, w, "GetBucketNotification")
  36. defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
  37. vars := mux.Vars(r)
  38. bucketName := vars["bucket"]
  39. objAPI := api.ObjectAPI()
  40. if objAPI == nil {
  41. writeErrorResponse(ctx, w, errorCodes.ToAPIErr(ErrServerNotInitialized), r.URL)
  42. return
  43. }
  44. if s3Error := checkRequestAuthType(ctx, r, policy.GetBucketNotificationAction, bucketName, ""); s3Error != ErrNone {
  45. writeErrorResponse(ctx, w, errorCodes.ToAPIErr(s3Error), r.URL)
  46. return
  47. }
  48. _, err := objAPI.GetBucketInfo(ctx, bucketName, BucketOptions{})
  49. if err != nil {
  50. writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL)
  51. return
  52. }
  53. config, err := globalBucketMetadataSys.GetNotificationConfig(bucketName)
  54. if err != nil {
  55. writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL)
  56. return
  57. }
  58. region := globalSite.Region()
  59. config.SetRegion(region)
  60. if err = config.Validate(region, globalEventNotifier.targetList); err != nil {
  61. arnErr, ok := err.(*event.ErrARNNotFound)
  62. if ok {
  63. for i, queue := range config.QueueList {
  64. // Remove ARN not found queues, because we previously allowed
  65. // adding unexpected entries into the config.
  66. //
  67. // With newer config disallowing changing / turning off
  68. // notification targets without removing ARN in notification
  69. // configuration we won't see this problem anymore.
  70. if reflect.DeepEqual(queue.ARN, arnErr.ARN) && i < len(config.QueueList) {
  71. config.QueueList = append(config.QueueList[:i],
  72. config.QueueList[i+1:]...)
  73. }
  74. // This is a one time activity we shall do this
  75. // here and allow stale ARN to be removed. We shall
  76. // never reach a stage where we will have stale
  77. // notification configs.
  78. }
  79. } else {
  80. writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL)
  81. return
  82. }
  83. }
  84. configData, err := xml.Marshal(config)
  85. if err != nil {
  86. writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL)
  87. return
  88. }
  89. writeSuccessResponseXML(w, configData)
  90. }
  91. // PutBucketNotificationHandler - This HTTP handler stores given notification configuration as per
  92. // http://docs.aws.amazon.com/AmazonS3/latest/dev/NotificationHowTo.html.
  93. func (api objectAPIHandlers) PutBucketNotificationHandler(w http.ResponseWriter, r *http.Request) {
  94. ctx := newContext(r, w, "PutBucketNotification")
  95. defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
  96. objectAPI := api.ObjectAPI()
  97. if objectAPI == nil {
  98. writeErrorResponse(ctx, w, errorCodes.ToAPIErr(ErrServerNotInitialized), r.URL)
  99. return
  100. }
  101. vars := mux.Vars(r)
  102. bucketName := vars["bucket"]
  103. if s3Error := checkRequestAuthType(ctx, r, policy.PutBucketNotificationAction, bucketName, ""); s3Error != ErrNone {
  104. writeErrorResponse(ctx, w, errorCodes.ToAPIErr(s3Error), r.URL)
  105. return
  106. }
  107. _, err := objectAPI.GetBucketInfo(ctx, bucketName, BucketOptions{})
  108. if err != nil {
  109. writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL)
  110. return
  111. }
  112. // PutBucketNotification always needs a Content-Length.
  113. if r.ContentLength <= 0 {
  114. writeErrorResponse(ctx, w, errorCodes.ToAPIErr(ErrMissingContentLength), r.URL)
  115. return
  116. }
  117. config, err := event.ParseConfig(io.LimitReader(r.Body, r.ContentLength), globalSite.Region(), globalEventNotifier.targetList)
  118. if err != nil {
  119. apiErr := errorCodes.ToAPIErr(ErrMalformedXML)
  120. if event.IsEventError(err) {
  121. apiErr = toAPIError(ctx, err)
  122. }
  123. writeErrorResponse(ctx, w, apiErr, r.URL)
  124. return
  125. }
  126. configData, err := xml.Marshal(config)
  127. if err != nil {
  128. writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL)
  129. return
  130. }
  131. if _, err = globalBucketMetadataSys.Update(ctx, bucketName, bucketNotificationConfig, configData); err != nil {
  132. writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL)
  133. return
  134. }
  135. rulesMap := config.ToRulesMap()
  136. globalEventNotifier.AddRulesMap(bucketName, rulesMap)
  137. writeSuccessResponseHeadersOnly(w)
  138. }