Skip to content

Commit

Permalink
Merge pull request #292 from rapid7/cleanup_formatting
Browse files Browse the repository at this point in the history
Only Cleanup!
  • Loading branch information
sgreen-r7 authored Sep 6, 2017
2 parents 20df639 + ed1be66 commit 92f3238
Show file tree
Hide file tree
Showing 50 changed files with 1,309 additions and 1,336 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ Metrics/ModuleLength:

Metrics/ClassLength:
Max: 200

Metrics/MethodLength:
Max: 20

28 changes: 12 additions & 16 deletions lib/nexpose/ajax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -220,12 +220,10 @@ def use_response_error_message?(request, response)
# @param [String] pref Preference key value to preserve.
#
def preserving_preference(nsc, pref)
begin
orig = get_rows(nsc, pref)
yield
ensure
set_rows(nsc, pref, orig)
end
orig = get_rows(nsc, pref)
yield
ensure
set_rows(nsc, pref, orig)
end

# Get a valid row preference value.
Expand All @@ -251,10 +249,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
Expand All @@ -264,10 +262,8 @@ 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

Expand Down
41 changes: 20 additions & 21 deletions lib/nexpose/alert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 == ''
Expand All @@ -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?
Expand Down Expand Up @@ -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

Expand All @@ -130,12 +130,10 @@ def validate
raise ArgumentError.new('Vuln filter is a required attribute.') unless @vuln_filter
end

private

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.has_key?(:name) || hash[:name].to_s == ''
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
Expand Down Expand Up @@ -174,22 +172,23 @@ 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.length > 0
unless recipients.is_a?(Array) && !recipients.empty?
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|
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

Expand All @@ -209,13 +208,12 @@ 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

Expand All @@ -225,10 +223,11 @@ 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
6 changes: 3 additions & 3 deletions lib/nexpose/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class APIObject
#
def object_from_hash(nsc, hash)
hash.each do |k, v|
next if k == :url # Do not store self-referential URL.
next if k == :url # Do not store self-referential URL.
# Store resource URLs separately and create lazy accessors.
if v.is_a?(Hash) && v.key?(:url)
self.class.send(:define_method, k, proc { |conn = nsc| load_resource(conn, k, v[:url].gsub(/.*\/api/, '/api')) })
Expand Down Expand Up @@ -52,7 +52,7 @@ def object_from_hash(nsc, hash)
# @return [Array[?]] Collection of "k" marshalled object.
#
def load_resource(nsc, k, url)
obj = class_from_string(k)
obj = class_from_string(k)
resp = AJAX.get(nsc, url, AJAX::CONTENT_TYPE::JSON)
hash = JSON.parse(resp, symbolize_names: true)
if hash.is_a?(Array)
Expand Down Expand Up @@ -85,7 +85,6 @@ def class_from_string(field)

module TypedAccessor
def typed_accessor(name, type)

# here we dynamically define accessor methods
define_method(name) do
instance_variable_get("@#{name}")
Expand All @@ -100,4 +99,5 @@ def typed_accessor(name, type)
end
end
end

end
46 changes: 23 additions & 23 deletions lib/nexpose/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Asset < APIObject
attr_accessor :unique_identifiers

def initialize
@addresses = []
@addresses = []
@host_names = []
end

Expand All @@ -53,7 +53,7 @@ def initialize
# @return [Asset] The requested asset, if found.
#
def self.load(nsc, id)
uri = "/api/2.1/assets/#{id}"
uri = "/api/2.1/assets/#{id}"
resp = AJAX.get(nsc, uri, AJAX::CONTENT_TYPE::JSON)
hash = JSON.parse(resp, symbolize_names: true)
new.object_from_hash(nsc, hash)
Expand Down Expand Up @@ -97,9 +97,9 @@ def to_h

def <=>(other)
c = port <=> other.port
return c unless c == 0
return c unless c.zero?
c = protocol <=> other.protocol
return c unless c == 0
return c unless c.zero?
name <=> other.name
end

Expand All @@ -114,27 +114,27 @@ def eql?(other)
# Valid protocol values for a service endpoint.
module Protocol
# Internet Protocol
IP = 'IP'
IP = 'IP'
# Internet Control Message Protocol
ICMP = 'ICMP'
# Internet Group Management Protocol
IGMP = 'IGMP'
# Gateway-to-Gateway Protocol
GGP = 'GGP'
GGP = 'GGP'
# Transmission Control Protocol
TCP = 'TCP'
TCP = 'TCP'
# PARC Universal Protocol
PUP = 'PUP'
PUP = 'PUP'
# User Datagram Protocol
UDP = 'UDP'
UDP = 'UDP'
# Internet Datagram Protocol
IDP = 'IDP'
IDP = 'IDP'
# Encapsulating Security Payload
ESP = 'ESP'
ESP = 'ESP'
# Network Disk Protocol
ND = 'ND'
ND = 'ND'
# Raw Packet (or unknown)
RAW = 'RAW'
RAW = 'RAW'
end
end

Expand Down Expand Up @@ -163,11 +163,11 @@ def to_h

def <=>(other)
c = name <=> other.name
return c unless c == 0
return c unless c.zero?
c = id <=> other.id
return c unless c == 0
return c unless c.zero?
c = full_name <=> other.full_name
return c unless c == 0
return c unless c.zero?
attributes <=> other.attributes
end

Expand Down Expand Up @@ -204,9 +204,9 @@ def to_h

def <=>(other)
c = name <=> other.name
return c unless c == 0
return c unless c.zero?
c = id <=> other.id
return c unless c == 0
return c unless c.zero?
attributes <=> other.attributes
end

Expand Down Expand Up @@ -248,11 +248,11 @@ def to_h

def <=>(other)
c = name <=> other.name
return c unless c == 0
return c unless c.zero?
c = size <=> other.size
return c unless c == 0
return c unless c.zero?
c = directory <=> other.directory
return c unless c == 0
return c unless c.zero?
attributes <=> other.attributes
end

Expand All @@ -274,7 +274,7 @@ class UniqueIdentifier < APIObject
attr_reader :id

def initialize(source = nil, id = nil)
@id = id
@id = id
@source = source
end

Expand All @@ -285,7 +285,7 @@ def to_h

def <=>(other)
c = source <=> other.source
return c unless c == 0
return c unless c.zero?
id <=> other.id
end

Expand Down
20 changes: 6 additions & 14 deletions lib/nexpose/blackout.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Nexpose
# Constants useful across the Nexpose module.
# Configuration structure for blackouts.
# Constants useful across the Nexpose module.
# Configuration structure for blackouts.
class Blackout < APIObject
# Whether or not this blackout is enabled.
attr_accessor :enabled
Expand All @@ -14,7 +14,7 @@ class Blackout < APIObject
# The amount of time, in minutes, a blackout period should last.
attr_accessor :blackout_duration

def initialize(start, enabled=true, duration, type, interval)
def initialize(start, enabled = true, duration, type, interval)
@blackout_start = start
@enabled = enabled
@blackout_duration = duration.to_i
Expand All @@ -24,30 +24,22 @@ def initialize(start, enabled=true, duration, type, interval)

def self.from_hash(hash)
repeat_blackout_hash = hash[:repeat_blackout]

if repeat_blackout_hash.nil?
type = 'daily'
interval = 0
else
type = repeat_blackout_hash[:type]
interval = repeat_blackout_hash[:interval]
end

new(hash[:start_date], hash[:enabled], hash[:blackout_duration], type, interval)
end

def to_h
blackout_hash = {
start_date: @blackout_start,
enabled: @enabled,
blackout_duration: @blackout_duration,
}
repeat_hash= {
type: @blackout_type,
interval: @blackout_interval
}
blackout_hash = { start_date: @blackout_start, enabled: @enabled, blackout_duration: @blackout_duration }
repeat_hash = { type: @blackout_type, interval: @blackout_interval }
blackout_hash[:repeat_blackout] = repeat_hash
blackout_hash
end
end

end
Loading

0 comments on commit 92f3238

Please sign in to comment.