Filter out non-200 responses
adamchainz opened this issue · 4 comments
Currently we don't filter out error responses from the metadata API, which means it can return data from error responses. There should be a resp.raise_for_status()
in there, or even filter for exactly resp.status_code == 200
Good examples why this is needed are the properties public_hostname
and public_ipv4
for instances in a private subnet
Ah yes, this ticket is actually representing two things.
Firstly, for some properties like public_hostname
we can expect a 404 from the API. In these cases we should always return None
. This is already done for some, e.g. https://github.com/adamchainz/ec2-metadata/blob/master/ec2_metadata.py#L181 . It should be done for all of them.
Secondly, for all properties, there's the chance of the API misfiring and returning 500, 504, etc. In this case raise_for_status()
or raising if resp.status_code
is not an expected value would be appropriate.
With PR #23 merged, could you create release 1.2.1? That way I can remove my local version in two of my projects and go back to your official version ;-)
It is done: https://pypi.org/project/ec2-metadata/ . I didn't release initially because I wanted to do some more changes, but hey can't hurt to release small fixes like that 👍