mirror of
https://github.com/clearml/clearml-agent
synced 2025-03-03 10:42:05 +00:00
Fix login uses GET with payload which breaks when trying to connect a server running in GCP
This commit is contained in:
parent
ebb955187d
commit
8cd12810f3
@ -112,18 +112,7 @@ class Session(TokenManager):
|
|||||||
self._logger = logger
|
self._logger = logger
|
||||||
self.__auth_token = None
|
self.__auth_token = None
|
||||||
|
|
||||||
if ENV_API_DEFAULT_REQ_METHOD.get(default=None):
|
self.update_default_api_method()
|
||||||
# Make sure we update the config object, so we pass it into the new containers when we map them
|
|
||||||
self.config.put("api.http.default_method", ENV_API_DEFAULT_REQ_METHOD.get())
|
|
||||||
# notice the default setting of Request.def_method are already set by the OS environment
|
|
||||||
elif self.config.get("api.http.default_method", None):
|
|
||||||
def_method = str(self.config.get("api.http.default_method", None)).strip()
|
|
||||||
if def_method.upper() not in ("GET", "POST", "PUT"):
|
|
||||||
raise ValueError(
|
|
||||||
"api.http.default_method variable must be 'get' or 'post' (any case is allowed)."
|
|
||||||
)
|
|
||||||
Request.def_method = def_method
|
|
||||||
Request._method = Request.def_method
|
|
||||||
|
|
||||||
if ENV_AUTH_TOKEN.get(
|
if ENV_AUTH_TOKEN.get(
|
||||||
value_cb=lambda key, value: print("Using environment access token {}=********".format(key))
|
value_cb=lambda key, value: print("Using environment access token {}=********".format(key))
|
||||||
@ -223,7 +212,22 @@ class Session(TokenManager):
|
|||||||
|
|
||||||
return http_retries_config, get_http_session_with_retry(config=self.config or None, **http_retries_config)
|
return http_retries_config, get_http_session_with_retry(config=self.config or None, **http_retries_config)
|
||||||
|
|
||||||
|
def update_default_api_method(self):
|
||||||
|
if ENV_API_DEFAULT_REQ_METHOD.get(default=None):
|
||||||
|
# Make sure we update the config object, so we pass it into the new containers when we map them
|
||||||
|
self.config.put("api.http.default_method", ENV_API_DEFAULT_REQ_METHOD.get())
|
||||||
|
# notice the default setting of Request.def_method are already set by the OS environment
|
||||||
|
elif self.config.get("api.http.default_method", None):
|
||||||
|
def_method = str(self.config.get("api.http.default_method", None)).strip()
|
||||||
|
if def_method.upper() not in ("GET", "POST", "PUT"):
|
||||||
|
raise ValueError(
|
||||||
|
"api.http.default_method variable must be 'get', 'post' or 'put' (any case is allowed)."
|
||||||
|
)
|
||||||
|
Request.def_method = def_method
|
||||||
|
Request._method = Request.def_method
|
||||||
|
|
||||||
def load_vaults(self):
|
def load_vaults(self):
|
||||||
|
# () -> Optional[bool]
|
||||||
if not self.check_min_api_version("2.15") or self.feature_set == "basic":
|
if not self.check_min_api_version("2.15") or self.feature_set == "basic":
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -244,12 +248,14 @@ class Session(TokenManager):
|
|||||||
|
|
||||||
# noinspection PyBroadException
|
# noinspection PyBroadException
|
||||||
try:
|
try:
|
||||||
res = self.send_request("users", "get_vaults", json={"enabled": True, "types": ["config"]})
|
# Use params and not data/json otherwise payload might be dropped if we're using GET with a strict firewall
|
||||||
|
res = self.send_request("users", "get_vaults", params="enabled=true&types=config&types=config")
|
||||||
if res.ok:
|
if res.ok:
|
||||||
vaults = res.json().get("data", {}).get("vaults", [])
|
vaults = res.json().get("data", {}).get("vaults", [])
|
||||||
data = list(filter(None, map(parse, vaults)))
|
data = list(filter(None, map(parse, vaults)))
|
||||||
if data:
|
if data:
|
||||||
self.config.set_overrides(*data)
|
self.config.set_overrides(*data)
|
||||||
|
return True
|
||||||
elif res.status_code != 404:
|
elif res.status_code != 404:
|
||||||
raise Exception(res.json().get("meta", {}).get("result_msg", res.text))
|
raise Exception(res.json().get("meta", {}).get("result_msg", res.text))
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
@ -272,6 +278,7 @@ class Session(TokenManager):
|
|||||||
data=None,
|
data=None,
|
||||||
json=None,
|
json=None,
|
||||||
refresh_token_if_unauthorized=True,
|
refresh_token_if_unauthorized=True,
|
||||||
|
params=None,
|
||||||
):
|
):
|
||||||
""" Internal implementation for making a raw API request.
|
""" Internal implementation for making a raw API request.
|
||||||
- Constructs the api endpoint name
|
- Constructs the api endpoint name
|
||||||
@ -303,7 +310,7 @@ class Session(TokenManager):
|
|||||||
else:
|
else:
|
||||||
timeout = self._session_timeout
|
timeout = self._session_timeout
|
||||||
res = self.__http_session.request(
|
res = self.__http_session.request(
|
||||||
method, url, headers=headers, auth=auth, data=data, json=json, timeout=timeout)
|
method, url, headers=headers, auth=auth, data=data, json=json, timeout=timeout, params=params)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
refresh_token_if_unauthorized
|
refresh_token_if_unauthorized
|
||||||
@ -348,6 +355,7 @@ class Session(TokenManager):
|
|||||||
data=None,
|
data=None,
|
||||||
json=None,
|
json=None,
|
||||||
async_enable=False,
|
async_enable=False,
|
||||||
|
params=None,
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Send a raw API request.
|
Send a raw API request.
|
||||||
@ -360,6 +368,7 @@ class Session(TokenManager):
|
|||||||
content type will be application/json)
|
content type will be application/json)
|
||||||
:param data: Dictionary, bytes, or file-like object to send in the request body
|
:param data: Dictionary, bytes, or file-like object to send in the request body
|
||||||
:param async_enable: whether request is asynchronous
|
:param async_enable: whether request is asynchronous
|
||||||
|
:param params: additional query parameters
|
||||||
:return: requests Response instance
|
:return: requests Response instance
|
||||||
"""
|
"""
|
||||||
headers = self.add_auth_headers(
|
headers = self.add_auth_headers(
|
||||||
@ -376,6 +385,7 @@ class Session(TokenManager):
|
|||||||
headers=headers,
|
headers=headers,
|
||||||
data=data,
|
data=data,
|
||||||
json=json,
|
json=json,
|
||||||
|
params=params,
|
||||||
)
|
)
|
||||||
|
|
||||||
def send_request_batch(
|
def send_request_batch(
|
||||||
@ -628,15 +638,14 @@ class Session(TokenManager):
|
|||||||
|
|
||||||
res = None
|
res = None
|
||||||
try:
|
try:
|
||||||
data = {"expiration_sec": exp} if exp else {}
|
|
||||||
res = self._send_request(
|
res = self._send_request(
|
||||||
method=Request.def_method,
|
method=Request.def_method,
|
||||||
service="auth",
|
service="auth",
|
||||||
action="login",
|
action="login",
|
||||||
auth=auth,
|
auth=auth,
|
||||||
json=data,
|
|
||||||
headers=headers,
|
headers=headers,
|
||||||
refresh_token_if_unauthorized=False,
|
refresh_token_if_unauthorized=False,
|
||||||
|
params={"expiration_sec": exp} if exp else {},
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
resp = res.json()
|
resp = res.json()
|
||||||
|
@ -1967,6 +1967,11 @@ class Worker(ServiceCommandSection):
|
|||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
print("Error: failed applying files from configuration: {}".format(ex))
|
print("Error: failed applying files from configuration: {}".format(ex))
|
||||||
|
|
||||||
|
try:
|
||||||
|
self._session.update_default_api_method()
|
||||||
|
except Exception as ex:
|
||||||
|
print("Error: failed updating default API method: {}".format(ex))
|
||||||
|
|
||||||
@resolve_names
|
@resolve_names
|
||||||
def build(
|
def build(
|
||||||
self,
|
self,
|
||||||
|
Loading…
Reference in New Issue
Block a user