Skip to content

Commit

Permalink
Secrets can now be set through environment variable
Browse files Browse the repository at this point in the history
Also modified the way the initialization works. Instead of relying on
some arbitrary data, a flag must be set in secrets.yml which dictactes
if the app is in onboarding mode.

Fix #163
  • Loading branch information
pier-oliviert committed Aug 12, 2016
1 parent 0a31a50 commit 8e4a4d0
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 20 deletions.
19 changes: 6 additions & 13 deletions lib/ecrire/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,15 @@ def config
end

class << self

##
# Returns true if Ecrire::Onboarding::Engine is loaded
# in the application runtime
#
def onboarding?
defined?(Ecrire::Onboarding::Engine)
end
#
##
# Returns true if secrets.yml exists at the
# specified path or if an enviroment variable
# named ECRIRE_SECRET is set.
#
def secret?
Ecrire::Application.secrets.blank?
secrets.fetch('onboarding', true)
end

end

Rails::Application::Bootstrap.initializers.select do |initializer|
Expand All @@ -113,10 +106,10 @@ def secret?

config.before_initialize do
require 'ecrire/config/environment'
if secret?
require 'ecrire/theme/engine'
else
if onboarding?
require 'ecrire/onboarding/engine'
else
require 'ecrire/theme/engine'
end
end

Expand Down
7 changes: 7 additions & 0 deletions lib/ecrire/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
module Ecrire
class Configuration < Rails::Application::Configuration
def secret_key_base
SecureRandom.hex(16)
end

def secret_token
SecureRandom.hex(16)
end
end
end
2 changes: 2 additions & 0 deletions lib/ecrire/onboarding/controllers/onboarding_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ def save_configurations!
config['development'] ||= {}
config['production'] ||= {}
config['development']['secret_key_base'] = config['production']['secret_key'] = Rails.application.secrets.secret_key_base
config['development']['secret_token'] = config['production']['secret_token'] = Rails.application.secrets.secret_token
config['onboarding'] = false
file.write(config.to_yaml)
end
end
Expand Down
4 changes: 0 additions & 4 deletions lib/ecrire/onboarding/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ class Engine < Rails::Engine
Rails.application.config.active_record.migration_error = :none
ActiveRecord::Base.configurations = {}

initializer 'ecrire.onboarding.dynamic_settings' do |app|
app.config.secret_key_base = SecureRandom.hex(16)
end

initializer 'ecrire.load_paths', before: :bootstrap_hook do |app|
ActiveSupport::Dependencies.autoload_paths.unshift(*self.paths.autoload_paths)
ActiveSupport::Dependencies.autoload_once_paths.unshift(*self.paths.autoload_once)
Expand Down
1 change: 1 addition & 0 deletions test/editor/initializers.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ENV[Ecrire::SECRET_ENVIRONMENT_KEY] = JSON.generate({onboarding: false})
Ecrire::Application.paths.add 'config/secrets', with: Dir.pwd + '/test/secrets.yml'
Ecrire::Application.paths.add 'config/database', with: Dir.pwd + '/test/secrets.yml'

Expand Down
8 changes: 5 additions & 3 deletions test/theme/initializers.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
ENV[Ecrire::SECRET_ENVIRONMENT_KEY] = JSON.generate({onboarding: false})

Ecrire::Application.paths.add 'config/secrets', with: Dir.pwd + '/test/secrets.yml'
Ecrire::Application.paths.add 'config/database', with: Dir.pwd + '/test/secrets.yml'

Ecrire::Application.initializer 'ecrire.automigrate', after: "active_record.initialize_database" do |app|
path = app.paths['db/migrate'].existent
ActiveRecord::Migrator.migrations_paths = path
Expand All @@ -14,9 +19,6 @@ class ActiveSupport::TestCase
fixtures :all
end

Ecrire::Application.paths.add 'config/secrets', with: Dir.pwd + '/test/secrets.yml'
Ecrire::Application.paths.add 'config/database', with: Dir.pwd + '/test/secrets.yml'

Dir.chdir "test/theme/theme" do
Ecrire::Application.initialize!
end

0 comments on commit 8e4a4d0

Please sign in to comment.