Skip to content
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

Add control to set-group and fetch-config actions #251

Closed
wants to merge 2 commits into from

Conversation

mcasas993
Copy link
Member

Description

Create a dedicated parser for set -group and fetch-config commands.

The set-group command accepts an array of strings within action.args, comprising the complete list of groups for this agent. In case of a mismatch on this command, the request will be rejected.

The fetch-config command accepts no arguments. In case of a mismatch on this command, mismatching values are ignored, and the request is accepted.

Issues Resolved

248

@mcasas993 mcasas993 requested a review from a team as a code owner January 27, 2025 13:17
@mcasas993 mcasas993 self-assigned this Jan 27, 2025
@mcasas993 mcasas993 linked an issue Jan 27, 2025 that may be closed by this pull request
2 tasks
@mcasas993 mcasas993 changed the title Add control to set-group action Add control to set-group and fetch-config actions Jan 27, 2025
@mcasas993
Copy link
Member Author

mcasas993 commented Jan 27, 2025

Test of "set-group" action

1. With an array of agent Request:
{
  "commands": [
    {
      "action": {
        "name": "set-group",
        "args": {
          "groups": [
            "group_1",
            "group_2",
            "group_N"
          ]
        },
        "version": "v4"
      },
      "source": "Engine",
      "user": "USER-SET-GROUP-1",
      "timeout": 30,
      "target": {
        "id": "target4",
        "type": "agent"
      }
    }
  ]
}

Response:

{
  "_index": "wazuh-commands",
  "_documents": [
    {
      "_id": "3A2eqJQBLpqli8LcqBc5"
    }
  ],
  "result": "OK"
}

Document in index command:

{
  "_index": "wazuh-commands",
  "_id": "3A2eqJQBLpqli8LcqBc5",
  "_score": 1,
  "_source": {
    "agent": {
      "groups": [
        "groups000"
      ]
    },
    "command": {
      "source": "Engine",
      "user": "USER-SET-GROUP-1",
      "target": {
        "type": "agent",
        "id": "target4"
      },
      "action": {
        "name": "set-group",
        "args": {
          "groups": [
            "group_1",
            "group_2",
            "group_N"
          ]
        },
        "version": "v4"
      },
      "timeout": 30,
      "status": "pending",
      "order_id": "2w2eqJQBLpqli8LcqBc1",
      "request_id": "2g2eqJQBLpqli8LcqBc1"
    },
    "@timestamp": "2025-01-27T16:35:57Z",
    "delivery_timestamp": "2025-01-27T16:36:27Z"
  }
}
2. With an array of agent and another attribute Request:
{
  "commands": [
    {
      "action": {
        "name": "set-group",
        "args": {
          "groups": [
            "group_1",
            "group_2",
            "group_N"
          ],
          "arg1": 45
        },
        "version": "v4"
      },
      "source": "Engine",
      "user": "USER-SET-GROUP-1",
      "timeout": 30,
      "target": {
        "id": "target4",
        "type": "agent"
      }
    }
  ]
}

Response:

{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "Incorrect request. An array of agents is expected in args."
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "Incorrect request. An array of agents is expected in args."
  },
  "status": 400
}
3. With an empty array in groups Request:
{
  "commands": [
    {
      "action": {
        "name": "set-group",
        "args": {
          "groups": []
        },
        "version": "v4"
      },
      "source": "Engine",
      "user": "USER-SET-GROUP-1",
      "timeout": 30,
      "target": {
        "id": "target4",
        "type": "agent"
      }
    }
  ]
}

Response:

{
  "_index": "wazuh-commands",
  "_documents": [
    {
      "_id": "1rSkqJQBxpy59GZ0wmxD"
    }
  ],
  "result": "OK"
}

Document in index command:

{
  "_index": "wazuh-commands",
  "_id": "1rSkqJQBxpy59GZ0wmxD",
  "_score": 1,
  "_source": {
    "agent": {
      "groups": [
        "groups000"
      ]
    },
    "command": {
      "source": "Engine",
      "user": "USER-SET-GROUP-1",
      "target": {
        "type": "agent",
        "id": "target4"
      },
      "action": {
        "name": "set-group",
        "args": {
          "groups": []
        },
        "version": "v4"
      },
      "timeout": 30,
      "status": "pending",
      "order_id": "1bSkqJQBxpy59GZ0wmxA",
      "request_id": "1LSkqJQBxpy59GZ0wmxA"
    },
    "@timestamp": "2025-01-27T16:42:37Z",
    "delivery_timestamp": "2025-01-27T16:43:07Z"
  }
}
4. With empty args object Request:
{
  "commands": [
    {
      "action": {
        "name": "set-group",
        "args": {
        },
        "version": "v4"
      },
      "source": "Engine",
      "user": "USER-SET-GROUP-1",
      "timeout": 30,
      "target": {
        "id": "target4",
        "type": "agent"
      }
    }
  ]
}

Response:

{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "Incorrect request. An array of agents is expected in args."
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "Incorrect request. An array of agents is expected in args."
  },
  "status": 400
}
5. With any other attribute in args Request:
{
  "commands": [
    {
      "action": {
        "name": "set-group",
        "args": {
          "any": "/path/to/executable/arg6"
        },
        "version": "v4"
      },
      "source": "Engine",
      "user": "USER-SET-GROUP-1",
      "timeout": 30,
      "target": {
        "id": "target4",
        "type": "agent"
      }
    }
  ]
}

Response:

{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "Incorrect request. An array of agents is expected in args."
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "Incorrect request. An array of agents is expected in args."
  },
  "status": 400
}

@mcasas993
Copy link
Member Author

mcasas993 commented Jan 27, 2025

Test of "fetch-config" action

1. With empty args object Request:
{
  "commands": [
    {
      "action": {
        "name": "fetch-config",
        "args": {},
        "version": "v4"
      },
      "source": "Engine",
      "user": "USER-SET-GROUP-1",
      "timeout": 30,
      "target": {
        "id": "target4",
        "type": "agent"
      }
    }
  ]
}

Response:

{
  "_index": "wazuh-commands",
  "_documents": [
    {
      "_id": "LUCSqJQB-iqktn3Au96S"
    }
  ],
  "result": "OK"
}

Document in index command:

{
  "_index": "wazuh-commands",
  "_id": "LUCSqJQB-iqktn3Au96S",
  "_score": 1,
  "_source": {
    "agent": {
      "groups": [
        "groups000"
      ]
    },
    "@timestamp": "2025-01-27T16:22:56Z",
    "delivery_timestamp": "2025-01-27T16:23:26Z",
    "command": {
      "action": {
        "args": {},
        "name": "fetch-config",
        "version": "v4"
      },
      "source": "Engine",
      "user": "USER-SET-GROUP-1",
      "order_id": "LECSqJQB-iqktn3Au96Q",
      "request_id": "K0CSqJQB-iqktn3Au96Q",
      "timeout": 30,
      "target": {
        "id": "target4",
        "type": "agent"
      },
      "status": "failure"
    }
  }
}
2. With any attribute in args object Request:
{
  "commands": [
    {
      "action": {
        "name": "fetch-group",
        "args": {
          "groups": [
            "group-1",
            "gruop-2"
          ],
          "other-string": "asdkoasdk_55",
          "other-number": 16565.5
        },
        "version": "v4"
      },
      "source": "Engine",
      "user": "USER-SET-GROUP-1",
      "timeout": 30,
      "target": {
        "id": "target4",
        "type": "agent"
      }
    }
  ]
}

Response:

{
  "_index": "wazuh-commands",
  "_documents": [
    {
      "_id": "pdatqJQBYNBuIuTXn0Gw"
    }
  ],
  "result": "OK"
}

Document in index command:

{
  "_index": "wazuh-commands",
  "_id": "pdatqJQBYNBuIuTXn0Gw",
  "_score": 1,
  "_source": {
    "agent": {
      "groups": [
        "groups000"
      ]
    },
    "@timestamp": "2025-01-27T16:52:18Z",
    "delivery_timestamp": "2025-01-27T16:52:48Z",
    "command": {
      "action": {
        "args": {},
        "name": "fetch-group",
        "version": "v4"
      },
      "source": "Engine",
      "user": "USER-SET-GROUP-1",
      "order_id": "pNatqJQBYNBuIuTXn0Gr",
      "request_id": "o9atqJQBYNBuIuTXn0Gq",
      "timeout": 30,
      "target": {
        "id": "target4",
        "type": "agent"
      },
      "status": "failure"
    }
  }
}

@mcasas993
Copy link
Member Author

Test of any other action

1. With empty args object

Request:

{
  "commands": [
    {
      "action": {
        "name": "any-action",
        "args": {},
        "version": "v4"
      },
      "source": "Engine",
      "user": "USER-SET-GROUP-1",
      "timeout": 30,
      "target": {
        "id": "target4",
        "type": "agent"
      }
    }
  ]
}

Response:

{
  "_index": "wazuh-commands",
  "_documents": [
    {
      "_id": "kzPAqJQB8VpaqYLueJyY"
    }
  ],
  "result": "OK"
}

Document in index command:

{
  "_index": "wazuh-commands",
  "_id": "kzPAqJQB8VpaqYLueJyY",
  "_score": 1,
  "_source": {
    "agent": {
      "groups": [
        "groups000"
      ]
    },
    "command": {
      "source": "Engine",
      "user": "USER-SET-GROUP-1",
      "target": {
        "type": "agent",
        "id": "target4"
      },
      "action": {
        "name": "any-action",
        "args": {},
        "version": "v4"
      },
      "timeout": 30,
      "status": "pending",
      "order_id": "kjPAqJQB8VpaqYLueJyX",
      "request_id": "kTPAqJQB8VpaqYLueJyX"
    },
    "@timestamp": "2025-01-27T17:12:53Z",
    "delivery_timestamp": "2025-01-27T17:13:23Z"
  }
}
2. With any attribute in args object Request:
{
  "commands": [
    {
      "action": {
        "name": "any-action",
        "args": {
          "groups": [
            "group-1",
            "gruop-2"
          ],
          "other-string": "asdkoasdk_55",
          "other-number": 16565.5
        },
        "version": "v4"
      },
      "source": "Engine",
      "user": "USER-SET-GROUP-1",
      "timeout": 30,
      "target": {
        "id": "target4",
        "type": "agent"
      }
    }
  ]
}

Response:

{
  "_index": "wazuh-commands",
  "_documents": [
    {
      "_id": "J--5qJQB46vf3V8zaC9S"
    }
  ],
  "result": "OK"
}

Document in index command:

{
  "_index": "wazuh-commands",
  "_id": "J--5qJQB46vf3V8zaC9S",
  "_score": 1,
  "_source": {
    "agent": {
      "groups": [
        "groups000"
      ]
    },
    "@timestamp": "2025-01-27T17:05:10Z",
    "delivery_timestamp": "2025-01-27T17:05:40Z",
    "command": {
      "action": {
        "args": {
          "other-number": 16565.5,
          "groups": [
            "group-1",
            "gruop-2"
          ],
          "other-string": "asdkoasdk_55"
        },
        "name": "any-action",
        "version": "v4"
      },
      "source": "Engine",
      "user": "USER-SET-GROUP-1",
      "order_id": "Ju-5qJQB46vf3V8zaC9R",
      "request_id": "Je-5qJQB46vf3V8zaC9R",
      "timeout": 30,
      "target": {
        "id": "target4",
        "type": "agent"
      },
      "status": "failure"
    }
  }
}

Copy link
Member

@QU3B1M QU3B1M left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@AlexRuiz7 AlexRuiz7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This solution is not approved as it does not meet the requirements:

  • Implementation of dedicated parsers classes for each model, inheriting from the Args class, which serves as a generic parser.
  • No unit tests.
  • Code does not match the code style (spotless pre-commit hook was bypassed).

As an alternate solution, check 248-add-hard-validation-to-commands.

@AlexRuiz7 AlexRuiz7 closed this Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Dedicated command.action.args models
3 participants