googleapis / google-auth-library-python

Google Auth Python Library
https://googleapis.dev/python/google-auth/latest/
Apache License 2.0
752 stars 301 forks source link

Allow setting metadata environment variable `GCE_METADATA_HOST` alone #1505

Open salrashid123 opened 3 months ago

salrashid123 commented 3 months ago

If users want to set a custom metadata server endpoint, you have to use both

GCE_METADATA_IP and GCE_METADATA_HOST.

The IP based env var is used here to ping the address of the server.

Using GCE_METADATA_IP seems to be unique to google-auth-python while other gcp sdk libraries allows you to override with just GCE_METADATA_HOST.

The usecase is if anyone wants to run a custom endpoint or local metadata emulator you'd have to know this specific environment variable just for python.


The suggestion is if a user set GCE_METADATA_HOST, just use whats there.

For backwards compatibility, if the user sets both GCE_METADATA_HOST and GCE_METADATA_IP, the behavior is the same as now.

i think one simplistic way to get to that is to alter _metadata.py as such

if os.getenv(environment_vars.GCE_METADATA_HOST, None) is not None and os.getenv(environment_vars.GCE_METADATA_IP,None) is None:
    _METADATA_IP_ROOT = "http://{}".format(_GCE_METADATA_HOST)
else:
    _METADATA_IP_ROOT = "http://{}".format(
        os.getenv(environment_vars.GCE_METADATA_IP, "169.254.169.254")
    )
salrashid123 commented 3 months ago

also noted here https://github.com/tensorflow/tensorflow/issues/60235#issuecomment-1497783248