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

Force option enabled by default when inserting agents #318

Closed
mgmacias95 opened this issue Feb 18, 2019 · 4 comments
Closed

Force option enabled by default when inserting agents #318

mgmacias95 opened this issue Feb 18, 2019 · 4 comments
Assignees
Labels

Comments

@mgmacias95
Copy link
Contributor

Hello team,

The /agents/insert API call is not raising an exception when an agent with the same ID is already present:

# curl -u foo:bar -k -X POST -d '{"name":"NewAgentPostInsert", "ip":"any", "id":"750", "key":"1abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghi64"}' -H 'Content-Type:application/json' "https://127.0.0.1:55000/agents/insert?pretty"
{
   "error": 0,
   "data": {
      "id": "750",
      "key": "NzUwIE5ld0FnZW50UG9zdEluc2VydCBhbnkgMWFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXphYmNkZWZnaGk2NA=="
   }
}
# curl -u foo:bar -k -X POST -d '{"name":"NewAgentPostInsert", "ip":"any", "id":"750", "key":"1abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghi64"}' -H 'Content-Type:application/json' "https://127.0.0.1:55000/agents/insert?pretty"{
   "error": 0,
   "data": {
      "id": "750",
      "key": "NzUwIE5ld0FnZW50UG9zdEluc2VydCBhbnkgMWFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXphYmNkZWZnaGk2NA=="
   }
}
# curl -u foo:bar -k -X POST -d '{"name":"NewAgentPostInsert", "ip":"any", "id":"750", "key":"1abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghi64"}' -H 'Content-Type:application/json' "https://127.0.0.1:55000/agents/insert?pretty"{
   "error": 0,
   "data": {
      "id": "750",
      "key": "NzUwIE5ld0FnZW50UG9zdEluc2VydCBhbnkgMWFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXphYmNkZWZnaGk2NA=="
   }
}

The issue can be fixed setting force=0 instead of force=-1 in agent.py file:

    def insert_agent(name, id, key, ip='any', force=0):

Best regards,
Marta

@davidjiglesias
Copy link
Member

davidjiglesias commented May 8, 2019

Fixed insert_agent in agent.py

Tested insert:

root@c4e0a50a28a0:/wazuh-api# curl -u foo:bar -k -X POST -d '{"name":"NewAgentPostInsert", "ip":"any", "id":"750", "key":"1abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghi64"}' -H 'Content-Type:application/json' "http://127.0.0.1:55000/agents/insert?pretty"
{
   "error": 0,
   "data": {
      "id": "750",
      "key": "NzUwIE5ld0FnZW50UG9zdEluc2VydCBhbnkgMWFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXphYmNkZWZnaGk2NA=="
   }
}
root@c4e0a50a28a0:/wazuh-api# curl -u foo:bar -k -X POST -d '{"name":"NewAgentPostInsert", "ip":"any", "id":"750", "key":"1abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghi64"}' -H 'Content-Type:application/json' "http://127.0.0.1:55000/agents/insert?pretty"
{
   "error": 9012,
   "message": "Duplicated ID"
}

If we use force=1 the insert is forced:

root@c4e0a50a28a0:/wazuh-api# curl -u foo:bar -k -X POST -d '{"name":"NewAgentPostInsert", "ip":"any", "id":"750", "key":"1abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghi64", "force":1}' -H 'Content-Type:application/json' "http://127.0.0.1:55000/agents/insert?pretty"
{
   "error": 0,
   "data": {
      "id": "750",
      "key": "NzUwIE5ld0FnZW50UG9zdEluc2VydCBhbnkgMWFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXphYmNkZWZnaGk2NA=="
   }
}

@druizz90
Copy link
Contributor

druizz90 commented Jun 3, 2019

Changes required causes that it was possible to add new agents with a name or IP already present:

# mocha test/test_agents_2.js


  Agents
    PUT/agents/:agent_name
       Request (345ms)Errors: Name already present (290ms)Params: Bad agent name
    DELETE/agents/:agent_id
       Request (288ms)Errors: ID is not present (291ms)Params: Bad agent id
    POST/agents
      Any
         Request (287ms)
         Check key (271ms)Errors: Name already present (256ms)Filters: Missing field nameFilters: Invalid field
      IP AutomaticRequest: Automatic IP (497ms)Errors: Duplicated IP (399ms)
      IP
         Request (479ms)Filters: Bad IPFilters: Bad IP 2
    POST/agents/insert
      Any
         Request (432ms)
        1) Errors: Name already present
        2) Errors: ID already presentErrors: Invalid key (312ms)Filters: Missing fieldsFilters: Invalid field
      IP AutomaticRequest: Automatic IP (342ms)
        3) Errors: Duplicated IP
      IP
         Request (283ms)Filters: Bad IPFilters: Bad IP 2


  24 passing (11s)
  3 failing

  1) Agents
       POST/agents/insert
         Any
           Errors: Name already present:
     Uncaught AssertionError: expected Object {
  error: 0,
  data: Object {
    id: '751',
    key: 'NzUxIE5ld0FnZW50UG9zdEluc2VydCBhbnkgMWFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXphYmNkZWZnaGk2NA=='
  }
} to have property message
      at Assertion.fail (node_modules/should/cjs/should.js:258:17)
      at Assertion.value [as properties] (node_modules/should/cjs/should.js:335:19)
      at Test.<anonymous> (test/test_agents_2.js:484:42)
      at Test.assert (node_modules/supertest/lib/test.js:179:6)
      at assert (node_modules/supertest/lib/test.js:131:12)
      at /shared/wazuh-api/node_modules/supertest/lib/test.js:128:5
      at Test.Request.callback (node_modules/superagent/lib/node/index.js:619:12)
      at /shared/wazuh-api/node_modules/superagent/lib/node/index.js:795:18
      at IncomingMessage.<anonymous> (node_modules/superagent/lib/node/parsers/json.js:16:7)
      at endReadableNT (_stream_readable.js:1064:12)
      at _combinedTickCallback (internal/process/next_tick.js:139:11)
      at process._tickCallback (internal/process/next_tick.js:181:9)

  2) Agents
       POST/agents/insert
         Any
           Errors: ID already present:
     Uncaught AssertionError: expected Object {
  error: 0,
  data: Object {
    id: '750',
    key: 'NzUwIE5ld0FnZW50UG9zdEluc2VydCBhbnkgMWFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXphYmNkZWZnaGk2NA=='
  }
} to have property message
      at Assertion.fail (node_modules/should/cjs/should.js:258:17)
      at Assertion.value [as properties] (node_modules/should/cjs/should.js:335:19)
      at Test.<anonymous> (test/test_agents_2.js:503:42)
      at Test.assert (node_modules/supertest/lib/test.js:179:6)
      at assert (node_modules/supertest/lib/test.js:131:12)
      at /shared/wazuh-api/node_modules/supertest/lib/test.js:128:5
      at Test.Request.callback (node_modules/superagent/lib/node/index.js:619:12)
      at /shared/wazuh-api/node_modules/superagent/lib/node/index.js:795:18
      at IncomingMessage.<anonymous> (node_modules/superagent/lib/node/parsers/json.js:16:7)
      at endReadableNT (_stream_readable.js:1064:12)
      at _combinedTickCallback (internal/process/next_tick.js:139:11)
      at process._tickCallback (internal/process/next_tick.js:181:9)

  3) Agents
       POST/agents/insert
         IP Automatic
           Errors: Duplicated IP:
     Uncaught AssertionError: expected Object {
  error: 0,
  data: Object {
    id: '756',
    key: 'NzU2IE5ld0FnZW50UG9zdEluc2VydDMgMTI3LjAuMC4xIDFhYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5emFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6YWJjZGVmZ2hpNjQ='
  }
} to have property message
      at Assertion.fail (node_modules/should/cjs/should.js:258:17)
      at Assertion.value [as properties] (node_modules/should/cjs/should.js:335:19)
      at Test.<anonymous> (test/test_agents_2.js:615:42)
      at Test.assert (node_modules/supertest/lib/test.js:179:6)
      at assert (node_modules/supertest/lib/test.js:131:12)
      at /shared/wazuh-api/node_modules/supertest/lib/test.js:128:5
      at Test.Request.callback (node_modules/superagent/lib/node/index.js:619:12)
      at /shared/wazuh-api/node_modules/superagent/lib/node/index.js:795:18
      at IncomingMessage.<anonymous> (node_modules/superagent/lib/node/parsers/json.js:16:7)
      at endReadableNT (_stream_readable.js:1064:12)
      at _combinedTickCallback (internal/process/next_tick.js:139:11)
      at process._tickCallback (internal/process/next_tick.js:181:9)

@crd1985
Copy link
Contributor

crd1985 commented Jun 10, 2019

Reopen this issue due to collateral effects.

Will close again when we reach a good solution.

@crd1985
Copy link
Contributor

crd1985 commented Jun 18, 2019

This issue will be fixed in wazuh/wazuh#3527.

@crd1985 crd1985 closed this as completed Jun 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants