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
|
# frozen_string_literal: true
require 'spec_helper'
describe 'Decryption events' do
require_enterprise
min_server_fcv '4.2'
require_libmongocrypt
include_context 'define shared FLE helpers'
require_topology :replica_set
min_server_version '7.0.0-rc0'
let(:setup_client) do
ClientRegistry.instance.new_local_client(
SpecConfig.instance.addresses,
SpecConfig.instance.test_options.merge(
database: SpecConfig.instance.test_db
)
)
end
let(:collection_name) do
'decryption_event'
end
let(:client_encryption) do
Mongo::ClientEncryption.new(
setup_client,
key_vault_namespace: "#{key_vault_db}.#{key_vault_coll}",
kms_providers: local_kms_providers
)
end
let(:key_id) do
client_encryption.create_data_key('local')
end
let(:unencrypted_value) do
'hello'
end
let(:ciphertext) do
client_encryption.encrypt(
unencrypted_value,
key_id: key_id,
algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic'
)
end
let(:malformed_ciphertext) do
ciphertext.dup.tap do |obj|
obj.data[-1] = 0.chr
end
end
let(:encrypted_client) do
ClientRegistry.instance.new_local_client(
SpecConfig.instance.addresses,
SpecConfig.instance.test_options.merge(
auto_encryption_options: {
key_vault_namespace: "#{key_vault_db}.#{key_vault_coll}",
kms_providers: local_kms_providers,
extra_options: extra_options,
},
database: SpecConfig.instance.test_db,
retry_reads: false,
max_read_retries: 0
)
)
end
let(:collection) do
encrypted_client[collection_name]
end
let(:subscriber) { Mrss::EventSubscriber.new }
let(:command_error) do
{
'configureFailPoint' => 'failCommand',
'mode' => { 'times' => 1 },
'data' => {
'errorCode' => 123,
'failCommands' => [ 'aggregate' ]
}
}
end
let(:network_error) do
{
'configureFailPoint' => 'failCommand',
'mode' => { 'times' => 1 },
'data' => {
'errorCode' => 123,
'closeConnection' => true,
'failCommands' => [ 'aggregate' ]
}
}
end
let(:aggregate_event) do
subscriber.succeeded_events.detect do |evt|
evt.command_name == 'aggregate'
end
end
before do
setup_client[collection_name].drop
setup_client[collection_name].create
encrypted_client.subscribe(Mongo::Monitoring::COMMAND, subscriber)
end
it 'tests command error' do
setup_client.use(:admin).command(command_error)
expect do
collection.aggregate([]).to_a
end.to raise_error(Mongo::Error::OperationFailure, /Failing command (?:via|due to) 'failCommand' failpoint/)
expect(subscriber.failed_events.length).to be 1
end
it 'tests network error' do
setup_client.use(:admin).command(network_error)
expect do
collection.aggregate([]).to_a
end.to raise_error(Mongo::Error::SocketError)
expect(subscriber.failed_events.length).to be 1
end
context 'when decrypt error' do
before do
collection.insert_one(encrypted: malformed_ciphertext)
end
it 'fails' do
expect { collection.aggregate([]).to_a }.to raise_error(Mongo::Error::CryptError)
expect(aggregate_event).not_to be_nil
expect(
aggregate_event.reply.dig('cursor', 'firstBatch')&.first&.dig('encrypted')
).to be_a(BSON::Binary)
end
end
context 'when decrypt success' do
before do
collection.insert_one(encrypted: ciphertext)
end
it 'succeeds' do
expect { collection.aggregate([]).to_a }.not_to raise_error
expect(aggregate_event).not_to be_nil
expect(
aggregate_event.reply.dig('cursor', 'firstBatch')&.first&.dig('encrypted')
).to be_a(BSON::Binary)
end
end
end
|