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

Fix commands manager arguments parsing #245

Merged
merged 5 commits into from
Jan 24, 2025

Conversation

mcasas993
Copy link
Member

@mcasas993 mcasas993 commented Jan 23, 2025

Description

Change the parsing of command.acttion.args so it can receive nested objects.

Issues Resolved

244

@mcasas993 mcasas993 requested a review from a team as a code owner January 23, 2025 18:38
@mcasas993 mcasas993 self-assigned this Jan 23, 2025
@mcasas993 mcasas993 linked an issue Jan 23, 2025 that may be closed by this pull request
@mcasas993
Copy link
Member Author

mcasas993 commented Jan 23, 2025

Tested, it works with:

  • Fields with numbers and text values
 "args": {
          "do70": 7.5,
          "amet__": 8,
          "arg1": "/path/to/executable/arg6"
        }
  • List with text and number and other arguments with text values
"args": {
          "groups": [
            "group1",
            "group2",
            8
          ],
          "other": "xxx"
        }
  • Empty list
"args": {
          "arg1": "/path/to/executable/arg6",
          "groups": [
            ""
          ]
        }
  • Empty arguments
    "args": {},

@QU3B1M
Copy link
Member

QU3B1M commented Jan 24, 2025

The API raises an error when the field args has a nested object inside of it

  • Request
    {
      "commands": [
        {
          "source": "Engine",
          "user": "user53",
          "target": {
            "id": "target4",
            "type": "agent"
          },
          "action": {
            "name": "restart",
            "args": {
              "magna_e85": {
                "nested": "ex eiusmod"
              },
              "arg1": "/path/to/executable/arg6"
            },
            "version": "v4"
          },
          "timeout": 30
        }
      ]
    }
  • Response
    {
      "error": {
        "root_cause": [
          {
            "type": "illegal_argument_exception",
            "reason": "Missing arguments: [timeout]"
          }
        ],
        "type": "illegal_argument_exception",
        "reason": "Missing arguments: [timeout]"
      },
      "status": 400
    }

The rest of scenarios were successful

  • Args with a list of objects
    • Request
        {
          "commands": [
            {
              "source": "Engine",
              "user": "user53",
              "target": {
                "id": "target4",
                "type": "agent"
              },
              "action": {
                "name": "restart",
                "args": {
                  "magna_e85": [
                    {
                      "nested": "ex eiusmod"
                    }
                  ],
                  "arg1": "/path/to/executable/arg6"
                },
                "version": "v4"
              },
              "timeout": 30
            }
          ]
        }
    • Response
      {
        "_index": ".commands",
        "_documents": [
          {
            "_id": "kqz3l5QBbMoYWG1Lg7_n"
          }
        ],
        "result": "OK"
      }
  • Args with an empty list
    • Request
        {
          "commands": [
            {
              "source": "Engine",
              "user": "user53",
              "target": {
                "id": "target4",
                "type": "agent"
              },
              "action": {
                "name": "restart",
                "args": {
                  "magna_e85": [ ],
                  "arg1": "/path/to/executable/arg6"
                },
                "version": "v4"
              },
              "timeout": 30
            }
          ]
        }
    • Response
      {
        "_index": ".commands",
        "_documents": [
          {
            "_id": "laz5l5QBbMoYWG1LUb-5"
          }
        ],
        "result": "OK"
      }
  • Args with a list of lists
    • Request
      {
        "commands": [
          {
            "source": "Engine",
            "user": "user53",
            "target": {
              "id": "target4",
              "type": "agent"
            },
            "action": {
              "name": "restart",
              "args": {
                "magna_e85": [
                  [
                    "string"
                  ],
                  [
                    "other"
                  ]
                ],
                "arg1": "/path/to/executable/arg6"
              },
              "version": "v4"
            },
            "timeout": 30
          }
        ]
      }
    - Response
        ```JSON
        {
          "_index": ".commands",
          "_documents": [
            {
              "_id": "nqz6l5QBbMoYWG1Lw78l"
            }
          ],
          "result": "OK"
        }
    

(apart from the already tested scenarios on the comment #245 (comment)

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.

Nested objects scenario failing, details on comment #245 (comment)

@AlexRuiz7
Copy link
Member

Last commit implements the parsing of nested objects within commands.action.args. For example, sending the following command to the API:

{
    "commands": [
        {
            "source": "Users/Services",
            "user": "Management API",
            "target": {
                "id": "0a96a0ab-5bef-415c-bb3c-ea3e294215a0",
                "type": "agent"
            },
            "action": {
                "name": "set-group",
                "version": "5.0.0",
                "args": {
                    "groups": [
                        "group_1",
                        "group_2",
                        "group_3"
                    ],
                    "type": {"1": "group_1"}
                }
            },
            "timeout": 100
        }
    ]
}

returns

{
  "_index": ".commands",
  "_documents": [
    {
      "_id": "DHJEmJQBLxruv0ftQq7p"
    }
  ],
  "result": "OK"
}

and the document is correctly indexed:

{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": ".commands",
        "_id": "BnJBmJQBLxruv0ft8q7f",
        "_score": 1,
        "_source": {
          "agent": {
            "groups": [
              "groups000"
            ]
          },
          "command": {
            "source": "Users/Services",
            "user": "Management API",
            "target": {
              "type": "agent",
              "id": "0a96a0ab-5bef-415c-bb3c-ea3e294215a0"
            },
            "action": {
              "name": "set-group",
              "args": {
                "type": {
                  "1": "group_1"
                },
                "groups": [
                  "group_1",
                  "group_2",
                  "group_3"
                ]
              },
              "version": "5.0.0"
            },
            "timeout": 100,
            "status": "pending",
            "order_id": "BXJBmJQBLxruv0ft8q7e",
            "request_id": "BHJBmJQBLxruv0ft8q7e"
          },
          "@timestamp": "2025-01-24T12:20:46Z",
          "delivery_timestamp": "2025-01-24T12:22:26Z"
        }
      }
    ]
  }
}

The command above is not a valid command, but for now we want to accept anything within commands.action.args to ease the development.

Valid commands are also accepted:

set-group

{
    "commands": [
        {
            "source": "Users/Services",
            "user": "Management API",
            "target": {
                "id": "0a96a0ab-5bef-415c-bb3c-ea3e294215a0",
                "type": "agent"
            },
            "action": {
                "name": "set-group",
                "version": "5.0.0",
                "args": {
                    "groups": [
                        "group_1",
                        "group_2",
                        "group_3"
                    ]
                }
            },
            "timeout": 100
        }
    ]
}

fetch-config

{
    "commands": [
        {
            "source": "Users/Services",
            "user": "Management API",
            "target": {
                "id": "0a96a0ab-5bef-415c-bb3c-ea3e294215a0",
                "type": "agent"
            },
            "action": {
                "name": "set-group",
                "version": "5.0.0",
                "args": {}
            },
            "timeout": 100
        }
    ]
}

@mcasas993 mcasas993 requested a review from QU3B1M January 24, 2025 12:32
@QU3B1M
Copy link
Member

QU3B1M commented Jan 24, 2025

The API handles correctly the nested args, even with multiple layers of nesting

Request

{
  "commands": [
    {
      "source": "Users/Services",
      "user": "Management API",
      "target": {
        "id": "0a96a0ab-5bef-415c-bb3c-ea3e294215a0",
        "type": "agent"
      },
      "action": {
        "name": "set-group",
        "version": "5.0.0",
        "args": {
          "nested1": {
            "1": "test",
            "nested2": {
              "2": "test"
            }
          }
        }
      },
      "timeout": 100
    }
  ]
}

Response

{
  "_index": ".commands",
  "_documents": [
    {
      "_id": "hJpWmJQBk1YoO7gM2krJ"
    }
  ],
  "result": "OK"
}

Document indexed on the cluster

% curl http://127.0.0.1:9200/.commands/_search                
{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": ".commands",
                "_id": "hJpWmJQBk1YoO7gM2krJ",
                "_score": 1.0,
                "_source": {
                    "agent": {
                        "groups": [
                            "groups000"
                        ]
                    },
                    "command": {
                        "source": "Users/Services",
                        "user": "Management API",
                        "target": {
                            "type": "agent",
                            "id": "0a96a0ab-5bef-415c-bb3c-ea3e294215a0"
                        },
                        "action": {
                            "name": "set-group",
                            "args": {
                                "nested1": {
                                    "1": "test",
                                    "nested2": {
                                        "2": "test"
                                    }
                                }
                            },
                            "version": "5.0.0"
                        },
                        "timeout": 100,
                        "status": "pending",
                        "order_id": "g5pWmJQBk1YoO7gM2krH",
                        "request_id": "gppWmJQBk1YoO7gM2krH"
                    },
                    "@timestamp": "2025-01-24T12:43:36Z",
                    "delivery_timestamp": "2025-01-24T12:45:16Z"
                }
            }
        ]
    }
}

@AlexRuiz7 AlexRuiz7 merged commit 9fe1cff into master Jan 24, 2025
1 check passed
@AlexRuiz7 AlexRuiz7 deleted the 652-change-commands-manager-parsing branch January 24, 2025 12:53
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.

Error on commands manager arguments parsing
3 participants