netbox-community / pynetbox

Python API client library for Netbox.
Apache License 2.0
538 stars 165 forks source link

render_config tries wrong url when Netbox is behind reverse proxy #590

Open dsecik opened 8 months ago

dsecik commented 8 months ago

pynetbox version

v7.1.0

NetBox version

v3.6.3

Python version

3.9

Steps to Reproduce

#!/usr/bin/python3

import pynetbox

def main():
  nb = pynetbox.api(url="netbox_url", token="my_netbox_token")
  device = nb.dcim.devices.get(680)
  print(device.render_config.create()["content"])

if __name__ == "__main__":
    main()

Expected Behavior

It should return rendered context for device id 680.

Observed Behavior

Works fine when pointed directly to the netbox URL. How ever when used with nginx reverse proxy it fails with:

Traceback (most recent call last):
  File "/home/daniel.secik/./test2.py", line 11, in <module>
    main()
  File "/home/daniel.secik/./test2.py", line 8, in main
    print(device.render_config.create()["content"])
  File "/usr/lib/python3.9/site-packages/pynetbox/core/endpoint.py", line 706, in create
    req = Request(**self.request_kwargs).post(data)
  File "/usr/lib/python3.9/site-packages/pynetbox/core/query.py", line 368, in post
    return self._make_call(verb="post", data=data)
  File "/usr/lib/python3.9/site-packages/pynetbox/core/query.py", line 258, in _make_call
    raise RequestError(req)
pynetbox.core.query.RequestError: The requested url: <proxy_url>:8080/netbox/api/680//680/render-config/ could not be found.

We only saw this with render_config, other api calls work fine with proxy.

Proxy config:
    location ~ "^/netbox/api/" {
        limit_except GET POST {
            deny all;
        }

        rewrite netbox/(.*)$ /$1 break;
        proxy_set_header Accept "application/json";
        proxy_set_header Content-Type "application/json";
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass <netbox_url>;
    }
markkuleinio commented 6 months ago

Works for me, but I have BASE_PATH set correctly in NetBox (= no rewrite on the first Nginx). If I set BASE_PATH = "" and use rewrite, then I get the same error.

Pynetbox uses internally the URLs that NetBox returns in the objects (the url attribute), so if NetBox doesn't know what is the client-accessible path, it cannot return correct URLs.