Skip to content

Commit 765d33f

Browse files
feat(ContainerAuthenticator): add support for code engine workload (#218)
Signed-off-by: Sascha Schwarze <[email protected]>
1 parent db16465 commit 765d33f

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

Authentication.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,10 @@ service = ExampleServiceV1.new_instance(service_name='example_service')
317317
## Container Authentication
318318
The `ContainerAuthenticator` is intended to be used by application code
319319
running inside a compute resource managed by the IBM Kubernetes Service (IKS)
320-
in which a secure compute resource token (CR token) has been stored in a file
321-
within the compute resource's local file system.
320+
or IBM Cloud Code Engine in which a secure compute resource token (CR token)
321+
has been stored in a file within the compute resource's local file system.
322322
The CR token is similar to an IAM apikey except that it is managed automatically by
323-
the compute resource provider (IKS).
323+
the compute resource provider (IKS or Code Engine).
324324
This allows the application developer to:
325325
- avoid storing credentials in application code, configuration files or a password vault
326326
- avoid managing or rotating credentials
@@ -340,7 +340,9 @@ The IAM access token is added to each outbound request in the `Authorization` he
340340

341341
- cr_token_filename: (optional) The name of the file containing the injected CR token value.
342342
If not specified, then the authenticator will first try `/var/run/secrets/tokens/vault-token`
343-
and then `/var/run/secrets/tokens/sa-token` as the default value (first file found is used).
343+
and then `/var/run/secrets/tokens/sa-token` and finally
344+
`/var/run/secrets/codeengine.cloud.ibm.com/compute-resource-token/token` as the default value
345+
(first file found is used).
344346
The application must have `read` permissions on the file containing the CR token value.
345347

346348
- iam_profile_name: (optional) The name of the linked trusted IAM profile to be used when obtaining the

ibm_cloud_sdk_core/token_managers/container_token_manager.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# coding: utf-8
22

3-
# Copyright 2021, 2024 IBM All Rights Reserved.
3+
# Copyright 2021, 2025 IBM All Rights Reserved.
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -53,8 +53,9 @@ class ContainerTokenManager(IAMRequestBasedTokenManager):
5353
This can be used to obtain an access token with a specific scope.
5454
5555
Keyword Args:
56-
cr_token_filename: The name of the file containing the injected CR token value
57-
(applies to IKS-managed compute resources). Defaults to "/var/run/secrets/tokens/vault-token".
56+
cr_token_filename: The name of the file containing the injected CR token value. Defaults to
57+
"/var/run/secrets/tokens/vault-token", or "/var/run/secrets/tokens/sa-token" and
58+
"/var/run/secrets/codeengine.cloud.ibm.com/compute-resource-token/token" when not provided.
5859
iam_profile_name: The name of the linked trusted IAM profile to be used when obtaining the IAM access token
5960
(a CR token might map to multiple IAM profiles).
6061
One of iam_profile_name or iam_profile_id must be specified.
@@ -82,6 +83,7 @@ class ContainerTokenManager(IAMRequestBasedTokenManager):
8283

8384
DEFAULT_CR_TOKEN_FILENAME1 = '/var/run/secrets/tokens/vault-token'
8485
DEFAULT_CR_TOKEN_FILENAME2 = '/var/run/secrets/tokens/sa-token'
86+
DEFAULT_CR_TOKEN_FILENAME3 = '/var/run/secrets/codeengine.cloud.ibm.com/compute-resource-token/token'
8587

8688
def __init__(
8789
self,
@@ -129,11 +131,14 @@ def retrieve_cr_token(self) -> str:
129131
# If the user specified a filename, then use that.
130132
cr_token = self.read_file(self.cr_token_filename)
131133
else:
132-
# If the user didn't specify a filename, then try our two defaults.
134+
# If the user didn't specify a filename, then try our three defaults.
133135
try:
134136
cr_token = self.read_file(self.DEFAULT_CR_TOKEN_FILENAME1)
135137
except:
136-
cr_token = self.read_file(self.DEFAULT_CR_TOKEN_FILENAME2)
138+
try:
139+
cr_token = self.read_file(self.DEFAULT_CR_TOKEN_FILENAME2)
140+
except:
141+
cr_token = self.read_file(self.DEFAULT_CR_TOKEN_FILENAME3)
137142
return cr_token
138143
except Exception as ex:
139144
# pylint: disable=broad-exception-raised

0 commit comments

Comments
 (0)