Skip to content

RUBY-3252 Show user friendly error if no ffi #2730

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 4 commits into from
Jun 12, 2023
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
11 changes: 11 additions & 0 deletions lib/mongo/crypt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,16 @@ module Crypt
autoload(:ExplicitEncrypter, 'mongo/crypt/explicit_encrypter')
autoload(:AutoEncrypter, 'mongo/crypt/auto_encrypter')
autoload(:KMS, 'mongo/crypt/kms')

def validate_ffi!
return if defined?(FFI)

require 'ffi'
rescue LoadError => e
raise Error::UnmetDependency, 'Cannot enable encryption because the ffi gem ' \
"has not been installed. Add \"gem 'ffi'\" to your Gemfile and run " \
"\"bundle install\" to install the gem. (#{e.class}: #{e})"
end
module_function :validate_ffi!
end
end
1 change: 1 addition & 0 deletions lib/mongo/crypt/auto_encrypter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class AutoEncrypter
# @raise [ ArgumentError ] If required options are missing or incorrectly
# formatted.
def initialize(options)
Crypt.validate_ffi!
# Note that this call may eventually, via other method invocations,
# create additional clients which have to be cleaned up.
@options = set_default_options(options).freeze
Expand Down
1 change: 1 addition & 0 deletions lib/mongo/crypt/explicit_encrypter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ExplicitEncrypter
# should be hashes of TLS connection options. The options are equivalent
# to TLS connection options of Mongo::Client.
def initialize(key_vault_client, key_vault_namespace, kms_providers, kms_tls_options)
Crypt.validate_ffi!
@crypt_handle = Handle.new(
kms_providers,
kms_tls_options,
Expand Down
21 changes: 21 additions & 0 deletions spec/mongo/crypt_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

require 'spec_helper'

describe Mongo::Crypt do
describe '.validate_ffi!' do
context 'when ffi is available' do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be something here to test for the cases where FFI is not installed? Or skip when it isn't?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't come up with a reasonable complex solution for testing this case. We have gem 'ffi' in our standard set of dependencies for testing. So, we would need a dedicated configuration just to test this feature; I think this is an overhead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! That makes sense. I'll trust that you've thought about this a lot more than I have. :) Thanks for the explanation. Maybe a comment in that spec, explaining the situation, would be helpful to future-us?

context 'when ffi is loaded' do
it 'does not raise' do
expect do
described_class.validate_ffi!
end.not_to raise_error
end
end
end
# There is no reasonably simple way to test the path where ffi is not
# available. The ffi gem is a part of our standard test dependencies, so
# it's always available. So, we would need a dedicated configuration
# just to test this feature; it seems to be an overhead.
end
end