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

Site api blackouts #150

Merged
merged 6 commits into from
Mar 27, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Global and site level blackouts
  • Loading branch information
dmurray-r7 committed Mar 27, 2015
commit dc4d97eac6c378276c119c8faa442a12c09daebd
47 changes: 47 additions & 0 deletions lib/nexpose/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,53 @@ module Type
end
end

# 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 :type
# The repeat interval based upon type.
attr_accessor :interval
# The earliest date to generate the report on (in ISO 8601 format).
attr_accessor :start
# The amount of time, in minutes, a blackout period should last.
attr_accessor :duration
# The timezone in which the blackout will start
attr_accessor :timezone

def initialize(start, enabled=true, duration, timezone, type, interval)

Choose a reason for hiding this comment

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

Surrounding space missing in default value assignment.

@blackout_start = start
@enabled =enabled

Choose a reason for hiding this comment

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

Surrounding space missing for operator '='.

@blackout_duration = duration.to_i
@blackout_timezone = timezone
@blackout_type = type
@blackout_interval = interval.to_i
end

def self.from_hash(hash)
repeat_scan_hash = hash[:repeat_scan]
blackout = new(hash[:start], hash[:duration], hash[:timezone])
blackout.type = repeat_scan_hash[:type]
blackout.interval = repeat_scan_hash[:interval]
blackout
end

def to_h
blackout_hash = {
start_date: @blackout_start,

Choose a reason for hiding this comment

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

Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is.

enabled: @enabled,
blackout_duration: @blackout_duration,
timezone: @blackout_timezone
}
repeat_hash= {

Choose a reason for hiding this comment

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

Surrounding space missing for operator '='.

type: @blackout_type,

Choose a reason for hiding this comment

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

Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is.

interval: @blackout_interval }
blackout_hash[:repeat_blackout] = repeat_hash
blackout_hash
end
end

# Organization configuration, as used in Site and Silo.
class Organization < APIObject
attr_accessor :name
Expand Down
48 changes: 48 additions & 0 deletions lib/nexpose/global_blackout.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module Nexpose

Choose a reason for hiding this comment

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

Extra empty line detected at module body beginning.

class Global_Blackout < APIObject
include JsonSerializer

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

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

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

def to_h
{
blackouts:

Choose a reason for hiding this comment

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

Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is.

(@global_blackout || []).map { |blackout| blackout.to_h }
}
end

def to_json
JSON.generate(to_h)
end

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

require 'json'

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)

Choose a reason for hiding this comment

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

Redundant self detected.

blackout.blackout = (hash[:blackouts] || []).map { |blackout| Nexpose::Blackout.from_hash(blackout) }

Choose a reason for hiding this comment

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

Shadowing outer local variable - blackout.

blackout
end
end
end



6 changes: 6 additions & 0 deletions lib/nexpose/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ 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 @@ -161,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 @@ -473,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 @@ -509,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::Schedule.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