class Schienenzeppelin::AddOns::Credentials

Public Instance Methods

apply() click to toggle source
# File lib/schienenzeppelin/addons/credentials.rb, line 6
def apply
  key_path = Pathname.new('config/credentials/production.key')
  create_credential_files(key_path, 'config/credentials/production.yml.enc')
end

Private Instance Methods

capturable_erb(match, source) click to toggle source
# File lib/schienenzeppelin/addons/credentials.rb, line 40
def capturable_erb(match, source)
  if match && match[1] >= '2.2.0' # Ruby 2.6+
    CapturableERB.new(::File.binread(source), trim_mode: '-', eoutvar: '@output_buffer')
  else
    CapturableERB.new(::File.binread(source), nil, '-', '@output_buffer')
  end
end
create_credential_files(key_path, file_path) click to toggle source
# File lib/schienenzeppelin/addons/credentials.rb, line 13
def create_credential_files(key_path, file_path)
  key = ActiveSupport::EncryptedFile.generate_key
  encryption_key_file_generator.add_key_file_silently(key_path, key)
  require 'active_support/encrypted_file'
  ActiveSupport::EncryptedFile.new(
    content_path: file_path,
    key_path: key_path,
    env_key: 'RAILS_MASTER_KEY',
    raise_if_missing_key: true
  ).write(credentials_template)
end
credentials_template() click to toggle source
# File lib/schienenzeppelin/addons/credentials.rb, line 30
def credentials_template
  context = instance_eval('binding', __FILE__, __LINE__)
  source = File.expand_path(find_in_source_paths('config/credentials.yml.erb'))
  match = ERB.version.match(/(\d+\.\d+\.\d+)/)
  capturable_erb = capturable_erb(match, source)
  capturable_erb.tap do |erb|
    erb.filename = source
  end.result(context)
end
encryption_key_file_generator() click to toggle source
# File lib/schienenzeppelin/addons/credentials.rb, line 25
def encryption_key_file_generator
  require 'rails/generators/rails/encrypted_file/encrypted_file_generator'
  Rails::Generators::EncryptionKeyFileGenerator.new
end