ansible / event-driven-ansible

Ansible Collection for EDA
https://ansible.readthedocs.io/projects/ansible-eda/
Apache License 2.0
270 stars 106 forks source link

Condition `not defined` does not work #306

Open DustinWi opened 2 weeks ago

DustinWi commented 2 weeks ago

Please confirm the following

Bug Summary

When trying to check if a 'Token' header is set for a webhook event, ... is not defined is not triggering. If I change to ... is defined the behavior works as expected.

Environment

OS:

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=22.04
DISTRIB_CODENAME=jammy
DISTRIB_DESCRIPTION="Ubuntu 22.04.4 LTS"

Python:

Python 3.10.12

Ansible-Rulebook:

1.1.0
  Executable location = /home/dustin/projects/eda/.venv/bin/ansible-rulebook
  Drools_jpy version = 0.3.9
  Java home = /usr/lib/jvm/java-17-openjdk-amd64
  Java version = 17.0.12
  Python version = 3.10.12 (main, Jul 29 2024, 16:56:48) [GCC 11.4.0]

Steps to reproduce

  1. Install ansible, ansible-rulebook, and dependencies.
  2. Create the test.yml rulebook
  3. Run the rule book
  4. Use curl to send sample data to the webhook listener

Actual results

The expected rule does not trigger, one of the latter rules triggers.

Expected results

When sending a request with no Token header set, the first rule should trigger.

Additional information

Rulebook:

# This 
---
- name: Webhook for testing
  hosts: localhost
  sources:
    - name: WEBHOOK TEST
      ansible.eda.webhook:
        host: 0.0.0.0
        port: 8100
  rules:
    - name: No Token
      condition: event.meta.headers.Token is not defined
      actions:
        - debug: 
            msg: "No Token Provided\n{{ event.meta.headers.Token is not defined }}\n{{ event.meta.headers }}"
    - name: Incorrect Token
      condition: event.meta.headers.Token != 'ThisIsMyToken'
      actions:
        - debug:
            msg: "Wrong Token Provided"
    - name: Incorrect Payload
      condition: event.meta.headers != "Hello Testing World"
      actions:
        - debug:
            msg: "Unexpected payload"

Test Commands and the resulting events using --verbose and --print-events are below. As you can see, the first command attempts to trigger the first rule. However, the third rule is triggered instead.


Command 1:

# No Token wrong payload
curl -H 'Content-Type: application/json' -d "{\"message\": \"Ansible is super cool\"}" 127.0.0.1:8100/endpoint

Result 1:

 2024-09-04 12:44:16.346942 [received event] *******************************************************************************************************************************************
Ruleset: Webhook for testing
Event:
{'meta': {'endpoint': 'endpoint',
          'headers': {'Accept': '*/*',
                      'Content-Length': '36',
                      'Content-Type': 'application/json',
                      'Host': '127.0.0.1:8100',
                      'User-Agent': 'curl/7.81.0'},
          'received_at': '2024-09-04T16:44:16.346549Z',
          'source': {'name': 'WEBHOOK TEST', 'type': 'ansible.eda.webhook'},
          'uuid': '126567d8-58c5-40bb-aff8-c1fdb17acc3e'},
 'payload': {'message': 'Ansible is super cool'}}
******************************************************************************************************************************************************************************************
2024-09-04 12:44:16 348 [main] INFO org.drools.ansible.rulebook.integration.api.rulesengine.MemoryMonitorUtil - Memory occupation threshold set to 90%
2024-09-04 12:44:16 348 [main] INFO org.drools.ansible.rulebook.integration.api.rulesengine.MemoryMonitorUtil - Memory check event count threshold set to 64
2024-09-04 12:44:16 348 [main] INFO org.drools.ansible.rulebook.integration.api.rulesengine.MemoryMonitorUtil - Exit above memory occupation threshold set to false

** 2024-09-04 12:44:16.359004 [debug] ****************************************************************************************************************************************************
Unexpected payload
******************************************************************************************************************************************************************************************

Command 2:

# No Token correct payload
curl -H 'Content-Type: application/json' -d "{\"message\": \"Hello Testing World\"}" 127.0.0.1:8100/endpoint

Result 2:

* 2024-09-04 12:45:13.180064 [received event] *******************************************************************************************************************************************
Ruleset: Webhook for testing
Event:
{'meta': {'endpoint': 'endpoint',
          'headers': {'Accept': '*/*',
                      'Content-Length': '34',
                      'Content-Type': 'application/json',
                      'Host': '127.0.0.1:8100',
                      'User-Agent': 'curl/7.81.0'},
          'received_at': '2024-09-04T16:45:13.179714Z',
          'source': {'name': 'WEBHOOK TEST', 'type': 'ansible.eda.webhook'},
          'uuid': '2ae95567-27ae-439f-993b-150e3a017350'},
 'payload': {'message': 'Hello Testing World'}}
******************************************************************************************************************************************************************************************

** 2024-09-04 12:45:13.181734 [debug] ****************************************************************************************************************************************************
Unexpected payload
******************************************************************************************************************************************************************************************

Command 3:

# Incorrect Token wrong payload
curl -H 'Content-Type: application/json' -H 'Token: WrongToken' -d "{\"message\": \"Ansible is super cool\"}" 127.0.0.1:8100/endpoint

Result 3:

** 2024-09-04 12:45:56.200383 [received event] *******************************************************************************************************************************************
Ruleset: Webhook for testing
Event:
{'meta': {'endpoint': 'endpoint',
          'headers': {'Accept': '*/*',
                      'Content-Length': '36',
                      'Content-Type': 'application/json',
                      'Host': '127.0.0.1:8100',
                      'Token': 'WrongToken',
                      'User-Agent': 'curl/7.81.0'},
          'received_at': '2024-09-04T16:45:56.200042Z',
          'source': {'name': 'WEBHOOK TEST', 'type': 'ansible.eda.webhook'},
          'uuid': 'c531c034-1d23-4c5c-ba34-0ca43763d6d0'},
 'payload': {'message': 'Ansible is super cool'}}
******************************************************************************************************************************************************************************************

** 2024-09-04 12:45:56.202681 [debug] ****************************************************************************************************************************************************
Wrong Token Provided
******************************************************************************************************************************************************************************************
2024-09-04 12:45:56 226 [Thread-0] INFO org.drools.ansible.rulebook.integration.api.rulesengine.AbstractRulesEvaluator - Match(es) caused by automatic clock advance: [[[ No Token active=
false ] [ [fact 0:0:166077534:1306428912:0:DEFAULT:NON_TRAIT:org.drools.base.reteoo.InitialFactImpl:org.drools.base.reteoo.InitialFactImpl@4dde85f0] ] ]

Command 4:

# Correct Token wrong payload
curl -H 'Content-Type: application/json' -H 'Token: ThisIsMyToken' -d "{\"message\": \"Ansible is super cool\"}" 127.0.0.1:8100/endpoint

Result 4:

** 2024-09-04 12:48:18.840516 [received event] *******************************************************************************************************************************************
Ruleset: Webhook for testing
Event:
{'meta': {'endpoint': 'endpoint',
          'headers': {'Accept': '*/*',
                      'Content-Length': '36',
                      'Content-Type': 'application/json',
                      'Host': '127.0.0.1:8100',
                      'Token': 'ThisIsMyToken',
                      'User-Agent': 'curl/7.81.0'},
          'received_at': '2024-09-04T16:48:18.840172Z',
          'source': {'name': 'WEBHOOK TEST', 'type': 'ansible.eda.webhook'},
          'uuid': 'b447910f-213f-4fd3-a947-95703e9d609f'},
 'payload': {'message': 'Ansible is super cool'}}
******************************************************************************************************************************************************************************************

** 2024-09-04 12:48:18.842121 [debug] ****************************************************************************************************************************************************
Unexpected payload
******************************************************************************************************************************************************************************************
2024-09-04 12:48:18 926 [Thread-0] INFO org.drools.ansible.rulebook.integration.api.rulesengine.AbstractRulesEvaluator - Match(es) caused by automatic clock advance: [[[ No Token active=
false ] [ [fact 0:0:166077534:1306428912:0:DEFAULT:NON_TRAIT:org.drools.base.reteoo.InitialFactImpl:org.drools.base.reteoo.InitialFactImpl@4dde85f0] ] ]]

Command 5:

# Correct token wrong payload
curl -H 'Content-Type: application/json' -H 'Token: WrongToken' -d "{\"message\": \"Hello Testing World\"}" 127.0.0.1:8100/endpoint

Result 5:

** 2024-09-04 12:49:07.951337 [received event] *******************************************************************************************************************************************
Ruleset: Webhook for testing
Event:
{'meta': {'endpoint': 'endpoint',
          'headers': {'Accept': '*/*',
                      'Content-Length': '34',
                      'Content-Type': 'application/json',
                      'Host': '127.0.0.1:8100',
                      'Token': 'WrongToken',
                      'User-Agent': 'curl/7.81.0'},
          'received_at': '2024-09-04T16:49:07.950979Z',
          'source': {'name': 'WEBHOOK TEST', 'type': 'ansible.eda.webhook'},
          'uuid': 'a4da0cbc-24f7-4f43-aacc-26681e83cef8'},
 'payload': {'message': 'Hello Testing World'}}
******************************************************************************************************************************************************************************************

** 2024-09-04 12:49:07.952631 [debug] ****************************************************************************************************************************************************
Wrong Token Provided
******************************************************************************************************************************************************************************************
2024-09-04 12:49:08 026 [Thread-0] INFO org.drools.ansible.rulebook.integration.api.rulesengine.AbstractRulesEvaluator - Match(es) caused by automatic clock advance: [[[ No Token active=
false ] [ [fact 0:0:166077534:1306428912:0:DEFAULT:NON_TRAIT:org.drools.base.reteoo.InitialFactImpl:org.drools.base.reteoo.InitialFactImpl@4dde85f0] ] ]]
mkanoor commented 1 week ago

@DustinWi The "is not defined" is tricky to use since it can lead to a lot of triggering of rules, when the payload doesn't have the required keys. In ansible-rulebook if a "key" is missing the rule is skipped, it doesn't raise an exception and stop. So what is your use case? Are you trying to guard against the key missing?

mkanoor commented 6 days ago

@DustinWi Another thing I noticed is

condition: event.meta.headers != "Hello Testing World"

Is that trying to compare a dict with a string. The event.meta.headers is a dictionary of HTTP headers created by ansible.eda.webhook