Sha256: ed564c1376e1e826b8eb39c4b3bcb2f9dea210f9a67a3290a3d9629868222876

Contents?: true

Size: 1.3 KB

Versions: 7

Compression:

Stored size: 1.3 KB

Contents

require 'active_support/concern'
require 'active_support/lazy_load_hooks'

module CryptKeeper
  module LogSubscriber
    module PostgresPgp
      FILTER = /(\(*)(?<operation>pgp_sym_encrypt|pgp_sym_decrypt|pgp_pub_encrypt|pgp_pub_decrypt|pgp_key_id)(\(+.*\)+)/im

      # Public: Prevents sensitive data from being logged
      #
      # event - An ActiveSupport::Notifications::Event
      #
      # Returns a boolean.
      def sql(event)
        payload = crypt_keeper_payload_parse(event.payload[:sql])
        event.payload[:sql] = crypt_keeper_filter_postgres_log(payload)
        super(event)
      end

      private

      # Private: Parses the payload to UTF.
      #
      # payload - the payload string
      #
      # Returns a string.
      def crypt_keeper_payload_parse(payload)
        payload.encode('UTF-8', 'binary',
          invalid: :replace, undef: :replace, replace: '')
      end

      # Private: Filters the payload.
      #
      # payload - the payload string
      #
      # Returns a string.
      def crypt_keeper_filter_postgres_log(payload)
        payload.gsub(FILTER) do |_|
          "#{$~[:operation]}([FILTERED])"
        end
      end
    end
  end
end

ActiveSupport.on_load :crypt_keeper_postgres_pgp_log do
  ActiveRecord::LogSubscriber.prepend CryptKeeper::LogSubscriber::PostgresPgp
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
crypt_keeper-2.3.0 lib/crypt_keeper/log_subscriber/postgres_pgp.rb
crypt_keeper-2.2.0 lib/crypt_keeper/log_subscriber/postgres_pgp.rb
crypt_keeper-2.1.0 lib/crypt_keeper/log_subscriber/postgres_pgp.rb
crypt_keeper-2.0.1 lib/crypt_keeper/log_subscriber/postgres_pgp.rb
crypt_keeper-2.0.0.rc2 lib/crypt_keeper/log_subscriber/postgres_pgp.rb
crypt_keeper-2.0.0.rc1 lib/crypt_keeper/log_subscriber/postgres_pgp.rb
crypt_keeper-1.1.1 lib/crypt_keeper/log_subscriber/postgres_pgp.rb