Module: Nexpose::Alert

Extended by:
TypedAccessor
Includes:
JsonSerializer
Included in:
SMTPAlert, SNMPAlert, SyslogAlert
Defined in:
lib/nexpose/alert.rb

Overview

Alert base behavior. The supported three alert types should have these properties and behaviors

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TypedAccessor

typed_accessor

Methods included from JsonSerializer

#deserialize, #serialize, #to_hash

Instance Attribute Details

#alert_typeObject

Alert type and its configuration. One of SMTPAlert, SyslogAlert, SNMPAlert



66
67
68
# File 'lib/nexpose/alert.rb', line 66

def alert_type
  @alert_type
end

#enabledObject

Whether or not this alert is currently active.



62
63
64
# File 'lib/nexpose/alert.rb', line 62

def enabled
  @enabled
end

#idObject

ID for this alert.



58
59
60
# File 'lib/nexpose/alert.rb', line 58

def id
  @id
end

#max_alertsObject

Send at most this many alerts per scan.



64
65
66
# File 'lib/nexpose/alert.rb', line 64

def max_alerts
  @max_alerts
end

#nameObject

Name for this alert.



60
61
62
# File 'lib/nexpose/alert.rb', line 60

def name
  @name
end

#serverObject

Server target the alerts



68
69
70
# File 'lib/nexpose/alert.rb', line 68

def server
  @server
end

#server_portObject

Server port



70
71
72
# File 'lib/nexpose/alert.rb', line 70

def server_port
  @server_port
end

Class Method Details

.create(hash) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/nexpose/alert.rb', line 133

def self.create(hash)
  alert_type = hash[:alert_type]
  raise 'An alert must have an alert type' if alert_type.nil?
  raise 'Alert name cannot be empty.' if !hash.key?(:name) || hash[:name].to_s == ''
  raise 'SNMP and Syslog alerts must have a server defined' if ['SNMP', 'Syslog'].include?(alert_type) && hash[:server].to_s == ''

  case alert_type
  when 'SMTP'
    alert = SMTPAlert.new(hash[:name],
                          hash[:sender],
                          hash[:server],
                          hash[:recipients],
                          hash[:enabled],
                          hash[:max_alerts],
                          hash[:verbose])
  when 'SNMP'
    alert = SNMPAlert.new(hash[:name],
                          hash[:community],
                          hash[:server],
                          hash[:enabled],
                          hash[:max_alerts])
  when 'Syslog'
    alert = SyslogAlert.new(hash[:name],
                            hash[:server],
                            hash[:enabled],
                            hash[:max_alerts])
  else
    fail "Unknown alert type: #{alert_type}"
  end

  alert.scan_filter = ScanFilter.new
  alert.vuln_filter = VulnFilter.new
  alert
end

.json_initializer(hash) ⇒ Object



101
102
103
# File 'lib/nexpose/alert.rb', line 101

def self.json_initializer(hash)
  create(hash)
end

.list_alerts(nsc, site_id) ⇒ Object

load a list of alerts for a given site



94
95
96
97
98
99
# File 'lib/nexpose/alert.rb', line 94

def self.list_alerts(nsc, site_id)
  uri  = "/api/2.1/site_configurations/#{site_id}/alerts"
  resp = AJAX.get(nsc, uri, AJAX::CONTENT_TYPE::JSON)
  data = JSON.parse(resp, symbolize_names: true)
  load_alerts(data) unless data.nil?
end

.load(nsc, site_id, alert_id) ⇒ Object

load a particular site alert



78
79
80
81
82
83
84
85
86
# File 'lib/nexpose/alert.rb', line 78

def self.load(nsc, site_id, alert_id)
  uri  = "/api/2.1/site_configurations/#{site_id}/alerts/#{alert_id}"
  resp = AJAX.get(nsc, uri, AJAX::CONTENT_TYPE::JSON)

  unless resp.to_s == ''
    data = JSON.parse(resp, symbolize_names: true)
    json_initializer(data).deserialize(data)
  end
end

.load_alerts(alerts) ⇒ Object

load alerts from an array of hashes



89
90
91
# File 'lib/nexpose/alert.rb', line 89

def self.load_alerts(alerts)
  alerts.map { |hash| json_initializer(hash).deserialize(hash) }
end

Instance Method Details

#delete(nsc, site_id) ⇒ Object

delete an alert from the given site



114
115
116
117
# File 'lib/nexpose/alert.rb', line 114

def delete(nsc, site_id)
  uri = "/api/2.1/site_configurations/#{site_id}/alerts/#{id}"
  AJAX.delete(nsc, uri, AJAX::CONTENT_TYPE::JSON)
end

#save(nsc, site_id) ⇒ Object

save an alert for a given site



120
121
122
123
124
125
# File 'lib/nexpose/alert.rb', line 120

def save(nsc, site_id)
  validate
  uri = "/api/2.1/site_configurations/#{site_id}/alerts"
  id  = AJAX.put(nsc, uri, self.to_json, AJAX::CONTENT_TYPE::JSON)
  @id = id.to_i
end

#to_hObject



105
106
107
# File 'lib/nexpose/alert.rb', line 105

def to_h
  to_hash(Hash.new)
end

#to_jsonObject



109
110
111
# File 'lib/nexpose/alert.rb', line 109

def to_json
  serialize
end

#validateObject

Raises:

  • (ArgumentError)


127
128
129
130
131
# File 'lib/nexpose/alert.rb', line 127

def validate
  raise ArgumentError.new('Name is a required attribute.') unless @name
  raise ArgumentError.new('Scan filter is a required attribute.') unless @scan_filter
  raise ArgumentError.new('Vuln filter is a required attribute.') unless @vuln_filter
end