From dcdc4d60a6a65046321ba1111b772285d360f174 Mon Sep 17 00:00:00 2001 From: Daniel Berger Date: Wed, 28 Mar 2018 14:24:34 -0600 Subject: [PATCH] Add custom Azure logger. Filter SharedKey and client_secret, add custom << method. Added some spaces back into replacement strings, and added some specs. Reworked to subclass Formatter. --- lib/vmdb/loggers.rb | 2 +- lib/vmdb/loggers/azure_logger.rb | 26 ++++++++++++++++++++++ spec/lib/vmdb/loggers/azure_logger_spec.rb | 26 ++++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 lib/vmdb/loggers/azure_logger.rb create mode 100644 spec/lib/vmdb/loggers/azure_logger_spec.rb diff --git a/lib/vmdb/loggers.rb b/lib/vmdb/loggers.rb index 9e158059c90..577255737d9 100644 --- a/lib/vmdb/loggers.rb +++ b/lib/vmdb/loggers.rb @@ -113,7 +113,7 @@ def self.create_loggers $api_log = create_multicast_logger(path_dir.join("api.log")) $miq_ae_logger = create_multicast_logger(path_dir.join("automation.log")) $aws_log = create_multicast_logger(path_dir.join("aws.log")) - $azure_log = create_multicast_logger(path_dir.join("azure.log")) + $azure_log = create_multicast_logger(path_dir.join("azure.log"), AzureLogger) $cn_monitoring_log = create_multicast_logger(path_dir.join("container_monitoring.log")) $datawarehouse_log = create_multicast_logger(path_dir.join("datawarehouse.log")) $fog_log = create_multicast_logger(path_dir.join("fog.log"), FogLogger) diff --git a/lib/vmdb/loggers/azure_logger.rb b/lib/vmdb/loggers/azure_logger.rb new file mode 100644 index 00000000000..c061db3d08f --- /dev/null +++ b/lib/vmdb/loggers/azure_logger.rb @@ -0,0 +1,26 @@ +module Vmdb::Loggers + class AzureLogger < VMDBLogger + def initialize(*loggers) + super + + # pulled from Ruby's `Logger::Formatter`, which is what it defaults to when it is `nil` + @datetime_format = "%Y-%m-%dT%H:%M:%S.%6N " + @formatter = Vmdb::Loggers::AzureLogger::Formatter.new + end + + def <<(msg) + msg = msg.strip + log(level, msg) + msg.size + end + + class Formatter < VMDBLogger::Formatter + def call(severity, datetime, progname, msg) + msg = msg.sub(/Bearer(.*?)\"/, 'Bearer [FILTERED] "') + msg = msg.sub(/SharedKey(.*?)\"/, 'SharedKey [FILTERED] "') + msg = msg.sub(/client_secret=(.*?)&/, "client_secret=[FILTERED]&") + super(severity, datetime, progname, msg) + end + end + end +end diff --git a/spec/lib/vmdb/loggers/azure_logger_spec.rb b/spec/lib/vmdb/loggers/azure_logger_spec.rb new file mode 100644 index 00000000000..cc42e4e318a --- /dev/null +++ b/spec/lib/vmdb/loggers/azure_logger_spec.rb @@ -0,0 +1,26 @@ +describe Vmdb::Loggers::AzureLogger do + before do + @log_stream = StringIO.new + @log = described_class.new(@log_stream) + end + + context "azure" do + it "filters out bearer tokens" do + @log.log(@log.level, 'Bearer abcd1234 "stuff"') + @log_stream.rewind + expect(@log_stream.read).to match(Regexp.quote('Bearer [FILTERED] "stuff"')) + end + + it "filters out sharedkey tokens" do + @log.log(@log.level, 'SharedKey xxx123 "stuff"') + @log_stream.rewind + expect(@log_stream.read).to match(Regexp.quote('SharedKey [FILTERED] "stuff"')) + end + + it "filters out client secret tokens" do + @log.log(@log.level, 'client_secret=abc123&management=yadayada') + @log_stream.rewind + expect(@log_stream.read).to match(Regexp.quote('client_secret=[FILTERED]&management=yadayada')) + end + end +end