Skip to content

Commit

Permalink
Merge pull request #121 from jmazzi/feature/silence-logs
Browse files Browse the repository at this point in the history
Allow silencing logging completely
  • Loading branch information
jmazzi authored Sep 1, 2016
2 parents 99cf351 + 25d6545 commit e61fbeb
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 5 deletions.
4 changes: 2 additions & 2 deletions gemfiles/activerecord_4_1.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ../
specs:
crypt_keeper (0.20.0)
crypt_keeper (0.21.0)
activerecord (>= 3.1, < 4.3)
activesupport (>= 3.1, < 4.3)
aes (~> 0.5.0)
Expand Down Expand Up @@ -117,4 +117,4 @@ DEPENDENCIES
sqlite3

BUNDLED WITH
1.12.4
1.12.5
4 changes: 2 additions & 2 deletions gemfiles/activerecord_4_2.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ../
specs:
crypt_keeper (0.20.0)
crypt_keeper (0.21.0)
activerecord (>= 3.1, < 4.3)
activesupport (>= 3.1, < 4.3)
aes (~> 0.5.0)
Expand Down Expand Up @@ -117,4 +117,4 @@ DEPENDENCIES
sqlite3

BUNDLED WITH
1.12.4
1.12.5
5 changes: 4 additions & 1 deletion lib/crypt_keeper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
module CryptKeeper
class << self
attr_accessor :stub_encryption
alias_method :stub_encryption?, :stub_encryption
alias_method :stub_encryption?, :stub_encryption

attr_accessor :silence_logs
alias_method :silence_logs?, :silence_logs
end
end

Expand Down
2 changes: 2 additions & 0 deletions lib/crypt_keeper/log_subscriber/mysql_aes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def sql_with_mysql_aes(event)
payload = event.payload[:sql]
.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')

return if CryptKeeper.silence_logs? && payload =~ filter

event.payload[:sql] = payload.gsub(filter) do |_|
"#{$1}([FILTERED])"
end
Expand Down
2 changes: 2 additions & 0 deletions lib/crypt_keeper/log_subscriber/postgres_pgp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def sql_with_postgres_pgp(event)
payload = event.payload[:sql]
.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')

return if CryptKeeper.silence_logs? && payload =~ filter

event.payload[:sql] = payload.gsub(filter) do |_|
"#{$~[:operation]}([FILTERED])"
end
Expand Down
12 changes: 12 additions & 0 deletions spec/log_subscriber/mysql_aes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

module CryptKeeper::LogSubscriber
describe MysqlAes do
before do
CryptKeeper.silence_logs = false
end

use_mysql

context "AES encryption" do
Expand Down Expand Up @@ -56,6 +60,14 @@ module CryptKeeper::LogSubscriber
string_encoding_query = "SELECT aes_encrypt('hi \255', 'test')"
subject.sql(ActiveSupport::Notifications::Event.new(:sql, 1, 1, 1, { sql: string_encoding_query }))
end

it "skips logging if CryptKeeper.silence_logs is set" do
CryptKeeper.silence_logs = true

subject.should_not_receive(:sql_without_mysql_aes)

subject.sql(ActiveSupport::Notifications::Event.new(:sql, 1, 1, 1, { sql: input_query }))
end
end
end
end
20 changes: 20 additions & 0 deletions spec/log_subscriber/postgres_pgp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

module CryptKeeper::LogSubscriber
describe PostgresPgp do
before do
CryptKeeper.silence_logs = false
end

use_postgres

context "Symmetric encryption" do
Expand Down Expand Up @@ -56,6 +60,14 @@ module CryptKeeper::LogSubscriber
string_encoding_query = "SELECT pgp_sym_encrypt('hi \255', 'test')"
subject.sql(ActiveSupport::Notifications::Event.new(:sql, 1, 1, 1, { sql: string_encoding_query }))
end

it "skips logging if CryptKeeper.silence_logs is set" do
CryptKeeper.silence_logs = true

subject.should_not_receive(:sql_without_postgres_pgp)

subject.sql(ActiveSupport::Notifications::Event.new(:sql, 1, 1, 1, { sql: input_query }))
end
end

context "Public key encryption" do
Expand Down Expand Up @@ -98,6 +110,14 @@ module CryptKeeper::LogSubscriber

subject.sql(ActiveSupport::Notifications::Event.new(:sql, 1, 1, 1, { sql: input_query.downcase }))
end

it "skips logging if CryptKeeper.silence_logs is set" do
CryptKeeper.silence_logs = true

subject.should_not_receive(:sql_without_postgres_pgp)

subject.sql(ActiveSupport::Notifications::Event.new(:sql, 1, 1, 1, { sql: input_query }))
end
end
end
end

0 comments on commit e61fbeb

Please sign in to comment.