jrxFive / python-nomad

Client library Hashicorp Nomad
https://python-nomad.readthedocs.io/en/latest/
MIT License
139 stars 73 forks source link

Trailing slash in NOMAD_ADDR leads to failing API requests after redirect #126

Open ercpe opened 2 years ago

ercpe commented 2 years ago

A trailing slash in the NOMAD_ADDR environment variable may lead to failing API requests with the message

nomad.api.exceptions.BaseNomadException: The BaseNomadException was raised with following response: Invalid method.

This happend to us using the nomad.api.jobs.Jobs.parse method with makes a call to the parse endpoint (https://www.nomadproject.io/api/jobs#parse-job) via POST. If the NOMAD_ADDR ends with a slash, the server first issues a 301 redirect and the following request would downgrade to GET and thus fail.

While the proper fix would be for the Nomad API to issue a 307 instead of a 301 (which retains method and body), the python-nomad library can easily account for that mistake by automatically stripping a trailing slash (e.g. with (os.getenv('NOMAD_ADDR', '').rstrip('/') or None in nomad.Nomad.__init__.