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

Allow jinja2 code in webhook payload_url #8295

Closed
rodvand opened this issue Jan 9, 2022 · 7 comments · Fixed by #8339
Closed

Allow jinja2 code in webhook payload_url #8295

rodvand opened this issue Jan 9, 2022 · 7 comments · Fixed by #8339
Assignees
Labels
status: accepted This issue has been accepted for implementation type: feature Introduction of new functionality to the application
Milestone

Comments

@rodvand
Copy link
Contributor

rodvand commented Jan 9, 2022

NetBox version

v3.1.5

Feature type

New functionality

Proposed functionality

Allow the use of jinja2 code in the webhook payload_url.

Use case

I have a custom field where I store the ID of an object in my NMS. This allows me to utilise NetBox to control my monitoring (pause monitoring if the device is set to offline as an example). The current setup requires some middleware where I POST to a generic URL and this web server again make a call to the REST API of the monitoring system using the ID in the custom field from the payload data. If I could reference the obj.cf_nms_id directly in the payload_url it would allow me to skip this middleware.

The payload_url would have to be generated on each webhook event.

Database changes

None

External dependencies

No response

@rodvand rodvand added the type: feature Introduction of new functionality to the application label Jan 9, 2022
@rodvand
Copy link
Contributor Author

rodvand commented Jan 9, 2022

I've had a quick look in the code and you could probably extend the Webhook model with a method

def render_payload_url(self, context):
        """
        Render the payload_url
        """
        return render_jinja2(self.payload_url, context)

and then reference it in the method

def process_webhook(webhook, model_name, event, data, snapshots, timestamp, username, request_id):
to get the generated URL.

If the FR is ok and accepted I can provide a PR for consideration.

@jeremystretch
Copy link
Member

The current setup requires some middleware where I POST to a generic URL and this web server again make a call to the REST API of the monitoring system using the ID in the custom field from the payload data.

Just as illustration for the use case, I take it you want to do something like this?

POST https://nms.local/devices/{{ object.cf.nms_id }}/edit/

Makes sense to me. My only concern is the potential for conflict with existing URLs if we enable Jinja2 rendering (for example, if someone has a URL defined that is evaluated as invalid Jinja2 code), although I'd expect such instances to be very rare.

@jeremystretch jeremystretch added the status: under review Further discussion is needed to determine this issue's scope and/or implementation label Jan 10, 2022
@rodvand
Copy link
Contributor Author

rodvand commented Jan 10, 2022

The current setup requires some middleware where I POST to a generic URL and this web server again make a call to the REST API of the monitoring system using the ID in the custom field from the payload data.

Just as illustration for the use case, I take it you want to do something like this?


POST https://nms.local/devices/{{ object.cf.nms_id }}/edit/

Yes, that's pretty much exactly my use case

Makes sense to me. My only concern is the potential for conflict with existing URLs if we enable Jinja2 rendering (for example, if someone has a URL defined that is evaluated as invalid Jinja2 code), although I'd expect such instances to be very rare.

I thought about the same thing, but couldn't really wrap my head around how a URL like that would look.

@jeremystretch
Copy link
Member

I thought about the same thing, but couldn't really wrap my head around how a URL like that would look.

I've learned never to underestimate the bizarre ways in which some enterprise application will abuse URL query parameters. 😆 But maybe it would be sufficient to gate this change in a milestone release (e.g. v3.2 or v3.3).

@geor-g
Copy link

geor-g commented Jan 11, 2022

This could potentially be gated via a config option, to lower potential risks, and make this opt-int; although I believe the risk is low in any case.

@rodvand
Copy link
Contributor Author

rodvand commented Jan 12, 2022

I thought about the same thing, but couldn't really wrap my head around how a URL like that would look.

I've learned never to underestimate the bizarre ways in which some enterprise application will abuse URL query parameters. 😆 But maybe it would be sufficient to gate this change in a milestone release (e.g. v3.2 or v3.3).

I agree it would make sense to add this potential breaking change to a milestone release. If it breaks an URL it should just be some small adjustments to make it work with jinja2.

@jeremystretch jeremystretch added status: accepted This issue has been accepted for implementation and removed status: under review Further discussion is needed to determine this issue's scope and/or implementation labels Jan 12, 2022
@jeremystretch jeremystretch added this to the v3.2 milestone Jan 12, 2022
@jeremystretch
Copy link
Member

@rodvand assigning this to you since you volunteered earlier. Just be sure to work off of the feature branch. (Or just let me know if you'd prefer I take it.) Thanks!

@rodvand rodvand linked a pull request Jan 12, 2022 that will close this issue
jeremystretch added a commit that referenced this issue Jan 13, 2022
Fixes: #8295 Render the payload_url of a Webhook with Jinja2
jeremystretch added a commit that referenced this issue Jan 13, 2022
This was referenced Apr 5, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: accepted This issue has been accepted for implementation type: feature Introduction of new functionality to the application
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants