MISP / PyMISP

Python library using the MISP Rest API
Other
444 stars 279 forks source link

Examples produce non-compliant JSON, example: edit-user.py #406

Closed SteveClement closed 3 years ago

SteveClement commented 5 years ago

Is it wanted that the examples return a Python dictionary?

(doing a json.dumps() produces 'correct' JSON)

PoC:

(MISP)  ~/MISP_Main/PyMISP/examples  $ ./edit_user.py -i 1 > user_1.json
(MISP)  ~/MISP_Main/PyMISP/examples  $ cat user_1.json|jq
parse error: Invalid numeric literal at line 1, column 8
# jq failing
# converting: ' ->  " -- None -> null -- True/False -> true/false
(MISP)  ~/MISP_Main/PyMISP/examples  $ cat user_1.json|jq
{
  "User": {
    "id": "1",
    "password": "******",
    "org_id": "1",
    "server_id": "0",
    "email": "info@admin.test",
    "autoalert": false,
    "authkey": "7fwRwxDmAKNsFE7WXXdoEeM2f3iXOJurYqoCD78d",
    "invited_by": "0",
    "gpgkey": null,
    "certif_public": "",
    "nids_sid": "4000000",
    "termsaccepted": false,
    "newsread": "0",
    "role_id": "1",
    "change_pw": "0",
    "contactalert": false,
    "disabled": false,
    "expiration": null,
    "current_login": "1560733179",
    "last_login": "1560572358",
    "force_logout": false,
    "date_created": null,
    "date_modified": "1549254149"
  }
}
Rafiot commented 5 years ago

Hmmm, I'll normalize the examples: it returns a python dictionary.

ITServ-DE commented 5 years ago

I confirm this mis-behavior described by @SteveClement and can reproduce it.

@Rafiot , I'm not sure if by saying "you'll normalize the output" you mean you will do something to fix it. It would be nice if you add a Bug tag to this issue if you think this is a bug (I do). Thank you.

ITServ-DE commented 5 years ago

And, by the way: the issue's subject is wrong.

Most (all?) examples return valid json. The problem seems only be associated with the edit_user.py example

The correct issue subject would be "Example edit-user.py produces non-compliant JSON"

ITServ-DE commented 5 years ago

Aaarg. The problem is in the example itself, i will post a fix in a few minutes.

To hot-fix: 1) add import json at the begining of the file 2) replace the last line by print(json.dumps(misp.edit_user(args.user_id, email=args.email)))

Rafiot commented 5 years ago

It meant I'll do something about it when I get the time. It wasn't the case until now, and it has very low priority.

I'll gladly merge your PR.

ITServ-DE commented 5 years ago

Rafiot,

I just started to look at all the examples for more json-output fixes.

There is one parameter in PyMISP that I don't understand. What's the purpose of pythonify?

And what's the logic behind it's default setting for various methods, meaning: why is pythonify=True in the method definition for add_event, but pythonify=False in add_object_reference?

Rafiot commented 5 years ago

when you pass pythonify=True to a method, it forces it to return a python object (MISPEvent. MISPAttribute, MISPObjectReference, ...)

The default should be pythonify=False, and I simply forgot to change it at a few places.

Rafiot commented 5 years ago

pythonify is normalized => https://github.com/MISP/PyMISP/commit/a6a0fcd4fbf42e1fcc2ee333ac6c61dd8e9695aa

And if you want to always pythonify the outputs, you can toggle it globally.