Skip to content

Commit

Permalink
find an alternate way to signal the end of a scan
Browse files Browse the repository at this point in the history
  • Loading branch information
jcran committed Feb 22, 2017
1 parent 42633ec commit 630fe18
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 32 deletions.
14 changes: 10 additions & 4 deletions app/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@ def start_task(queue, project, existing_scan_result, task_name, entity, depth, o
:depth => depth
})

# if we were passed a scan result, we know this new task belongs to it, and we should associate those
# if we were passed a scan result, we know this new task
# belongs to it, and we should associate those
if existing_scan_result
task_result.scan_result_id = existing_scan_result.id
# lets also add one to the incomplete task count, so we can determine later
# if we're actually done
task_result.scan_result.incomplete_task_count += 1
task_result.save
end

# If the depth is greater than 1, AND we don't have a prexisting scan id, start a new scan
# If the depth is greater than 1, AND we don't have a
# prexisting scan id, start a new scan
if !existing_scan_result && depth > 1

scan_result = Intrigue::Model::ScanResult.create({
Expand All @@ -36,7 +41,8 @@ def start_task(queue, project, existing_scan_result, task_name, entity, depth, o
:logger => Intrigue::Model::Logger.create(:project => project),
:depth => depth,
:strategy => strategy_name,
:handlers => handlers
:handlers => handlers,
:incomplete_task_count => 1
})

# Add the task result
Expand All @@ -49,7 +55,7 @@ def start_task(queue, project, existing_scan_result, task_name, entity, depth, o
scan_result.start(queue)

else
# Start it
# otherwise, we're a task, and we're ready to go
task_result.start(queue)
end

Expand Down
26 changes: 15 additions & 11 deletions app/routes/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ class IntrigueApp < Sinatra::Base
###
### UPDATE
###
get '/system_update' do
global_config = Intrigue::Config::GlobalConfig.new

if global_config.config["environment"] == "production"
Dir.chdir($intrigue_basedir) do
result = `git checkout master && git pull origin master`
Thread.new { `sleep 10 && service intrigue restart` }
end
end
result
end
#get '/system_update' do
# global_config = Intrigue::Config::GlobalConfig.new
#
# if global_config.config["environment"] == "production"
# Dir.chdir($intrigue_basedir) do
# result = `git checkout master && git pull origin master`
# Thread.new { `sleep 10 && service intrigue restart` }
# end
# end
#result
#end

### ###
### System Config ###
Expand Down Expand Up @@ -157,6 +157,8 @@ class IntrigueApp < Sinatra::Base
#}.to_json
post '/:project/results/?' do

puts "Got request!"

project_name = params[:project]

# Parse the incoming request
Expand Down Expand Up @@ -199,6 +201,8 @@ class IntrigueApp < Sinatra::Base
entity.save
end

puts "Starting task!!"

# Start the task_run
task_result = start_task("task", project, nil, task_name, entity, depth, options, handlers, strategy_name)
status 200 if task_result
Expand Down
1 change: 1 addition & 0 deletions data/dns_record_banks.entities.list
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DnsRecord#wellsfargo.com
9 changes: 9 additions & 0 deletions db/005_incomplete_task_count.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Sequel.migration do
change do

alter_table :scan_results do
add_column :incomplete_task_count, Integer
end

end
end
42 changes: 25 additions & 17 deletions lib/tasks/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,29 +91,37 @@ def perform(task_result_id)
end
@task_result.handlers_complete = true


########################
# Scan Result Handlers #
########################
if @task_result.scan_result && @task_result.scan_result.handlers.count > 0
incomplete_task_count = @task_result.scan_result.task_results.select{|tr| tr.complete == false }.count
_log "We are part of a scan result... checking our incomplete count: #{incomplete_task_count}"

if incomplete_task_count == 1 # We are the last one!!
_log "Last task standing, let's handle it!"
@task_result.scan_result.handlers.each do |handler_type|
handler = Intrigue::HandlerFactory.create_by_type(handler_type)
_log "Calling #{handler_type} handler on #{scan_result.name}"
handler.process(@task_result.scan_result)
scan_result = @task_result.scan_result
if scan_result
scan_result.incomplete_task_count -= 1
scan_result.save

if scan_result.handlers.count > 0
_log "We are part of a scan result... checking our incomplete count: #{scan_result.incomplete_task_count}"

# Check our incomplete task count on the scan to see if this is the last one
if scan_result.incomplete_task_count <= 0
_log "Last task standing, let's handle it!"

scan_result.handlers.each do |handler_type|
handler = Intrigue::HandlerFactory.create_by_type(handler_type)
_log "Calling #{handler_type} handler on #{scan_result.name}"
handler.process(scan_result)
end

# let's mark it complete if there's nothing else to do here.
scan_result.handlers_complete = true
scan_result.complete = true
scan_result.save
else
_log "More tasks for this scan to complete: #{incomplete_task_count}!"
end

# let's mark it complete if there's nothing else to do here.
@task_result.scan_result.handlers_complete = true
@task_result.scan_result.complete = true
@task_result.scan_result.save
else
_log "More tasks for this scan to complete: #{incomplete_task_count}!"
end

end

ensure # Mark it complete and save it
Expand Down

0 comments on commit 630fe18

Please sign in to comment.