Skip to content

Commit ea7dc44

Browse files
RUBY-2974 Sync spec tests (#2513)
1 parent cbe5946 commit ea7dc44

18 files changed

+1302
-4
lines changed

lib/mongo/crypt/handle.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def set_bypass_query_analysis
123123
)
124124
end
125125

126-
Binding.setopt_bypass_query_analysis(self, @bypass_query_analysis)
126+
Binding.setopt_bypass_query_analysis(self) if @bypass_query_analysis
127127
end
128128

129129
# Send the logs from libmongocrypt to the Mongo::Logger

lib/mongo/protocol/msg.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,19 @@ def maybe_encrypt(connection, context)
229229
if cmd.key?('$db') && !enc_cmd.key?('$db')
230230
enc_cmd['$db'] = cmd['$db']
231231
end
232+
if schema = enc_cmd.dig('encryptionInformation', 'schema')
233+
enc_cmd['encryptionInformation']['schema'] = schema.map do |coll, params|
234+
if params['fields']
235+
params['fields'] = params['fields'].map do |field|
236+
if contention = field.dig('queries', 'contention')
237+
field['queries']['contention'] = BSON::Int64.new(contention)
238+
end
239+
field
240+
end
241+
end
242+
[coll, params]
243+
end.to_h
244+
end
232245

233246
Msg.new(@flags, @options, enc_cmd)
234247
else

spec/integration/client_side_encryption/auto_encryption_mongocryptd_spawn_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
.with(
5252
hash_including(
5353
'insert' => 'users',
54-
'$db' => 'auto_encryption',
5554
'ordered' => true,
5655
'lsid' => kind_of(Hash),
5756
'documents' => kind_of(Array),

spec/runners/crud/spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def initialize(test_path)
2020
# Introduced with Client-Side Encryption tests
2121
@json_schema = BSON::ExtJSON.parse_obj(@spec['json_schema'])
2222
@key_vault_data = BSON::ExtJSON.parse_obj(@spec['key_vault_data'])
23+
@encrypted_fields = BSON::ExtJSON.parse_obj(@spec['encrypted_fields'])
2324

2425
@requirements = if run_on = @spec['runOn']
2526
run_on.map do |spec|
@@ -46,6 +47,10 @@ def initialize(test_path)
4647
# running each test.
4748
attr_reader :key_vault_data
4849

50+
# @return [ Hash ] An encryptedFields option that should be set on the
51+
# collection (using createCollection) before each test run.
52+
attr_reader :encrypted_fields
53+
4954
def collection_name
5055
# Older spec tests do not specify a collection name, thus
5156
# we provide a default here

spec/runners/transactions/test.rb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,28 @@ def setup_test
255255
{}
256256
end
257257

258-
support_client.command(
258+
create_collection_spec = {
259259
create: @spec.collection_name,
260260
validator: collection_validator,
261261
writeConcern: { w: 'majority' }
262-
)
262+
}
263+
if @spec.encrypted_fields
264+
encrypted_fields = @spec.encrypted_fields.dup
265+
if encrypted_fields.key?('fields')
266+
encrypted_fields['fields'] = encrypted_fields['fields'].dup.map do |field|
267+
if field['queries'] && field['queries'].key?('contention')
268+
new_field = field.dup
269+
new_field['queries'] = field['queries'].dup
270+
new_field['queries']['contention'] = BSON::Int64.new(field['queries']['contention'])
271+
new_field
272+
else
273+
field
274+
end
275+
end
276+
end
277+
create_collection_spec[:encryptedFields] = encrypted_fields
278+
end
279+
support_client.command(create_collection_spec)
263280

264281
coll.insert_many(@data) unless @data.empty?
265282

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
runOn:
2+
- minServerVersion: "6.0.0"
3+
# FLE 2 Encrypted collections are not supported on standalone.
4+
topology: [ "replicaset", "sharded" ]
5+
database_name: &database_name "default"
6+
collection_name: &collection_name "default"
7+
data: []
8+
encrypted_fields: &encrypted_fields {'escCollection': 'enxcol_.default.esc', 'eccCollection': 'enxcol_.default.ecc', 'ecocCollection': 'enxcol_.default.ecoc', 'fields': [{'keyId': {'$binary': {'base64': 'EjRWeBI0mHYSNBI0VniQEg==', 'subType': '04'}}, 'path': 'encryptedIndexed', 'bsonType': 'string', 'queries': {'queryType': 'equality', 'contention': {'$numberLong': '0'}}}, {'keyId': {'$binary': {'base64': 'q83vqxI0mHYSNBI0VniQEg==', 'subType': '04'}}, 'path': 'encryptedUnindexed', 'bsonType': 'string'}]}
9+
key_vault_data: [{'_id': {'$binary': {'base64': 'EjRWeBI0mHYSNBI0VniQEg==', 'subType': '04'}}, 'keyMaterial': {'$binary': {'base64': 'sHe0kz57YW7v8g9VP9sf/+K1ex4JqKc5rf/URX3n3p8XdZ6+15uXPaSayC6adWbNxkFskuMCOifDoTT+rkqMtFkDclOy884RuGGtUysq3X7zkAWYTKi8QAfKkajvVbZl2y23UqgVasdQu3OVBQCrH/xY00nNAs/52e958nVjBuzQkSb1T8pKJAyjZsHJ60+FtnfafDZSTAIBJYn7UWBCwQ==', 'subType': '00'}}, 'creationDate': {'$date': {'$numberLong': '1648914851981'}}, 'updateDate': {'$date': {'$numberLong': '1648914851981'}}, 'status': {'$numberInt': '0'}, 'masterKey': {'provider': 'local'}}, {'_id': {'$binary': {'base64': 'q83vqxI0mHYSNBI0VniQEg==', 'subType': '04'}}, 'keyMaterial': {'$binary': {'base64': 'HBk9BWihXExNDvTp1lUxOuxuZK2Pe2ZdVdlsxPEBkiO1bS4mG5NNDsQ7zVxJAH8BtdOYp72Ku4Y3nwc0BUpIKsvAKX4eYXtlhv5zUQxWdeNFhg9qK7qb8nqhnnLeT0f25jFSqzWJoT379hfwDeu0bebJHr35QrJ8myZdPMTEDYF08QYQ48ShRBli0S+QzBHHAQiM2iJNr4svg2WR8JSeWQ==', 'subType': '00'}}, 'creationDate': {'$date': {'$numberLong': '1648914851981'}}, 'updateDate': {'$date': {'$numberLong': '1648914851981'}}, 'status': {'$numberInt': '0'}, 'masterKey': {'provider': 'local'}} ]
10+
11+
tests:
12+
- description: "BypassQueryAnalysis decrypts"
13+
clientOptions:
14+
autoEncryptOpts:
15+
kmsProviders:
16+
local: {'key': {'$binary': {'base64': 'Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk', 'subType': '00'}}}
17+
bypassQueryAnalysis: true
18+
operations:
19+
- name: insertOne
20+
arguments:
21+
document: &doc0_encrypted {
22+
"_id": 1,
23+
"encryptedIndexed": {
24+
"$binary": {
25+
# Payload has an IndexKey of key1 and UserKey of key2.
26+
"base64": "BHEBAAAFZAAgAAAAAHb62aV7+mqmaGcotPLdG3KP7S8diFwWMLM/5rYtqLrEBXMAIAAAAAAVJ6OWHRv3OtCozHpt3ZzfBhaxZirLv3B+G8PuaaO4EgVjACAAAAAAsZXWOWA+UiCBbrJNB6bHflB/cn7pWSvwWN2jw4FPeIUFcABQAAAAAMdD1nV2nqeI1eXEQNskDflCy8I7/HvvqDKJ6XxjhrPQWdLqjz+8GosGUsB7A8ee/uG9/guENuL25XD+Fxxkv1LLXtavHOlLF7iW0u9yabqqBXUAEAAAAAQSNFZ4EjSYdhI0EjRWeJASEHQAAgAAAAV2AE0AAAAAq83vqxI0mHYSNBI0VniQEkzZZBBDgeZh+h+gXEmOrSFtVvkUcnHWj/rfPW7iJ0G3UJ8zpuBmUM/VjOMJCY4+eDqdTiPIwX+/vNXegc8FZQAgAAAAAOuac/eRLYakKX6B0vZ1r3QodOQFfjqJD+xlGiPu4/PsAA==",
27+
"subType": "06"
28+
}
29+
}
30+
}
31+
- name: find
32+
arguments:
33+
filter: { "_id": 1 }
34+
result: [{"_id": 1, "encryptedIndexed": "value123" }]
35+
expectations:
36+
- command_started_event:
37+
command:
38+
listCollections: 1
39+
filter:
40+
name: *collection_name
41+
command_name: listCollections
42+
- command_started_event:
43+
command:
44+
insert: *collection_name
45+
documents:
46+
- *doc0_encrypted
47+
ordered: true
48+
command_name: insert
49+
- command_started_event:
50+
command:
51+
find: *collection_name
52+
filter: { "_id": 1 }
53+
command_name: find
54+
- command_started_event:
55+
command:
56+
find: datakeys
57+
filter: {
58+
"$or": [
59+
{
60+
"_id": {
61+
"$in": [
62+
{'$binary': {'base64': 'EjRWeBI0mHYSNBI0VniQEg==', 'subType': '04'}}
63+
]
64+
}
65+
},
66+
{
67+
"keyAltNames": {
68+
"$in": []
69+
}
70+
}
71+
]
72+
}
73+
$db: keyvault
74+
readConcern: { level: "majority" }
75+
command_name: find
76+
- command_started_event:
77+
command:
78+
find: datakeys
79+
filter: {
80+
"$or": [
81+
{
82+
"_id": {
83+
"$in": [
84+
{'$binary': {'base64': 'q83vqxI0mHYSNBI0VniQEg==', 'subType': '04'}}
85+
]
86+
}
87+
},
88+
{
89+
"keyAltNames": {
90+
"$in": []
91+
}
92+
}
93+
]
94+
}
95+
$db: keyvault
96+
readConcern: { level: "majority" }
97+
command_name: find
98+
outcome:
99+
collection:
100+
data:
101+
- {"_id": 1, "encryptedIndexed": { "$$type": "binData" }, "__safeContent__": [{ "$binary" : { "base64" : "ThpoKfQ8AkOzkFfNC1+9PF0pY2nIzfXvRdxQgjkNbBw=", "subType" : "00" } }] }
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
runOn:
2+
- minServerVersion: "6.0.0"
3+
topologies: [ "replicaset", "sharded" ]
4+
database_name: &database_name "default"
5+
collection_name: &collection_name "default"
6+
data: []
7+
encrypted_fields: &encrypted_fields {'escCollection': 'enxcol_.default.esc', 'eccCollection': 'enxcol_.default.ecc', 'ecocCollection': 'enxcol_.default.ecoc', 'fields': [{'keyId': {'$binary': {'base64': 'EjRWeBI0mHYSNBI0VniQEg==', 'subType': '04'}}, 'path': 'encryptedIndexed', 'bsonType': 'string', 'queries': {'queryType': 'equality', 'contention': {'$numberLong': '0'}}}, {'keyId': {'$binary': {'base64': 'q83vqxI0mHYSNBI0VniQEg==', 'subType': '04'}}, 'path': 'encryptedUnindexed', 'bsonType': 'string'}]}
8+
key_vault_data: [ {'_id': {'$binary': {'base64': 'EjRWeBI0mHYSNBI0VniQEg==', 'subType': '04'}}, 'keyMaterial': {'$binary': {'base64': 'sHe0kz57YW7v8g9VP9sf/+K1ex4JqKc5rf/URX3n3p8XdZ6+15uXPaSayC6adWbNxkFskuMCOifDoTT+rkqMtFkDclOy884RuGGtUysq3X7zkAWYTKi8QAfKkajvVbZl2y23UqgVasdQu3OVBQCrH/xY00nNAs/52e958nVjBuzQkSb1T8pKJAyjZsHJ60+FtnfafDZSTAIBJYn7UWBCwQ==', 'subType': '00'}}, 'creationDate': {'$date': {'$numberLong': '1648914851981'}}, 'updateDate': {'$date': {'$numberLong': '1648914851981'}}, 'status': {'$numberInt': '0'}, 'masterKey': {'provider': 'local'}}, {'_id': {'$binary': {'base64': 'q83vqxI0mHYSNBI0VniQEg==', 'subType': '04'}}, 'keyMaterial': {'$binary': {'base64': 'HBk9BWihXExNDvTp1lUxOuxuZK2Pe2ZdVdlsxPEBkiO1bS4mG5NNDsQ7zVxJAH8BtdOYp72Ku4Y3nwc0BUpIKsvAKX4eYXtlhv5zUQxWdeNFhg9qK7qb8nqhnnLeT0f25jFSqzWJoT379hfwDeu0bebJHr35QrJ8myZdPMTEDYF08QYQ48ShRBli0S+QzBHHAQiM2iJNr4svg2WR8JSeWQ==', 'subType': '00'}}, 'creationDate': {'$date': {'$numberLong': '1648914851981'}}, 'updateDate': {'$date': {'$numberLong': '1648914851981'}}, 'status': {'$numberInt': '0'}, 'masterKey': {'provider': 'local'}} ]
9+
tests:
10+
- description: "Compact works"
11+
clientOptions:
12+
autoEncryptOpts:
13+
kmsProviders:
14+
local: {'key': {'$binary': {'base64': 'Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk', 'subType': '00'}}}
15+
operations:
16+
- name: runCommand
17+
object: database
18+
command_name: compactStructuredEncryptionData
19+
arguments:
20+
command:
21+
compactStructuredEncryptionData: *collection_name
22+
expectations:
23+
- command_started_event:
24+
command:
25+
listCollections: 1
26+
filter:
27+
name: *collection_name
28+
command_name: listCollections
29+
- command_started_event:
30+
command:
31+
find: datakeys
32+
filter: {
33+
"$or": [
34+
{
35+
"_id": {
36+
"$in": [
37+
{'$binary': {'base64': 'EjRWeBI0mHYSNBI0VniQEg==', 'subType': '04'}},
38+
{'$binary': {'base64': 'q83vqxI0mHYSNBI0VniQEg==', 'subType': '04'}}
39+
]
40+
}
41+
},
42+
{
43+
"keyAltNames": {
44+
"$in": []
45+
}
46+
}
47+
]
48+
}
49+
$db: keyvault
50+
readConcern: { level: "majority" }
51+
command_name: find
52+
- command_started_event:
53+
command:
54+
compactStructuredEncryptionData: *collection_name
55+
compactionTokens: {
56+
"encryptedIndexed": {
57+
"$binary": {
58+
"base64": "noN+05JsuO1oDg59yypIGj45i+eFH6HOTXOPpeZ//Mk=",
59+
"subType": "00"
60+
}
61+
},
62+
"encryptedUnindexed": {
63+
"$binary": {
64+
"base64": "SWO8WEoZ2r2Kx/muQKb7+COizy85nIIUFiHh4K9kcvA=",
65+
"subType": "00"
66+
}
67+
}
68+
}
69+
command_name: compactStructuredEncryptionData
70+
- description: "Compact errors on an unencrypted client"
71+
operations:
72+
- name: runCommand
73+
object: database
74+
command_name: compactStructuredEncryptionData
75+
arguments:
76+
command:
77+
compactStructuredEncryptionData: *collection_name
78+
result:
79+
errorContains: "'compactStructuredEncryptionData.compactionTokens' is missing"
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
runOn:
2+
- minServerVersion: "6.0.0"
3+
# FLE 2 Encrypted collections are not supported on standalone.
4+
topology: [ "replicaset", "sharded" ]
5+
database_name: &database_name "default"
6+
collection_name: &collection_name "default"
7+
data: [
8+
&doc0 {
9+
"_id": 1,
10+
"encryptedUnindexed": {
11+
"$binary": {
12+
"base64": "BqvN76sSNJh2EjQSNFZ4kBICTQaVZPWgXp41I7mPV1rLFTtw1tXzjcdSEyxpKKqujlko5TeizkB9hHQ009dVY1+fgIiDcefh+eQrm3CkhQ==",
13+
"subType": "06"
14+
}
15+
}
16+
}
17+
]
18+
key_vault_data: [ {'_id': {'$binary': {'base64': 'q83vqxI0mHYSNBI0VniQEg==', 'subType': '04'}}, 'keyMaterial': {'$binary': {'base64': 'HBk9BWihXExNDvTp1lUxOuxuZK2Pe2ZdVdlsxPEBkiO1bS4mG5NNDsQ7zVxJAH8BtdOYp72Ku4Y3nwc0BUpIKsvAKX4eYXtlhv5zUQxWdeNFhg9qK7qb8nqhnnLeT0f25jFSqzWJoT379hfwDeu0bebJHr35QrJ8myZdPMTEDYF08QYQ48ShRBli0S+QzBHHAQiM2iJNr4svg2WR8JSeWQ==', 'subType': '00'}}, 'creationDate': {'$date': {'$numberLong': '1648914851981'}}, 'updateDate': {'$date': {'$numberLong': '1648914851981'}}, 'status': {'$numberInt': '0'}, 'masterKey': {'provider': 'local'}}]
19+
tests:
20+
- description: "FLE2 decrypt of existing data succeeds"
21+
clientOptions:
22+
autoEncryptOpts:
23+
kmsProviders:
24+
local: {'key': {'$binary': {'base64': 'Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk', 'subType': '00'}}}
25+
operations:
26+
- name: find
27+
arguments:
28+
filter: { _id: 1 }
29+
result:
30+
[{ "_id": 1, "encryptedUnindexed": "value123" }]
31+
expectations:
32+
- command_started_event:
33+
command:
34+
listCollections: 1
35+
filter:
36+
name: *collection_name
37+
command_name: listCollections
38+
- command_started_event:
39+
command:
40+
find: *collection_name
41+
filter: { "_id": 1 }
42+
command_name: find
43+
- command_started_event:
44+
command:
45+
find: datakeys
46+
filter: {
47+
"$or": [
48+
{
49+
"_id": {
50+
"$in": [
51+
{'$binary': {'base64': 'q83vqxI0mHYSNBI0VniQEg==', 'subType': '04'}}
52+
]
53+
}
54+
},
55+
{
56+
"keyAltNames": {
57+
"$in": []
58+
}
59+
}
60+
]
61+
}
62+
$db: keyvault
63+
readConcern: { level: "majority" }
64+
command_name: find

0 commit comments

Comments
 (0)