Skip to content

Commit 7a4c172

Browse files
Add tests
1 parent 1e4d916 commit 7a4c172

File tree

6 files changed

+114
-10
lines changed

6 files changed

+114
-10
lines changed

lib/mongo.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ def self.delegate_option(obj, opt)
100100
delegate_option Config, :broken_view_aggregate
101101
delegate_option Config, :broken_view_options
102102
delegate_option Config, :validate_update_replace
103+
delegate_option Config, :csfle_convert_to_ruby_types
103104
end
104105

105106
# Clears the driver's OCSP response cache.

lib/mongo/config.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module Config
2929

3030
# When this flag is set to true, the CSFLE will use Ruby types for
3131
# decryption instead of BSON types.
32-
option :fle_use_ruby_types, default: false
32+
option :csfle_convert_to_ruby_types, default: false
3333

3434
# Set the configuration options.
3535
#

lib/mongo/crypt/auto_decryption_context.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ def initialize(mongocrypt, io, command)
4141

4242
# Which BSON mode to use when creating documents from the outcome of
4343
# the state machine. The returned value is based on the
44-
# +Mongo::Config.fle_use_ruby_types+ option.
44+
# +Mongo::Config.csfle_convert_to_ruby_types+ option.
4545
#
4646
# @return [ Symbol, nil ] The BSON mode.
4747
def bson_mode
48-
Mongo::Config.fle_use_ruby_types ? nil : :bson
48+
Mongo::Config.csfle_convert_to_ruby_types ? nil : :bson
4949
end
5050
end
5151
end

lib/mongo/crypt/explicit_decryption_context.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ def initialize(mongocrypt, io, doc)
4141

4242
# Which BSON mode to use when creating documents from the outcome of
4343
# the state machine. The returned value is based on the
44-
# +Mongo::Config.fle_use_ruby_types+ option.
44+
# +Mongo::Config.csfle_convert_to_ruby_types+ option.
4545
#
4646
# @return [ Symbol, nil ] The BSON mode.
4747
def bson_mode
48-
Mongo::Config.fle_use_ruby_types ? nil : :bson
48+
Mongo::Config.csfle_convert_to_ruby_types ? nil : :bson
4949
end
5050
end
5151
end
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'Auto Encryption Type Conversion' do
6+
require_libmongocrypt
7+
require_enterprise
8+
min_server_fcv '4.2'
9+
10+
include_context 'define shared FLE helpers'
11+
include_context 'with local kms_providers'
12+
13+
let(:client) do
14+
new_local_client(
15+
SpecConfig.instance.addresses,
16+
SpecConfig.instance.test_options.merge(
17+
auto_encryption_options: {
18+
kms_providers: kms_providers,
19+
key_vault_namespace: key_vault_namespace,
20+
schema_map: { 'auto_encryption.users' => schema_map },
21+
# Spawn mongocryptd on non-default port for sharded cluster tests
22+
extra_options: extra_options,
23+
},
24+
database: 'auto_encryption'
25+
)
26+
)
27+
end
28+
29+
let(:large_number) { 2**40 }
30+
31+
let(:ssn) { '123-45-6789' }
32+
33+
let(:decrypted_doc) do
34+
client['users'].find(_id: 1).first
35+
end
36+
37+
before do
38+
authorized_client.use('auto_encryption')['users'].drop
39+
40+
key_vault_collection.drop
41+
key_vault_collection.insert_one(data_key)
42+
43+
client['users'].insert_one(
44+
_id: 1,
45+
large_number: large_number,
46+
ssn: ssn
47+
)
48+
end
49+
50+
context 'when csfle_convert_to_ruby_types is true' do
51+
config_override :csfle_convert_to_ruby_types, true
52+
53+
it 'returns Int64 as Integer' do
54+
expect(decrypted_doc['ssn']).to eq(ssn) # To check that decryption works
55+
expect(decrypted_doc['large_number']).to be_a(Integer)
56+
expect(decrypted_doc['large_number']).to eq(large_number)
57+
end
58+
end
59+
60+
context 'when csfle_convert_to_ruby_types is false' do
61+
config_override :csfle_convert_to_ruby_types, false
62+
63+
it 'returns Int64 as BSON::Int64' do
64+
expect(decrypted_doc['ssn']).to eq(ssn) # To check that decryption works
65+
expect(decrypted_doc['large_number']).to be_a(BSON::Int64)
66+
expect(decrypted_doc['large_number'].value).to eq(large_number)
67+
end
68+
end
69+
end

spec/integration/client_side_encryption/explicit_encryption_spec.rb

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
client.use(key_vault_db)[key_vault_coll].drop
2929
end
3030

31-
shared_examples 'an explicit encrypter' do
31+
shared_examples 'an explicit encrypter' do |decrypted_value|
3232
it 'encrypts and decrypts the value using key_id' do
3333
data_key_id = client_encryption.create_data_key(
3434
kms_provider_name,
@@ -44,8 +44,13 @@
4444
)
4545

4646
decrypted = client_encryption.decrypt(encrypted)
47-
expect(decrypted).to eq(value)
48-
expect(decrypted).to be_a_kind_of(value.class)
47+
if decrypted_value.nil?
48+
expect(decrypted).to eq(value)
49+
expect(decrypted).to be_a_kind_of(value.class)
50+
else
51+
expect(decrypted).to eq(decrypted_value)
52+
expect(decrypted).to be_a_kind_of(decrypted_value.class)
53+
end
4954
end
5055

5156
it 'encrypts and decrypts the value using key_alt_name' do
@@ -63,8 +68,13 @@
6368
)
6469

6570
decrypted = client_encryption.decrypt(encrypted)
66-
expect(decrypted).to eq(value)
67-
expect(decrypted).to be_a_kind_of(value.class)
71+
if decrypted_value.nil?
72+
expect(decrypted).to eq(value)
73+
expect(decrypted).to be_a_kind_of(value.class)
74+
else
75+
expect(decrypted).to eq(decrypted_value)
76+
expect(decrypted).to be_a_kind_of(decrypted_value.class)
77+
end
6878
end
6979
end
7080

@@ -140,6 +150,30 @@
140150
end
141151
end
142152

153+
context 'values is a large integer that requires BSON::Int64' do
154+
let(:value) { 2**40 }
155+
156+
context 'when csfle_convert_to_ruby_types is true' do
157+
config_override :csfle_convert_to_ruby_types, true
158+
159+
context 'with local KMS provider' do
160+
include_context 'with local kms_providers'
161+
162+
it_behaves_like 'an explicit encrypter'
163+
end
164+
end
165+
166+
context 'when csfle_convert_to_ruby_types is false' do
167+
config_override :csfle_convert_to_ruby_types, false
168+
169+
context 'with local KMS provider' do
170+
include_context 'with local kms_providers'
171+
172+
it_behaves_like 'an explicit encrypter', BSON::Int64.new(2**40)
173+
end
174+
end
175+
end
176+
143177
context 'value is an symbol' do
144178
let(:value) { BSON::Symbol::Raw.new(:hello_world) }
145179

0 commit comments

Comments
 (0)