Skip to content

Commit 7fdb1f6

Browse files
RUBY-3252 Show user friendly error if no ffi (#2730) (#2732)
* RUBY-3252 Show user friendly error if no ffi * Cleanup * Remove one test * Add comment explaining why test is omited
1 parent 9a3ac50 commit 7fdb1f6

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

lib/mongo/crypt.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,16 @@ module Crypt
3535
autoload(:ExplicitEncrypter, 'mongo/crypt/explicit_encrypter')
3636
autoload(:AutoEncrypter, 'mongo/crypt/auto_encrypter')
3737
autoload(:KMS, 'mongo/crypt/kms')
38+
39+
def validate_ffi!
40+
return if defined?(FFI)
41+
42+
require 'ffi'
43+
rescue LoadError => e
44+
raise Error::UnmetDependency, 'Cannot enable encryption because the ffi gem ' \
45+
"has not been installed. Add \"gem 'ffi'\" to your Gemfile and run " \
46+
"\"bundle install\" to install the gem. (#{e.class}: #{e})"
47+
end
48+
module_function :validate_ffi!
3849
end
3950
end

lib/mongo/crypt/auto_encrypter.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ class AutoEncrypter
9191
# @raise [ ArgumentError ] If required options are missing or incorrectly
9292
# formatted.
9393
def initialize(options)
94+
Crypt.validate_ffi!
9495
# Note that this call may eventually, via other method invocations,
9596
# create additional clients which have to be cleaned up.
9697
@options = set_default_options(options).freeze

lib/mongo/crypt/explicit_encrypter.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class ExplicitEncrypter
3636
# should be hashes of TLS connection options. The options are equivalent
3737
# to TLS connection options of Mongo::Client.
3838
def initialize(key_vault_client, key_vault_namespace, kms_providers, kms_tls_options)
39+
Crypt.validate_ffi!
3940
@crypt_handle = Handle.new(
4041
kms_providers,
4142
kms_tls_options,

spec/mongo/crypt_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe Mongo::Crypt do
6+
describe '.validate_ffi!' do
7+
context 'when ffi is available' do
8+
context 'when ffi is loaded' do
9+
it 'does not raise' do
10+
expect do
11+
described_class.validate_ffi!
12+
end.not_to raise_error
13+
end
14+
end
15+
end
16+
# There is no reasonably simple way to test the path where ffi is not
17+
# available. The ffi gem is a part of our standard test dependencies, so
18+
# it's always available. So, we would need a dedicated configuration
19+
# just to test this feature; it seems to be an overhead.
20+
end
21+
end

0 commit comments

Comments
 (0)