Skip to content

Commit f90f4d8

Browse files
committed
chore: fix some issues
1 parent 24a6b02 commit f90f4d8

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

ibm_cloud_sdk_core/authenticators/vpc_instance_authenticator.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from typing import Optional
1818

1919
from requests import Request
20-
from requests.api import head
2120

2221
from ..token_managers.vpc_instance_token_manager import VPCInstanceTokenManager
2322
from .authenticator import Authenticator
@@ -52,10 +51,15 @@ class VPCInstanceAuthenticator(Authenticator):
5251
url (str, optional): The VPC Instance Metadata Service's base endpoint URL.
5352
"""
5453

54+
DEFAULT_IMS_ENDPOINT = 'http://169.254.169.254'
55+
5556
def __init__(self,
5657
iam_profile_crn: Optional[str] = None,
5758
iam_profile_id: Optional[str] = None,
58-
url: Optional[str] = 'http://169.254.169.254') -> None:
59+
url: Optional[str] = None) -> None:
60+
61+
if not url:
62+
url = self.DEFAULT_IMS_ENDPOINT
5963

6064
self.token_manager = VPCInstanceTokenManager(
6165
url=url, iam_profile_crn=iam_profile_crn, iam_profile_id=iam_profile_id)

ibm_cloud_sdk_core/token_managers/vpc_instance_token_manager.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
import json
1718
import logging
1819
from typing import Optional
1920

@@ -49,11 +50,15 @@ class VPCInstanceTokenManager(JWTTokenManager):
4950
"""
5051

5152
METADATA_SERVICE_VERSION = '2021-09-20'
53+
DEFAULT_IMS_ENDPOINT = 'http://169.254.169.254'
5254

5355
def __init__(self,
5456
iam_profile_crn: Optional[str] = None,
5557
iam_profile_id: Optional[str] = None,
56-
url: Optional[str] = 'http://169.254.169.254') -> None:
58+
url: Optional[str] = None) -> None:
59+
if not url:
60+
url = self.DEFAULT_IMS_ENDPOINT
61+
5762
super().__init__(url)
5863

5964
self.iam_profile_crn = iam_profile_crn
@@ -74,13 +79,11 @@ def request_token(self) -> dict:
7479
url = self.url + '/instance_identity/v1/iam_token'
7580

7681
if self.iam_profile_crn:
77-
#pylint: disable=missing-format-argument-key
7882
request_payload = {
79-
'trusted_profile': '{"crn": "{0}"}'.format(self.iam_profile_crn)}
83+
'trusted_profile': '{{"crn": "{0}"}}'.format(self.iam_profile_crn)}
8084
if self.iam_profile_id:
81-
#pylint: disable=missing-format-argument-key
8285
request_payload = {
83-
'trusted_profile': '{"id": "{0}"}'.format(self.iam_profile_id)}
86+
'trusted_profile': '{{"id": "{0}"}}'.format(self.iam_profile_id)}
8487

8588
headers = {
8689
'Content-Type': 'application/json',
@@ -92,11 +95,11 @@ def request_token(self) -> dict:
9295
'Invoking VPC \'create_iam_token\' operation: %s', url)
9396
response = self._request(
9497
method='POST',
95-
url=(self.url + self.OPERATION_PATH) if self.url else self.url,
98+
url=url,
9699
headers=headers,
97100
params={'version': self.METADATA_SERVICE_VERSION},
98-
data=request_payload)
99-
logging.debug('Returned from VPC \'create_access_token\' operation."')
101+
data=json.dumps(request_payload))
102+
logging.debug('Returned from VPC \'create_iam_token\' operation."')
100103

101104
return response
102105

@@ -143,8 +146,7 @@ def retrieve_instance_identity_token(self) -> str:
143146
method='PUT',
144147
url=url,
145148
headers=headers,
146-
data=request_body,
147-
proxies=self.proxies)
149+
data=json.dumps(request_body))
148150
logging.debug('Returned from VPC \'create_access_token\' operation."')
149151

150152
return response['access_token']

0 commit comments

Comments
 (0)