Skip to content

Webhook receiver - #2049

Closed
matmair wants to merge 20 commits into
inventree:masterfrom
matmair:webhook-2036
Closed

matmair wants to merge 20 commits into
inventree:masterfrom
matmair:webhook-2036

Conversation

@matmair

@matmair matmair commented Sep 12, 2021 •

Copy link
Copy Markdown
Member

Closes #2036

This PR adds a Webhook receiver under the /api/webhook/<endpoint-id>/ URL. The view is designed to be inherited so each step is split into actionable functions. While there is the possibility for the (admin) user to create webhook-endpoints this is in preparation for integrating 3rd party software via plugins.

@matmair
matmair marked this pull request as draft September 12, 2021 22:19
@matmair
matmair marked this pull request as ready for review September 13, 2021 05:46
@matmair matmair added api Relates to the API plugin Plugin ecosystem labels Sep 13, 2021
@SchrodingersGat SchrodingersGat added this to the 0.6.0 milestone Sep 13, 2021
@SchrodingersGat

Copy link
Copy Markdown
Member

@matmair could you provide a little bit more background on how this webhook architecture would be used? Is this intended to be used directly for third-party integration?

I'm afraid I've fallen behind on my reading of the related issues, sorry if I'm asking you to reiterate this.

@matmair

matmair commented Sep 13, 2021

Copy link
Copy Markdown
Member Author

@SchrodingersGat no problem. This is one of the building blocks for #1421 - 3rd party integration.

I will give you an example: I currently develop a IntegrationPlugin that connects InvenTree with Shopify.

On startup the plugin checks that it has deployed a webhook-receiver for getting notifications from Shopify. Then it registers the random URL for this endpoint with Shopify's webhook ressource for a couple of events. So each time a order gets created in Shopify a JSON message with all information will be received on the endpoint. The plugin then gets fed this information and uses is to create a SO in InvenTree.

The webhook receiver supports inheritance so you only need 4 loc to implement one in a plugin. Because of its usage of the database to define those you can have multiple webhooks per plugin and action as some 3rd parties might require that.

@matmair

matmair commented Sep 13, 2021

Copy link
Copy Markdown
Member Author

The sample Plugin should be PR ready in a few days so you can try out all pieces working together.

Comment thread InvenTree/common/api.py Resolved
@eeintech

Copy link
Copy Markdown
Contributor

@matmair Wondering how to test the webhooks are functional? I've created an end-point but using the DRF form I'm not able to post a message into that webhook:

image

@matmair

matmair commented Sep 13, 2021

Copy link
Copy Markdown
Member Author

@eeintech the unit tests in common/test.py show best how it works. You need to log into the admin site and create an endpoint.

Then there are three methods of Authentication:

  • If no token is set the webhook just accepts everything
  • If a token is set the token needs to be supplied as a header named 'Token'
  • If a secret is set you need to supply a header named 'Token' with a valid HMAC calculated with the secret and the payload

I personally use postman to test the webhooks.

@eeintech

Copy link
Copy Markdown
Contributor

I'm not sure why the DRF form is not working but it seems straightforward otherwise.

You need to log into the admin site and create an endpoint.

Then there are three methods of Authentication:

  • If no token is set the webhook just accepts everything
  • If a token is set the token needs to be supplied as a header named 'Token'
  • If a secret is set you need to supply a header named 'Token' with a valid HMAC calculated with the secret and the payload

All good information that need to go into the doc 😉

@matmair

matmair commented Sep 13, 2021

Copy link
Copy Markdown
Member Author

@eeintech my idea would be to hide the whole function from admins because this only safely works if you know what you are doing. With a badly made Plugin one could do arbitrary code execution via a webhook.

@eeintech

Copy link
Copy Markdown
Contributor

@matmair Oh okay so what you are saying is to have hardcoded webhooks instead? Wouldn't it be more flexible to be able to add them dynamically?

@matmair

matmair commented Sep 13, 2021

Copy link
Copy Markdown
Member Author

@matmair Oh okay so what you are saying is to have hardcoded webhooks instead? Wouldn't it be more flexible to be able to add them dynamically?

My idea would be to remove the adding function on this specific admin page.

@matmair

matmair commented Sep 15, 2021

Copy link
Copy Markdown
Member Author

I'm not sure why the DRF form is not working but it seems straightforward otherwise.

You need to log into the admin site and create an endpoint.

Then there are three methods of Authentication:

  • If no token is set the webhook just accepts everything
  • If a token is set the token needs to be supplied as a header named 'Token'
  • If a secret is set you need to supply a header named 'Token' with a valid HMAC calculated with the secret and the payload

All good information that need to go into the doc 😉

@eeintech did you get it to run btw?

@eeintech

Copy link
Copy Markdown
Contributor

@matmair Nope but haven't tried harder though

@matmair matmair mentioned this pull request Sep 23, 2021
@SchrodingersGat

Copy link
Copy Markdown
Member

@matmair which PR should I look at first, this one or #2074?

@matmair

matmair commented Oct 14, 2021

Copy link
Copy Markdown
Member Author

@matmair which PR should I look at first, this one or #2074?

@SchrodingersGat please this one first - I discovered some security concerns with the plugin system tonight that I am working on addressing.

@matmair

matmair commented Oct 19, 2021

Copy link
Copy Markdown
Member Author

@SchrodingersGat this is included and improved in #2074

@matmair matmair closed this Oct 19, 2021
@matmair
matmair deleted the webhook-2036 branch October 19, 2021 20:09
@SchrodingersGat

Copy link
Copy Markdown
Member

@matmair ok great, that makes things simpler!

I should have some time starting next week to sit down and look at this closely :)

@matmair

matmair commented Oct 20, 2021

Copy link
Copy Markdown
Member Author

@matmair ok great, that makes things simpler!

I should have some time starting next week to sit down and look at this closely :)

No hurries 👍

@matmair matmair mentioned this pull request Nov 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api Relates to the API plugin Plugin ecosystem

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[3rd party integration] Webhook receiver

3 participants