|
| 1 | +/* |
| 2 | + * MinIO Go Library for Amazon S3 Compatible Cloud Storage |
| 3 | + * Copyright 2025-2026 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 | + |
| 18 | +package minio |
| 19 | + |
| 20 | +import ( |
| 21 | + "bytes" |
| 22 | + "context" |
| 23 | + "encoding/xml" |
| 24 | + "net/http" |
| 25 | + "net/url" |
| 26 | + |
| 27 | + "github.com/minio/minio-go/v7/pkg/s3utils" |
| 28 | +) |
| 29 | + |
| 30 | +// updateObjectEncryptionSSEKMS represents the SSE-KMS element in the request body. |
| 31 | +type updateObjectEncryptionSSEKMS struct { |
| 32 | + BucketKeyEnabled bool `xml:"BucketKeyEnabled,omitempty"` |
| 33 | + KMSKeyArn string `xml:"KMSKeyArn"` |
| 34 | +} |
| 35 | + |
| 36 | +// updateObjectEncryptionRequest represents the XML request body for UpdateObjectEncryption. |
| 37 | +type updateObjectEncryptionRequest struct { |
| 38 | + XMLName xml.Name `xml:"ObjectEncryption"` |
| 39 | + XMLNS string `xml:"xmlns,attr"` |
| 40 | + SSEKMS *updateObjectEncryptionSSEKMS `xml:"SSE-KMS"` |
| 41 | +} |
| 42 | + |
| 43 | +// UpdateObjectEncryptionOptions holds options for the UpdateObjectEncryption call. |
| 44 | +type UpdateObjectEncryptionOptions struct { |
| 45 | + // KMSKeyArn is the KMS key name or ARN to encrypt the object with. |
| 46 | + KMSKeyArn string |
| 47 | + |
| 48 | + // BucketKeyEnabled enables S3 Bucket Key for KMS encryption. |
| 49 | + BucketKeyEnabled bool |
| 50 | + |
| 51 | + // VersionID targets a specific object version. |
| 52 | + VersionID string |
| 53 | +} |
| 54 | + |
| 55 | +// UpdateObjectEncryption changes the encryption configuration of an existing object in-place. |
| 56 | +// The object must already be encrypted with SSE-S3 or SSE-KMS. SSE-C objects are not supported. |
| 57 | +// This operation rotates the data encryption key envelope without re-reading/re-writing object data. |
| 58 | +// |
| 59 | +// Parameters: |
| 60 | +// - ctx: Context for request cancellation and timeout |
| 61 | +// - bucketName: Name of the bucket |
| 62 | +// - objectName: Name of the object |
| 63 | +// - opts: Options including KMSKeyArn (required), optional BucketKeyEnabled, and optional VersionID |
| 64 | +// |
| 65 | +// Returns an error if the operation fails. |
| 66 | +func (c *Client) UpdateObjectEncryption(ctx context.Context, bucketName, objectName string, opts UpdateObjectEncryptionOptions) error { |
| 67 | + // Input validation. |
| 68 | + if err := s3utils.CheckValidBucketName(bucketName); err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + if err := s3utils.CheckValidObjectName(objectName); err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + |
| 76 | + if opts.KMSKeyArn == "" { |
| 77 | + return errInvalidArgument("KMSKeyArn is required for UpdateObjectEncryption.") |
| 78 | + } |
| 79 | + |
| 80 | + // Get resources properly escaped and lined up before |
| 81 | + // using them in http request. |
| 82 | + urlValues := make(url.Values) |
| 83 | + urlValues.Set("encryption", "") |
| 84 | + |
| 85 | + if opts.VersionID != "" { |
| 86 | + urlValues.Set("versionId", opts.VersionID) |
| 87 | + } |
| 88 | + |
| 89 | + reqBody := updateObjectEncryptionRequest{ |
| 90 | + XMLNS: "http://s3.amazonaws.com/doc/2006-03-01/", |
| 91 | + SSEKMS: &updateObjectEncryptionSSEKMS{ |
| 92 | + BucketKeyEnabled: opts.BucketKeyEnabled, |
| 93 | + KMSKeyArn: opts.KMSKeyArn, |
| 94 | + }, |
| 95 | + } |
| 96 | + |
| 97 | + bodyData, err := xml.Marshal(reqBody) |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + |
| 102 | + reqMetadata := requestMetadata{ |
| 103 | + bucketName: bucketName, |
| 104 | + objectName: objectName, |
| 105 | + queryValues: urlValues, |
| 106 | + contentBody: bytes.NewReader(bodyData), |
| 107 | + contentLength: int64(len(bodyData)), |
| 108 | + contentMD5Base64: sumMD5Base64(bodyData), |
| 109 | + contentSHA256Hex: sum256Hex(bodyData), |
| 110 | + } |
| 111 | + |
| 112 | + // Execute PUT Object Encryption. |
| 113 | + resp, err := c.executeMethod(ctx, http.MethodPut, reqMetadata) |
| 114 | + defer closeResponse(resp) |
| 115 | + if err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + if resp.StatusCode != http.StatusOK { |
| 119 | + return httpRespToErrorResponse(resp, bucketName, objectName) |
| 120 | + } |
| 121 | + return nil |
| 122 | +} |
0 commit comments