Skip to content

Commit

Permalink
Merge pull request #142 from rapid7/site-api-adhoc-schedule
Browse files Browse the repository at this point in the history
Update gem to support XML API adding of schedules to adhoc scans
  • Loading branch information
gschneider-r7 committed Mar 25, 2015
2 parents d15a91b + 9239c25 commit 54ed13f
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 71 deletions.
95 changes: 65 additions & 30 deletions lib/nexpose/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,41 @@ def self.parse(xml)
end
end

# Configuration structure for ad-hoc schedules
class AdHocSchedule < APIObject
# Start time in ISO8601 format
attr_accessor :start

# The template to use to scan the assets
attr_accessor :scan_template_id

# The amount of time, in minutes, to allow execution before stopping.
attr_accessor :max_duration

# Force the scan to run during a blackout
attr_accessor :force

def initialize(start, scan_template_id, max_duration, force = false)
@start = start
@scan_template_id = scan_template_id
@max_duration = max_duration
@force = force
end

def as_xml
xml = REXML::Element.new('AdHocSchedule')
xml.attributes['start'] = @start
xml.attributes['maxDuration'] = @max_duration
xml.attributes['template'] = @scan_template_id
xml.attributes['force'] = @force if @force
xml
end

def to_xml
as_xml.to_s
end
end

# Configuration structure for schedules.
class Schedule < APIObject
# Whether or not this schedule is enabled.
Expand All @@ -107,7 +142,7 @@ class Schedule < APIObject
# The date after which the schedule is disabled, in ISO 8601 format.
attr_accessor :not_valid_after

#TODO Remove this unused attribute
# TODO: Remove this unused attribute
attr_accessor :incremental

# Extended attributes added with the new scheduler implementation
Expand Down Expand Up @@ -170,37 +205,37 @@ def self.from_hash(hash)
end

def to_h
schedule_hash = {
enabled: @enabled,
scan_template_id: @scan_template_id,
maximum_scan_duration: @max_duration
schedule_hash = {
enabled: @enabled,
scan_template_id: @scan_template_id,
maximum_scan_duration: @max_duration
}
schedule_hash[:start_date] = Nexpose::ISO8601.to_string(@start) if @start
schedule_hash[:not_valid_after_date] = Nexpose::ISO8601.to_string(@not_valid_after) if @not_valid_after
schedule_hash[:time_zone] = @timezone if @timezone

unless (@type.nil? || @interval == 0) && !@is_extended
repeat_scan_hash = {
type: @type,
interval: @interval
}
schedule_hash[:start_date] = Nexpose::ISO8601.to_string(@start) if @start
schedule_hash[:not_valid_after_date] = Nexpose::ISO8601.to_string(@not_valid_after) if @not_valid_after
schedule_hash[:time_zone] = @timezone if @timezone

unless (@type.nil? || @interval == 0) && !@is_extended
repeat_scan_hash = {
type: @type,
interval: @interval
}
repeat_scan_hash[:on_repeat] = 'restart-scan' if @repeater_type == 'restart'
repeat_scan_hash[:on_repeat] = 'resume-scan' if @repeater_type == 'continue'

if @is_extended
repeat_scan_hash[:is_extended] = @is_extended
repeat_scan_hash[:hour] = @hour if @hour
repeat_scan_hash[:minute] = @minute if @minute
repeat_scan_hash[:date] = @date if @date
repeat_scan_hash[:day] = @day if @day
repeat_scan_hash[:occurrence] = @occurrence if @occurrence
repeat_scan_hash[:start_month] = @start_month if @start_month
end

schedule_hash[:repeat_scan] = repeat_scan_hash
repeat_scan_hash[:on_repeat] = 'restart-scan' if @repeater_type == 'restart'
repeat_scan_hash[:on_repeat] = 'resume-scan' if @repeater_type == 'continue'

if @is_extended
repeat_scan_hash[:is_extended] = @is_extended
repeat_scan_hash[:hour] = @hour if @hour
repeat_scan_hash[:minute] = @minute if @minute
repeat_scan_hash[:date] = @date if @date
repeat_scan_hash[:day] = @day if @day
repeat_scan_hash[:occurrence] = @occurrence if @occurrence
repeat_scan_hash[:start_month] = @start_month if @start_month
end

schedule_hash
schedule_hash[:repeat_scan] = repeat_scan_hash
end

schedule_hash
end

def as_xml
Expand Down Expand Up @@ -294,7 +329,7 @@ def to_h
state: state,
city: city,
zip: zip,
country: country,
country: country
}
end

Expand Down
Loading

0 comments on commit 54ed13f

Please sign in to comment.