api-get-logging.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * MinIO Go Library for Amazon S3 Compatible Cloud Storage
  3. * Copyright 2015-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 s3cli
  18. import (
  19. "context"
  20. "encoding/xml"
  21. "net/http"
  22. "net/url"
  23. "github.com/minio/minio-go/v6/pkg/s3utils"
  24. )
  25. const (
  26. GRANTEE_TYPE_GROUP = "Group"
  27. GRANTEE_TYPE_EMAIL = "AmazonCustomerByEmail"
  28. GRANTEE_TYPE_USER = "CanonicalUser"
  29. GRANTEE_GROUP_URI_ALL_USERS = "http://acs.amazonaws.com/groups/global/AllUsers"
  30. GRANTEE_GROUP_URI_AUTH_USERS = "http://acs.amazonaws.com/groups/global/AuthenticatedUsers"
  31. PERMISSION_FULL_CONTROL = "FULL_CONTROL"
  32. PERMISSION_READ = "READ"
  33. PERMISSION_WRITE = "WRITE"
  34. )
  35. type BucketLoggingStatus struct {
  36. XMLName xml.Name `xml:"BucketLoggingStatus"`
  37. LoggingEnabled LoggingEnabled `xml:"LoggingEnabled"`
  38. }
  39. type LoggingEnabled struct {
  40. TargetBucket string `xml:"TargetBucket"`
  41. TargetPrefix string `xml:"TargetPrefix"`
  42. TargetGrants TargetGrants `xml:"TargetGrants"`
  43. }
  44. type TargetGrants struct {
  45. Grant []Grant `xml:"Grant"`
  46. }
  47. type Grant struct {
  48. Permission string `xml:"Permission"`
  49. Grantee Grantee `xml:"Grantee"`
  50. }
  51. type Grantee struct {
  52. Type string `xml:"xsi:type,attr"`
  53. EmailAddress string `xml:"EmailAddress,omitempty"`
  54. ID string `xml:"ID,omitempty"`
  55. DisplayName string `xml:"DisplayName,omitempty"`
  56. URI string `xml:"URI,omitempty"`
  57. }
  58. // GetBucketLogging - get bucket logging at a given path.
  59. func (c Client) GetBucketLogging(bucketName string) (*BucketLoggingStatus, error) {
  60. // Input validation.
  61. if err := s3utils.CheckValidBucketName(bucketName); err != nil {
  62. return nil, err
  63. }
  64. bucketLogging, err := c.getBucketLogging(bucketName)
  65. if err != nil {
  66. errResponse := ToErrorResponse(err)
  67. if errResponse.Code == "NoSuchBucketPolicy" {
  68. return nil, nil
  69. }
  70. return nil, err
  71. }
  72. return bucketLogging, nil
  73. }
  74. // Request server for current bucket Logging.
  75. func (c Client) getBucketLogging(bucketName string) (*BucketLoggingStatus, error) {
  76. // Get resources properly escaped and lined up before
  77. // using them in http request.
  78. urlValues := make(url.Values)
  79. urlValues.Set("logging", "")
  80. // Execute GET on bucket to list objects.
  81. resp, err := c.executeMethod(context.Background(), "GET", requestMetadata{
  82. bucketName: bucketName,
  83. queryValues: urlValues,
  84. contentSHA256Hex: emptySHA256Hex,
  85. })
  86. defer closeResponse(resp)
  87. if err != nil {
  88. return nil, err
  89. }
  90. if resp != nil {
  91. if resp.StatusCode != http.StatusOK {
  92. return nil, httpRespToErrorResponse(resp, bucketName, "")
  93. }
  94. }
  95. bucketLoggingStatus := BucketLoggingStatus{}
  96. err = xmlDecoder(resp.Body, &bucketLoggingStatus)
  97. if err != nil {
  98. return nil, err
  99. }
  100. return &bucketLoggingStatus, nil
  101. }