github.com/crowdsecurity/crowdsec@v1.6.1/test/bats/04_capi.bats (about)

     1  #!/usr/bin/env bats
     2  # vim: ft=bats:list:ts=8:sts=4:sw=4:et:ai:si:
     3  
     4  set -u
     5  
     6  setup_file() {
     7      load "../lib/setup_file.sh"
     8      ./instance-data load
     9  }
    10  
    11  teardown_file() {
    12      load "../lib/teardown_file.sh"
    13  }
    14  
    15  setup() {
    16      load "../lib/setup.sh"
    17      load "../lib/bats-file/load.bash"
    18  }
    19  
    20  #----------
    21  
    22  @test "cscli capi status: fails without credentials" {
    23      config_enable_capi
    24      ONLINE_API_CREDENTIALS_YAML="$(config_get '.api.server.online_client.credentials_path')"
    25      # bogus values, won't be used
    26      echo '{"login":"login","password":"password","url":"url"}' > "${ONLINE_API_CREDENTIALS_YAML}"
    27  
    28      config_set "$ONLINE_API_CREDENTIALS_YAML" 'del(.url)'
    29      rune -1 cscli capi status
    30      assert_stderr --partial "can't load CAPI credentials from '$ONLINE_API_CREDENTIALS_YAML' (missing url field)"
    31  
    32      config_set "$ONLINE_API_CREDENTIALS_YAML" 'del(.password)'
    33      rune -1 cscli capi status
    34      assert_stderr --partial "can't load CAPI credentials from '$ONLINE_API_CREDENTIALS_YAML' (missing password field)"
    35  
    36      config_set "$ONLINE_API_CREDENTIALS_YAML" 'del(.login)'
    37      rune -1 cscli capi status
    38      assert_stderr --partial "can't load CAPI credentials from '$ONLINE_API_CREDENTIALS_YAML' (missing login field)"
    39  
    40      rm "${ONLINE_API_CREDENTIALS_YAML}"
    41      rune -1 cscli capi status
    42      assert_stderr --partial "failed to load Local API: loading online client credentials: open ${ONLINE_API_CREDENTIALS_YAML}: no such file or directory"
    43  
    44      config_set 'del(.api.server.online_client)'
    45      rune -1 cscli capi status
    46      assert_stderr --regexp "no configuration for Central API \(CAPI\) in '$(echo $CONFIG_YAML|sed s#//#/#g)'"
    47  }
    48  
    49  @test "cscli capi status" {
    50      ./instance-data load
    51      config_enable_capi
    52      rune -0 cscli capi register --schmilblick githubciXXXXXXXXXXXXXXXXXXXXXXXX
    53      rune -1 cscli capi status
    54      assert_stderr --partial "no scenarios installed, abort"
    55  
    56      rune -0 cscli scenarios install crowdsecurity/ssh-bf
    57      rune -0 cscli capi status
    58      assert_stderr --partial "Loaded credentials from"
    59      assert_stderr --partial "Trying to authenticate with username"
    60      assert_stderr --partial " on https://api.crowdsec.net/"
    61      assert_stderr --partial "You can successfully interact with Central API (CAPI)"
    62  }
    63  
    64  @test "cscli alerts list: receive a community pull when capi is enabled" {
    65      sleep 2
    66      ./instance-crowdsec start
    67      for ((i=0; i<15; i++)); do
    68          sleep 2
    69          [[ $(cscli alerts list -a -o json) != "[]" ]] && break
    70      done
    71  
    72      rune -0 cscli alerts list -a -o json
    73      rune -0 jq -r '. | length' <(output)
    74      refute_output 0
    75  }
    76  
    77  @test "we have exactly one machine, localhost" {
    78      rune -0 cscli machines list -o json
    79      rune -0 jq -c '[. | length, .[0].machineId[0:32], .[0].isValidated, .[0].ipAddress]' <(output)
    80      assert_json '[1,"githubciXXXXXXXXXXXXXXXXXXXXXXXX",true,"127.0.0.1"]'
    81  }
    82  
    83  @test "no agent: capi status should be ok" {
    84      ./instance-crowdsec stop
    85      config_disable_agent
    86      ./instance-crowdsec start
    87      rune -0 cscli capi status
    88      assert_stderr --partial "You can successfully interact with Central API (CAPI)"
    89  }
    90  
    91  @test "capi register must be run from lapi" {
    92      config_disable_lapi
    93      rune -1 cscli capi register --schmilblick githubciXXXXXXXXXXXXXXXXXXXXXXXX
    94      assert_stderr --partial "local API is disabled -- this command must be run on the local API machine"
    95  }