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

Command Manager client prototype #274

Merged
merged 50 commits into from
Feb 17, 2025

Conversation

QU3B1M
Copy link
Member

@QU3B1M QU3B1M commented Feb 12, 2025

Description

This PR implements a refactor of the HttpClient making it a generic abstract class to handle HTTP requests which is inherited in the specific CommandManagerClient, and CTIClient classes to handle the connections to its corresponding APIs.

The endpoints were proved to keep working as they were and how it should

Unit tests were added only for the base HttpClient class, this is because I've found difficult to mock the ClusterService object required by the PluginSetting used in the rest of clients.

Working evidence

  • Get catalog endpoint

    % curl http://localhost:9200/_plugins/_content_manager/vd-catalog                                                                                           
    {
      "id": 4,
      "name": "vd_4.8.0",
      "context": "vd_1.0.0",
      "operations": null,
      "inserted_at": "2023-11-23T19:34:18.698495Z",
      "updated_at": "2025-02-13T16:18:44.799607Z",
      "paths_filter": null,
      "last_offset": 1301373,
      "changes_url": "cti.wazuh.com/api/v1/catalog/contexts/vd_1.0.0/consumers/vd_4.8.0/changes",
      "last_snapshot_at": "2025-02-10T09:54:39.291196Z",
      "last_snapshot_link": "https://cti.wazuh.com/store/contexts/vd_1.0.0/consumers/vd_4.8.0/1287675_1739181279.zip",
      "last_snapshot_offset": 1287675,
    }
  • Get changes endpoint

    % curl "http://localhost:9200/_plugins/_content_manager/vd-changes?from_offset=1301371&to_offset=1301373&with_empties=true"
    {
        "data": [
            {
                "context": "vd_1.0.0",
                "offset": 1301372,
                "resource": "CVE-2025-24158",
                "type": "update",
                "version": 10,
                "payload": {}
            },
            {
                "context": "vd_1.0.0",
                "offset": 1301373,
                "resource": "CVE-2025-24162",
                "type": "update",
                "version": 10,
                "payload": {}
            }
        ]
    }
  • Command Manager request is correctly sent and captured by imposter

    • Request sent captured by ContentManager logs

      [2025-02-14T12:41:31,405][INFO ][c.w.c.c.HttpClient       ] [integTest-0] Sending POST request to [http://127.0.0.1:8080/_plugins/_command_manager/commands]
      [2025-02-14T12:41:31,406][INFO ][c.w.c.c.HttpClient       ] [integTest-0] Request bodyClassicRequestBuilder [method=POST, scheme=null, authority=null, path=null, parameters=null, headerGroup=[Content-Type: application/json], body=SimpleBody{content length=208, content type=application/json; charset=UTF-8}]
      [2025-02-14T12:41:31,473][INFO ][c.w.c.a.c.GetChangesAction] [integTest-0] Command Manager response: 200 null HTTP/1.1
    • Request received by imposter

      INFO[0002] watching for changes to: /Users/quebim_wz/IdeaProjects/wazuh-indexer-plugins/imposter 
      {
        "commands": [
          {
            "action": {
              "args": {
                "index": "content-index"
              },
              "name": "update",
              "version": "5.0.0"
            },
            "source": "Content Manager",
            "user": "Content Manager",
            "timeout": 100,
            "target": {
              "id": "TO-BE-DEFINED",
              "type": "server"
            }
          }
        ]
      }
      15:41:31 DEBUG i.g.i.s.s.ScriptedResponseServiceImpl - Executed script '/opt/imposter/config/response.js' for request: POST http://127.0.0.1:8080/_plugins/_command_manager/commands in 45.67ms
      15:41:31 WARN  i.g.i.s.ResponseServiceImpl - Response file and data are blank for [0921aaf7-b7d3-4217-9b0f-2ca5f6fef4a4] POST http://127.0.0.1:8080/_plugins/_command_manager/commands
      15:41:31 DEBUG i.g.i.p.o.s.ExampleServiceImpl - No matching examples found in specification for POST http://127.0.0.1:8080/_plugins/_command_manager/commands and status code 200
      15:41:31 WARN  i.g.i.p.o.OpenApiPluginImpl - No example match found in OpenAPI specification for POST http://127.0.0.1:8080/_plugins/_command_manager/commands with status code 200 and no response file or content set - sending empty response

Note

To receive the request in imposter we have to modify the setting that defines the Command Manager URL settings/PluginSettings.java: Line 108 to return the the hardcoded value http://127.0.0.1:8080, this is because the API URL is obtained using the Cluster service.

Issues Resolved

Closes #239

Add Command Manager related settings

Add ClusterInfoHelper utility class
@QU3B1M QU3B1M self-assigned this Feb 12, 2025
@QU3B1M QU3B1M linked an issue Feb 14, 2025 that may be closed by this pull request
@QU3B1M QU3B1M changed the base branch from main to 238-cti-client-prototype February 14, 2025 01:43
@QU3B1M QU3B1M changed the base branch from 238-cti-client-prototype to main February 14, 2025 01:44
Make HttpClient non-abstract

Remove dependencies licenses validation
Add docstrings to new classes

Remove unused functions
Add more docstrings

Remove legacy HttpClient
Add required headers to CommandManager command generation endpoint
@QU3B1M QU3B1M marked this pull request as ready for review February 14, 2025 16:02
@QU3B1M QU3B1M requested a review from a team as a code owner February 14, 2025 16:02
@QU3B1M QU3B1M requested a review from AlexRuiz7 February 14, 2025 16:16
Copy link
Member

@mcasas993 mcasas993 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.

  • Code review: ✔️
  • Test: ✔️

@AlexRuiz7 AlexRuiz7 merged commit cd71bf3 into main Feb 17, 2025
1 check passed
@AlexRuiz7 AlexRuiz7 deleted the enhancement/239-command-manager-client branch February 17, 2025 11:46
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.

Command Manager client prototype
4 participants