-
Notifications
You must be signed in to change notification settings - Fork 738
Expand file tree
/
Copy pathapi-update-object-encryption_test.go
More file actions
288 lines (250 loc) · 8.07 KB
/
api-update-object-encryption_test.go
File metadata and controls
288 lines (250 loc) · 8.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* MinIO Go Library for Amazon S3 Compatible Cloud Storage
* Copyright 2025-2026 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package minio
import (
"context"
"encoding/xml"
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/minio/minio-go/v7/pkg/credentials"
)
func TestUpdateObjectEncryptionXMLMarshal(t *testing.T) {
req := updateObjectEncryptionRequest{
XMLNS: "http://s3.amazonaws.com/doc/2006-03-01/",
SSEKMS: &updateObjectEncryptionSSEKMS{
BucketKeyEnabled: true,
KMSKeyArn: "my-minio-key",
},
}
data, err := xml.Marshal(req)
if err != nil {
t.Fatalf("Failed to marshal XML: %v", err)
}
// Verify we can unmarshal back.
var decoded updateObjectEncryptionRequest
if err := xml.Unmarshal(data, &decoded); err != nil {
t.Fatalf("Failed to unmarshal XML: %v", err)
}
if decoded.SSEKMS == nil {
t.Fatal("Expected SSE-KMS element to be present")
}
if decoded.SSEKMS.KMSKeyArn != "my-minio-key" {
t.Fatalf("Expected KMSKeyArn 'my-minio-key', got %q", decoded.SSEKMS.KMSKeyArn)
}
if !decoded.SSEKMS.BucketKeyEnabled {
t.Fatal("Expected BucketKeyEnabled to be true")
}
}
func TestUpdateObjectEncryptionXMLMarshalNoBucketKey(t *testing.T) {
req := updateObjectEncryptionRequest{
XMLNS: "http://s3.amazonaws.com/doc/2006-03-01/",
SSEKMS: &updateObjectEncryptionSSEKMS{
KMSKeyArn: "my-minio-key",
},
}
data, err := xml.Marshal(req)
if err != nil {
t.Fatalf("Failed to marshal XML: %v", err)
}
var decoded updateObjectEncryptionRequest
if err := xml.Unmarshal(data, &decoded); err != nil {
t.Fatalf("Failed to unmarshal XML: %v", err)
}
if decoded.SSEKMS.BucketKeyEnabled {
t.Fatal("Expected BucketKeyEnabled to be false (omitted)")
}
}
func TestUpdateObjectEncryptionSuccess(t *testing.T) {
var capturedMethod string
var capturedPath string
var capturedQuery url.Values
var capturedBody []byte
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
capturedMethod = r.Method
capturedPath = r.URL.Path
capturedQuery = r.URL.Query()
capturedBody, _ = io.ReadAll(r.Body)
w.Header().Set("x-amz-version-id", "returned-version-id")
w.WriteHeader(http.StatusOK)
}))
defer ts.Close()
srv, err := url.Parse(ts.URL)
if err != nil {
t.Fatal(err)
}
client, err := New(srv.Host, &Options{
Creds: credentials.NewStaticV4("accesskey", "secretkey", ""),
Secure: false,
Region: "us-east-1",
})
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
opts := UpdateObjectEncryptionOptions{
KMSKeyArn: "my-minio-key",
BucketKeyEnabled: true,
VersionID: "test-version-id",
}
result, err := client.UpdateObjectEncryption(context.Background(), "mybucket", "myobject", opts)
if err != nil {
t.Fatalf("UpdateObjectEncryption failed: %v", err)
}
// Verify returned version ID from response header.
if result.VersionID != "returned-version-id" {
t.Fatalf("Expected VersionID 'returned-version-id', got %q", result.VersionID)
}
// Verify request method.
if capturedMethod != http.MethodPut {
t.Fatalf("Expected PUT, got %s", capturedMethod)
}
// Verify request path.
if capturedPath != "/mybucket/myobject" {
t.Fatalf("Expected path '/mybucket/myobject', got %q", capturedPath)
}
// Verify query parameters.
if _, ok := capturedQuery["encryption"]; !ok {
t.Fatal("Expected 'encryption' query parameter")
}
if capturedQuery.Get("versionId") != "test-version-id" {
t.Fatalf("Expected versionId 'test-version-id', got %q", capturedQuery.Get("versionId"))
}
// Verify XML body.
var body updateObjectEncryptionRequest
if err := xml.Unmarshal(capturedBody, &body); err != nil {
t.Fatalf("Failed to unmarshal request body: %v", err)
}
if body.SSEKMS == nil {
t.Fatal("Expected SSE-KMS element in request body")
}
if body.SSEKMS.KMSKeyArn != "my-minio-key" {
t.Fatalf("Expected KMSKeyArn 'my-minio-key', got %q", body.SSEKMS.KMSKeyArn)
}
if !body.SSEKMS.BucketKeyEnabled {
t.Fatal("Expected BucketKeyEnabled to be true in request body")
}
}
func TestUpdateObjectEncryptionNoVersionID(t *testing.T) {
var capturedQuery url.Values
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
capturedQuery = r.URL.Query()
w.WriteHeader(http.StatusOK)
}))
defer ts.Close()
srv, err := url.Parse(ts.URL)
if err != nil {
t.Fatal(err)
}
client, err := New(srv.Host, &Options{
Creds: credentials.NewStaticV4("accesskey", "secretkey", ""),
Secure: false,
Region: "us-east-1",
})
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
_, err = client.UpdateObjectEncryption(context.Background(), "mybucket", "myobject", UpdateObjectEncryptionOptions{
KMSKeyArn: "my-key",
})
if err != nil {
t.Fatalf("UpdateObjectEncryption failed: %v", err)
}
// versionId should not be present when not specified.
if capturedQuery.Get("versionId") != "" {
t.Fatalf("Expected no versionId query parameter, got %q", capturedQuery.Get("versionId"))
}
}
func TestUpdateObjectEncryptionServerError(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?><Error><Code>InvalidRequest</Code><Message>The encryption type change is not supported</Message><Resource>/mybucket/myobject</Resource><RequestId>test-req-id</RequestId></Error>`))
}))
defer ts.Close()
srv, err := url.Parse(ts.URL)
if err != nil {
t.Fatal(err)
}
client, err := New(srv.Host, &Options{
Creds: credentials.NewStaticV4("accesskey", "secretkey", ""),
Secure: false,
Region: "us-east-1",
})
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
_, err = client.UpdateObjectEncryption(context.Background(), "mybucket", "myobject", UpdateObjectEncryptionOptions{
KMSKeyArn: "my-key",
})
if err == nil {
t.Fatal("Expected error for 400 response, got nil")
}
errResp := ToErrorResponse(err)
if errResp.Code != "InvalidRequest" {
t.Fatalf("Expected error code 'InvalidRequest', got %q", errResp.Code)
}
}
func TestUpdateObjectEncryptionInvalidBucket(t *testing.T) {
client, err := New("localhost:9000", &Options{
Creds: credentials.NewStaticV4("accesskey", "secretkey", ""),
Secure: false,
})
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
_, err = client.UpdateObjectEncryption(context.Background(), "", "myobject", UpdateObjectEncryptionOptions{
KMSKeyArn: "my-key",
})
if err == nil {
t.Fatal("Expected error for empty bucket name, got nil")
}
}
func TestUpdateObjectEncryptionInvalidObject(t *testing.T) {
client, err := New("localhost:9000", &Options{
Creds: credentials.NewStaticV4("accesskey", "secretkey", ""),
Secure: false,
})
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
_, err = client.UpdateObjectEncryption(context.Background(), "mybucket", "", UpdateObjectEncryptionOptions{
KMSKeyArn: "my-key",
})
if err == nil {
t.Fatal("Expected error for empty object name, got nil")
}
}
func TestUpdateObjectEncryptionEmptyKMSKeyArn(t *testing.T) {
client, err := New("localhost:9000", &Options{
Creds: credentials.NewStaticV4("accesskey", "secretkey", ""),
Secure: false,
})
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
_, err = client.UpdateObjectEncryption(context.Background(), "mybucket", "myobject", UpdateObjectEncryptionOptions{
KMSKeyArn: "",
})
if err == nil {
t.Fatal("Expected error for empty KMSKeyArn, got nil")
}
errResp := ToErrorResponse(err)
if errResp.Code != InvalidArgument {
t.Fatalf("Expected error code %q, got %q", InvalidArgument, errResp.Code)
}
}