wazuh-qa icon indicating copy to clipboard operation
wazuh-qa copied to clipboard

Implement execution ID to workflow engine

Open QU3B1M opened this issue 1 year ago • 0 comments

Description

This PR adds the possibility to have and use internal/magic variables in the workflow execution, it is something similar to what Ansible have (link). For now only one variable is configured, the execution_id this var will be used to easily identify each execution and its reports on the observability module.

The magic variables are also configured in each execution as environment vars, so any tool, module, script executed in a task can access these variables through the ENV vars.

The InfluxDB pytest plugin was also updated to take adventage of this new variable.

Usage + Test proof

Full test.py file to use for testing purposes
import argparse
import os


parser = argparse.ArgumentParser()
parser.add_argument('--execution_id', type=str)
parser.parse_args()


if exec_id := parser.parse_args().execution_id:
    # Get the value of the argument 'execution_id'
    print(f'The value of the Execution ID var as argument is: {exec_id}')
elif exec_id := os.getenv('execution_id'):
    # Get the value of the environment variable 'execution_id'
    print(f'The value of the Execution ID environment variable is: {exec_id}')

# Get the value of the workflow defined env variable 'TEST'
if test_env := os.getenv('TEST'):
    print(f'The value of the workflow defined env var is: {test_env}')

Basically there are two ways to take adventage of this magic variables:

  1. Actively calling this var in the task to inject it (as a tipical declared variable but without the need of declaration) workflow.yaml
    version: 0.1
    description: This workflow is used to test
    
    tasks:
      - task: "test-magic-vars"
        description: "Use a simple script to the new magic & env variables."
        do:
          this: process
          with:
            path: python3
            args:
              - /home/quebim/Wazuh/repos/wazuh-qa/deployability/test.py
              - execution_id: '{execution_id}' # Usage as injected var
    
    test.py
    parser = argparse.ArgumentParser()
    parser.add_argument('--execution_id', type=str, default=None)
    parser.parse_args()
    
    
    exec_id = parser.parse_args().execution_id
    print(f'The value of the Execution ID var as argument is: {exec_id}')
    
    Result:
    [2024-03-20 10:51:55] [INFO] [9669] [ThreadPoolExecutor-0_0] [workflow_engine]: [test-magic-vars] Starting task.
    [2024-03-20 10:51:55] [DEBUG] [9669] [ThreadPoolExecutor-0_0] [workflow_engine]: Running task "test-magic-vars" with arguments: ['/home/quebim/Wazuh/repos/wazuh-qa/deployability/test.py', '725e117f-f9ce-4e00-8dc4-8a7cf6348439']
    [2024-03-20 10:51:55] [DEBUG] [9669] [ThreadPoolExecutor-0_0] [workflow_engine]: Finished task "test-magic-vars" execution with result:
    The value of the Execution ID var as argument is: 725e117f-f9ce-4e00-8dc4-8a7cf6348439
    
  2. Consuming it as Environment variable workflow.yaml
    version: 0.1
    description: This workflow is used to test
    
    tasks:
      - task: "test-magic-vars"
        description: "Use a simple script to the new magic & env variables."
        do:
          this: process
          with:
            path: python3
            args:
              - /home/quebim/Wazuh/repos/wazuh-qa/deployability/test.py
    
    test.py
    exec_id = os.getenv('execution_id')
    # Get the value of the environment variable 'execution_id'
    print(f'The value of the Execution ID environment variable is: {exec_id}')
    
    Result:
    [2024-03-20 10:55:13] [INFO] [9887] [ThreadPoolExecutor-0_0] [workflow_engine]: [test-magic-vars] Starting task.
    [2024-03-20 10:55:13] [DEBUG] [9887] [ThreadPoolExecutor-0_0] [workflow_engine]: Running task "test-magic-vars" with arguments: ['/home/quebim/Wazuh/repos/wazuh-qa/deployability/test.py', '--execution_id=108fe6ba-a174-48fb-b6bf-4adda112a93a']
    [2024-03-20 10:55:13] [DEBUG] [9887] [ThreadPoolExecutor-0_0] [workflow_engine]: Finished task "test-magic-vars" execution with result:
    The value of the Execution ID var as argument is: 108fe6ba-a174-48fb-b6bf-4adda112a93a
    

Extra

A new env tag was implemented in the Task block to let the user define environment variables that can be used in the task execution.

  • Example: workflow.yaml
    version: 0.1
    description: This workflow is used to test
    
    tasks:
      - task: "test-magic-vars"
        description: "Use a simple script to the new magic & env variables."
        do:
          this: process
          with:
            path: python3
            args:
              - /home/quebim/Wazuh/repos/wazuh-qa/deployability/test.py
            env:
              TEST: 'Im a test'
    
    test.py
    test_env = os.getenv('TEST')
    print(f'The value of the workflow defined env var is: {test_env}')
    
    Result:
    24-03-20 10:59:13] [INFO] [10051] [ThreadPoolExecutor-0_0] [workflow_engine]: [test-magic-vars] Starting task.
    [2024-03-20 10:59:13] [DEBUG] [10051] [ThreadPoolExecutor-0_0] [workflow_engine]: Running task "test-magic-vars" with arguments: ['/home/quebim/Wazuh/repos/wazuh-qa/deployability/test.py']
    [2024-03-20 10:59:13] [DEBUG] [10051] [ThreadPoolExecutor-0_0] [workflow_engine]: Finished task "test-magic-vars" execution with result:
    The value of the workflow defined env var is: Im a test
    

QU3B1M avatar Mar 18 '24 19:03 QU3B1M