File: test_iam_assume_token_manager.py

package info (click to toggle)
python-ibm-cloud-sdk-core 3.24.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 944 kB
  • sloc: python: 5,750; makefile: 40; xml: 7; sh: 7
file content (241 lines) | stat: -rw-r--r-- 9,566 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# coding: utf-8

# Copyright 2019, 2024 IBM All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# pylint: disable=missing-docstring
import json
import logging
import time
import urllib

import jwt
import pytest
import responses

from ibm_cloud_sdk_core import IAMAssumeTokenManager
from ibm_cloud_sdk_core.api_exception import ApiException
from .utils.logger_utils import setup_test_logger


setup_test_logger(logging.ERROR)


def _get_current_time() -> int:
    return int(time.time())


IAM_URL = "https://iam.cloud.ibm.com/identity/token"
MY_PROFILE_ID = 'my-profile-id'
MY_PROFILE_CRN = 'my-profile-crn'
MY_PROFILE_NAME = 'my-profile-name'
MY_ACCOUNT_ID = 'my-account_id'


# The base layout of an access token.
ACCESS_TOKEN_LAYOUT = {
    "username": "dummy",
    "role": "Admin",
    "permissions": ["administrator", "manage_catalog"],
    "sub": "admin",
    "iss": "sss",
    "aud": "sss",
    "uid": "sss",
    "iat": _get_current_time(),
    "exp": _get_current_time() + 3600,
}
# Create two different access tokens by using different secrets for the encoding.
ACCESS_TOKEN = jwt.encode(
    ACCESS_TOKEN_LAYOUT, 'secret', algorithm='HS256', headers={'kid': '230498151c214b788dd97f22b85410a5'}
)
OTHER_ACCESS_TOKEN = jwt.encode(
    ACCESS_TOKEN_LAYOUT, 'other_secret', algorithm='HS256', headers={'kid': '230498151c214b788dd97f22b85410a5'}
)
# Create a base response and serialize it to a JSON string to avoid doing that in each test case.
BASE_RESPONSE = {
    "access_token": ACCESS_TOKEN,
    "token_type": "Bearer",
    "expires_in": 3600,
    "expiration": _get_current_time() + 3600,
    "refresh_token": "not_available",
}
BASE_RESPONSE_JSON = json.dumps(BASE_RESPONSE)
# Create a second base response just like we did above, but use the other access token.
OTHER_BASE_RESPONSE = BASE_RESPONSE.copy()
OTHER_BASE_RESPONSE['access_token'] = OTHER_ACCESS_TOKEN
OTHER_BASE_RESPONSE_JSON = json.dumps(OTHER_BASE_RESPONSE)


def request_callback(request):
    """Parse the form data, and return a response based on the `grant_type` value."""
    form_data = urllib.parse.unquote(request.body)
    params = dict(param.split('=') for param in form_data.split('&'))
    if params.get('grant_type') == 'urn:ibm:params:oauth:grant-type:apikey':
        return (200, {}, BASE_RESPONSE_JSON)
    if params.get('grant_type') == 'urn:ibm:params:oauth:grant-type:assume':
        return (200, {}, OTHER_BASE_RESPONSE_JSON)

    raise ApiException(400)


@responses.activate
def test_request_token_with_profile_id():
    responses.add_callback(responses.POST, url=IAM_URL, callback=request_callback)

    token_manager = IAMAssumeTokenManager("apikey", iam_profile_id=MY_PROFILE_ID)

    # Make sure we don't have an access token yet.
    assert token_manager.request_payload.get('access_token') is None

    token_manager.request_token()

    # Now the access token should be set along with the profile ID.
    assert token_manager.request_payload.get('access_token') == ACCESS_TOKEN
    assert token_manager.request_payload.get('profile_id') == MY_PROFILE_ID
    assert token_manager.request_payload.get('profile_crn') is None
    assert token_manager.request_payload.get('profile_name') is None
    assert token_manager.request_payload.get('account_id') is None


@responses.activate
def test_request_token_with_profile_crn():
    responses.add_callback(responses.POST, url=IAM_URL, callback=request_callback)

    token_manager = IAMAssumeTokenManager("apikey", iam_profile_crn=MY_PROFILE_CRN)

    # Make sure we don't have an access token yet.
    assert token_manager.request_payload.get('access_token') is None

    token_manager.request_token()

    # Now the access token should be set along with the profile ID.
    assert token_manager.request_payload.get('access_token') == ACCESS_TOKEN
    assert token_manager.request_payload.get('profile_id') is None
    assert token_manager.request_payload.get('profile_crn') == MY_PROFILE_CRN
    assert token_manager.request_payload.get('profile_name') is None
    assert token_manager.request_payload.get('account_id') is None


@responses.activate
def test_request_token_with_profile_name_and_account_id():
    responses.add_callback(responses.POST, url=IAM_URL, callback=request_callback)

    token_manager = IAMAssumeTokenManager("apikey", iam_profile_name=MY_PROFILE_NAME, iam_account_id=MY_ACCOUNT_ID)

    # Make sure we don't have an access token yet.
    assert token_manager.request_payload.get('access_token') is None

    token_manager.request_token()

    # Now the access token should be set along with the profile ID.
    assert token_manager.request_payload.get('access_token') == ACCESS_TOKEN
    assert token_manager.request_payload.get('profile_id') is None
    assert token_manager.request_payload.get('profile_crn') is None
    assert token_manager.request_payload.get('profile_name') == MY_PROFILE_NAME
    assert token_manager.request_payload.get('account') == MY_ACCOUNT_ID


@responses.activate
def test_request_token_uses_the_correct_grant_types():
    responses.add(responses.POST, url=IAM_URL, body=BASE_RESPONSE_JSON, status=200)

    token_manager = IAMAssumeTokenManager("apikey", iam_profile_id='my_profile_id')
    token_manager.request_token()

    assert token_manager.request_payload.get('grant_type') == 'urn:ibm:params:oauth:grant-type:assume'
    assert token_manager.iam_delegate.request_payload.get('grant_type') == 'urn:ibm:params:oauth:grant-type:apikey'


@responses.activate
def test_request_token_uses_the_correct_headers():
    responses.add_callback(responses.POST, url=IAM_URL, callback=request_callback)

    token_manager = IAMAssumeTokenManager("apikey", iam_profile_id='my_profile_id')
    token_manager.request_token()

    assert len(responses.calls) == 2
    assert responses.calls[0].request.headers.get('User-Agent').startswith('ibm-python-sdk-core/iam-authenticator')
    assert (
        responses.calls[1].request.headers.get('User-Agent').startswith('ibm-python-sdk-core/iam-assume-authenticator')
    )


@responses.activate
def test_get_token():
    responses.add_callback(responses.POST, url=IAM_URL, callback=request_callback)

    token_manager = IAMAssumeTokenManager("apikey", iam_profile_id=MY_PROFILE_ID)

    # Make sure we don't have an access token yet.
    assert token_manager.request_payload.get('access_token') is None

    access_token = token_manager.get_token()
    # Now the access token should be set along with the profile ID.
    assert token_manager.request_payload.get('access_token') == ACCESS_TOKEN
    assert token_manager.request_payload.get('profile_id') == MY_PROFILE_ID
    assert token_manager.request_payload.get('profile_crn') is None
    assert token_manager.request_payload.get('profile_name') is None
    assert token_manager.request_payload.get('account_id') is None

    # The final result should be the other access token, which belong to the "assume" request.
    assert access_token == OTHER_ACCESS_TOKEN

    # Make sure `refresh_token` is None.
    assert token_manager.refresh_token is None


@responses.activate
def test_correct_properties_used_in_calls():
    responses.add_callback(responses.POST, url=IAM_URL, callback=request_callback)

    token_manager = IAMAssumeTokenManager(
        "apikey",
        iam_profile_id=MY_PROFILE_ID,
        client_id='my_client_id',
        client_secret='my_client_secret',
        scope='my_scope',
    )
    token_manager.get_token()

    # Make sure the all properties were used in the first request via the IAM delegate,
    assert responses.calls[0].request.headers.get('Authorization') == 'Basic bXlfY2xpZW50X2lkOm15X2NsaWVudF9zZWNyZXQ='
    assert 'scope=my_scope' in responses.calls[0].request.body
    # but those were not included in the second, assume type request.
    assert responses.calls[1].request.headers.get('Authorization') is None
    assert 'scope=my_scope' not in responses.calls[1].request.body


@responses.activate
def test_iam_assume_authenticator_unsupported_methods():
    token_manager = IAMAssumeTokenManager('my_apikey', iam_profile_id='my_profile_id')

    with pytest.raises(AttributeError) as err:
        token_manager.set_scope('my_scope')
    assert str(err.value) == "'IAMAssumeTokenManager' has no attribute 'set_scope'"

    with pytest.raises(AttributeError) as err:
        token_manager.set_client_id_and_secret('my_client_id', 'my_client_secret')
    assert str(err.value) == "'IAMAssumeTokenManager' has no attribute 'set_client_id_and_secret'"

    with pytest.raises(AttributeError) as err:
        token_manager.set_headers({})
    assert str(err.value) == "'IAMAssumeTokenManager' has no attribute 'set_headers'"

    with pytest.raises(AttributeError) as err:
        token_manager.set_proxies({})
    assert str(err.value) == "'IAMAssumeTokenManager' has no attribute 'set_proxies'"

    with pytest.raises(AttributeError) as err:
        token_manager.set_disable_ssl_verification(True)
    assert str(err.value) == "'IAMAssumeTokenManager' has no attribute 'set_disable_ssl_verification'"