Skip to content

Commit e7f1983

Browse files
using update method
1 parent 13a50a9 commit e7f1983

5 files changed

Lines changed: 50 additions & 93 deletions

File tree

google-cloud-storage/acceptance/storage/bucket_encryption_test.rb

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@
104104

105105
bucket.customer_supplied_encryption_enforcement_config = customer_supplied_config
106106
_(bucket.customer_supplied_encryption_enforcement_config.restriction_mode).must_equal "FullyRestricted"
107-
bucket.update_bucket_encryption_enforcement_config google_managed_config_complete
107+
bucket.update do |b|
108+
b.google_managed_encryption_enforcement_config = google_managed_config
109+
end
108110
_(bucket.google_managed_encryption_enforcement_config.restriction_mode).must_equal "FullyRestricted"
109111

110112
bucket.reload!
@@ -113,14 +115,6 @@
113115
end
114116

115117
it "deletes all encryption enforcement configs" do
116-
# For the update, need to specify all three configs
117-
bucket.update do |b|
118-
b.customer_supplied_encryption_enforcement_config = customer_supplied_config
119-
b.google_managed_encryption_enforcement_config = google_managed_config
120-
end
121-
_(bucket.customer_managed_encryption_enforcement_config).wont_be :nil?
122-
_(bucket.customer_supplied_encryption_enforcement_config).wont_be :nil?
123-
_(bucket.google_managed_encryption_enforcement_config).wont_be :nil?
124118

125119
bucket.update do |b|
126120
b.customer_managed_encryption_enforcement_config = nil

google-cloud-storage/lib/google/cloud/storage/bucket.rb

Lines changed: 23 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -771,62 +771,6 @@ def customer_managed_encryption_enforcement_config= new_customer_managed_encrypt
771771
patch_gapi! :encryption
772772
end
773773

774-
# Updates the bucket's encryption enforcement configuration.
775-
#
776-
# This method applies a patch to the bucket's encryption settings using the
777-
# provided configuration.
778-
#
779-
# @param incoming_config [Hash, Google::Apis::StorageV1::Bucket::Encryption]
780-
# The encryption configuration to apply. If a Hash is provided, it should
781-
# contain keys corresponding to the encryption enforcement types.
782-
#
783-
# @example Updating to Google-Managed Encryption
784-
# storage = Google::Cloud::Storage.new
785-
# bucket = storage.bucket "my-bucket"
786-
#
787-
# new_config = {
788-
# google_managed_encryption_enforcement_config: { restriction_mode: "NotRestricted" }
789-
# }
790-
#
791-
# bucket.update_bucket_encryption_enforcement_config new_config
792-
#
793-
# @example Passing a Request Object
794-
# require "google/apis/storage_v1"
795-
# config_obj = Google::Apis::StorageV1::Bucket::Encryption.new(
796-
# google_managed_encryption_enforcement_config: { restriction_mode: "NotRestricted" }
797-
# )
798-
# bucket.update_bucket_encryption_enforcement_config config_obj
799-
# @raise [ArgumentError] If the config is empty, contains invalid keys, or is the wrong type.
800-
def update_bucket_encryption_enforcement_config incoming_config
801-
allowed_keys = [
802-
:google_managed_encryption_enforcement_config,
803-
:customer_managed_encryption_enforcement_config,
804-
:customer_supplied_encryption_enforcement_config
805-
]
806-
807-
if incoming_config.is_a? Hash
808-
input_keys = incoming_config.keys
809-
raise ArgumentError, "Config cannot be empty" if input_keys.empty?
810-
811-
extra_keys = input_keys - allowed_keys
812-
unless extra_keys.empty?
813-
raise ArgumentError, "Invalid config detected: #{extra_keys.join(', ')}. " \
814-
"Only #{allowed_keys.join(', ')} are allowed."
815-
end
816-
817-
elsif incoming_config.is_a? Google::Apis::StorageV1::Bucket::Encryption
818-
# For objects, ensure at least one of the allowed enforcement configs is present
819-
has_any_config = allowed_keys.any? { |key| !incoming_config.send(key).nil? }
820-
binding.pry
821-
raise ArgumentError, "Encryption request object must have at least one enforcement config set" unless has_any_config
822-
823-
else
824-
raise ArgumentError, "incoming_config must be a Hash or Google::Apis::StorageV1::Bucket::Encryption"
825-
end
826-
827-
patch_gapi! :encryption, bucket_encryption_config: incoming_config
828-
end
829-
830774
##
831775
# The bucket's encryption configuration for customer-supplied encryption keys.
832776
# For more information, see [Bucket encryption](https://docs.cloud.google.com/storage/docs/encryption/).
@@ -1595,6 +1539,7 @@ def update if_metageneration_match: nil, if_metageneration_not_match: nil
15951539
updater.check_for_changed_labels!
15961540
updater.check_for_mutable_cors!
15971541
updater.check_for_mutable_lifecycle!
1542+
updater.check_for_encryption_enforcement_config!
15981543
return if updater.updates.empty?
15991544
update_gapi! updater.updates,
16001545
if_metageneration_match: if_metageneration_match,
@@ -3474,18 +3419,13 @@ def ensure_gapi!
34743419

34753420
def patch_gapi! attributes,
34763421
if_metageneration_match: nil,
3477-
if_metageneration_not_match: nil,
3478-
bucket_encryption_config: nil
3422+
if_metageneration_not_match: nil
34793423
attributes = Array(attributes)
34803424
attributes.flatten!
34813425
return if attributes.empty?
34823426
ensure_service!
34833427
patch_args = attributes.to_h do |attr|
3484-
if bucket_encryption_config
3485-
[attr, bucket_encryption_config]
3486-
else
3487-
[attr, @gapi.send(attr)]
3488-
end
3428+
[attr, @gapi.send(attr)]
34893429
end
34903430
patch_gapi = API::Bucket.new(**patch_args)
34913431
@gapi = service.patch_bucket name,
@@ -3613,6 +3553,26 @@ def check_for_mutable_lifecycle!
36133553
patch_gapi! :lifecycle
36143554
end
36153555

3556+
def check_for_encryption_enforcement_config!
3557+
return unless @gapi.encryption
3558+
3559+
[
3560+
:google_managed_encryption_enforcement_config,
3561+
:customer_managed_encryption_enforcement_config,
3562+
:customer_supplied_encryption_enforcement_config
3563+
].each do |attr|
3564+
config = @gapi.encryption.send(attr)
3565+
next unless config
3566+
unless config.respond_to?(:to_h)
3567+
raise ArgumentError, "Encryption config for #{attr} must be a Hash or valid Config object"
3568+
end
3569+
clean_config = config.to_h
3570+
clean_config.delete :effective_time
3571+
clean_config.delete "effective_time"
3572+
@gapi.encryption.send "#{attr}=", clean_config
3573+
end
3574+
end
3575+
36163576
protected
36173577

36183578
##

google-cloud-storage/samples/acceptance/buckets_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
require_relative "../storage_get_autoclass"
5959
require_relative "../storage_set_autoclass"
6060
require_relative "../storage_move_object"
61+
require 'pry'
6162

6263
describe "Buckets Snippets" do
6364
let(:storage_client) { Google::Cloud::Storage.new }

google-cloud-storage/samples/storage_update_bucket_encryption_enforcement_config.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@ def update_bucket_encryption_enforcement_config bucket_name:
2323
storage = Google::Cloud::Storage.new
2424
bucket = storage.bucket bucket_name
2525
# Update a specific type (e.g., change GMEK to NotRestricted)
26-
new_config = {
27-
google_managed_encryption_enforcement_config: { restriction_mode: "NotRestricted" }
28-
}
29-
30-
bucket.update_bucket_encryption_enforcement_config new_config
26+
new_config = { restriction_mode: "NotRestricted" }
3127

28+
bucket.update do |b|
29+
b.google_managed_encryption_enforcement_config = new_config
30+
end
3231
puts "Updated google_managed_config to " \
3332
"#{bucket.google_managed_encryption_enforcement_config.restriction_mode} " \
3433
"for bucket #{bucket.name}."

google-cloud-storage/test/google/cloud/storage/bucket_encryption_test.rb

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149

150150
it "updates encryption_enforcement_config using update_bucket_encryption_enforcement_config" do
151151
mock = Minitest::Mock.new
152-
incoming_config = { google_managed_encryption_enforcement_config: { restriction_mode: "FullyRestricted" } }
152+
incoming_config = { restriction_mode: "FullyRestricted" }
153153

154154
patch_bucket_gapi = Google::Apis::StorageV1::Bucket.new(
155155
encryption: Google::Apis::StorageV1::Bucket::Encryption.new(
@@ -162,39 +162,42 @@
162162
returned_bucket_gapi = bucket_gapi.dup
163163
returned_bucket_gapi.encryption = bucket_gapi.encryption.dup
164164
returned_bucket_gapi.encryption.google_managed_encryption_enforcement_config = patch_bucket_gapi.encryption.google_managed_encryption_enforcement_config
165-
mock.expect :patch_bucket, returned_bucket_gapi, [bucket_name, patch_bucket_gapi], **patch_bucket_args(options: { retries: 0 })
165+
166+
mock.expect :update_bucket, returned_bucket_gapi do |name, patch_obj, **kwargs|
167+
cm_config = patch_obj.encryption.customer_managed_encryption_enforcement_config
168+
cs_config = patch_obj.encryption.customer_supplied_encryption_enforcement_config
169+
gm_config = patch_obj.encryption.google_managed_encryption_enforcement_config
170+
171+
end
166172

167173
bucket.service.mocked_service = mock
168174
_(bucket.customer_managed_encryption_enforcement_config.restriction_mode).must_equal "FullyRestricted"
169175
_(bucket.customer_supplied_encryption_enforcement_config.restriction_mode).must_equal "NotRestricted"
170176
_(bucket.google_managed_encryption_enforcement_config.restriction_mode).must_equal "NotRestricted"
171177

172-
bucket.update_bucket_encryption_enforcement_config incoming_config
178+
bucket.update do |b|
179+
b.google_managed_encryption_enforcement_config = incoming_config
180+
end
181+
# bucket.update_bucket_encryption_enforcement_config incoming_config
173182
_(bucket.customer_managed_encryption_enforcement_config.restriction_mode).must_equal "FullyRestricted"
174183
_(bucket.customer_supplied_encryption_enforcement_config.restriction_mode).must_equal "NotRestricted"
175184
_(bucket.google_managed_encryption_enforcement_config.restriction_mode).must_equal "FullyRestricted"
176185

177186
mock.verify
178187
end
179188

180-
it "raises error when invalid config is provided for update " do
181-
config = {
182-
wrongconfig: { restriction_mode: "NotRestricted" }
183-
}
184-
err = assert_raises(ArgumentError) do
185-
bucket.update_bucket_encryption_enforcement_config(config)
186-
end
187-
assert_match /Invalid config detected: wrongconfig/, err.message
188189

189-
end
190+
it "raises error when a Hash is not provided for encryption enforcement config" do
191+
invalid_config = "test"
190192

191-
it "raises error when a Hash in not provided for update " do
192-
config = "wrongconfig"
193193
err = assert_raises(ArgumentError) do
194-
bucket.update_bucket_encryption_enforcement_config(config)
194+
bucket.update do |b|
195+
b.google_managed_encryption_enforcement_config = invalid_config
196+
end
195197
end
196-
assert /incoming_config must be a Hash/
197198

199+
# Update the regex to match the error message in your validation method
200+
assert_match(/must be a Hash or valid Config object/, err.message)
198201
end
199202

200203
it "deletes all encryption enforcement configs together and preserves default_kms_key" do

0 commit comments

Comments
 (0)