Skip to content

RUBY-3156 Error if RewrapManyDataKey is called with masterKey and without provider #2720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/mongo/crypt/explicit_encrypter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ def remove_key_alt_name(id, key_alt_name)
#
# @return [ Crypt::RewrapManyDataKeyResult ] Result of the operation.
def rewrap_many_data_key(filter, opts = {})
validate_rewrap_options!(opts)

master_key_document = if opts[:provider]
options = opts.dup
provider = options.delete(:provider)
Expand Down Expand Up @@ -291,6 +293,18 @@ def rewrap_many_data_key(filter, opts = {})
@encryption_io.update_data_keys(updates)
)
end

# Ensures the consistency of the options passed to #rewrap_many_data_keys.
#
# @param [Hash] opts the options hash to validate
#
# @raise [ ArgumentError ] if the options are not consistent or
# compatible.
def validate_rewrap_options!(opts)
if opts.key?(:master_key) && !opts.key?(:provider)
raise ArgumentError, 'If :master_key is specified, :provider must also be given'
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@
expect(client_encryption1.decrypt(ciphertext)).to eq('test')
expect(client_encryption2.decrypt(ciphertext)).to eq('test')
end

context 'when master_key is present without provider' do
it 'raises an exception' do
expect { client_encryption1.rewrap_many_data_key({}, master_key: {}) }
.to raise_error(ArgumentError, /provider/)
end
end
end
end
end
Expand Down