Skip to content

Commit 7ae183d

Browse files
pyrookapadamstx
andcommitted
chore: address issues #2
Co-authored-by: Phil Adams <[email protected]>
1 parent acce4d9 commit 7ae183d

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

Authentication.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ The python-sdk-core project supports the following types of authentication:
33
- Basic Authentication
44
- Bearer Token Authentication
55
- Identity and Access Management (IAM) Authentication
6-
- VPC Instance Authentication
76
- Container Authentication
7+
- VPC Instance Authentication
88
- Cloud Pak for Data Authentication
99
- No Authentication
1010

@@ -295,7 +295,7 @@ service = ExampleServiceV1.new_instance(service_name='example_service')
295295

296296

297297
## VPC Instance Authentication
298-
The `VpcInstanceAuthenticator` is intended to be used by application code
298+
The `VPCInstanceAuthenticator` is intended to be used by application code
299299
running inside a VPC-managed compute resource (virtual server instance) that has been configured
300300
to use the "compute resource identity" feature.
301301
The compute resource identity feature allows you to assign a trusted IAM profile to the compute resource as its "identity".
@@ -305,7 +305,7 @@ This results in a simplified security model that allows the application develope
305305
- avoid storing credentials in application code, configuraton files or a password vault
306306
- avoid managing or rotating credentials
307307

308-
The `VpcInstanceAuthenticator` will invoke the appropriate operations on the compute resource's locally-available
308+
The `VPCInstanceAuthenticator` will invoke the appropriate operations on the compute resource's locally-available
309309
VPC Instance Metadata Service to (1) retrieve an instance identity token
310310
and then (2) exchange that instance identity token for an IAM access token.
311311
The authenticator will repeat these steps to obtain a new IAM access token whenever the current access token expires.

ibm_cloud_sdk_core/token_managers/vpc_instance_token_manager.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ def request_token(self) -> dict:
7979

8080
url = self.url + '/instance_identity/v1/iam_token'
8181

82+
request_payload = None
8283
if self.iam_profile_crn:
8384
request_payload = {'trusted_profile': {'crn': self.iam_profile_crn}}
8485
if self.iam_profile_id:
@@ -97,7 +98,7 @@ def request_token(self) -> dict:
9798
url=url,
9899
headers=headers,
99100
params={'version': self.METADATA_SERVICE_VERSION},
100-
data=json.dumps(request_payload))
101+
data=json.dumps(request_payload) if request_payload else None)
101102
logging.debug('Returned from VPC \'create_iam_token\' operation."')
102103

103104
return response

test/test_vpc_instance_token_manager.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,37 @@ def mock_retrieve_instance_identity_token():
179179
assert caplog.record_tuples[1][2] == 'Returned from VPC \'create_iam_token\' operation."'
180180

181181

182+
@responses.activate
183+
def test_request_token(caplog):
184+
caplog.set_level(logging.DEBUG)
185+
186+
token_manager = VPCInstanceTokenManager()
187+
188+
# Mock the retrieve instance identity token method.
189+
def mock_retrieve_instance_identity_token():
190+
return TEST_TOKEN
191+
token_manager.retrieve_instance_identity_token = mock_retrieve_instance_identity_token
192+
193+
response = {
194+
'access_token': TEST_IAM_TOKEN,
195+
}
196+
197+
responses.add(responses.POST, 'http://169.254.169.254/instance_identity/v1/iam_token',
198+
body=json.dumps(response), status=200)
199+
200+
response = token_manager.request_token()
201+
assert len(responses.calls) == 1
202+
assert responses.calls[0].request.headers['Content-Type'] == 'application/json'
203+
assert responses.calls[0].request.headers['Accept'] == 'application/json'
204+
assert responses.calls[0].request.headers['Authorization'] == 'Bearer ' + TEST_TOKEN
205+
assert responses.calls[0].request.body is None
206+
assert responses.calls[0].request.params['version'] == '2021-09-20'
207+
# Check the logs.
208+
#pylint: disable=line-too-long
209+
assert caplog.record_tuples[0][2] == 'Invoking VPC \'create_iam_token\' operation: http://169.254.169.254/instance_identity/v1/iam_token'
210+
assert caplog.record_tuples[1][2] == 'Returned from VPC \'create_iam_token\' operation."'
211+
212+
182213
@responses.activate
183214
def test_request_token_failed(caplog):
184215
caplog.set_level(logging.DEBUG)

0 commit comments

Comments
 (0)