api-notification.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Minio Go Library for Amazon S3 Compatible Cloud Storage
  3. * Copyright 2017 Minio, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package minio
  18. import (
  19. "bufio"
  20. "context"
  21. "encoding/json"
  22. "io"
  23. "net/http"
  24. "net/url"
  25. "time"
  26. "github.com/minio/minio-go/pkg/s3utils"
  27. )
  28. // GetBucketNotification - get bucket notification at a given path.
  29. func (c Client) GetBucketNotification(bucketName string) (bucketNotification BucketNotification, err error) {
  30. // Input validation.
  31. if err := s3utils.CheckValidBucketName(bucketName); err != nil {
  32. return BucketNotification{}, err
  33. }
  34. notification, err := c.getBucketNotification(bucketName)
  35. if err != nil {
  36. return BucketNotification{}, err
  37. }
  38. return notification, nil
  39. }
  40. // Request server for notification rules.
  41. func (c Client) getBucketNotification(bucketName string) (BucketNotification, error) {
  42. urlValues := make(url.Values)
  43. urlValues.Set("notification", "")
  44. // Execute GET on bucket to list objects.
  45. resp, err := c.executeMethod(context.Background(), "GET", requestMetadata{
  46. bucketName: bucketName,
  47. queryValues: urlValues,
  48. contentSHA256Hex: emptySHA256Hex,
  49. })
  50. defer closeResponse(resp)
  51. if err != nil {
  52. return BucketNotification{}, err
  53. }
  54. return processBucketNotificationResponse(bucketName, resp)
  55. }
  56. // processes the GetNotification http response from the server.
  57. func processBucketNotificationResponse(bucketName string, resp *http.Response) (BucketNotification, error) {
  58. if resp.StatusCode != http.StatusOK {
  59. errResponse := httpRespToErrorResponse(resp, bucketName, "")
  60. return BucketNotification{}, errResponse
  61. }
  62. var bucketNotification BucketNotification
  63. err := xmlDecoder(resp.Body, &bucketNotification)
  64. if err != nil {
  65. return BucketNotification{}, err
  66. }
  67. return bucketNotification, nil
  68. }
  69. // Indentity represents the user id, this is a compliance field.
  70. type identity struct {
  71. PrincipalID string `json:"principalId"`
  72. }
  73. // Notification event bucket metadata.
  74. type bucketMeta struct {
  75. Name string `json:"name"`
  76. OwnerIdentity identity `json:"ownerIdentity"`
  77. ARN string `json:"arn"`
  78. }
  79. // Notification event object metadata.
  80. type objectMeta struct {
  81. Key string `json:"key"`
  82. Size int64 `json:"size,omitempty"`
  83. ETag string `json:"eTag,omitempty"`
  84. VersionID string `json:"versionId,omitempty"`
  85. Sequencer string `json:"sequencer"`
  86. }
  87. // Notification event server specific metadata.
  88. type eventMeta struct {
  89. SchemaVersion string `json:"s3SchemaVersion"`
  90. ConfigurationID string `json:"configurationId"`
  91. Bucket bucketMeta `json:"bucket"`
  92. Object objectMeta `json:"object"`
  93. }
  94. // sourceInfo represents information on the client that
  95. // triggered the event notification.
  96. type sourceInfo struct {
  97. Host string `json:"host"`
  98. Port string `json:"port"`
  99. UserAgent string `json:"userAgent"`
  100. }
  101. // NotificationEvent represents an Amazon an S3 bucket notification event.
  102. type NotificationEvent struct {
  103. EventVersion string `json:"eventVersion"`
  104. EventSource string `json:"eventSource"`
  105. AwsRegion string `json:"awsRegion"`
  106. EventTime string `json:"eventTime"`
  107. EventName string `json:"eventName"`
  108. UserIdentity identity `json:"userIdentity"`
  109. RequestParameters map[string]string `json:"requestParameters"`
  110. ResponseElements map[string]string `json:"responseElements"`
  111. S3 eventMeta `json:"s3"`
  112. Source sourceInfo `json:"source"`
  113. }
  114. // NotificationInfo - represents the collection of notification events, additionally
  115. // also reports errors if any while listening on bucket notifications.
  116. type NotificationInfo struct {
  117. Records []NotificationEvent
  118. Err error
  119. }
  120. // ListenBucketNotification - listen on bucket notifications.
  121. func (c Client) ListenBucketNotification(bucketName, prefix, suffix string, events []string, doneCh <-chan struct{}) <-chan NotificationInfo {
  122. notificationInfoCh := make(chan NotificationInfo, 1)
  123. // Only success, start a routine to start reading line by line.
  124. go func(notificationInfoCh chan<- NotificationInfo) {
  125. defer close(notificationInfoCh)
  126. // Validate the bucket name.
  127. if err := s3utils.CheckValidBucketName(bucketName); err != nil {
  128. notificationInfoCh <- NotificationInfo{
  129. Err: err,
  130. }
  131. return
  132. }
  133. // Check ARN partition to verify if listening bucket is supported
  134. if s3utils.IsAmazonEndpoint(c.endpointURL) || s3utils.IsGoogleEndpoint(c.endpointURL) {
  135. notificationInfoCh <- NotificationInfo{
  136. Err: ErrAPINotSupported("Listening for bucket notification is specific only to `minio` server endpoints"),
  137. }
  138. return
  139. }
  140. // Continuously run and listen on bucket notification.
  141. // Create a done channel to control 'ListObjects' go routine.
  142. retryDoneCh := make(chan struct{}, 1)
  143. // Indicate to our routine to exit cleanly upon return.
  144. defer close(retryDoneCh)
  145. // Wait on the jitter retry loop.
  146. for range c.newRetryTimerContinous(time.Second, time.Second*30, MaxJitter, retryDoneCh) {
  147. urlValues := make(url.Values)
  148. urlValues.Set("prefix", prefix)
  149. urlValues.Set("suffix", suffix)
  150. urlValues["events"] = events
  151. // Execute GET on bucket to list objects.
  152. resp, err := c.executeMethod(context.Background(), "GET", requestMetadata{
  153. bucketName: bucketName,
  154. queryValues: urlValues,
  155. contentSHA256Hex: emptySHA256Hex,
  156. })
  157. if err != nil {
  158. notificationInfoCh <- NotificationInfo{
  159. Err: err,
  160. }
  161. return
  162. }
  163. // Validate http response, upon error return quickly.
  164. if resp.StatusCode != http.StatusOK {
  165. errResponse := httpRespToErrorResponse(resp, bucketName, "")
  166. notificationInfoCh <- NotificationInfo{
  167. Err: errResponse,
  168. }
  169. return
  170. }
  171. // Initialize a new bufio scanner, to read line by line.
  172. bio := bufio.NewScanner(resp.Body)
  173. // Close the response body.
  174. defer resp.Body.Close()
  175. // Unmarshal each line, returns marshalled values.
  176. for bio.Scan() {
  177. var notificationInfo NotificationInfo
  178. if err = json.Unmarshal(bio.Bytes(), &notificationInfo); err != nil {
  179. continue
  180. }
  181. // Send notifications on channel only if there are events received.
  182. if len(notificationInfo.Records) > 0 {
  183. select {
  184. case notificationInfoCh <- notificationInfo:
  185. case <-doneCh:
  186. return
  187. }
  188. }
  189. }
  190. // Look for any underlying errors.
  191. if err = bio.Err(); err != nil {
  192. // For an unexpected connection drop from server, we close the body
  193. // and re-connect.
  194. if err == io.ErrUnexpectedEOF {
  195. resp.Body.Close()
  196. }
  197. }
  198. }
  199. }(notificationInfoCh)
  200. // Returns the notification info channel, for caller to start reading from.
  201. return notificationInfoCh
  202. }