Skip to content
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

Merged
merged 1 commit into from
Sep 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Revert "Only Cleanup!"
  • Loading branch information
sgreen-r7 authored Sep 6, 2017
commit d1cd123d81f197eb29a3f02f0c6d09e1b37b8a91
4 changes: 0 additions & 4 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,3 @@ Metrics/ModuleLength:

Metrics/ClassLength:
Max: 200

Metrics/MethodLength:
Max: 20

28 changes: 16 additions & 12 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'

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.

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,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

Choose a reason for hiding this comment

The 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.
Expand All @@ -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
Expand All @@ -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

Expand Down
41 changes: 21 additions & 20 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,10 +130,12 @@ def validate
raise ArgumentError.new('Vuln filter is a required attribute.') unless @vuln_filter
end

private

Choose a reason for hiding this comment

The 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 == ''

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use recipients.length.positive? instead of recipients.length > 0.
Use !empty? instead of 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|

Choose a reason for hiding this comment

The 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

Expand All @@ -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

Expand All @@ -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
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,6 +85,7 @@ 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 @@ -99,5 +100,4 @@ 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.zero?
return c unless c == 0
c = protocol <=> other.protocol
return c unless c.zero?
return c unless c == 0
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.zero?
return c unless c == 0
c = id <=> other.id
return c unless c.zero?
return c unless c == 0
c = full_name <=> other.full_name
return c unless c.zero?
return c unless c == 0
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.zero?
return c unless c == 0
c = id <=> other.id
return c unless c.zero?
return c unless c == 0
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.zero?
return c unless c == 0
c = size <=> other.size
return c unless c.zero?
return c unless c == 0
c = directory <=> other.directory
return c unless c.zero?
return c unless c == 0
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.zero?
return c unless c == 0
id <=> other.id
end

Expand Down
20 changes: 14 additions & 6 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,22 +24,30 @@ 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