Skip to content

Commit c7a7a40

Browse files
committed
feat(IamAssumeAuthenticator): introduce a new authenticator
Signed-off-by: Norbert Biczo <[email protected]>
1 parent dc37d94 commit c7a7a40

12 files changed

+913
-21
lines changed

Authentication.md

Lines changed: 135 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
The python-sdk-core project supports the following types of authentication:
33
- Basic Authentication
44
- Bearer Token Authentication
5-
- Identity and Access Management (IAM) Authentication
5+
- Identity and Access Management (IAM) Authentication (grant type: apikey)
6+
- Identity and Access Management (IAM) Authentication (grant type: assume)
67
- Container Authentication
78
- VPC Instance Authentication
89
- Cloud Pak for Data Authentication
@@ -16,7 +17,7 @@ to understand which authenticators are supported for that service.
1617

1718
The python-sdk-core allows an authenticator to be specified in one of two ways:
1819
1. programmatically - the SDK user invokes the appropriate function(s) to create an instance of the
19-
desired authenticator and then passes the authenticator instance when constructing an instance of the service.
20+
desired authenticator and then passes the authenticator instance when constructing an instance of the service client.
2021
2. configuration - the SDK user provides external configuration information (in the form of environment variables
2122
or a credentials file) to indicate the type of authenticator, along with the configuration of the necessary properties for that authenticator. The SDK user then invokes the configuration-based service client constructor method to construct an instance of the authenticator and service client that reflect the external configuration information.
2223

@@ -26,7 +27,7 @@ which will include the following:
2627
- The properties associated with the authenticator
2728
- An example of how to construct the authenticator programmatically
2829
- An example of how to configure the authenticator through the use of external
29-
configuration information. The configuration examples below will use
30+
configuration information. The configuration examples below will use
3031
environment variables, although the same properties could be specified in a
3132
credentials file instead.
3233

@@ -122,16 +123,16 @@ service = ExampleServiceV1.new_instance(service_name='example_service')
122123

123124
Note that the use of external configuration is not as useful with the `BearerTokenAuthenticator` as it
124125
is for other authenticator types because bearer tokens typically need to be obtained and refreshed
125-
programmatically since they normally have a relatively short lifespan before they expire. This
126+
programmatically since they normally have a relatively short lifespan before they expire. This
126127
authenticator type is intended for situations in which the application will be managing the bearer
127128
token itself in terms of initial acquisition and refreshing as needed.
128129

129130

130-
## Identity and Access Management (IAM) Authentication
131-
The `IamAuthenticator` will accept a user-supplied api key and will perform
131+
## Identity and Access Management (IAM) Authentication (grant type: apikey)
132+
The `IamAuthenticator` will accept a user-supplied apikey and will perform
132133
the necessary interactions with the IAM token service to obtain a suitable
133-
bearer token for the specified api key. The authenticator will also obtain
134-
a new bearer token when the current token expires. The bearer token is
134+
bearer token for the specified apikey. The authenticator will also obtain
135+
a new bearer token when the current token expires. The bearer token is
135136
then added to each outbound request in the `Authorization` header in the
136137
form:
137138
```
@@ -140,7 +141,7 @@ form:
140141

141142
### Properties
142143

143-
- apikey: (required) the IAM api key
144+
- apikey: (required) the IAM api key to be used to obtain an IAM access token.
144145

145146
- url: (optional) The base endpoint URL of the IAM token service.
146147
The default value of this property is the "prod" IAM token service endpoint
@@ -157,7 +158,7 @@ endpoint as well (`https://iam.test.cloud.ibm.com`).
157158

158159
- client_id/client_secret: (optional) The `client_id` and `client_secret` fields are used to form a
159160
"basic auth" Authorization header for interactions with the IAM token server. If neither field
160-
is specified, then no Authorization header will be sent with token server requests. These fields
161+
is specified, then no Authorization header will be sent with token server requests. These fields
161162
are optional, but must be specified together.
162163

163164
- scope: (optional) the scope to be associated with the IAM access token.
@@ -199,6 +200,120 @@ service = ExampleServiceV1.new_instance(service_name='example_service')
199200
```
200201

201202

203+
## Identity and Access Management (IAM) Authentication (grant type: assume)
204+
The `IAMAssumeAuthenticator` performs a two-step token fetch sequence to obtain
205+
a bearer token that allows the application to assume the identity of a trusted profile:
206+
1. First, the authenticator obtains an initial bearer token using grant type
207+
`urn:ibm:params:oauth:grant-type:apikey`.
208+
This initial token will reflect the identity associated with the input apikey.
209+
2. Second, the authenticator uses the grant type `urn:ibm:params:oauth:grant-type:assume` to obtain a bearer token
210+
that reflects the identity of the trusted profile, passing in the initial bearer token
211+
from the first step, along with the trusted profile-related inputs.
212+
213+
The authenticator will also obtain a new bearer token when the current token expires.
214+
The bearer token is then added to each outbound request in the `Authorization` header in the
215+
form:
216+
```
217+
Authorization: Bearer <bearer-token>
218+
```
219+
220+
### Properties
221+
222+
- apikey: (required) the IAM apikey to be used to obtain the initial IAM access token.
223+
224+
- iam_profile_crn: (optional) the Cloud Resource Name (CRN) associated with the trusted profile
225+
for which an access token should be fetched.
226+
Exactly one of iam_profile_crn, iam_profile_id or iam_profile_name must be specified.
227+
228+
- iam_profile_id: (optional) the ID associated with the trusted profile
229+
for which an access token should be fetched.
230+
Exactly one of iam_profile_crn, iam_profile_id or iam_profile_name must be specified.
231+
232+
- iam_profile_name: (optional) the name associated with the trusted profile
233+
for which an access token should be fetched. When specifying this property, you must also
234+
specify the iam_account_id property as well.
235+
Exactly one of iam_profile_crn, iam_profile_id or iam_profile_name must be specified.
236+
237+
- iam_account_id: (optional) the ID associated with the IAM account that contains the trusted profile
238+
referenced by the iam_profile_name property. The imaAccountId property must be specified if and only if
239+
the iam_profile_name property is specified.
240+
241+
- url: (optional) The base endpoint URL of the IAM token service.
242+
The default value of this property is the "prod" IAM token service endpoint
243+
(`https://iam.cloud.ibm.com`).
244+
Make sure that you use an IAM token service endpoint that is appropriate for the
245+
location of the service being used by your application.
246+
For example, if you are using an instance of a service in the "production" environment
247+
(e.g. `https://resource-controller.cloud.ibm.com`),
248+
then the default "prod" IAM token service endpoint should suffice.
249+
However, if your application is using an instance of a service in the "staging" environment
250+
(e.g. `https://resource-controller.test.cloud.ibm.com`),
251+
then you would also need to configure the authenticator to use the IAM token service "staging"
252+
endpoint as well (`https://iam.test.cloud.ibm.com`).
253+
254+
- client_id/client_secret: (optional) The `client_id` and `client_secret` fields are used to form a
255+
"basic auth" Authorization header for interactions with the IAM token server when fetching the
256+
initial IAM access token. These fields are optional, but must be specified together.
257+
258+
- scope: (optional) the scope to be used when obtaining the initial IAM access token.
259+
If not specified, then no scope will be associated with the access token.
260+
261+
- disable_ssl_verification: (optional) A flag that indicates whether verification of the server's SSL
262+
certificate should be disabled or not. The default value is `false`.
263+
264+
- headers: (optional) A set of key/value pairs that will be sent as HTTP headers in requests
265+
made to the IAM token service.
266+
267+
### Usage Notes
268+
- The IAMAssumeAuthenticator is used to obtain an access token (a bearer token) from the IAM token service
269+
that allows an application to "assume" the identity of a trusted profile.
270+
271+
- The authenticator first uses the apikey, url, client_id/client_secret, scope, disable_ssl_verification, and headers
272+
properties to obtain an initial access token by invoking the IAM `get_token`
273+
(grant_type=`urn:ibm:params:oauth:grant-type:apikey`) operation.
274+
275+
- The authenticator then uses the initial access token along with the url, iam_profile_crn, iam_profile_id,
276+
iam_profile_name, iam_account_id, disable_ssl_verification, and headers properties to obtain an access token by invoking
277+
the IAM `get_token` (grant_type=`urn:ibm:params:oauth:grant-type:assume`) operation.
278+
The access token resulting from this second step will reflect the identity of the specified trusted profile.
279+
280+
- When providing the trusted profile information, you must specify exactly one of: iam_profile_crn, iam_profile_id
281+
or iam_profile_name. If you specify iam_profile_crn or iam_profile_id, then the trusted profile must exist in the same account that is
282+
associated with the input apikey. If you specify iam_profile_name, then you must also specify the iam_account_id property
283+
to indicate the IAM account in which the named trusted profile can be found.
284+
285+
### Programming example
286+
```python
287+
from ibm_cloud_sdk_core.authenticators import IAMAssumeAuthenticator
288+
from <sdk-package-name>.example_service_v1 import *
289+
290+
# Create the authenticator.
291+
authenticator = IAMAssumeAuthenticator('myapikey')
292+
293+
# Construct the service instance.
294+
service = ExampleServiceV1(authenticator=authenticator)
295+
296+
# 'service' can now be used to invoke operations.
297+
```
298+
299+
### Configuration example
300+
External configuration:
301+
```
302+
export EXAMPLE_SERVICE_AUTH_TYPE=iamAssume
303+
export EXAMPLE_SERVICE_APIKEY=myapikey
304+
export EXAMPLE_SERVICE_IAM_PROFILE_ID=myprofile-1
305+
```
306+
Application code:
307+
```python
308+
from <sdk-package-name>.example_service_v1 import *
309+
310+
# Construct the service instance.
311+
service = ExampleServiceV1.new_instance(service_name='example_service')
312+
313+
# 'service' can now be used to invoke operations.
314+
```
315+
316+
202317
## Container Authentication
203318
The `ContainerAuthenticator` is intended to be used by application code
204319
running inside a compute resource managed by the IBM Kubernetes Service (IKS)
@@ -207,7 +322,7 @@ within the compute resource's local file system.
207322
The CR token is similar to an IAM apikey except that it is managed automatically by
208323
the compute resource provider (IKS).
209324
This allows the application developer to:
210-
- avoid storing credentials in application code, configuraton files or a password vault
325+
- avoid storing credentials in application code, configuration files or a password vault
211326
- avoid managing or rotating credentials
212327

213328
The `ContainerAuthenticator` will retrieve the CR token from
@@ -251,7 +366,7 @@ endpoint as well (`https://iam.test.cloud.ibm.com`).
251366

252367
- client_id/client_secret: (optional) The `client_id` and `client_secret` fields are used to form a
253368
"basic auth" Authorization header for interactions with the IAM token server. If neither field
254-
is specified, then no Authorization header will be sent with token server requests. These fields
369+
is specified, then no Authorization header will be sent with token server requests. These fields
255370
are optional, but must be specified together.
256371

257372
- scope (optional): the scope to be associated with the IAM access token.
@@ -304,7 +419,7 @@ The compute resource identity feature allows you to assign a trusted IAM profile
304419
This, in turn, allows applications running within the compute resource to take on this identity when interacting with
305420
IAM-secured IBM Cloud services.
306421
This results in a simplified security model that allows the application developer to:
307-
- avoid storing credentials in application code, configuraton files or a password vault
422+
- avoid storing credentials in application code, configuration files or a password vault
308423
- avoid managing or rotating credentials
309424

310425
The `VPCInstanceAuthenticator` will invoke the appropriate operations on the compute resource's locally-available
@@ -323,11 +438,11 @@ The IAM access token is added to each outbound request in the `Authorization` he
323438
- iam_profile_id: (optional) the id of the linked trusted IAM profile to be used when obtaining the IAM access token.
324439

325440
- url: (optional) The VPC Instance Metadata Service's base URL.
326-
The default value of this property is `http://169.254.169.254`. However, if the VPC Instance Metadata Service is configured
441+
The default value of this property is `http://169.254.169.254`. However, if the VPC Instance Metadata Service is configured
327442
with the HTTP Secure Protocol setting (`https`), then you should configure this property to be `https://api.metadata.cloud.ibm.com`.
328443

329444
Usage Notes:
330-
1. At most one of `iam_profile_crn` or `iam_profile_id` may be specified. The specified value must map
445+
1. At most one of `iam_profile_crn` or `iam_profile_id` may be specified. The specified value must map
331446
to a trusted IAM profile that has been linked to the compute resource (virtual server instance).
332447

333448
2. If both `iam_profile_crn` and `iam_profile_id` are specified, then an error occurs.
@@ -367,11 +482,11 @@ service = ExampleServiceV1.new_instance(service_name='example_service')
367482
```
368483

369484

370-
## Cloud Pak for Data
485+
## Cloud Pak for Data
371486
The `CloudPakForDataAuthenticator` will accept a user-supplied username value, along with either a
372-
password or apikey, and will
373-
perform the necessary interactions with the Cloud Pak for Data token service to obtain a suitable
374-
bearer token. The authenticator will also obtain a new bearer token when the current token expires.
487+
password or apikey, and will perform the necessary interactions with the Cloud Pak for Data token
488+
service to obtain a suitable bearer token. The authenticator will also obtain a new bearer token
489+
when the current token expires.
375490
The bearer token is then added to each outbound request in the `Authorization` header in the
376491
form:
377492
```
@@ -449,7 +564,7 @@ form:
449564
- url: (required) The URL representing the MCSP token service endpoint's base URL string. Do not include the
450565
operation path (e.g. `/siusermgr/api/1.0/apikeys/token`) as part of this property's value.
451566

452-
- disable_ssl_verification: (optional) A flag that indicates whether verificaton of the server's SSL
567+
- disable_ssl_verification: (optional) A flag that indicates whether verification of the server's SSL
453568
certificate should be disabled or not. The default value is `false`.
454569

455570
- headers: (optional) A set of key/value pairs that will be sent as HTTP headers in requests

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ python -m pip install --upgrade ibm-cloud-sdk-core
2323
The python-sdk-core project supports the following types of authentication:
2424
- Basic Authentication
2525
- Bearer Token Authentication
26-
- Identity and Access Management (IAM) Authentication
26+
- Identity and Access Management (IAM) Authentication (grant type: apikey)
27+
- Identity and Access Management (IAM) Authentication (grant type: assume)
2728
- Container Authentication
2829
- VPC Instance Authentication
2930
- Cloud Pak for Data Authentication

ibm_cloud_sdk_core/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
BaseService: Abstract class for common functionality between each service.
1919
DetailedResponse: The object returned from successful service operations.
2020
IAMTokenManager: Requests and refreshes IAM tokens using an apikey, and optionally a client_id and client_secret.
21+
IAMAssumeTokenManager: Requests and refreshes IAM tokens using an apikey and a trusted profile.
2122
JWTTokenManager: Abstract class for common functionality between each JWT token manager.
2223
CP4DTokenManager: Requests and refreshes CP4D tokens given a username and password.
2324
ApiException: Custom exception class for errors returned from service operations.
@@ -39,6 +40,7 @@
3940
from .base_service import BaseService
4041
from .detailed_response import DetailedResponse
4142
from .token_managers.iam_token_manager import IAMTokenManager
43+
from .token_managers.iam_assume_token_manager import IAMAssumeTokenManager
4244
from .token_managers.jwt_token_manager import JWTTokenManager
4345
from .token_managers.cp4d_token_manager import CP4DTokenManager
4446
from .token_managers.container_token_manager import ContainerTokenManager

ibm_cloud_sdk_core/authenticators/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
from .container_authenticator import ContainerAuthenticator
4040
from .cp4d_authenticator import CloudPakForDataAuthenticator
4141
from .iam_authenticator import IAMAuthenticator
42+
from .iam_assume_authenticator import IAMAssumeAuthenticator
4243
from .vpc_instance_authenticator import VPCInstanceAuthenticator
4344
from .no_auth_authenticator import NoAuthAuthenticator
4445
from .mcsp_authenticator import MCSPAuthenticator

ibm_cloud_sdk_core/authenticators/authenticator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Authenticator(ABC):
2424
AUTHTYPE_BASIC = 'basic'
2525
AUTHTYPE_BEARERTOKEN = 'bearerToken'
2626
AUTHTYPE_IAM = 'iam'
27+
AUTHTYPE_IAM_ASSUME = 'iamAssume'
2728
AUTHTYPE_CONTAINER = 'container'
2829
AUTHTYPE_CP4D = 'cp4d'
2930
AUTHTYPE_VPC = 'vpc'

0 commit comments

Comments
 (0)