wazuh / wazuh-api

Wazuh - RESTful API
https://wazuh.com
GNU General Public License v2.0
69 stars 57 forks source link

New endpoint for getting a full summary of agents #429

Closed druizz90 closed 5 years ago

druizz90 commented 5 years ago

Hi team,

This PR closes #426. PR #428 is going to be closed because this new endpoint is needed for 3.10 version. Below there is an example of the new endpoint:

# curl -u foo:bar "localhost:55000/agents/full_summary?pretty"
{
   "error": 0,
   "data": {
      "unique_node_names": {
         "items": [
            {
               "node_name": "master",
               "count": 2
            },
            {
               "node_name": "worker-1",
               "count": 1
            },
            {
               "node_name": "worker-2",
               "count": 1
            }
         ],
         "totalItems": 4
      },
      "groups": {
         "items": [
            {
               "count": 2,
               "name": "default",
               "mergedSum": "ddda4e15b99efad1d3be7ae9d7ff14ff",
               "configSum": "ab73af41699f13fdd81903b5f23d8d00"
            },
            {
               "count": 1,
               "name": "dmz",
               "mergedSum": "dd77862c4a41ae1b3854d67143f3d3e4",
               "configSum": "ab73af41699f13fdd81903b5f23d8d00"
            },
            {
               "count": 0,
               "name": "testsagentconf",
               "mergedSum": "2acdb385658097abb9528aa5ec18c490",
               "configSum": "297b4cea942e0b7d2d9c59f9433e3e97"
            },
            {
               "count": 0,
               "name": "testsagentconf2",
               "mergedSum": "391ae29c1b0355c610f45bf133d5ea55",
               "configSum": "297b4cea942e0b7d2d9c59f9433e3e97"
            },
            {
               "count": 0,
               "name": "webserver",
               "configSum": "ab73af41699f13fdd81903b5f23d8d00"
            }
         ],
         "totalItems": 5
      },
      "unique_agent_os": {
         "items": [
            {
               "os": {
                  "name": "CentOS Linux",
                  "platform": "centos",
                  "version": "7.6"
               },
               "count": 3
            },
            {
               "os": {
                  "name": "CentOS Linux",
                  "platform": "centos",
                  "version": "7"
               },
               "count": 1
            }
         ],
         "totalItems": 4
      },
      "summary": {
         "Total": 4,
         "Active": 4,
         "Disconnected": 0,
         "Never connected": 0,
         "Pending": 0
      },
      "unique_agent_version": {
         "items": [
            {
               "count": 1,
               "version": "Wazuh v3.10.0"
            },
            {
               "count": 2,
               "version": "Wazuh v3.9.3"
            },
            {
               "count": 1,
               "version": "Wazuh v3.5.0"
            }
         ],
         "totalItems": 4
      },
      "last_registered_agent": {
         "os": {
            "arch": "x86_64",
            "codename": "Core",
            "major": "7",
            "name": "CentOS Linux",
            "platform": "centos",
            "uname": "Linux |08f2dc895a84 |5.2.7-200.fc30.x86_64 |#1 SMP Thu Aug 8 05:35:29 UTC 2019 |x86_64",
            "version": "7"
         },
         "id": "003",
         "configSum": "ab73af41699f13fdd81903b5f23d8d00",
         "ip": "172.18.0.7",
         "node_name": "worker-2",
         "version": "Wazuh v3.5.0",
         "status": "Active",
         "mergedSum": "ddda4e15b99efad1d3be7ae9d7ff14ff",
         "registerIP": "172.18.0.7",
         "dateAdd": "2019-08-13 08:01:23",
         "group": [
            "default"
         ],
         "manager": "0023fc1ecd6a",
         "name": "08f2dc895a84",
         "lastKeepAlive": "2019-08-13 08:04:00"
      }
   }
}

A mocha test was added too:

# mocha test/test_agents.js --timeout=10000

  Agents
    GET/agents
      ✓ Request (346ms)
      ✓ Pagination (401ms)
      ✓ Retrieve all elements with limit=0 (382ms)
      ✓ Sort (320ms)
      ✓ Wrong Sort (331ms)
      ✓ Search (338ms)
      ✓ Selector (336ms)
      ✓ Not allowed selector (349ms)
      ✓ Version (476ms)
      ✓ Os.platform (337ms)
      ✓ Os.version (378ms)
      ✓ ManagerHost (323ms)
      ✓ Filters: status (320ms)
      ✓ Filters: status 2 (342ms)
      ✓ Filters: Invalid filter
      ✓ Filters: Invalid filter - Extra field
      ✓ Filters: older_than (330ms)
      ✓ Filters: group (323ms)
      ✓ Select: single field (327ms)
      ✓ Select: multiple fields (345ms)
      ✓ Select: wrong field (343ms)
      ✓ Select: invalid character
      ✓ Filters: query (326ms)
    GET/agents/summary
      ✓ Request (362ms)
    GET/agents/full_summary
      ✓ Request (339ms)
    GET/agents/summary/os
      ✓ Request (324ms)
    GET/agents/outdated
      ✓ Request (305ms)
    GET/agents/:agent_id
      ✓ Request (manager) (323ms)
      ✓ Request (agent) (354ms)
      ✓ Selector (339ms)
      ✓ Not allowed selector (319ms)
      ✓ Params: Bad agent id
      ✓ Errors: No agent (354ms)
      ✓ Select (347ms)
      ✓ Select: wrong field (340ms)
    GET/agents/name/:agent_name
      ✓ Request (320ms)
      ✓ Wrong name (314ms)
      ✓ Selector (319ms)
      ✓ Not allowed selector (393ms)
    GET/agents/:agent_id/key
      ✓ Request (354ms)
      ✓ Params: Bad agent id
      ✓ Errors: No key (382ms)
    PUT/agents/groups/:group_id
      ✓ Request (336ms)
      ✓ Params: Bad group name
      ✓ Params: Group already exists (319ms)
    PUT/agents/:agent_id/group/:group_id
      ✓ Request (374ms)
      ✓ Params: Bad agent name
      ✓ Params: Agent does not exist (324ms)
      ✓ Params: Replace parameter (353ms)
    POST/agents/groups/:group_id/files/:file_name
      ✓ Request (331ms)
      ✓ ErrorOnBadGroup (320ms)
      ✓ ErrorOnEmptyConf
      ✓ OnlyAgentConfAllowed (367ms)
      ✓ InvalidConfDetected
      ✓ WrongConfDetected (336ms)
      ✓ TooBigXML
    GET/agents/no_group
      ✓ Request (340ms)
      ✓ Pagination (320ms)
      ✓ Retrieve all elements with limit=0 (351ms)
      ✓ Sort (324ms)
      ✓ Search (324ms)
      ✓ Select (337ms)
      ✓ Wrong select (391ms)
      ✓ Filter: status (473ms)
    GET/agents/groups
      ✓ Request (471ms)
      ✓ Retrieve all elements with limit=0 (328ms)
      ✓ Hash algorithm (323ms)
      ✓ Wrong Hash algorithm (442ms)
    GET/agents/groups/:group_id
      ✓ Request (409ms)
      ✓ Params: Bad group name
      ✓ Retrieve all elements with limit=0 (394ms)
      ✓ Select (386ms)
      ✓ Filter: status (378ms)
    GET/agents/groups/:group_id/configuration
      ✓ Request (389ms)
      ✓ Params: Bad group name
      ✓ Retrieve all elements with limit=0 (609ms)
    GET/agents/groups/:group_id/files
      ✓ Request (1290ms)
      ✓ Params: Bad group name
      ✓ Retrieve all elements with limit=0 (648ms)
      ✓ Hash algorithm (828ms)
      ✓ Wrong Hash algorithm (372ms)
    GET/agents/groups/:group_id/files/:filename
      ✓ Request (325ms)
      ✓ UsingFormatAgentConfXML (311ms)
      ✓ UsingFormatAgentConfJSON (317ms)
      ✓ UsingFormatRootcheckXML (317ms)
      ✓ UsingFormatRootcheckJSON (378ms)
      ✓ Params: Bad group name
    POST/agents/groups/:group_id/configuration
      ✓ Request (469ms)
      ✓ ErrorOnBadGroup (397ms)
      ✓ ErrorOnEmptyConf
      ✓ InvalidConfDetected
      ✓ WrongConfDetected (396ms)
      ✓ TooBigXML
    DELETE/agents/:agent_id/group
      ✓ Request (306ms)
      ✓ Errors: ID is not present (399ms)
      ✓ Params: Bad agent id
    DELETE/agents/:agent_id/group/:group_id
      ✓ Request (348ms)
      ✓ Errors: ID is not present (402ms)
      ✓ Errors: Group is not present (441ms)
      ✓ Params: Bad agent id
      ✓ Params: Bad group id (579ms)
    DELETE/agents/groups/:group_id
      ✓ Request (714ms)
      ✓ Params: Bad group id (39ms)
    DELETE/agents
      ✓ Request
      ✓ Filter: older_than, status and ids (4049ms)
      ✓ Errors: Get deleted agent (493ms)
      ✓ Filter: older_than (390ms)
    GET/agents/stats/distinct
      ✓ Request (318ms)
      ✓ Pagination (301ms)
      ✓ Retrieve all elements with limit=0 (310ms)
      ✓ Sort (327ms)
      ✓ Search (306ms)
      ✓ Select (311ms)
      ✓ Wrong select (300ms)
    GET/agents/:agent/config/:component/:configuration
      ✓ Request-Agent-Client (343ms)
      ✓ Request-Agent-Buffer (372ms)
      ✓ Request-Agent-Labels (330ms)
      ✓ Request-Agent-Internal (349ms)
      ✓ Request-Agentless-Agentless (458ms)
      ✓ Request-Analysis-Global (343ms)
      ✓ Request-Analysis-Active-response (490ms)
      ✓ Request-Analysis-Alerts (696ms)
      ✓ Request-Analysis-Command (722ms)
      ✓ Request-Analysis-Internal (596ms)
      ✓ Request-Auth-Auth (598ms)
      ✓ Request-Com-Active-response (601ms)
      ✓ Request-Com-Internal (507ms)
      ✓ Request-Csyslog-Csyslog (395ms)
      ✓ Request-Integrator-Integration (366ms)
      ✓ Request-Logcollector-Localfile (322ms)
      ✓ Request-Logcollector-Socket (384ms)
      ✓ Request-Logcollector-Internal (383ms)
      ✓ Request-Mail-Global (383ms)
      ✓ Request-Mail-Alerts (441ms)
      ✓ Request-Mail-Internal (508ms)
      ✓ Request-Monitor-Internal (408ms)
      ✓ Request-Request-Remote (535ms)
      ✓ Request-Request-Internal (466ms)
      ✓ Request-Syscheck-Syscheck (605ms)
      ✓ Request-Syscheck-Rootcheck (452ms)
      ✓ Request-Syscheck-Internal (364ms)
      ✓ Request-Wmodules-Wmodules (426ms)
    PUT/agents/restart
      ✓ Request (386ms)
    PUT/agents/:agent_id/restart
      ✓ Request (362ms)
      ✓ Params: Bad agent id
      ✓ Request (377ms)
    POST/agents/restart
      ✓ Request (373ms)
      ✓ Params: A good id and a bad one (394ms)
      ✓ Params: Bad agent id
      ✓ Request (388ms)

  150 passing (59s)

Best regards,

Demetrio.

davidjiglesias commented 5 years ago

Updated API response:

root@1a683501f77f:/# curl -u foo:bar "http://localhost:55000/summary/agents?pretty"
{
   "error": 0,
   "data": {
      "nodes": {
         "items": [
            {
               "count": 1,
               "node_name": "worker1"
            },
            {
               "count": 1,
               "node_name": "worker2"
            },
            {
               "count": 4,
               "node_name": "master-node"
            }
         ],
         "totalItems": 6
      },
      "groups": {
         "items": [
            {
               "count": 5,
               "name": "default",
               "mergedSum": "8cf9a9972b793548a1bb6fbb00e58af6",
               "configSum": "ab73af41699f13fdd81903b5f23d8d00"
            }
         ],
         "totalItems": 1
      },
      "agent_os": {
         "items": [
            {
               "os": {
                  "name": "Ubuntu",
                  "platform": "ubuntu",
                  "version": "18.04.2 LTS"
               },
               "count": 6
            }
         ],
         "totalItems": 6
      },
      "agent_status": {
         "Total": 6,
         "Active": 6,
         "Disconnected": 0,
         "Never connected": 0,
         "Pending": 0
      },
      "agent_version": {
         "items": [
            {
               "version": "Wazuh v3.10.0",
               "count": 1
            },
            {
               "version": "Wazuh v3.9.5",
               "count": 5
            }
         ],
         "totalItems": 6
      },
      "last_registered_agent": {
         "os": {
            "arch": "x86_64",
            "codename": "Bionic Beaver",
            "major": "18",
            "minor": "04",
            "name": "Ubuntu",
            "platform": "ubuntu",
            "uname": "Linux |ee7d4f51c0ae |4.18.0-16-generic |#17~18.04.1-Ubuntu SMP Tue Feb 12 13:35:51 UTC 2019 |x86_64",
            "version": "18.04.2 LTS"
         },
         "version": "Wazuh v3.9.5",
         "dateAdd": "2019-08-20 11:42:14",
         "node_name": "master-node",
         "status": "Active",
         "group": [
            "default"
         ],
         "mergedSum": "8cf9a9972b793548a1bb6fbb00e58af6",
         "name": "ee7d4f51c0ae",
         "lastKeepAlive": "2019-08-20 11:48:20",
         "configSum": "ab73af41699f13fdd81903b5f23d8d00",
         "registerIP": "any",
         "ip": "172.20.0.8",
         "id": "005",
         "manager": "1a683501f77f"
      }
   }
}