Add an option to disable logging for jobs with sensitive arguments

class SensitiveJob < ApplicationJob
      self.log_arguments = false

      def perform(my_sensitive_argument)
      end
    end

When dealing with sensitive arugments as password and tokens it is
now possible to configure the job to not put the sensitive argument
in the logs.

Closes #34438.
This commit is contained in:
Rafael Mendonça França 2019-11-07 14:06:06 -05:00
parent 53627bd55e
commit ce085f62d4
No known key found for this signature in database
GPG Key ID: FC23B6D0F1EEE948
6 changed files with 44 additions and 1 deletions

View File

@ -1,3 +1,17 @@
* Add an option to disable logging of the job arguments when enqueuing and executing the job.
class SensitiveJob < ApplicationJob
self.log_arguments = false
def perform(my_sensitive_argument)
end
end
When dealing with sensitive arugments as password and tokens it is now possible to configure the job
to not put the sensitive argument in the logs.
*Rafael Mendonça França*
* Changes in `queue_name_prefix` of a job no longer affects all other jobs. Fixes #37084.
*Lucas Mansur*

View File

@ -77,7 +77,7 @@ module ActiveJob
end
def args_info(job)
if job.arguments.any?
if job.class.log_arguments? && job.arguments.any?
" with arguments: " +
job.arguments.map { |arg| format(arg).inspect }.join(", ")
else

View File

@ -10,6 +10,7 @@ module ActiveJob
included do
cattr_accessor :logger, default: ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT))
class_attribute :log_arguments, instance_accessor: false, default: true
around_enqueue { |_, block| tag_logger(&block) }
around_perform { |job, block| tag_logger(job.class.name, job.job_id, &block) }

View File

@ -9,6 +9,7 @@ require "jobs/overridden_logging_job"
require "jobs/nested_job"
require "jobs/rescue_job"
require "jobs/retry_job"
require "jobs/disable_log_job"
require "models/person"
class LoggingTest < ActiveSupport::TestCase
@ -122,6 +123,18 @@ class LoggingTest < ActiveSupport::TestCase
end
end
def test_perform_disabled_job_logging
perform_enqueued_jobs do
DisableLogJob.perform_later "Dummy"
assert_no_match(/Enqueued DisableLogJob \(Job ID: .*?\) from .*? with arguments:.*Dummy/, @logger.messages)
assert_no_match(/Performing DisableLogJob \(Job ID: .*?\) from .*? with arguments:.*Dummy/, @logger.messages)
assert_match(/enqueued at /, @logger.messages)
assert_match(/Dummy, here is it: Dummy/, @logger.messages)
assert_match(/Performed DisableLogJob \(Job ID: .*?\) from .*? in .*ms/, @logger.messages)
end
end
def test_perform_nested_jobs_logging
perform_enqueued_jobs do
NestedJob.perform_later

View File

@ -0,0 +1,13 @@
# frozen_string_literal: true
class DisableLogJob < ActiveJob::Base
self.log_arguments = false
def perform(dummy)
logger.info "Dummy, here is it: #{dummy}"
end
def job_id
"LOGGING-JOB-ID"
end
end

View File

@ -805,6 +805,8 @@ There are a few configuration options available in Active Support:
* `config.active_job.return_false_on_aborted_enqueue` change the return value of `#enqueue` to false instead of the job instance when the enqueuing is aborted. Defaults to `false`.
* `config.active_job.log_arguments` controls if the arguments of a job are logged. Defaults to `true`.
### Configuring Action Cable
* `config.action_cable.url` accepts a string for the URL for where