github.com/crowdsecurity/crowdsec@v1.6.1/docker/test/tests/test_hub_scenarios.py (about)

     1  #!/usr/bin/env python
     2  
     3  """
     4  Test scenario management
     5  """
     6  
     7  from http import HTTPStatus
     8  import json
     9  
    10  import pytest
    11  
    12  pytestmark = pytest.mark.docker
    13  
    14  
    15  def test_install_two_scenarios(crowdsec, flavor):
    16      """Test installing scenarios at startup"""
    17      it1 = 'crowdsecurity/cpanel-bf-attempt'
    18      it2 = 'crowdsecurity/asterisk_bf'
    19      env = {
    20          'SCENARIOS': f'{it1} {it2}'
    21      }
    22      with crowdsec(flavor=flavor, environment=env) as cs:
    23          cs.wait_for_log([
    24              f'*scenarios install "{it1}"*',
    25              f'*scenarios install "{it2}"*',
    26              "*Starting processing data*"
    27          ])
    28          cs.wait_for_http(8080, '/health', want_status=HTTPStatus.OK)
    29          res = cs.cont.exec_run('cscli scenarios list -o json')
    30          assert res.exit_code == 0
    31          j = json.loads(res.output)
    32          items = {c['name']: c for c in j['scenarios']}
    33          assert items[it1]['status'] == 'enabled'
    34          assert items[it2]['status'] == 'enabled'
    35  
    36  
    37  def test_disable_scenario(crowdsec, flavor):
    38      """Test removing a pre-installed scenario at startup"""
    39      it = 'crowdsecurity/ssh-bf'
    40      env = {
    41          'DISABLE_SCENARIOS': it
    42      }
    43      with crowdsec(flavor=flavor, environment=env) as cs:
    44          cs.wait_for_log([
    45              f'*scenarios remove "{it}"*',
    46              "*Starting processing data*"
    47          ])
    48          cs.wait_for_http(8080, '/health', want_status=HTTPStatus.OK)
    49          res = cs.cont.exec_run('cscli scenarios list -o json')
    50          assert res.exit_code == 0
    51          j = json.loads(res.output)
    52          items = {c['name'] for c in j['scenarios']}
    53          assert it not in items
    54  
    55  
    56  def test_install_and_disable_scenario(crowdsec, flavor):
    57      """Declare a scenario to install AND disable: disable wins"""
    58      it = 'crowdsecurity/asterisk_bf'
    59      env = {
    60          'SCENARIOS': it,
    61          'DISABLE_SCENARIOS': it,
    62      }
    63      with crowdsec(flavor=flavor, environment=env) as cs:
    64          cs.wait_for_log("*Starting processing data*")
    65          cs.wait_for_http(8080, '/health', want_status=HTTPStatus.OK)
    66          res = cs.cont.exec_run('cscli scenarios list -o json')
    67          assert res.exit_code == 0
    68          j = json.loads(res.output)
    69          items = {c['name'] for c in j['scenarios']}
    70          assert it not in items
    71          logs = cs.cont.logs().decode().splitlines()
    72          # check that there was no attempt to install
    73          assert not any(f'scenarios install "{it}"' in line for line in logs)