Skip to content

Commit

Permalink
feat(runner): add previous results to results
Browse files Browse the repository at this point in the history
  • Loading branch information
ocervell committed Dec 3, 2024
1 parent d0938ba commit cdb0b0b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
14 changes: 9 additions & 5 deletions secator/runners/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ def __init__(self, config, inputs=[], results=[], run_opts={}, hooks={}, validat
if not isinstance(inputs, list):
inputs = [inputs]
self.inputs = inputs
self.results = results
self.uuids = []
self.output = ''
self.results = []
self.workspace_name = context.get('workspace_name', 'default')
self.run_opts = run_opts.copy()
self.sync = run_opts.get('sync', True)
Expand All @@ -86,14 +88,12 @@ def __init__(self, config, inputs=[], results=[], run_opts={}, hooks={}, validat
self.last_updated_progress = None
self.end_time = None
self._hooks = hooks
self.output = ''
self.progress = 0
self.context = context
self.delay = run_opts.get('delay', False)
self.celery_result = None
self.celery_ids = []
self.celery_ids_map = {}
self.uuids = []
self.caller = self.run_opts.get('caller', None)
self.threads = []

Expand Down Expand Up @@ -163,6 +163,10 @@ def __init__(self, config, inputs=[], results=[], run_opts={}, hooks={}, validat
self.unique_name = self.name.replace('/', '_')
self.unique_name = f'{self.unique_name}_{self.chunk}' if self.chunk else self.unique_name

# Process prior results
for result in results:
list(self._process_item(result))

# Input post-process
self.run_hooks('before_init')

Expand Down Expand Up @@ -836,8 +840,8 @@ def _process_item(self, item):
elif isinstance(item, Info) and item.task_id and item.task_id not in self.celery_ids:
self.celery_ids.append(item.task_id)

# Run on_item hooks
if isinstance(item, tuple(FINDING_TYPES)):
# If finding, run on_item hooks
elif isinstance(item, tuple(FINDING_TYPES)):
item = self.run_hooks('on_item', item)
if not item:
return
Expand Down
24 changes: 7 additions & 17 deletions tests/integration/test_workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

from secator.template import TemplateLoader
from secator.runners import Task
from secator.output_types import Target, Port, Url
from secator.output_types import Port, Url
from secator.definitions import DEBUG
from secator.rich import console
from secator.runners import Command, Workflow
from secator.utils import setup_logging, merge_opts
from secator.utils_test import TEST_WORKFLOWS, ALL_WORKFLOWS, CommandOutputTester, load_fixture
Expand Down Expand Up @@ -95,9 +94,9 @@ def test_inline_workflow(self):

# Expected results / context
expected_results = [
Port(port=9999, host='localhost', ip='127.0.0.1', service_name='fake', _source='unknown'),
Port(port=3000, host='localhost', ip='127.0.0.1', _source='naabu'),
Port(port=8080, host='localhost', ip='127.0.0.1', _source='naabu'),
Port(port=9999, host='localhost', ip='127.0.0.1', state='open', service_name='fake', _source='unknown'),
Port(port=3000, host='localhost', ip='127.0.0.1', state='open', _source='naabu'),
Port(port=8080, host='localhost', ip='127.0.0.1', state='open', _source='naabu'),
Url(url='http://localhost:3000', host='127.0.0.1', status_code=200, title='OWASP Juice Shop', content_type='text/html', _source='httpx'),
Url(url='http://localhost:8080', host='127.0.0.1', status_code=400, title='', content_type='application/json', _source='httpx'),
]
Expand All @@ -109,6 +108,7 @@ def test_inline_workflow(self):
# Create ad-hoc workflow
conf = {
'name': 'my_workflow',
'type': 'workflow',
'description': 'Test workflow',
'tasks': {
'naabu': {},
Expand All @@ -122,7 +122,7 @@ def test_inline_workflow(self):
config,
inputs=['localhost'],
results=[
Port(port=9999, host='localhost', ip='127.0.0.1', service_name='fake', _source='unknown', _context=expected_context)
Port(port=9999, host='localhost', ip='127.0.0.1', state='open', service_name='fake', _source='unknown', _context=expected_context)
],
hooks = {
Workflow: {
Expand All @@ -139,20 +139,10 @@ def test_inline_workflow(self):

# Verify no duplicates and context added from hook is present in output
for result in workflow:
self.assertEqual(result._context, expected_context)
self.assertEqual(result._context, {**result._context, **expected_context})
self.assertNotIn(result._uuid, uuids)
uuids.append(result._uuid)
results.append(result)

# Verify number of URLs is good
urls = [r.url for r in results if r._type == 'url']
ports = [r.port for r in results if r._type == 'port']
self.assertEqual(len(urls), 2)
self.assertEqual(len(ports), 3)

# Verify results yielded from workflow and workflow.results are equal
self.assertEqual(results, workflow.results)

# Verify expected results are there
for res in expected_results:
self.assertIn(res, workflow.results)

0 comments on commit cdb0b0b

Please sign in to comment.