Skip to content

Commit 5237277

Browse files
pyrookapadamstx
andauthored
feat: implement container authentication (#119)
This PR adds an authenticator and a token manager to support container authentication in the Python SDKs. Co-authored-by: Phil Adams <[email protected]>
1 parent e0aeed7 commit 5237277

22 files changed

+984
-31
lines changed

.secrets.baseline

Lines changed: 73 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"files": "package-lock.json|^.secrets.baseline$",
44
"lines": null
55
},
6-
"generated_at": "2021-05-13T11:02:55Z",
6+
"generated_at": "2021-08-06T15:18:39Z",
77
"plugins_used": [
88
{
99
"name": "AWSKeyDetector"
@@ -25,6 +25,7 @@
2525
"name": "CloudantDetector"
2626
},
2727
{
28+
"ghe_instance": "github.ibm.com",
2829
"name": "GheDetector"
2930
},
3031
{
@@ -69,39 +70,39 @@
6970
"hashed_secret": "98635b2eaa2379f28cd6d72a38299f286b81b459",
7071
"is_secret": false,
7172
"is_verified": false,
72-
"line_number": 155,
73+
"line_number": 156,
7374
"type": "Secret Keyword",
7475
"verified_result": null
7576
},
7677
{
7778
"hashed_secret": "5eb942810a75ebc850972a89285d570d484c89c4",
7879
"is_secret": false,
7980
"is_verified": false,
80-
"line_number": 167,
81+
"line_number": 168,
8182
"type": "Secret Keyword",
8283
"verified_result": null
8384
},
8485
{
8586
"hashed_secret": "4080eeeaf54faf879b9e8d99c49a8503f7e855bb",
8687
"is_secret": false,
8788
"is_verified": false,
88-
"line_number": 174,
89+
"line_number": 175,
8990
"type": "Secret Keyword",
9091
"verified_result": null
9192
},
9293
{
9394
"hashed_secret": "91dfd9ddb4198affc5c194cd8ce6d338fde470e2",
9495
"is_secret": false,
9596
"is_verified": false,
96-
"line_number": 188,
97+
"line_number": 189,
9798
"type": "Secret Keyword",
9899
"verified_result": null
99100
},
100101
{
101102
"hashed_secret": "47fcf185ee7e15fe05cae31fbe9e4ebe4a06a40d",
102103
"is_secret": false,
103104
"is_verified": false,
104-
"line_number": 194,
105+
"line_number": 195,
105106
"type": "Secret Keyword",
106107
"verified_result": null
107108
}
@@ -116,6 +117,16 @@
116117
"verified_result": null
117118
}
118119
],
120+
"resources/ibm-credentials-container.env": [
121+
{
122+
"hashed_secret": "4e44e97dae1aa4e93c01536f48bbd8602133a86d",
123+
"is_secret": false,
124+
"is_verified": false,
125+
"line_number": 8,
126+
"type": "Secret Keyword",
127+
"verified_result": null
128+
}
129+
],
119130
"resources/ibm-credentials-cp4d.env": [
120131
{
121132
"hashed_secret": "5eb942810a75ebc850972a89285d570d484c89c4",
@@ -197,25 +208,69 @@
197208
"hashed_secret": "da2f27d2c57a0e1ed2dc3a34b4ef02faf2f7a4c2",
198209
"is_secret": false,
199210
"is_verified": false,
200-
"line_number": 113,
211+
"line_number": 117,
201212
"type": "Hex High Entropy String",
202213
"verified_result": null
203214
}
204215
],
216+
"test/test_container_authenticator.py": [
217+
{
218+
"hashed_secret": "37e94c31b6a756ba2afd2fe9a9765172cd79ac47",
219+
"is_secret": false,
220+
"is_verified": false,
221+
"line_number": 65,
222+
"type": "Secret Keyword",
223+
"verified_result": null
224+
}
225+
],
226+
"test/test_container_token_manager.py": [
227+
{
228+
"hashed_secret": "c8f0df25bade89c1873f5f01b85bcfb921443ac6",
229+
"is_secret": false,
230+
"is_verified": false,
231+
"line_number": 14,
232+
"type": "JSON Web Token",
233+
"verified_result": null
234+
},
235+
{
236+
"hashed_secret": "f06e1073ca9afdd800a2cf27f944d06530b5b755",
237+
"is_secret": false,
238+
"is_verified": false,
239+
"line_number": 15,
240+
"type": "JSON Web Token",
241+
"verified_result": null
242+
},
243+
{
244+
"hashed_secret": "360c23c1ac7d9d6dad1d0710606b0df9de6e1a18",
245+
"is_secret": false,
246+
"is_verified": false,
247+
"line_number": 19,
248+
"type": "Secret Keyword",
249+
"verified_result": null
250+
},
251+
{
252+
"hashed_secret": "62cdb7020ff920e5aa642c3d4066950dd1f01f4d",
253+
"is_secret": false,
254+
"is_verified": false,
255+
"line_number": 121,
256+
"type": "Secret Keyword",
257+
"verified_result": null
258+
}
259+
],
205260
"test/test_cp4d_authenticator.py": [
206261
{
207262
"hashed_secret": "5eb942810a75ebc850972a89285d570d484c89c4",
208263
"is_secret": false,
209264
"is_verified": false,
210-
"line_number": 65,
265+
"line_number": 72,
211266
"type": "Secret Keyword",
212267
"verified_result": null
213268
},
214269
{
215270
"hashed_secret": "da2f27d2c57a0e1ed2dc3a34b4ef02faf2f7a4c2",
216271
"is_secret": false,
217272
"is_verified": false,
218-
"line_number": 104,
273+
"line_number": 111,
219274
"type": "Hex High Entropy String",
220275
"verified_result": null
221276
}
@@ -225,7 +280,7 @@
225280
"hashed_secret": "da2f27d2c57a0e1ed2dc3a34b4ef02faf2f7a4c2",
226281
"is_secret": false,
227282
"is_verified": false,
228-
"line_number": 30,
283+
"line_number": 32,
229284
"type": "Hex High Entropy String",
230285
"verified_result": null
231286
}
@@ -235,23 +290,23 @@
235290
"hashed_secret": "4080eeeaf54faf879b9e8d99c49a8503f7e855bb",
236291
"is_secret": false,
237292
"is_verified": false,
238-
"line_number": 45,
293+
"line_number": 49,
239294
"type": "Secret Keyword",
240295
"verified_result": null
241296
},
242297
{
243298
"hashed_secret": "37e94c31b6a756ba2afd2fe9a9765172cd79ac47",
244299
"is_secret": false,
245300
"is_verified": false,
246-
"line_number": 68,
301+
"line_number": 72,
247302
"type": "Secret Keyword",
248303
"verified_result": null
249304
},
250305
{
251306
"hashed_secret": "da2f27d2c57a0e1ed2dc3a34b4ef02faf2f7a4c2",
252307
"is_secret": false,
253308
"is_verified": false,
254-
"line_number": 93,
309+
"line_number": 97,
255310
"type": "Hex High Entropy String",
256311
"verified_result": null
257312
}
@@ -287,7 +342,7 @@
287342
"hashed_secret": "da2f27d2c57a0e1ed2dc3a34b4ef02faf2f7a4c2",
288343
"is_secret": false,
289344
"is_verified": false,
290-
"line_number": 37,
345+
"line_number": 39,
291346
"type": "Hex High Entropy String",
292347
"verified_result": null
293348
}
@@ -297,29 +352,29 @@
297352
"hashed_secret": "34a0a47a51d5bf739df0214450385e29ee7e9847",
298353
"is_secret": false,
299354
"is_verified": false,
300-
"line_number": 353,
355+
"line_number": 367,
301356
"type": "Secret Keyword",
302357
"verified_result": null
303358
},
304359
{
305360
"hashed_secret": "2863fa4b5510c46afc2bd2998dfbc0cf3d6df032",
306361
"is_secret": false,
307362
"is_verified": false,
308-
"line_number": 429,
363+
"line_number": 443,
309364
"type": "Secret Keyword",
310365
"verified_result": null
311366
},
312367
{
313368
"hashed_secret": "b9cad336062c0dc3bb30145b1a6697fccfe755a6",
314369
"is_secret": false,
315370
"is_verified": false,
316-
"line_number": 490,
371+
"line_number": 504,
317372
"type": "Secret Keyword",
318373
"verified_result": null
319374
}
320375
]
321376
},
322-
"version": "0.13.1+ibm.34.dss",
377+
"version": "0.13.1+ibm.38.dss",
323378
"word_list": {
324379
"file": null,
325380
"hash": null

Authentication.md

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ The python-sdk-core project supports the following types of authentication:
44
- Bearer Token
55
- Identity and Access Management (IAM)
66
- Cloud Pak for Data
7+
- Container
78
- No Authentication
89

910
The SDK user configures the appropriate type of authentication for use with service instances.
@@ -153,7 +154,7 @@ form:
153154
- password: (required if apikey is not specified) the password used to obtain a bearer token.
154155
- url: (required) The URL representing the Cloud Pak for Data token service endpoint.
155156
- apikey: (required if password is not specified) the apikey used to obtain a bearer token.
156-
- disableSSLVerification: (optional) A flag that indicates whether verificaton of the server's SSL
157+
- disable_ssl_verification: (optional) A flag that indicates whether verification of the server's SSL
157158
certificate should be disabled or not. The default value is `false`.
158159
- headers: (optional) A set of key/value pairs that will be sent as HTTP headers in requests
159160
made to the IAM token service.
@@ -202,6 +203,62 @@ authenticator = get_authenticator_from_environment('example_service')
202203
service = ExampleService(authenticator=authenticator)
203204
```
204205

206+
## Container
207+
The `ContainerAuthenticator` is intended to be used by application code
208+
running inside a compute resource managed by the IBM Kubernetes Service (IKS)
209+
in which a secure compute resource token (CR token) has been stored in a file
210+
within the compute resource's local file system.
211+
The CR token is similar to an IAM apikey except that it is managed automatically by
212+
the compute resource provider (IKS).
213+
This allows the application developer to:
214+
- avoid storing credentials in application code, configuraton files or a password vault
215+
- avoid managing or rotating credentials
216+
217+
The `ContainerAuthenticator` will retrieve the CR token from
218+
the compute resource in which the application is running, and will then perform
219+
the necessary interactions with the IAM token service to obtain an IAM access token
220+
using the IAM "get token" operation with grant-type `cr-token`.
221+
The authenticator will repeat these steps to obtain a new IAM access token when the
222+
current access token expires.
223+
The IAM access token is added to each outbound request in the `Authorization` header in the form:
224+
```
225+
Authorization: Bearer <IAM-access-token>
226+
```
227+
228+
### Properties
229+
- cr_token_filename: (optional) The name of the file containing the injected CR token value. If not specified, then `/var/run/secrets/tokens/vault-token` is used as the default value. The application must have `read` permissions on the file containing the CR token value.
230+
- iam_profile_name: (optional) The name of the linked trusted IAM profile to be used when obtaining the IAM access token (a CR token might map to multiple IAM profiles). One of `iam_profile_name` or `iam_profile_id` must be specified.
231+
- iam_profile_id: (optional) The ID of the linked trusted IAM profile to be used when obtaining the IAM access token (a CR token might map to multiple IAM profiles). One of `iam_profile_name` or `iam_profile_id` must be specified.
232+
- url: (optional) The URL representing the IAM token service endpoint. If not specified, a suitable default value is used.
233+
- client_id/client_secret: (optional) The `client_id` and `client_secret` fields are used to form a "basic auth" Authorization header for interactions with the IAM token server. If neither field is specified, then no Authorization header will be sent with token server requests. These fields are optional, but must be specified together.
234+
- disable_ssl_verification: (optional) A flag that indicates whether verification of the server's SSL certificate should be disabled or not. The default value is `False`.
235+
- scope (optional): the scope to be associated with the IAM access token.
236+
If not specified, then no scope will be associated with the access token.
237+
- proxies (optional): The proxy endpoint to use for HTTP(S) requests.
238+
- headers: (optional) A set of key/value pairs that will be sent as HTTP headers in requests made to the IAM token service.
239+
240+
### Programming example
241+
```python
242+
from ibm_cloud_sdk_core.authenticators import ContainerAuthenticatior
243+
244+
authenticator = ContainerAuthenticator(iam_profile_name='iam-user-123')
245+
service = ExampleService(authenticator=authenticator)
246+
```
247+
248+
### Configuration example
249+
External configuration:
250+
```
251+
export EXAMPLE_SERVICE_AUTH_TYPE=container
252+
export EXAMPLE_SERVICE_IAM_PROFILE_NAME=iam-user-123
253+
```
254+
Application code:
255+
```python
256+
from ibm_cloud_sdk_core import get_authenticator_from_environment
257+
258+
authenticator = get_authenticator_from_environment('example_service')
259+
service = ExampleService(authenticator=authenticator)
260+
```
261+
205262
## No Auth Authentication
206263
The `NoAuthAuthenticator` is a placeholder authenticator which performs no actual authentication function. It can be used in situations where authentication needs to be bypassed, perhaps while developing or debugging an application or service.
207264
### Properties

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ The python-sdk-core project supports the following types of authentication:
3131
- Bearer Token
3232
- Identity and Access Management (IAM)
3333
- Cloud Pak for Data
34+
- Container
3435
- No Authentication
3536

3637
For more information about the various authentication types and how to use them with your services, click [here](Authentication.md)

ibm_cloud_sdk_core/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
from .token_managers.iam_token_manager import IAMTokenManager
4242
from .token_managers.jwt_token_manager import JWTTokenManager
4343
from .token_managers.cp4d_token_manager import CP4DTokenManager
44+
from .token_managers.container_token_manager import ContainerTokenManager
4445
from .api_exception import ApiException
4546
from .utils import datetime_to_string, string_to_datetime, read_external_sources
4647
from .utils import datetime_to_string_list, string_to_datetime_list

ibm_cloud_sdk_core/authenticators/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from .authenticator import Authenticator
3737
from .basic_authenticator import BasicAuthenticator
3838
from .bearer_token_authenticator import BearerTokenAuthenticator
39+
from .container_authenticator import ContainerAuthenticator
3940
from .cp4d_authenticator import CloudPakForDataAuthenticator
4041
from .iam_authenticator import IAMAuthenticator
4142
from .no_auth_authenticator import NoAuthAuthenticator

0 commit comments

Comments
 (0)