-
Notifications
You must be signed in to change notification settings - Fork 103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Revert "Only Cleanup!" #293
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,3 @@ Metrics/ModuleLength: | |
|
||
Metrics/ClassLength: | ||
Max: 200 | ||
|
||
Metrics/MethodLength: | ||
Max: 20 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ module AJAX | |
# Content type strings acceptect by Nexpose. | ||
# | ||
module CONTENT_TYPE | ||
XML = 'text/xml; charset=UTF-8' | ||
XML = 'text/xml; charset=UTF-8' | ||
JSON = 'application/json; charset-utf-8' | ||
FORM = 'application/x-www-form-urlencoded; charset=UTF-8' | ||
end | ||
|
@@ -192,8 +192,8 @@ def get_request_api_version(request) | |
# Get an error message from the response body if the request url api version | ||
# is 2.1 or greater otherwise use the request body | ||
def get_error_message(request, response) | ||
version = get_request_api_version(request) | ||
data_request = use_response_error_message?(request, response) | ||
version = get_request_api_version(request) | ||
data_request = use_response_error_message?(request, response) | ||
return_response = (version >= 2.1 || data_request) | ||
(return_response && response.body) ? "response body: #{response.body}" : "request body: #{request.body}" | ||
end | ||
|
@@ -220,10 +220,12 @@ def use_response_error_message?(request, response) | |
# @param [String] pref Preference key value to preserve. | ||
# | ||
def preserving_preference(nsc, pref) | ||
orig = get_rows(nsc, pref) | ||
yield | ||
ensure | ||
set_rows(nsc, pref, orig) | ||
begin | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant begin block detected. |
||
orig = get_rows(nsc, pref) | ||
yield | ||
ensure | ||
set_rows(nsc, pref, orig) | ||
end | ||
end | ||
|
||
# Get a valid row preference value. | ||
|
@@ -249,10 +251,10 @@ def row_pref_of(val) | |
end | ||
|
||
def get_rows(nsc, pref) | ||
uri = '/data/user/preferences/all' | ||
uri = '/data/user/preferences/all' | ||
pref_key = "#{pref}.rows" | ||
resp = get(nsc, uri) | ||
json = JSON.parse(resp) | ||
resp = get(nsc, uri) | ||
json = JSON.parse(resp) | ||
if json.key?(pref_key) | ||
rows = json[pref_key].to_i | ||
rows > 0 ? rows : 10 | ||
|
@@ -262,8 +264,10 @@ def get_rows(nsc, pref) | |
end | ||
|
||
def set_rows(nsc, pref, value) | ||
uri = '/data/user/preference' | ||
params = { 'name' => "#{pref}.rows", 'value' => value } | ||
uri = '/data/user/preference' | ||
params = { 'name' => "#{pref}.rows", | ||
'value' => value } | ||
|
||
form_post(nsc, uri, params) | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,7 +76,7 @@ module Alert | |
|
||
# load a particular site alert | ||
def self.load(nsc, site_id, alert_id) | ||
uri = "/api/2.1/site_configurations/#{site_id}/alerts/#{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 == '' | ||
|
@@ -92,7 +92,7 @@ def self.load_alerts(alerts) | |
|
||
# load a list of alerts for a given site | ||
def self.list_alerts(nsc, site_id) | ||
uri = "/api/2.1/site_configurations/#{site_id}/alerts" | ||
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? | ||
|
@@ -120,7 +120,7 @@ def delete(nsc, site_id) | |
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 = AJAX.put(nsc, uri, self.to_json, AJAX::CONTENT_TYPE::JSON) | ||
@id = id.to_i | ||
end | ||
|
||
|
@@ -130,10 +130,12 @@ def validate | |
raise ArgumentError.new('Vuln filter is a required attribute.') unless @vuln_filter | ||
end | ||
|
||
private | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Useless private access modifier. |
||
|
||
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 'Alert name cannot be empty.' if !hash.has_key?(:name) || hash[:name].to_s == '' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use Hash#key? instead of Hash#has_key?. |
||
raise 'SNMP and Syslog alerts must have a server defined' if ['SNMP', 'Syslog'].include?(alert_type) && hash[:server].to_s == '' | ||
|
||
case alert_type | ||
|
@@ -172,23 +174,22 @@ class SMTPAlert | |
attr_accessor :recipients, :sender, :verbose | ||
|
||
def initialize(name, sender, server, recipients, enabled = 1, max_alerts = -1, verbose = 0) | ||
unless recipients.is_a?(Array) && !recipients.empty? | ||
unless recipients.is_a?(Array) && recipients.length > 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use recipients.length.positive? instead of recipients.length > 0. |
||
raise 'An SMTP alert must contain an array of recipient emails with at least 1 recipient' | ||
end | ||
|
||
recipients.each do |recipient| | ||
recipients.each do |recipient| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trailing whitespace detected. |
||
unless recipient =~ /^.+@.+\..+$/ | ||
raise "Recipients must contain valid emails, #{recipient} has an invalid format" | ||
end | ||
end | ||
|
||
@alert_type = 'SMTP' | ||
@name = name | ||
@enabled = enabled | ||
@name = name | ||
@enabled = enabled | ||
@max_alerts = max_alerts | ||
@sender = sender | ||
@server = server | ||
@verbose = verbose | ||
@sender = sender | ||
@server = server | ||
@verbose = verbose | ||
@recipients = recipients.nil? ? [] : recipients | ||
end | ||
|
||
|
@@ -208,12 +209,13 @@ class SNMPAlert | |
|
||
def initialize(name, community, server, enabled = 1, max_alerts = -1) | ||
raise 'SNMP alerts must have a community defined.' if community.nil? | ||
|
||
@alert_type = 'SNMP' | ||
@name = name | ||
@enabled = enabled | ||
@name = name | ||
@enabled = enabled | ||
@max_alerts = max_alerts | ||
@community = community | ||
@server = server | ||
@community = community | ||
@server = server | ||
end | ||
end | ||
|
||
|
@@ -223,11 +225,10 @@ class SyslogAlert | |
|
||
def initialize(name, server, enabled = 1, max_alerts = -1) | ||
@alert_type = 'Syslog' | ||
@name = name | ||
@enabled = enabled | ||
@name = name | ||
@enabled = enabled | ||
@max_alerts = max_alerts | ||
@server = server | ||
@server = server | ||
end | ||
end | ||
|
||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Freeze mutable objects assigned to constants.