Trouble adding vault-rails gem to a plugin

I’m trying to add vault-rails to a plugin to encrypt keys that are part of a model I have added, following an example here: Plugin with custom gems

(Is there some way to do that already in Discourse that I’ve not found? I only see the encrypt plugin. . .)

If I put this line in my plugin.rb, I get Could not find 'rails' (>= 5.0) among 348 total gem(s). Adding the same line to the Discourse Gemfile works fine.

gem "vault-rails", "0.7.0", { require: false }
 (master *) pfaffman@shinytim:~/src/discourse-repos/discourse$ ./bin/rails c
Traceback (most recent call last):
	43: from ./bin/rails:17:in `<main>'
	42: from /home/pfaffman/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/activesupport-6.0.3.3/lib/active_support/dependencies.rb:324:in `require'
	41: from /home/pfaffman/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/activesupport-6.0.3.3/lib/active_support/dependencies.rb:291:in `load_dependency'
	40: from /home/pfaffman/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/activesupport-6.0.3.3/lib/active_support/dependencies.rb:324:in `block in require'
	39: from /home/pfaffman/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
	38: from /home/pfaffman/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
	37: from /home/pfaffman/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
	36: from /home/pfaffman/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
	35: from /home/pfaffman/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
	34: from /home/pfaffman/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/railties-6.0.3.3/lib/rails/commands.rb:18:in `<main>'
	33: from /home/pfaffman/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/railties-6.0.3.3/lib/rails/command.rb:46:in `invoke'
	32: from /home/pfaffman/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/railties-6.0.3.3/lib/rails/command/base.rb:69:in `perform'
	31: from /home/pfaffman/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/thor-1.0.1/lib/thor.rb:392:in `dispatch'
	30: from /home/pfaffman/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/thor-1.0.1/lib/thor/invocation.rb:127:in `invoke_command'
	29: from /home/pfaffman/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/thor-1.0.1/lib/thor/command.rb:27:in `run'
	28: from /home/pfaffman/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/railties-6.0.3.3/lib/rails/commands/console/console_command.rb:101:in `perform'
	27: from /home/pfaffman/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/railties-6.0.3.3/lib/rails/command/actions.rb:14:in `require_application_and_environment!'
	26: from /home/pfaffman/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/railties-6.0.3.3/lib/rails/command/actions.rb:22:in `require_application!'
	25: from /home/pfaffman/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/activesupport-6.0.3.3/lib/active_support/dependencies.rb:324:in `require'
	24: from /home/pfaffman/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/activesupport-6.0.3.3/lib/active_support/dependencies.rb:291:in `load_dependency'
	23: from /home/pfaffman/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/activesupport-6.0.3.3/lib/active_support/dependencies.rb:324:in `block in require'
	22: from /home/pfaffman/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
	21: from /home/pfaffman/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
	20: from /home/pfaffman/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
	19: from /home/pfaffman/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
	18: from /home/pfaffman/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
	17: from /home/pfaffman/src/discourse-repos/discourse/config/application.rb:70:in `<main>'
	16: from /home/pfaffman/src/discourse-repos/discourse/config/application.rb:71:in `<module:Discourse>'
	15: from /home/pfaffman/src/discourse-repos/discourse/config/application.rb:304:in `<class:Application>'
	14: from /home/pfaffman/src/discourse-repos/discourse/lib/plugin_initialization_guard.rb:5:in `plugin_initialization_guard'
	13: from /home/pfaffman/src/discourse-repos/discourse/config/application.rb:305:in `block in <class:Application>'
	12: from lib/discourse.rb:225:in `activate_plugins!'
	11: from lib/discourse.rb:225:in `each'
	10: from lib/discourse.rb:228:in `block in activate_plugins!'
	 9: from /home/pfaffman/src/discourse-repos/discourse/lib/plugin/instance.rb:611:in `activate!'
	 8: from /home/pfaffman/src/discourse-repos/discourse/lib/plugin/instance.rb:611:in `instance_eval'
	 7: from /home/pfaffman/src/discourse-repos/discourse/plugins/discourse-pfaffmanager/plugin.rb:13:in `activate!'
	 6: from /home/pfaffman/src/discourse-repos/discourse/lib/plugin/instance.rb:705:in `gem'
	 5: from /home/pfaffman/src/discourse-repos/discourse/lib/plugin_gem.rb:22:in `load'
	 4: from /home/pfaffman/.rbenv/versions/2.7.1/lib/ruby/site_ruby/2.7.0/rubygems/specification.rb:1371:in `activate'
	 3: from /home/pfaffman/.rbenv/versions/2.7.1/lib/ruby/site_ruby/2.7.0/rubygems/specification.rb:1389:in `activate_dependencies'
	 2: from /home/pfaffman/.rbenv/versions/2.7.1/lib/ruby/site_ruby/2.7.0/rubygems/specification.rb:1389:in `each'
	 1: from /home/pfaffman/.rbenv/versions/2.7.1/lib/ruby/site_ruby/2.7.0/rubygems/specification.rb:1400:in `block in activate_dependencies'
/home/pfaffman/.rbenv/versions/2.7.1/lib/ruby/site_ruby/2.7.0/rubygems/dependency.rb:311:in `to_specs': Could not find 'rails' (>= 5.0) among 348 total gem(s) (Gem::MissingSpecError)
Checked in 'GEM_PATH=/home/pfaffman/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0:/home/pfaffman/Dropbox/home/bin/dotfiles/.gem/ruby/2.7.0', execute `gem env` for more information

The post you linked says:

Have you tried doing that?

Discourse doesn’t have a dependency on the Rails gem, we only import the modules we need. This gem you want depends on the full rails gem so you will need to recursively adds each dependency Discourse doesn’t already ships with.

I’m afraid it will be a lot of stuff, but at least it tells you exactly what you are missing.

3 Likes

Oh. It never would have occurred to me that rails wasn’t loaded.

all the dependency tree! I see.

It does seem like a lot of stuff.

I’ll be storing stuff like a digital ocean api key and an ssh key in the database. Do you know, offhand, another simple way to do that safely? I suppose that I could just shell out and run vault or something like it with an exec. It won’t be happening that often.

Maybe use just the vault gem that only depends on aws-sigv4 and aws-eventstream?

What do you mean by “safely” here? What is the Threat model - Wikipedia ? Are you trying to protect it from Admins with Data Explorer access? In that case using a simple ENV variable should be easier.

1 Like

“safely” means safe enough that you’d be willing to provide your Digital Ocean API key & Mail gun API key to have my little plugin create a droplet and install Discourse on it and then have an SSH key that it uses to perform upgrades and such.

I think the only production I’ll be adding is that if someone steals the database, they won’t have those keys. Of course if they hack the server then all bets are off.

I guess I may need to find a more lightweight way to encrypt these few fields.

EDIT: https://pawelurbanek.com/rails-secure-encrypt-decrypt looks simple enough and uses ActiveSupport::MessageEncryptor, which seems to be available already.