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

Error updating alert artifact #195

Closed
egorborisow opened this issue Nov 10, 2020 · 1 comment · Fixed by #196
Closed

Error updating alert artifact #195

egorborisow opened this issue Nov 10, 2020 · 1 comment · Fixed by #196

Comments

@egorborisow
Copy link

Request Type

Bug

Work Environment

Question Answer
OS version (server) Ubuntu
OS version (client) Windows
TheHive4py version / git hash thehive4py==1.7.2

Problem Description

Error updating alert artifact:

thehive_api = TheHiveApi()
thehive_api.update_alert(alert_id=alert_id, alert=Alert(**alert), fields=['artifacts'])

--------------
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type AlertArtifact is not JSON serializable


Possible Solutions

#api.py

def update_alert(self, alert_id, alert, fields=[]):

    req = self.url + "/api/alert/{}".format(alert_id)

    # update only the alert attributes that are not read-only
    update_keys = ['tlp', 'severity', 'tags', 'caseTemplate', 'title', 'description', 'customFields']

    if len(fields) > 0:
        data = {k: v for k, v in alert.__dict__.items() if k in fields}
    else:
        data = {k: v for k, v in alert.__dict__.items() if k in update_keys}
-   if hasattr(data, 'artifacts'):
+   if hasattr(alert, 'artifacts'):
        data['artifacts'] = [a.__dict__ for a in alert.artifacts]
    try:
        return requests.patch(req, headers={'Content-Type': 'application/json'}, json=data, proxies=self.proxies, auth=self.auth, verify=self.cert)
    except requests.exceptions.RequestException as e:
        raise AlertException("Alert update error: {}".format(e))
@Kamforka
Copy link
Collaborator

I also encountered in the same error today. However I disagree with the suggested solution by @efscratch .

I'd suggest the following:

-   if hasattr(data, 'artifacts'):
+   if 'artifacts' in data:
        data['artifacts'] = [a.__dict__ for a in alert.artifacts]

The reason is that the original fix would introduce a bug as the artifacts field was patched even if it wasn't specified in the fields argument.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants