ibm-cloud-sdk-core 3.16.0__py3-none-any.whl → 3.20.6__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. ibm_cloud_sdk_core/__init__.py +1 -0
  2. ibm_cloud_sdk_core/api_exception.py +18 -4
  3. ibm_cloud_sdk_core/authenticators/__init__.py +1 -0
  4. ibm_cloud_sdk_core/authenticators/authenticator.py +2 -1
  5. ibm_cloud_sdk_core/authenticators/basic_authenticator.py +5 -6
  6. ibm_cloud_sdk_core/authenticators/bearer_token_authenticator.py +1 -1
  7. ibm_cloud_sdk_core/authenticators/container_authenticator.py +25 -16
  8. ibm_cloud_sdk_core/authenticators/cp4d_authenticator.py +33 -21
  9. ibm_cloud_sdk_core/authenticators/iam_authenticator.py +22 -13
  10. ibm_cloud_sdk_core/authenticators/iam_request_based_authenticator.py +5 -7
  11. ibm_cloud_sdk_core/authenticators/mcsp_authenticator.py +130 -0
  12. ibm_cloud_sdk_core/authenticators/vpc_instance_authenticator.py +7 -10
  13. ibm_cloud_sdk_core/base_service.py +107 -91
  14. ibm_cloud_sdk_core/detailed_response.py +21 -15
  15. ibm_cloud_sdk_core/get_authenticator.py +28 -16
  16. ibm_cloud_sdk_core/http_adapter.py +28 -0
  17. ibm_cloud_sdk_core/private_helpers.py +34 -0
  18. ibm_cloud_sdk_core/token_managers/container_token_manager.py +61 -30
  19. ibm_cloud_sdk_core/token_managers/cp4d_token_manager.py +30 -22
  20. ibm_cloud_sdk_core/token_managers/iam_request_based_token_manager.py +43 -20
  21. ibm_cloud_sdk_core/token_managers/iam_token_manager.py +24 -13
  22. ibm_cloud_sdk_core/token_managers/jwt_token_manager.py +3 -16
  23. ibm_cloud_sdk_core/token_managers/mcsp_token_manager.py +102 -0
  24. ibm_cloud_sdk_core/token_managers/token_manager.py +13 -23
  25. ibm_cloud_sdk_core/token_managers/vpc_instance_token_manager.py +33 -13
  26. ibm_cloud_sdk_core/utils.py +121 -46
  27. ibm_cloud_sdk_core/version.py +1 -1
  28. {ibm_cloud_sdk_core-3.16.0.dist-info → ibm_cloud_sdk_core-3.20.6.dist-info}/METADATA +40 -28
  29. ibm_cloud_sdk_core-3.20.6.dist-info/RECORD +34 -0
  30. {ibm_cloud_sdk_core-3.16.0.dist-info → ibm_cloud_sdk_core-3.20.6.dist-info}/WHEEL +1 -1
  31. ibm_cloud_sdk_core-3.20.6.dist-info/top_level.txt +1 -0
  32. ibm_cloud_sdk_core-3.16.0.dist-info/RECORD +0 -52
  33. ibm_cloud_sdk_core-3.16.0.dist-info/top_level.txt +0 -3
  34. ibm_cloud_sdk_core-3.16.0.dist-info/zip-safe +0 -1
  35. test/__init__.py +0 -0
  36. test/test_api_exception.py +0 -73
  37. test/test_authenticator.py +0 -21
  38. test/test_base_service.py +0 -933
  39. test/test_basic_authenticator.py +0 -36
  40. test/test_bearer_authenticator.py +0 -28
  41. test/test_container_authenticator.py +0 -105
  42. test/test_container_token_manager.py +0 -283
  43. test/test_cp4d_authenticator.py +0 -171
  44. test/test_cp4d_token_manager.py +0 -56
  45. test/test_detailed_response.py +0 -57
  46. test/test_iam_authenticator.py +0 -157
  47. test/test_iam_token_manager.py +0 -362
  48. test/test_jwt_token_manager.py +0 -109
  49. test/test_no_auth_authenticator.py +0 -15
  50. test/test_token_manager.py +0 -84
  51. test/test_utils.py +0 -634
  52. test/test_vpc_instance_authenticator.py +0 -66
  53. test/test_vpc_instance_token_manager.py +0 -266
  54. test_integration/__init__.py +0 -0
  55. test_integration/test_cp4d_authenticator_integration.py +0 -45
  56. {ibm_cloud_sdk_core-3.16.0.dist-info → ibm_cloud_sdk_core-3.20.6.dist-info}/LICENSE +0 -0
@@ -1,66 +0,0 @@
1
- # pylint: disable=missing-docstring
2
- import pytest
3
-
4
- from ibm_cloud_sdk_core.authenticators import VPCInstanceAuthenticator, Authenticator
5
-
6
-
7
- TEST_IAM_PROFILE_CRN = 'crn:iam-profile:123'
8
- TEST_IAM_PROFILE_ID = 'iam-id-123'
9
-
10
-
11
- def test_constructor():
12
- authenticator =VPCInstanceAuthenticator(iam_profile_id=TEST_IAM_PROFILE_ID, url='someurl.com')
13
- assert authenticator is not None
14
- assert authenticator.authentication_type() == Authenticator.AUTHTYPE_VPC
15
- assert authenticator.token_manager.iam_profile_crn is None
16
- assert authenticator.token_manager.iam_profile_id == TEST_IAM_PROFILE_ID
17
- assert authenticator.token_manager.url == 'someurl.com'
18
-
19
-
20
- def test_setters():
21
- authenticator =VPCInstanceAuthenticator(iam_profile_id=TEST_IAM_PROFILE_ID, url='someurl.com')
22
- assert authenticator is not None
23
- assert authenticator.authentication_type() == Authenticator.AUTHTYPE_VPC
24
- assert authenticator.token_manager.iam_profile_crn is None
25
- assert authenticator.token_manager.iam_profile_id == TEST_IAM_PROFILE_ID
26
- assert authenticator.token_manager.url == 'someurl.com'
27
-
28
- # Set the IAM profile CRN to trigger a validation which will fail,
29
- # because at most one of iam_profile_crn or iam_profile_id may be specified.
30
- with pytest.raises(ValueError) as err:
31
- authenticator.set_iam_profile_crn(TEST_IAM_PROFILE_CRN)
32
- assert str(
33
- err.value) == 'At most one of "iam_profile_id" or "iam_profile_crn" may be specified.'
34
-
35
- authenticator.set_iam_profile_id(None)
36
- assert authenticator.token_manager.iam_profile_id is None
37
-
38
- authenticator.set_iam_profile_crn(TEST_IAM_PROFILE_CRN)
39
- assert authenticator.token_manager.iam_profile_crn == TEST_IAM_PROFILE_CRN
40
-
41
-
42
- def test_constructor_validate_failed():
43
- with pytest.raises(ValueError) as err:
44
- VPCInstanceAuthenticator(
45
- iam_profile_crn=TEST_IAM_PROFILE_CRN,
46
- iam_profile_id=TEST_IAM_PROFILE_ID,
47
- )
48
- assert str(
49
- err.value) == 'At most one of "iam_profile_id" or "iam_profile_crn" may be specified.'
50
-
51
-
52
- def test_authenticate():
53
- def mock_get_token():
54
- return 'mock_token'
55
-
56
- authenticator = VPCInstanceAuthenticator(iam_profile_crn=TEST_IAM_PROFILE_CRN)
57
- authenticator.token_manager.get_token = mock_get_token
58
-
59
- # Simulate an SDK API request that needs to be authenticated.
60
- request = {'headers': {}}
61
-
62
- # Trigger the "get token" processing to obtain the access token and add to the "SDK request".
63
- authenticator.authenticate(request)
64
-
65
- # Verify that the "authenticate()" method added the Authorization header
66
- assert request['headers']['Authorization'] == 'Bearer mock_token'
@@ -1,266 +0,0 @@
1
- # coding: utf-8
2
-
3
- # Copyright 2021 IBM All Rights Reserved.
4
- #
5
- # Licensed under the Apache License, Version 2.0 (the "License");
6
- # you may not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing, software
12
- # distributed under the License is distributed on an "AS IS" BASIS,
13
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- # See the License for the specific language governing permissions and
15
- # limitations under the License.
16
-
17
- # pylint: disable=missing-docstring
18
- import json
19
- import logging
20
-
21
- import pytest
22
- import responses
23
-
24
- from ibm_cloud_sdk_core import ApiException, VPCInstanceTokenManager
25
-
26
-
27
- #pylint: disable=line-too-long
28
- TEST_ACCESS_TOKEN = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6ImhlbGxvIiwicm9sZSI6InVzZXIiLCJwZXJtaXNzaW9ucyI6WyJhZG1pbmlzdHJhdG9yIiwiZGVwbG95bWVudF9hZG1pbiJdLCJzdWIiOiJoZWxsbyIsImlzcyI6IkpvaG4iLCJhdWQiOiJEU1giLCJ1aWQiOiI5OTkiLCJpYXQiOjE1NjAyNzcwNTEsImV4cCI6MTU2MDI4MTgxOSwianRpIjoiMDRkMjBiMjUtZWUyZC00MDBmLTg2MjMtOGNkODA3MGI1NDY4In0.cIodB4I6CCcX8vfIImz7Cytux3GpWyObt9Gkur5g1QI'
29
- TEST_TOKEN = 'abc123'
30
- TEST_IAM_TOKEN = 'iam-abc123'
31
- TEST_IAM_PROFILE_CRN = 'crn:iam-profile:123'
32
- TEST_IAM_PROFILE_ID = 'iam-id-123'
33
-
34
-
35
- def test_constructor():
36
- token_manager = VPCInstanceTokenManager(
37
- iam_profile_crn=TEST_IAM_PROFILE_CRN,
38
- )
39
-
40
- assert token_manager.iam_profile_crn is TEST_IAM_PROFILE_CRN
41
- assert token_manager.iam_profile_id is None
42
- assert token_manager.access_token is None
43
-
44
-
45
- def test_setters():
46
- token_manager = VPCInstanceTokenManager(
47
- iam_profile_crn=TEST_IAM_PROFILE_CRN,
48
- )
49
-
50
- assert token_manager.iam_profile_crn is TEST_IAM_PROFILE_CRN
51
- assert token_manager.iam_profile_id is None
52
- assert token_manager.access_token is None
53
-
54
- token_manager.set_iam_profile_crn(None)
55
- assert token_manager.iam_profile_crn is None
56
-
57
- token_manager.set_iam_profile_id(TEST_IAM_PROFILE_ID)
58
- assert token_manager.iam_profile_id == TEST_IAM_PROFILE_ID
59
-
60
-
61
- @responses.activate
62
- def test_retrieve_instance_identity_token(caplog):
63
- caplog.set_level(logging.DEBUG)
64
-
65
- token_manager = VPCInstanceTokenManager(
66
- iam_profile_crn=TEST_IAM_PROFILE_CRN,
67
- url='http://someurl.com',
68
- )
69
-
70
- response = {
71
- 'access_token': TEST_TOKEN,
72
- }
73
-
74
- responses.add(responses.PUT, 'http://someurl.com/instance_identity/v1/token',
75
- body=json.dumps(response), status=200)
76
-
77
- ii_token = token_manager.retrieve_instance_identity_token()
78
- assert len(responses.calls) == 1
79
- assert responses.calls[0].request.headers['Content-Type'] == 'application/json'
80
- assert responses.calls[0].request.headers['Accept'] == 'application/json'
81
- assert responses.calls[0].request.headers['Metadata-Flavor'] == 'ibm'
82
- assert responses.calls[0].request.params['version'] == '2021-09-20'
83
- assert responses.calls[0].request.body == '{"expires_in": 300}'
84
- assert ii_token == TEST_TOKEN
85
- # Check the logs.
86
- #pylint: disable=line-too-long
87
- assert caplog.record_tuples[0][2] == 'Invoking VPC \'create_access_token\' operation: http://someurl.com/instance_identity/v1/token'
88
- assert caplog.record_tuples[1][2] == 'Returned from VPC \'create_access_token\' operation."'
89
-
90
-
91
- @responses.activate
92
- def test_retrieve_instance_identity_token_failed(caplog):
93
- caplog.set_level(logging.DEBUG)
94
-
95
- token_manager = VPCInstanceTokenManager(
96
- iam_profile_crn=TEST_IAM_PROFILE_CRN,
97
- url='http://someurl.com',
98
- )
99
-
100
- response = {
101
- 'errors': ['Ooops'],
102
- }
103
-
104
- responses.add(responses.PUT, 'http://someurl.com/instance_identity/v1/token',
105
- body=json.dumps(response), status=400)
106
-
107
- with pytest.raises(ApiException):
108
- token_manager.retrieve_instance_identity_token()
109
-
110
- assert len(responses.calls) == 1
111
- # Check the logs.
112
- #pylint: disable=line-too-long
113
- assert caplog.record_tuples[0][2] == 'Invoking VPC \'create_access_token\' operation: http://someurl.com/instance_identity/v1/token'
114
-
115
-
116
- @responses.activate
117
- def test_request_token_with_crn(caplog):
118
- caplog.set_level(logging.DEBUG)
119
-
120
- token_manager = VPCInstanceTokenManager(
121
- iam_profile_crn=TEST_IAM_PROFILE_CRN,
122
- )
123
-
124
- # Mock the retrieve instance identity token method.
125
- def mock_retrieve_instance_identity_token():
126
- return TEST_TOKEN
127
- token_manager.retrieve_instance_identity_token = mock_retrieve_instance_identity_token
128
-
129
- response = {
130
- 'access_token': TEST_IAM_TOKEN,
131
- }
132
-
133
- responses.add(responses.POST, 'http://169.254.169.254/instance_identity/v1/iam_token',
134
- body=json.dumps(response), status=200)
135
-
136
- response = token_manager.request_token()
137
- assert len(responses.calls) == 1
138
- assert responses.calls[0].request.headers['Content-Type'] == 'application/json'
139
- assert responses.calls[0].request.headers['Accept'] == 'application/json'
140
- assert responses.calls[0].request.headers['Authorization'] == 'Bearer ' + TEST_TOKEN
141
- assert responses.calls[0].request.body == '{"trusted_profile": {"crn": "crn:iam-profile:123"}}'
142
- assert responses.calls[0].request.params['version'] == '2021-09-20'
143
- # Check the logs.
144
- #pylint: disable=line-too-long
145
- assert caplog.record_tuples[0][2] == 'Invoking VPC \'create_iam_token\' operation: http://169.254.169.254/instance_identity/v1/iam_token'
146
- assert caplog.record_tuples[1][2] == 'Returned from VPC \'create_iam_token\' operation."'
147
-
148
-
149
- @responses.activate
150
- def test_request_token_with_id(caplog):
151
- caplog.set_level(logging.DEBUG)
152
-
153
- token_manager = VPCInstanceTokenManager(
154
- iam_profile_id=TEST_IAM_PROFILE_ID,
155
- )
156
-
157
- # Mock the retrieve instance identity token method.
158
- def mock_retrieve_instance_identity_token():
159
- return TEST_TOKEN
160
- token_manager.retrieve_instance_identity_token = mock_retrieve_instance_identity_token
161
-
162
- response = {
163
- 'access_token': TEST_IAM_TOKEN,
164
- }
165
-
166
- responses.add(responses.POST, 'http://169.254.169.254/instance_identity/v1/iam_token',
167
- body=json.dumps(response), status=200)
168
-
169
- response = token_manager.request_token()
170
- assert len(responses.calls) == 1
171
- assert responses.calls[0].request.headers['Content-Type'] == 'application/json'
172
- assert responses.calls[0].request.headers['Accept'] == 'application/json'
173
- assert responses.calls[0].request.headers['Authorization'] == 'Bearer ' + TEST_TOKEN
174
- assert responses.calls[0].request.body == '{"trusted_profile": {"id": "iam-id-123"}}'
175
- assert responses.calls[0].request.params['version'] == '2021-09-20'
176
- # Check the logs.
177
- #pylint: disable=line-too-long
178
- assert caplog.record_tuples[0][2] == 'Invoking VPC \'create_iam_token\' operation: http://169.254.169.254/instance_identity/v1/iam_token'
179
- assert caplog.record_tuples[1][2] == 'Returned from VPC \'create_iam_token\' operation."'
180
-
181
-
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
-
213
- @responses.activate
214
- def test_request_token_failed(caplog):
215
- caplog.set_level(logging.DEBUG)
216
-
217
- token_manager = VPCInstanceTokenManager(
218
- iam_profile_id=TEST_IAM_PROFILE_ID,
219
- )
220
-
221
- # Mock the retrieve instance identity token method.
222
- def mock_retrieve_instance_identity_token():
223
- return TEST_TOKEN
224
- token_manager.retrieve_instance_identity_token = mock_retrieve_instance_identity_token
225
-
226
- response = {
227
- 'errors': ['Ooops'],
228
- }
229
-
230
- responses.add(responses.POST, 'http://169.254.169.254/instance_identity/v1/iam_token',
231
- body=json.dumps(response), status=400)
232
-
233
- with pytest.raises(ApiException):
234
- token_manager.request_token()
235
- assert len(responses.calls) == 1
236
- # Check the logs.
237
- #pylint: disable=line-too-long
238
- assert caplog.record_tuples[0][2] == 'Invoking VPC \'create_iam_token\' operation: http://169.254.169.254/instance_identity/v1/iam_token'
239
-
240
-
241
- @responses.activate
242
- def test_access_token():
243
- token_manager = VPCInstanceTokenManager(
244
- iam_profile_id=TEST_IAM_PROFILE_ID,
245
- )
246
-
247
- response_ii = {
248
- 'access_token': TEST_TOKEN,
249
- }
250
- response_iam = {
251
- 'access_token': TEST_ACCESS_TOKEN,
252
- }
253
-
254
- responses.add(responses.PUT, 'http://169.254.169.254/instance_identity/v1/token',
255
- body=json.dumps(response_ii), status=200)
256
- responses.add(responses.POST, 'http://169.254.169.254/instance_identity/v1/iam_token',
257
- body=json.dumps(response_iam), status=200)
258
-
259
- assert token_manager.access_token is None
260
- assert token_manager.expire_time == 0
261
- assert token_manager.refresh_time == 0
262
-
263
- token_manager.get_token()
264
- assert token_manager.access_token == TEST_ACCESS_TOKEN
265
- assert token_manager.expire_time > 0
266
- assert token_manager.refresh_time > 0
File without changes
@@ -1,45 +0,0 @@
1
- # pylint: disable=missing-docstring
2
- import os
3
-
4
- from ibm_cloud_sdk_core import get_authenticator_from_environment
5
-
6
- # Note: Only the unit tests are run by default.
7
- #
8
- # In order to test with a live CP4D server, rename "ibm-credentials-cp4dtest.env.example" to
9
- # "ibm-credentials-cp4dtest.env" in the resources folder and populate the fields.
10
- # Then run this command:
11
- # pytest test_integration
12
-
13
- IBM_CREDENTIALS_FILE = '../resources/ibm-credentials-cp4dtest.env'
14
-
15
-
16
- def test_cp4d_authenticator_password():
17
- file_path = os.path.join(
18
- os.path.dirname(__file__), IBM_CREDENTIALS_FILE)
19
- os.environ['IBM_CREDENTIALS_FILE'] = file_path
20
-
21
- authenticator = get_authenticator_from_environment('cp4d_password_test')
22
- assert authenticator is not None
23
- assert authenticator.token_manager.password is not None
24
- assert authenticator.token_manager.apikey is None
25
-
26
- request = {'headers': {}}
27
- authenticator.authenticate(request)
28
- assert request['headers']['Authorization'] is not None
29
- assert 'Bearer' in request['headers']['Authorization']
30
-
31
-
32
- def test_cp4d_authenticator_apikey():
33
- file_path = os.path.join(
34
- os.path.dirname(__file__), IBM_CREDENTIALS_FILE)
35
- os.environ['IBM_CREDENTIALS_FILE'] = file_path
36
-
37
- authenticator = get_authenticator_from_environment('cp4d_apikey_test')
38
- assert authenticator is not None
39
- assert authenticator.token_manager.password is None
40
- assert authenticator.token_manager.apikey is not None
41
-
42
- request = {'headers': {}}
43
- authenticator.authenticate(request)
44
- assert request['headers']['Authorization'] is not None
45
- assert 'Bearer' in request['headers']['Authorization']