Class: Nexpose::ScanTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/nexpose/scan_template.rb

Overview

Configuration object for a scan template. This class is only a partial representation of some of the features available for configuration.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ ScanTemplate

Returns a new instance of ScanTemplate.



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/nexpose/scan_template.rb', line 43

def initialize(xml)
  @xml = xml

  root = REXML::XPath.first(xml, 'ScanTemplate')
  @id = root.attributes['id']

  desc = REXML::XPath.first(root, 'templateDescription')
  @name = desc.attributes['title']
  @description = desc.text.to_s

  vuln_checks = REXML::XPath.first(root, 'VulnerabilityChecks')
  @correlate = vuln_checks.attributes['correlate'] == '1'
end

Instance Attribute Details

#correlateObject

Whether to correlate reliable checks with regular checks.



38
39
40
# File 'lib/nexpose/scan_template.rb', line 38

def correlate
  @correlate
end

#descriptionObject

Returns the value of attribute description.



35
36
37
# File 'lib/nexpose/scan_template.rb', line 35

def description
  @description
end

#idObject

Unique identifier of the scan template.



32
33
34
# File 'lib/nexpose/scan_template.rb', line 32

def id
  @id
end

#nameObject

Returns the value of attribute name.



34
35
36
# File 'lib/nexpose/scan_template.rb', line 34

def name
  @name
end

#xmlObject

Parsed XML of a scan template



41
42
43
# File 'lib/nexpose/scan_template.rb', line 41

def xml
  @xml
end

Class Method Details

.copy(nsc, id) ⇒ ScanTemplate

Copy an existing scan template, changing the id and title.

Parameters:

  • nsc (Connection)

    API connection to a Nexpose console.

  • id (String)

    Unique identifier of an existing scan template.

Returns:

  • (ScanTemplate)

    A copy of the requested scan template configuration.



96
97
98
99
100
101
# File 'lib/nexpose/scan_template.rb', line 96

def self.copy(nsc, id)
  dupe = load(nsc, id)
  dupe.id = "#{dupe.id}-copy"
  dupe.title = "#{dupe.title} Copy"
  dupe
end

.load(nsc, id) ⇒ ScanTemplate

Load an existing scan template.

Parameters:

  • nsc (Connection)

    API connection to a Nexpose console.

  • id (String)

    Unique identifier of an existing scan template.

Returns:

  • (ScanTemplate)

    The requested scan template configuration.



85
86
87
88
# File 'lib/nexpose/scan_template.rb', line 85

def self.load(nsc, id)
  response = JSON.parse(AJAX.get(nsc, "/data/scan/templates/#{URI.encode(id)}"))
  new(REXML::Document.new(response['value']))
end

Instance Method Details

#save(nsc) ⇒ Object

Save this scan template configuration to a Nexpose console.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/nexpose/scan_template.rb', line 59

def save(nsc)
  root = REXML::XPath.first(@xml, 'ScanTemplate')
  existing = root.attributes['id'] == @id
  root.attributes['id'] = @id unless existing

  desc = REXML::XPath.first(root, 'templateDescription')
  desc.attributes['title'] = @name
  desc.text = @description

  vuln_checks = REXML::XPath.first(root, 'VulnerabilityChecks')
  vuln_checks.attributes['correlate'] = (@correlate ? '1' : '0')

  if existing
    response = AJAX.put(nsc, "/data/scan/templates/#{URI.encode(id)}", xml)
  else
    response = JSON.parse(AJAX.post(nsc, '/data/scan/templates', xml))
    @id = response['value']
  end
end