-
Notifications
You must be signed in to change notification settings - Fork 567
Expand file tree
/
Copy pathfile_encryption_test.rb
More file actions
296 lines (231 loc) · 10.3 KB
/
file_encryption_test.rb
File metadata and controls
296 lines (231 loc) · 10.3 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
289
290
291
292
293
294
295
296
# Copyright 2018 Google LLC
#
# 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
#
# https://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.
require "storage_helper"
require "net/http"
require "uri"
require "zlib"
describe Google::Cloud::Storage::File, :storage do
let(:bucket_name) { "#{$bucket_names[1]}-encryption" }
let(:bucket_location) { "us-central1" }
let :bucket do
safe_gcs_execute { storage.bucket(bucket_name) || storage.create_bucket(bucket_name, location: bucket_location) }
end
let(:file_path) { "acceptance/data/abc.txt" }
let(:file_name) { "abc.txt" }
let(:cipher) do
cipher = OpenSSL::Cipher.new "aes-256-cfb"
cipher.encrypt
cipher
end
let(:encryption_key) { cipher.random_key }
let(:encryption_key_2) { cipher.random_key }
before do
# always create the bucket
bucket
end
after do
bucket.files.all &:delete
safe_gcs_execute { bucket.delete }
end
describe "customer-supplied encryption key (CSEK)" do
it "should upload and download a file with customer-supplied encryption key" do
original = File.new file_path
uploaded = bucket.create_file original, file_name, encryption_key: encryption_key
Tempfile.open ["abc", ".txt"] do |tmpfile|
downloaded = uploaded.download tmpfile.path, encryption_key: encryption_key
_(downloaded.size).must_equal original.size
_(downloaded.size).must_equal uploaded.size
_(downloaded.size).must_equal original.size # Same file
_(File.read(downloaded.path)).must_equal "abc"
end
uploaded.delete
end
it "should upload and partially download a file with customer-supplied encryption key" do
original = File.new file_path
uploaded = bucket.create_file original, file_name, encryption_key: encryption_key
Tempfile.open ["CloudLogo", ".png"] do |tmpfile|
downloaded = uploaded.download tmpfile.path, range: 1..2, encryption_key: encryption_key
_(downloaded.size).must_equal 2
_(File.read(downloaded.path, mode: "rb")).must_equal File.read(original.path, mode: "rb")[1..2]
end
uploaded.delete
end
it "should copy an existing file with customer-supplied encryption key" do
uploaded = bucket.create_file file_path, file_name, encryption_key: encryption_key
copied = try_with_backoff "copying existing file with encryption key" do
uploaded.copy "CloudLogoCopy.png", encryption_key: encryption_key
end
_(uploaded.name).must_equal file_name
_(copied.name).must_equal "CloudLogoCopy.png"
_(copied.size).must_equal uploaded.size
Tempfile.open ["abc", ".txt"] do |tmpfile1|
Tempfile.open ["abc-copy", ".txt"] do |tmpfile2|
downloaded1 = uploaded.download tmpfile1.path, encryption_key: encryption_key
downloaded2 = copied.download tmpfile2.path, encryption_key: encryption_key
_(downloaded1.size).must_equal downloaded2.size
_(File.read(downloaded1.path)).must_equal "abc"
end
end
uploaded.delete
copied.delete
end
it "should add, rotate, and remove customer-supplied encryption keys for an existing file" do
uploaded = bucket.create_file file_path, file_name, content_language: "en"
rewritten = try_with_backoff "add encryption key" do
uploaded.rotate new_encryption_key: encryption_key
end
_(rewritten.name).must_equal uploaded.name
_(rewritten.size).must_equal uploaded.size
_(rewritten.content_language).must_equal "en"
rewritten2 = try_with_backoff "rotate encryption keys" do
uploaded.rotate encryption_key: encryption_key, new_encryption_key: encryption_key_2
end
_(rewritten2.name).must_equal uploaded.name
_(rewritten2.size).must_equal uploaded.size
Tempfile.open ["abc", ".txt"] do |tmpfile|
downloaded = rewritten2.download tmpfile.path, encryption_key: encryption_key_2
_(downloaded.size).must_equal uploaded.size
end
rewritten4 = try_with_backoff "remove encryption key" do
uploaded.rotate encryption_key: encryption_key_2
end
_(rewritten4.name).must_equal uploaded.name
_(rewritten4.size).must_equal uploaded.size
Tempfile.open ["abc", ".txt"] do |tmpfile|
downloaded = rewritten4.download tmpfile.path
_(downloaded.size).must_equal uploaded.size
end
rewritten4.delete
end
it "should compose existing files with customer-supplied encryption key into a new file with customer-supplied encryption key" do
uploaded_a = bucket.create_file StringIO.new("a"), "a.txt", encryption_key: encryption_key
uploaded_b = bucket.create_file StringIO.new("b"), "b.txt", encryption_key: encryption_key
composed = try_with_backoff "copying existing file" do
bucket.compose [uploaded_a, uploaded_b], "ab.txt", encryption_key: encryption_key
end
_(composed.name).must_equal "ab.txt"
_(composed.size).must_equal uploaded_a.size + uploaded_b.size
Tempfile.open ["ab", ".txt"] do |tmpfile|
downloaded = composed.download tmpfile, encryption_key: encryption_key
_(File.read(downloaded.path)).must_equal "ab"
end
uploaded_a.delete
uploaded_b.delete
composed.delete
end
end
describe "KMS customer-managed encryption key (CMEK)" do
let(:kms_key) {
ENV["GCLOUD_TEST_STORAGE_KMS_KEY_1"] ||
"projects/#{storage.project_id}/locations/#{bucket_location}/keyRings/ruby-test/cryptoKeys/ruby-test-key-1"
}
let(:kms_key_2) {
ENV["GCLOUD_TEST_STORAGE_KMS_KEY_2"] ||
"projects/#{storage.project_id}/locations/#{bucket_location}/keyRings/ruby-test/cryptoKeys/ruby-test-key-2"
}
it "should upload and download a file with default_kms_key" do
bucket.default_kms_key = kms_key
original = File.new file_path
uploaded = bucket.create_file original, file_name
_(uploaded.kms_key).must_equal versioned(kms_key)
uploaded_copy = bucket.file file_name
_(uploaded_copy.kms_key).must_equal versioned(kms_key)
Tempfile.open ["abc", ".txt"] do |tmpfile|
downloaded = uploaded_copy.download tmpfile.path
_(downloaded.size).must_equal original.size
_(File.read(downloaded.path)).must_equal "abc"
end
# Ensure kms_key is visible in file listings.
uploaded_from_list = bucket.files.find { |f| f.name == file_name }
_(uploaded_from_list).wont_be :nil?
_(uploaded_from_list.kms_key).must_equal versioned(kms_key)
uploaded_copy.delete
end
it "should upload and download a file with kms_key option" do
original = File.new file_path
_(bucket.default_kms_key).must_be :nil?
uploaded = bucket.create_file original, file_name, kms_key: kms_key_2
_(uploaded.kms_key).must_equal versioned(kms_key_2)
uploaded_copy = bucket.file file_name
_(uploaded_copy.kms_key).must_equal versioned(kms_key_2)
Tempfile.open ["abc", ".txt"] do |tmpfile|
downloaded = uploaded_copy.download tmpfile.path
_(downloaded.size).must_equal original.size
_(File.read(downloaded.path)).must_equal "abc"
end
# Ensure kms_key is visible in file listings.
uploaded_from_list = bucket.files.find { |f| f.name == file_name }
_(uploaded_from_list).wont_be :nil?
_(uploaded_from_list.kms_key).must_equal versioned(kms_key_2)
uploaded_copy.delete
end
it "should upload a file with no kms key" do
original = File.new file_path
_(bucket.default_kms_key).must_be :nil?
uploaded = bucket.create_file original, file_name
_(uploaded.kms_key).must_be :nil?
uploaded_copy = bucket.file file_name
_(uploaded_copy.kms_key).must_be :nil?
uploaded_copy.delete
end
it "should rotate a customer-supplied encryption key (CSEK) to a kms key (CMEK), to no key and back to CSEK" do
uploaded = bucket.create_file file_path, file_name, encryption_key: encryption_key
_(uploaded.kms_key).must_be :nil?
rewritten = try_with_backoff "rotate from CSEK to CMEK" do
uploaded.rotate encryption_key: encryption_key, new_kms_key: kms_key
end
_(rewritten.kms_key).must_equal versioned(kms_key)
_(rewritten.size).must_equal uploaded.size
Tempfile.open ["abc", ".txt"] do |tmpfile|
downloaded = rewritten.download tmpfile.path
_(downloaded.size).must_equal uploaded.size
_(File.read(downloaded.path)).must_equal "abc"
end
rewritten2 = try_with_backoff "rotate from CMEK to default encryption" do
uploaded.rotate
end
_(rewritten2.kms_key).must_be :nil?
_(rewritten2.size).must_equal uploaded.size
Tempfile.open ["abc", ".txt"] do |tmpfile|
downloaded = rewritten2.download tmpfile.path
_(downloaded.size).must_equal uploaded.size
_(File.read(downloaded.path)).must_equal "abc"
end
rewritten3 = try_with_backoff "rotate from default encryption to CMEK" do
uploaded.rotate new_kms_key: kms_key_2
end
_(rewritten3.kms_key).must_equal versioned(kms_key_2)
_(rewritten3.size).must_equal uploaded.size
Tempfile.open ["abc", ".txt"] do |tmpfile|
downloaded = rewritten3.download tmpfile.path
_(downloaded.size).must_equal uploaded.size
_(File.read(downloaded.path)).must_equal "abc"
end
rewritten4 = try_with_backoff "rotate from CMEK to CSEK" do
uploaded.rotate new_encryption_key: encryption_key_2
end
_(rewritten4.kms_key).must_be :nil?
_(rewritten4.size).must_equal uploaded.size
Tempfile.open ["abc", ".txt"] do |tmpfile|
downloaded = rewritten4.download tmpfile.path, encryption_key: encryption_key_2
_(downloaded.size).must_equal uploaded.size
_(File.read(downloaded.path)).must_equal "abc"
end
rewritten4.delete
end
def versioned kms_key
"#{kms_key}/cryptoKeyVersions/1"
end
end
end