Skip to content

Commit

Permalink
Merge pull request rapid7#170 from rapid7/blackouts
Browse files Browse the repository at this point in the history
Blackouts
  • Loading branch information
erran-r7 committed May 19, 2015
2 parents 0aaf068 + b6e3827 commit 70bd9fd
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/nexpose.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
require 'nexpose/ajax'
require 'nexpose/api_request'
require 'nexpose/asset'
require 'nexpose/blackout'
require 'nexpose/common'
require 'nexpose/console'
require 'nexpose/credential'
Expand All @@ -78,6 +79,7 @@
require 'nexpose/filter'
require 'nexpose/discovery'
require 'nexpose/discovery/filter'
require 'nexpose/global_blackout'
require 'nexpose/global_settings'
require 'nexpose/group'
require 'nexpose/dag'
Expand Down
44 changes: 44 additions & 0 deletions lib/nexpose/blackout.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module Nexpose
# Constants useful across the Nexpose module.
# Configuration structure for blackouts.
class Blackout < APIObject
# Whether or not this blackout is enabled.
attr_accessor :enabled
# Valid schedule types: daily, hourly, monthly-date, monthly-day, weekly.
attr_accessor :blackout_type
# The repeat interval based upon type.
attr_accessor :blackout_interval
# The earliest date to generate the report on (in ISO 8601 format).
attr_accessor :blackout_start
# The amount of time, in minutes, a blackout period should last.
attr_accessor :blackout_duration

def initialize(start, enabled=true, duration, type, interval)
@blackout_start = start
@enabled =enabled
@blackout_duration = duration.to_i
@blackout_type = type
@blackout_interval = interval.to_i
end

def self.from_hash(hash)
repeat_blackout_hash = hash[:repeat_blackout]
blackout = new(hash[:start_date], hash[:blackout_duration], repeat_blackout_hash[:type], repeat_blackout_hash[:interval])
blackout
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[:repeat_blackout] = repeat_hash
blackout_hash
end
end
end
43 changes: 43 additions & 0 deletions lib/nexpose/global_blackout.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module Nexpose

class GlobalBlackout < APIObject
require 'json'
include JsonSerializer

# [Array] Blackout starting dates, times and duration for blackout periods.
attr_accessor :blackout

def initialize(blackout)
@blackout = Array(blackout)
end

def save(nsc)
params = to_json
AJAX.post(nsc, '/api/2.1/silo_blackout/', params, AJAX::CONTENT_TYPE::JSON)
end

def to_h
{
blackouts:
(@blackout || []).map { |blackout| blackout.to_h }
}
end

def to_json
JSON.generate(to_h)
end

def self.json_initializer(data)
new(blackout: data)
end

def self.load(nsc)
uri = '/api/2.1/silo_blackout/'
resp = AJAX.get(nsc, uri, AJAX::CONTENT_TYPE::JSON)
hash = JSON.parse(resp, symbolize_names: true)
blackout = self.json_initializer(hash).deserialize(hash)
blackout.blackout = (hash[:blackouts] || []).map { |blackout| Nexpose::Blackout.from_hash(blackout) }
blackout
end
end
end
5 changes: 5 additions & 0 deletions lib/nexpose/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ class Site < APIObject
# [Array] Schedule starting dates and times for scans, and set their frequency.
attr_accessor :schedules

# [Array] Blackout starting dates, times and duration for blackout periods.
attr_accessor :blackouts

# The risk factor associated with this site. Default: 1.0
attr_accessor :risk_factor
Expand Down Expand Up @@ -162,6 +164,7 @@ def initialize(name = nil, scan_template_id = 'full-audit-without-web-spider')
@risk_factor = 1.0
@config_version = 3
@schedules = []
@blackouts = []
@included_scan_targets = { addresses: [], asset_groups: [] }
@excluded_scan_targets = { addresses: [], asset_groups: [] }
@site_credentials = []
Expand Down Expand Up @@ -474,6 +477,7 @@ def to_h
scan_template_id: @scan_template_id,
risk_factor: @risk_factor,
schedules: (@schedules || []).map {|schedule| schedule.to_h},
blackouts: (@blackouts || []).map {|blackout| blackout.to_h},
shared_credentials: (@shared_credentials || []).map {|cred| cred.to_h},
site_credentials: (@site_credentials || []).map {|cred| cred.to_h},
web_credentials: (@web_credentials || []).map {|webCred| webCred.to_h},
Expand Down Expand Up @@ -510,6 +514,7 @@ def self.load(nsc, id)

site.organization = Organization.create(site.organization)
site.schedules = (hash[:schedules] || []).map {|schedule| Nexpose::Schedule.from_hash(schedule) }
site.blackouts = (hash[:blackouts] || []).map {|blackout| Nexpose::Blackout.from_hash(blackout) }
site.site_credentials = hash[:site_credentials].map {|cred| Nexpose::SiteCredentials.new.object_from_hash(nsc,cred)}
site.shared_credentials = hash[:shared_credentials].map {|cred| Nexpose::SiteCredentials.new.object_from_hash(nsc,cred)}
site.discovery_config = Nexpose::DiscoveryConnection.new.object_from_hash(nsc, hash[:discovery_config]) unless hash[:discovery_config].nil?
Expand Down

0 comments on commit 70bd9fd

Please sign in to comment.