require 'chamber/errors/decryption_failure'

module  Chamber
module  Filters
class   FailedDecryptionFilter
  SECURE_KEY_TOKEN      = /\A_secure_/
  BASE64_STRING_PATTERN = %r{\A[A-Za-z0-9\+/]{342}==\z}

  def initialize(options = {})
    self.data = options.fetch(:data).dup
  end

  def self.execute(options = {})
    new(options).send(:execute)
  end

  protected

  attr_accessor :data

  def execute(raw_data = data)
    settings = raw_data

    raw_data.each_pair do |key, value|
      if value.respond_to? :each_pair
        execute(value)
      elsif key.match(SECURE_KEY_TOKEN) &&
            value.respond_to?(:match)   &&
            value.match(BASE64_STRING_PATTERN)

        fail Chamber::Errors::DecryptionFailure,
             'Failed to decrypt values in your settings.'
      end
    end

    settings
  end
end
end
end

require 'chamber/errors/decryption_failure'

module  Chamber
module  Filters
class   FailedDecryptionFilter
  SECURE_KEY_TOKEN      = /\A_secure_/
  BASE64_STRING_PATTERN = %r{\A[A-Za-z0-9\+/]{342}==\z}

  def initialize(options = {})
    self.data = options.fetch(:data).dup
  end

  def self.execute(options = {})
    new(options).send(:execute)
  end

  protected

  attr_accessor :data

  def execute(raw_data = data)
    settings = raw_data

    raw_data.each_pair do |key, value|
      if value.respond_to? :each_pair
        execute(value)
      elsif key.match(SECURE_KEY_TOKEN) &&
            value.respond_to?(:match)   &&
            value.match(BASE64_STRING_PATTERN)

        fail Chamber::Errors::DecryptionFailure,
             "Failed to decrypt #{key} (with an encrypted value of '#{value}') " \
             'in your settings.'
      end
    end

    settings
  end
end
end
end

Add key and encrypted value to failed decryption error message
Feature: Add key and encrypted value to failed decryption error message

Previously it would just say that it failed to decrypt a value, but wouldn't
give you any information on which one it was, so it was impossible to fix.

--------------------------------------------------------------------------------
Change-Id:     I1f3f92dc889cd3a901af66de57fd63a4ed2d380a
Signed-off-by: Jeff Felchner <8a84c204e2d4c387d61d20f7892a2bbbf0bc8ae8@thekompanee.com>
