Skip to content

Commit

Permalink
more work to enable workflows (and more groundwork for user-defined w…
Browse files Browse the repository at this point in the history
…orkflows)
  • Loading branch information
jcran committed Jan 3, 2021
1 parent b174887 commit 59b2249
Show file tree
Hide file tree
Showing 16 changed files with 617 additions and 328 deletions.
141 changes: 10 additions & 131 deletions app/models/workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,159 +12,38 @@ def validate
validates_unique([:name])
end

###
### ###################################################################
###

def self.load_default_workflows
Dir.glob("#{$intrigue_basedir}/data/workflows/*.json").each do |f|
template = JSON.parse(File.open("#{f}","r"))
create_from_template(template)
end
end

###
### Assumes we're handed a hash, and creates/stores the template
###
def self.create_from_template(template)
def self.add_user_workflow(template)

# set a sensible default
template["depth"] = 5 unless template["depth"]

# create a worfklow from the template, note that symbolize only gets the
# top level hash keys
t = template.symbolize_keys!
w = Intrigue::Core::Model::Workflow.update_or_create(t.except(:definition))
w.definition = t[:definition]
w.save_changes
end

###
### ###################################################################
###

###
### Returns a calculated value based on all tasks
###
def passive?
# Also save it in the database
begin
w = Intrigue::Core::Model::Workflow.update_or_create(t.except(:definition))
w.definition = t[:definition]
w.save_changes
rescue Sequel::ValidationFailed => e
return nil
end

# only if know how to handle this
#return false unless flow == "recursive"

out = false # default

# Check if each task is passive by looking at its metadata
tasks = definition.values.flatten.map{|x| x["task"] }
out = true if tasks.map{|t| TaskFactory.class_by_name(t).metadata[:passive] }.all? true

out
end

def to_hash
def to_h
{
name: name,
pretty_name: pretty_name,
user_selectable: user_selectable,
maintainer: maintainer,
description: description,
flow: flow,
passive: self.passive?,
definition: definition
}
end

def start(entity, task_result)
# sanity check before sending us off
return unless entity && task_result

# lookup what we need to do in the definition, and do the right thing
if type == "recursive"

tasks_to_call = definition[entity.type_string]

# now go through each task to call and call it
tasks_to_call.each do |t|
task_name = t["task"]
options = t["options"]
auto_scope = t["auto_scope"]

# start the task
Intrigue::Core::Model::Workflow.start_recursive_task(
task_result, task_name, entity, options, auto_scope)
end

end

end

###
### Helper method for starting a task run, unaware of workflow, but handy to have here
###
def self.start_recursive_task(old_task_result, task_name, entity, options=[], auto_scope=false)
project = old_task_result.project

# check to see if it already exists, return nil if it does
existing_task_result = Intrigue::Core::Model::TaskResult.first(
:project => project,
:task_name => "#{task_name}",
:base_entity_id => entity.id
)

if existing_task_result && (existing_task_result.options == options)
# Don't schedule a new one, just notify that it's already scheduled.
return nil
else

task_class = Intrigue::TaskFactory.create_by_name(task_name).class
task_forced_queue = task_class.metadata[:queue]

new_task_result = start_task(task_forced_queue || "task_autoscheduled",
project,
old_task_result.scan_result.id,
task_name,
entity,
old_task_result.depth - 1,
options,
old_task_result.handlers,
old_task_result.scan_result.workflow,
old_task_result.auto_enrich,
auto_scope)

end

new_task_result
end


=begin
TODO...
- change flow -> type
- change recurse -> definition
- remove passive (should be calculated)
"name": "intrigueio_precollection",
"pretty_name": "Intrigue.io Pre-Collection",
"passive": true,
"user_selectable": false,
"authors": ["jcran"],
"description": "This workflow performs a VERY light passive enumeration for organizations. Start with a Domain or NetBlock.",
"flow" : "recursive",
"depth": 4,
"definition": {
"AwsS3Bucket": [],
"Domain": [{
"task": "enumerate_nameservers",
"options": []
},
=end

=begin
extend Intrigue::Core::System::Helpers
extend Intrigue::Task::Data
=end

end
end
Expand Down
4 changes: 2 additions & 2 deletions app/routes/workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class CoreApp < Sinatra::Base
post '/:project_name/workflow/?' do

# then set up the initial task results
workflow = "#{@params["workflow"]}"
workflow_name = "#{@params["workflow"]}"

###
### Standard file type (entity list)
Expand All @@ -13,7 +13,7 @@ class CoreApp < Sinatra::Base
end

### Workflow definition, make sure we have a valid type
if wf = Intrigue::Core::Model::Workflow.first(:name => "#{workflow}")
if wf = Intrigue::WorkflowFactory.create_workflow_by_name(workflow_name)
workflow_name = wf.name
workflow_depth = wf.depth
else
Expand Down
6 changes: 3 additions & 3 deletions app/views/results/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@
</td>
<td>
<div class='td-images'>
<% if x.complete %>
<img width=12 height=12 src='/img/icons/checkmark-white.png'>
<% if x.complete %>
<%= checkmark_image %>
<% else %>
<img width=12 height=12 src='/img/icons/xmark-white.png'>
<%= xmark_image %>
<% end %>
</div>
</td>
Expand Down
27 changes: 12 additions & 15 deletions core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ class CoreApp < Sinatra::Base
set :allow_headers, "content-type,if-modified-since,allow"
set :expose_headers, "location,link"
set :allow_credentials, true

set :sessions => true
set :root, "#{$intrigue_basedir}"
set :views, "#{$intrigue_basedir}/app/views"
Expand All @@ -126,7 +125,7 @@ class CoreApp < Sinatra::Base
end

###
### Helpers
## Helpers
###
helpers do
def h(text)
Expand All @@ -135,7 +134,7 @@ def h(text)
end

###
### (Very) Simple Auth
## Enable Basic Auth
###
if Intrigue::Core::System::Config.config
if Intrigue::Core::System::Config.config["http_security"]
Expand Down Expand Up @@ -178,14 +177,12 @@ def h(text)

# Set the project based on the directive
project = Intrigue::Core::Model::Project.first(:name => directive)
@project_name = project.name if project

#if !directive && !project
# # # Creating a default project since it doesn't appear to exist (it should always exist)
#
# project = Intrigue::Core::Model::Project.create(:name => "Default", :created_at => Time.now.utc )
#
#end
if project
@project_name = project.name
else
session[:flash] = "Missing Project!?"
redirect FRONT_PAGE
end

end

Expand Down Expand Up @@ -231,10 +228,10 @@ def h(text)
# Core libraries
require_relative "lib/all"

puts "Loading Default Workflows..."
Intrigue::Core::Model::Workflow.load_default_workflows
puts "Available Workflows: #{Intrigue::Core::Model::Workflow.all.map{|x| x.name }.join(',')}"

###
## Relevant to hosted/managed configurations, load in a Sentry DSN from
## the so we can report errors to a Sentry instance
###
#configure sentry.io error reporting (only if a key was provided)
if (Intrigue::Core::System::Config.config && Intrigue::Core::System::Config.config["sentry_dsn"])
require "raven"
Expand Down
126 changes: 0 additions & 126 deletions data/workflows/external_discovery_light_active.json

This file was deleted.

Loading

0 comments on commit 59b2249

Please sign in to comment.