Class: Metascan::Scan

Inherits:
Object
  • Object
show all
Defined in:
lib/metascan/scan.rb

Overview

A single scan on the Metascan service. Initialized with the parameters to scan, exposes methods to inspect the scan results.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, client, archivepwd: nil) ⇒ Scan

Returns a new instance of Scan.



13
14
15
16
17
# File 'lib/metascan/scan.rb', line 13

def initialize(filename, client, archivepwd: nil)
  @filename   = filename
  @client     = client
  @archivepwd = archivepwd
end

Instance Attribute Details

#data_idObject (readonly)

Returns the value of attribute data_id.



10
11
12
# File 'lib/metascan/scan.rb', line 10

def data_id
  @data_id
end

#filenameObject (readonly)

Returns the value of attribute filename.



11
12
13
# File 'lib/metascan/scan.rb', line 11

def filename
  @filename
end

Instance Method Details

#clean?(poll: false) ⇒ Boolean

Returns true iff the Metascan virus scan found no threats. If POLL is true (false by default) then retrieve_results first.

Returns:

  • (Boolean)


47
48
49
50
51
52
53
# File 'lib/metascan/scan.rb', line 47

def clean?(poll: false)
  if self.results(poll: poll)["scan_results"]["progress_percentage"] < 100 then
    nil
  else
    self.results(poll: poll)["scan_results"]["scan_all_result_i"] == 0
  end
end

#requestObject

Construct and return the request I use, for the purpose of queueing in a Typhoeus::Hydra.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/metascan/scan.rb', line 26

def request
  request = Typhoeus::Request.new(
    Metascan::PATHS[:scan_file],
    headers: {
      'filename' => @filename,
      'archivepwd' => @archivepwd,
      'apikey' => @client.api_key
    }.select { |k, v| !v.nil? },
    method: :post,
    body: { file: File.open(@filename, "r") }
  )

  request.on_complete do |r|
    @data_id = JSON.parse(r.body)["data_id"]
    @rest_ip = JSON.parse(r.body)["rest_ip"].split(":")[0] + '/v2/file'
  end
  request
end

#results(poll: false) ⇒ Object

Return the results of my scan. If the optional argument “poll” is set to true, then attempt to requery Metascan for the results before returning them.



63
64
65
66
67
68
69
# File 'lib/metascan/scan.rb', line 63

def results(poll: false)
  if poll and
    (!@results or @results["scan_results"]["progress_percentage"] < 100) then
    retrieve_results.run
  end
  @results
end

#results=(results) ⇒ Object

Only useful for testing.



56
57
58
# File 'lib/metascan/scan.rb', line 56

def results=(results)
  @results = results
end

#retrieve_resultsObject

Returns a HTTP request to retrieve the latest scan results for me. Fails if called before @data_id is set (when self.run is called, or my Batch runs me)



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/metascan/scan.rb', line 74

def retrieve_results
  request = Typhoeus::Request.new(
    @rest_ip + '/' + @data_id,
    headers: {
      'apikey' => @client.api_key
    },
    method: :get
  )

  request.on_complete do |r|
    @results = JSON.parse(r.body)
  end

  request
end

#runObject

Initiate a scan of @filename



20
21
22
# File 'lib/metascan/scan.rb', line 20

def run
  self.request.run
end