Class: S3Antivirus::Scan

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
AwsServices, Conf, Logger
Defined in:
lib/s3_antivirus/scan.rb

Instance Method Summary collapse

Methods included from Conf

#conf

Methods included from Logger

#logger

Methods included from AwsServices

#s3, #sns

Constructor Details

#initialize(options = {}) ⇒ Scan

Returns a new instance of Scan.



15
16
17
# File 'lib/s3_antivirus/scan.rb', line 15

def initialize(options={})
  @options = options
end

Instance Method Details

#runObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/s3_antivirus/scan.rb', line 19

def run
  logger.info "Polling SQS queue for S3 antivirus findings. Started #{Time.now}..."
  return if @options[:noop]

  poller = Aws::SQS::QueuePoller.new(conf['queue'])
  poller.poll do |msg|
    begin
      body = JSON.parse(msg.body)

      logger.debug "body #{body}"
      if body.key?('Records')
        body['Records'].each do |record|
          # Instance variables change on each iteration
          @s3_record = S3Record.new(record)
          @notifier = Notifier.new(@s3_record)
          @tagger = Tagger.new(@s3_record)

          logger.info("Checking #{@s3_record.human_key}")
          within_size_limit = check_file_size
          next unless within_size_limit # continue to next file because oversized
          filename = "/tmp/#{SecureRandom.uuid}"
          success = download_file(filename)
          next unless success # continue to next file because unable to download
          scan_file(filename)
        end
      end
    rescue Exception => e
      logger.error "message failed: #{e.inspect} #{msg.inspect}"
      raise e
    end
    # Sometimes stdout doesnt get shown but the message is processed file.
    # Know this because the SNS message gets sent.
    # Even this $stdout.flush doesn't seem to be enough.
    $stdout.flush
  end
end