From 75438151f308715a45c1f9bbe5862333839e4af8 Mon Sep 17 00:00:00 2001 From: sam-mosleh <41725025+sam-mosleh@users.noreply.github.com> Date: Tue, 26 Nov 2024 14:29:31 +0400 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Refactor=20URL=20logic=20t?= =?UTF-8?q?o=20match=20minio-go=20implementation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- minio/credentials/providers.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/minio/credentials/providers.py b/minio/credentials/providers.py index d1bf5f44..9576ec01 100644 --- a/minio/credentials/providers.py +++ b/minio/credentials/providers.py @@ -31,7 +31,7 @@ from datetime import timedelta from pathlib import Path from typing import Callable, cast -from urllib.parse import urlencode, urlsplit +from urllib.parse import urlencode, urlsplit, urljoin from xml.etree import ElementTree as ET import certifi @@ -503,19 +503,25 @@ def retrieve(self) -> Credentials: ) token = res.data.decode("utf-8") headers = {"X-aws-ec2-metadata-token": token} if token else None - + iam_security_creds_url = urlsplit(url)._replace( + path="/latest/meta-data/iam/security-credentials/" + ) # Get role name res = _urlopen( self._http_client, "GET", - url+"/latest/meta-data/iam/security-credentials/", + iam_security_creds_url.geturl(), headers=headers, ) role_names = res.data.decode("utf-8").split("\n") if not role_names: raise ValueError(f"no IAM roles attached to EC2 service {url}") - url += "/latest/meta-data/iam/security-credentials/" + \ - role_names[0].strip("\r") + url = iam_security_creds_url._replace( + path=urljoin( + iam_security_creds_url.path, + role_names[0].strip("\r"), + ) + ).geturl() if not url: raise ValueError("url is empty; this should not happen") self._credentials = self.fetch(url, headers=headers)