Sha256: 32028bb03180464cfe72669024d147b644cd4ed58875f3e52e10627b5da06426

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module EJSON
  module Rails
    Rails = ::Rails
    private_constant :Rails

    class Railtie < Rails::Railtie
      singleton_class.attr_accessor(:set_secrets)
      @set_secrets = true

      config.before_configuration do
        json_file = json_files.detect { |file| valid?(file) }
        next unless json_file

        secrets = JSON.parse(json_file.read, symbolize_names: true)
        Rails.application.secrets.deep_merge!(secrets) if set_secrets
        # Merging into `credentials.config` because in Rails 7.0, reading a credential with
        # Rails.application.credentials[:some_credential] won't work otherwise.
        Rails.application.credentials.config.deep_merge!(secrets) do |key|
          raise "A credential already exists with the same name: #{key}"
        end

        # Delete the loaded JSON files so they are no longer readable by the app.
        if ENV["EJSON_RAILS_DELETE_SECRETS"] == "true"
          json_files.each do |pathname|
            File.delete(pathname) if File.writable?(pathname)
          end
        end
      end

      class << self
        private

        def valid?(pathname)
          pathname.exist?
        end

        def json_files
          [
            Rails.root.join("config", "secrets.json"),
            Rails.root.join("config", "secrets.#{Rails.env}.json"),
          ]
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ejson-rails-0.2.1 lib/ejson/rails/railtie.rb