8000 feat: use os system for CLI and better labs by ocervell · Pull Request #649 · freelabz/secator · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: use os system for CLI and better labs #649

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

Merged
merged 6 commits into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
57 changes: 38 additions & 19 deletions secator/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ def scan(ctx):
@click.option('--dev', is_flag=True, help='Start a worker in dev mode (celery multi).')
@click.option('--stop', is_flag=True, help='Stop a worker in dev mode (celery multi).')
@click.option('--show', is_flag=True, help='Show command (celery multi).')
def worker(hostname, concurrency, reload, queue, pool, quiet, loglevel, check, dev, stop, show):
@click.option('--use-command-runner', is_flag=True, default=False, help='Use command runner to run the command.')
def worker(hostname, concurrency, reload, queue, pool, quiet, loglevel, check, dev, stop, show, use_command_runner):
"""Run a worker."""

# Check Celery addon is installed
Expand Down Expand Up @@ -186,8 +187,13 @@ def worker(hostname, concurrency, reload, queue, pool, quiet, loglevel, check, d
patterns = "celery.py;tasks/*.py;runners/*.py;serializers/*.py;output_types/*.py;hooks/*.py;exporters/*.py"
cmd = f'watchmedo auto-restart --directory=./ --patterns="{patterns}" --recursive -- {cmd}'

ret = Command.execute(cmd, name='secator_worker')
sys.exit(ret.return_code)
if use_command_runner:
ret = Command.execute(cmd, name='secator_worker')
sys.exit(ret.return_code)
else:
console.print(f'[bold red]{cmd}[/]')
ret = os.system(cmd)
sys.exit(ret)


#-------#
Expand Down Expand Up @@ -1455,28 +1461,38 @@ def test():
pass


def run_test(cmd, name=None, exit=True, verbose=False):
def run_test(cmd, name=None, exit=True, verbose=False, use_os_system=False):
"""Run a test and return the result.

Args:
cmd (str): Command to run.
name (str, optional): Name of the test.
exit (bool, optional): Exit after running the test with the return code.
verbose (bool, optional): Print verbose output.
use_os_system (bool, optional): Use os.system to run the command.

Returns:
Return code of the test.
"""
cmd_name = name + ' tests' if name else 'tests'
result = Command.execute(cmd, name=cmd_name, cwd=ROOT_FOLDER, quiet=not verbose)
if name:
if result.return_code == 0:
console.print(f':tada: {name.capitalize()} tests passed !', style='bold green')
else:
console.print(f':x: {name.capitalize()} tests failed !', style='bold red')
if exit:
sys.exit(result.return_code)
return result.return_code
if use_os_system:
console.print(f'[bold red]{cmd}[/]')
if not verbose:
cmd += ' >/dev/null 2>&1'
ret = os.system(cmd)
if exit:
sys.exit(ret)
return ret
else:
result = Command.execute(cmd, name=cmd_name, cwd=ROOT_FOLDER, quiet=not verbose)
if name:
if result.return_code == 0:
console.print(f':tada: {name.capitalize()} tests passed !', style='bold green')
else:
console.print(f':x: {name.capitalize()} tests failed !', style='bold red')
if exit:
sys.exit(result.return_code)
return result.return_code


@test.command()
Expand All @@ -1489,7 +1505,7 @@ def lint(linter):
elif linter == 'ruff':
opts = ' check'
cmd = f'{sys.executable} -m {linter} {opts} secator/'
run_test(cmd, 'lint', verbose=True)
run_test(cmd, 'lint', verbose=True, use_os_system=True)


@test.command()
Expand Down Expand Up @@ -1521,21 +1537,23 @@ def unit(tasks, workflows, scans, test):
if test:
test_str = ' or '.join(test.split(','))
cmd += f' -k "{test_str}"'
run_test(cmd, 'unit', verbose=True)
run_test(cmd, 'unit', verbose=True, use_os_system=True)


@test.command()
@click.option('--tasks', type=str, default='', help='Secator tasks to test (comma-separated)')
@click.option('--workflows', type=str, default='', help='Secator workflows to test (comma-separated)')
@click.option('--scans', type=str, default='', help='Secator scans to test (comma-separated)')
@click.option('--test', '-t', type=str, help='Secator test to run')
def integration(tasks, workflows, scans, test):
@click.option('--no-cleanup', '-nc', is_flag=True, help='Do not perform cleanup (keep lab running, faster for relaunching tests)') # noqa: E501
def integration(tasks, workflows, scans, test, no_cleanup):
"""Run integration tests."""
os.environ['TEST_TASKS'] = tasks or ''
os.environ['TEST_WORKFLOWS'] = workflows or ''
os.environ['TEST_SCANS'] = scans or ''
os.environ['SECATOR_DIRS_DATA'] = '/tmp/.secator'
os.environ['SECATOR_RUNNERS_SKIP_CVE_SEARCH'] = '1'
os.environ['TEST_NO_CLEANUP'] = '1' if no_cleanup else '0'

if not test:
if tasks:
Expand All @@ -1552,7 +1570,7 @@ def integration(tasks, workflows, scans, test):
if test:
test_str = ' or '.join(test.split(','))
cmd += f' -k "{test_str}"'
run_test(cmd, 'integration', verbose=True)
run_test(cmd, 'integration', verbose=True, use_os_system=True)


@test.command()
Expand Down Expand Up @@ -1606,7 +1624,7 @@ def performance(tasks, workflows, scans, test):
if test:
test_str = ' or '.join(test.split(','))
cmd += f' -k "{test_str}"'
run_test(cmd, 'performance', verbose=True)
run_test(cmd, 'performance', verbose=True, use_os_system=True)


@test.command()
Expand Down Expand Up @@ -1777,4 +1795,5 @@ def coverage(unit_only, integration_only, template_only):
cmd += ' --data-file=.coverage.template'
else:
Command.execute(f'{sys.executable} -m coverage combine --keep', name='coverage combine', cwd=ROOT_FOLDER)
run_test(cmd, 'coverage', verbose=True)
run_test(cmd, 'coverage', use_os_system=True)

2 changes: 1 addition & 1 deletion secator/runners/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,9 +548,9 @@ def _print_item(self, item, force=False):

# Item is an output type
if isinstance(item, OutputType):
self.debug(item, lazy=lambda x: repr(x), sub='item.print', allow_no_process=False, verbose=True)
_type = item._type
print_this_type = getattr(self, f'print_{_type}', True)
self.debug(item, lazy=lambda x: repr(x), sub='item', allow_no_process=False, verbose=print_this_type)
if not print_this_type:
return

EDBE Expand Down
2 changes: 1 addition & 1 deletion secator/tasks/arjun.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class arjun(Command):
output_types = [Url]
tags = ['url', 'fuzz', 'params']
input_flag = '-u'
version_flag = ' '
input_chunk_size = 1
version_flag = ' '
opts = {
'chunk_size': {'type': int, 'help': 'Control query/chunk size'},
'stable': {'is_flag': True, 'default': False, 'help': 'Use stable mode'},
Expand Down
6 changes: 4 additions & 2 deletions tests/integration/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ if ! command -v $BIN_NAME 2>&1 > /dev/null; then
exit 1
fi

$BIN_NAME pull
$BIN_NAME up -d
if [ "$TEST_NO_CLEANUP" != "1" ]; then
$BIN_NAME pull
fi
$BIN_NAME up -d --wait --no-recreate
4 changes: 4 additions & 0 deletions tests/integration/teardown.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ if ! command -v $BIN_NAME 2>&1 > /dev/null; then
exit 1
fi

if [ "$TEST_NO_CLEANUP" = "1" ]; then
echo "Aborting cleanup since TEST_NO_CLEANUP is set"
exit 0
fi
$BIN_NAME down -v
2 changes: 1 addition & 1 deletion tests/integration/test_celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def setUpClass(cls):
cwd=INTEGRATION_DIR
)
cls.queue = queue.Queue()
cls.cmd = Command.execute('secator worker', quiet=True, run=False)
cls.cmd = Command.execute('secator worker --use-command-runner', quiet=True, run=False)
cls.thread = Thread(target=cls.cmd.run)
cls.thread.start()
sleep(5)
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class TestWorker(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.queue = queue.Queue()
cls.cmd = Command.execute('secator worker', name='secator_worker', quiet=True, run=False)
cls.cmd = Command.execute('secator worker --use-command-runner', name='secator_worker', quiet=True, run=False)
cls.thread = Thread(target=cls.cmd.run)
cls.thread.start()
sleep(3)
Expand Down
Loading
0