|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'pathname' |
| 4 | +require 'tmpdir' |
| 5 | + |
| 6 | +require 'active_support/ordered_options' |
| 7 | +require 'active_support/core_ext/hash' |
| 8 | +require 'active_support/core_ext/module/delegation' |
| 9 | +require 'active_support/core_ext/object/inclusion' |
| 10 | + |
| 11 | +module Diffcrypt |
| 12 | + module Rails |
| 13 | + class EncryptedConfiguration |
| 14 | + attr_reader :content_path |
| 15 | + attr_reader :key_path |
| 16 | + attr_reader :env_key |
| 17 | + attr_reader :raise_if_missing_key |
| 18 | + |
| 19 | + delegate :[], :fetch, to: :config |
| 20 | + delegate_missing_to :options |
| 21 | + |
| 22 | + def initialize(config_path:, key_path:, env_key:, raise_if_missing_key:) |
| 23 | + @content_path = Pathname.new(File.absolute_path(config_path)).yield_self do |path| |
| 24 | + path.symlink? ? path.realpath : path |
| 25 | + end |
| 26 | + @key_path = Pathname.new(key_path) |
| 27 | + @env_key = env_key |
| 28 | + @raise_if_missing_key = raise_if_missing_key |
| 29 | + @active_support_encryptor = ActiveSupport::MessageEncryptor.new([key].pack('H*'), cipher: Encryptor::CIPHER) |
| 30 | + end |
| 31 | + |
| 32 | + # Allow a config to be started without a file present |
| 33 | + # @return [String] Returns decryped content or a blank string |
| 34 | + def read |
| 35 | + raise MissingContentError, content_path unless !key.nil? && content_path.exist? |
| 36 | + |
| 37 | + decrypt content_path.binread |
| 38 | + rescue MissingContentError |
| 39 | + '' |
| 40 | + end |
| 41 | + |
| 42 | + def write(contents) |
| 43 | + deserialize(contents) |
| 44 | + |
| 45 | + IO.binwrite "#{content_path}.tmp", encrypt(contents) |
| 46 | + FileUtils.mv "#{content_path}.tmp", content_path |
| 47 | + end |
| 48 | + |
| 49 | + def config |
| 50 | + @config ||= deserialize(read).deep_symbolize_keys |
| 51 | + end |
| 52 | + |
| 53 | + # @raise [MissingKeyError] Will raise if key is not set |
| 54 | + # @return [String] |
| 55 | + def key |
| 56 | + read_env_key || read_key_file || handle_missing_key |
| 57 | + end |
| 58 | + |
| 59 | + def change(&block) |
| 60 | + writing read, &block |
| 61 | + end |
| 62 | + |
| 63 | + protected |
| 64 | + |
| 65 | + def writing(contents) |
| 66 | + tmp_file = "#{Process.pid}.#{content_path.basename.to_s.chomp('.enc')}" |
| 67 | + tmp_path = Pathname.new File.join(Dir.tmpdir, tmp_file) |
| 68 | + tmp_path.binwrite contents |
| 69 | + |
| 70 | + yield tmp_path |
| 71 | + |
| 72 | + updated_contents = tmp_path.binread |
| 73 | + |
| 74 | + write(updated_contents) if updated_contents != contents |
| 75 | + ensure |
| 76 | + FileUtils.rm(tmp_path) if tmp_path&.exist? |
| 77 | + end |
| 78 | + |
| 79 | + # @param [String] contents |
| 80 | + # @return [String] |
| 81 | + def encrypt(contents) |
| 82 | + encryptor.encrypt contents |
| 83 | + end |
| 84 | + |
| 85 | + # @param [String] contents |
| 86 | + # @return [String] |
| 87 | + def decrypt(contents) |
| 88 | + if contents.index('---').nil? |
| 89 | + @active_support_encryptor.decrypt_and_verify contents |
| 90 | + else |
| 91 | + encryptor.decrypt contents |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + # @return [Encryptor] |
| 96 | + def encryptor |
| 97 | + @encryptor ||= Encryptor.new key |
| 98 | + end |
| 99 | + |
| 100 | + def read_env_key |
| 101 | + ENV[env_key] |
| 102 | + end |
| 103 | + |
| 104 | + def read_key_file |
| 105 | + key_path.binread.strip if key_path.exist? |
| 106 | + end |
| 107 | + |
| 108 | + # @raise [MissingKeyError] |
| 109 | + # @return [void] |
| 110 | + def handle_missing_key |
| 111 | + raise MissingKeyError.new(key_path: key_path, env_key: env_key) if raise_if_missing_key |
| 112 | + end |
| 113 | + |
| 114 | + def options |
| 115 | + @options ||= ActiveSupport::InheritableOptions.new(config) |
| 116 | + end |
| 117 | + |
| 118 | + def deserialize(config) |
| 119 | + YAML.safe_load(config).presence || {} |
| 120 | + end |
| 121 | + end |
| 122 | + end |
| 123 | +end |
0 commit comments