Sha256: 4a2dae8417d192c79f7ad1c866679f307127bce68fb60a3f00dc9c51bed226e2

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

module Mongoid
  module Encrypted
    class Encryptor
      class MissingKeyError < RuntimeError
        def initialize(key_path:, env_key:)
          super \
            "Missing encryption key to decrypt file with. " +
            "Ask your team for your master key and write it to #{key_path} or put it in the ENV['#{env_key}']."
        end
      end

      class << self
        def encrypt(value)
          instance.encrypt_and_sign(value)
        end

        def decrypt(value)
          crypt = instance

          if config.rotations.present?
            config.rotations.each { |r| crypt.rotate *Array.wrap(r) }
          end

          crypt.decrypt_and_verify(value)
        end

        private

        def instance
          ActiveSupport::MessageEncryptor.new(
            [ key ].pack("H*"),
            cipher: config.cipher
          )
        end

        def key
          read_env_key || read_key_file || handle_missing_key
        end

        def read_env_key
          ENV[config.env_key]
        end

        def read_key_file
          Rails.root.join(config.key_path).binread.strip rescue nil
        end

        def handle_missing_key
          raise(
            MissingKeyError,
            key_path: config.key_path,
            env_key: config.env_key
          )
        end

        def config
          Encrypted.configuration
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongoid-encrypted-1.0.0 lib/mongoid/encrypted/encryptor.rb