api-bucket-encryption.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * MinIO Go Library for Amazon S3 Compatible Cloud Storage
  3. * Copyright 2020 MinIO, Inc.
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package minio
  17. import (
  18. "bytes"
  19. "context"
  20. "encoding/xml"
  21. "net/http"
  22. "net/url"
  23. "github.com/minio/minio-go/v7/pkg/s3utils"
  24. "github.com/minio/minio-go/v7/pkg/sse"
  25. )
  26. // SetBucketEncryption sets the default encryption configuration on an existing bucket.
  27. func (c Client) SetBucketEncryption(ctx context.Context, bucketName string, config *sse.Configuration) error {
  28. // Input validation.
  29. if err := s3utils.CheckValidBucketName(bucketName); err != nil {
  30. return err
  31. }
  32. if config == nil {
  33. return errInvalidArgument("configuration cannot be empty")
  34. }
  35. buf, err := xml.Marshal(config)
  36. if err != nil {
  37. return err
  38. }
  39. // Get resources properly escaped and lined up before
  40. // using them in http request.
  41. urlValues := make(url.Values)
  42. urlValues.Set("encryption", "")
  43. // Content-length is mandatory to set a default encryption configuration
  44. reqMetadata := requestMetadata{
  45. bucketName: bucketName,
  46. queryValues: urlValues,
  47. contentBody: bytes.NewReader(buf),
  48. contentLength: int64(len(buf)),
  49. contentMD5Base64: sumMD5Base64(buf),
  50. }
  51. // Execute PUT to upload a new bucket default encryption configuration.
  52. resp, err := c.executeMethod(ctx, http.MethodPut, reqMetadata)
  53. defer closeResponse(resp)
  54. if err != nil {
  55. return err
  56. }
  57. if resp.StatusCode != http.StatusOK {
  58. return httpRespToErrorResponse(resp, bucketName, "")
  59. }
  60. return nil
  61. }
  62. // RemoveBucketEncryption removes the default encryption configuration on a bucket with a context to control cancellations and timeouts.
  63. func (c Client) RemoveBucketEncryption(ctx context.Context, bucketName string) error {
  64. // Input validation.
  65. if err := s3utils.CheckValidBucketName(bucketName); err != nil {
  66. return err
  67. }
  68. // Get resources properly escaped and lined up before
  69. // using them in http request.
  70. urlValues := make(url.Values)
  71. urlValues.Set("encryption", "")
  72. // DELETE default encryption configuration on a bucket.
  73. resp, err := c.executeMethod(ctx, http.MethodDelete, requestMetadata{
  74. bucketName: bucketName,
  75. queryValues: urlValues,
  76. contentSHA256Hex: emptySHA256Hex,
  77. })
  78. defer closeResponse(resp)
  79. if err != nil {
  80. return err
  81. }
  82. if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
  83. return httpRespToErrorResponse(resp, bucketName, "")
  84. }
  85. return nil
  86. }
  87. // GetBucketEncryption gets the default encryption configuration
  88. // on an existing bucket with a context to control cancellations and timeouts.
  89. func (c Client) GetBucketEncryption(ctx context.Context, bucketName string) (*sse.Configuration, error) {
  90. // Input validation.
  91. if err := s3utils.CheckValidBucketName(bucketName); err != nil {
  92. return nil, err
  93. }
  94. // Get resources properly escaped and lined up before
  95. // using them in http request.
  96. urlValues := make(url.Values)
  97. urlValues.Set("encryption", "")
  98. // Execute GET on bucket to get the default encryption configuration.
  99. resp, err := c.executeMethod(ctx, http.MethodGet, requestMetadata{
  100. bucketName: bucketName,
  101. queryValues: urlValues,
  102. })
  103. defer closeResponse(resp)
  104. if err != nil {
  105. return nil, err
  106. }
  107. if resp.StatusCode != http.StatusOK {
  108. return nil, httpRespToErrorResponse(resp, bucketName, "")
  109. }
  110. encryptionConfig := &sse.Configuration{}
  111. if err = xmlDecoder(resp.Body, encryptionConfig); err != nil {
  112. return nil, err
  113. }
  114. return encryptionConfig, nil
  115. }