Skip to content

Commit 80518d2

Browse files
feat(ContainerAuthenticator): add support for code engine workload (#244)
Signed-off-by: Sascha Schwarze <[email protected]> Co-authored-by: Phil Adams <[email protected]>
1 parent 3289d00 commit 80518d2

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

Authentication.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -452,10 +452,10 @@ if err != nil {
452452
## Container Authentication
453453
The `ContainerAuthenticator` is intended to be used by application code
454454
running inside a compute resource managed by the IBM Kubernetes Service (IKS)
455-
in which a secure compute resource token (CR token) has been stored in a file
456-
within the compute resource's local file system.
455+
or IBM Cloud Code Engine in which a secure compute resource token (CR token)
456+
has been stored in a file within the compute resource's local file system.
457457
The CR token is similar to an IAM apikey except that it is managed automatically by
458-
the compute resource provider (IKS).
458+
the compute resource provider (IKS or Code Engine).
459459
This allows the application developer to:
460460
- avoid storing credentials in application code, configuration files or a password vault
461461
- avoid managing or rotating credentials
@@ -475,7 +475,9 @@ The IAM access token is added to each outbound request in the `Authorization` he
475475

476476
- CRTokenFilename: (optional) the name of the file containing the injected CR token value.
477477
If not specified, then the authenticator will first try `/var/run/secrets/tokens/vault-token`
478-
and then `/var/run/secrets/tokens/sa-token` as the default value (first file found is used).
478+
and then `/var/run/secrets/tokens/sa-token` and finally
479+
`/var/run/secrets/codeengine.cloud.ibm.com/compute-resource-token/token` as the default value
480+
(first file found is used).
479481
The application must have `read` permissions on the file containing the CR token value.
480482

481483
- IAMProfileName: (optional) the name of the linked trusted IAM profile to be used when obtaining the

core/container_authenticator.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package core
22

3-
// (C) Copyright IBM Corp. 2021, 2024.
3+
// (C) Copyright IBM Corp. 2021, 2025.
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.
@@ -29,16 +29,16 @@ import (
2929
)
3030

3131
// ContainerAuthenticator implements an IAM-based authentication schema whereby it
32-
// retrieves a "compute resource token" from the local compute resource (VM)
32+
// retrieves a "compute resource token" from the local compute resource (IKS pod, or Code Engine application, function, or job)
3333
// and uses that to obtain an IAM access token by invoking the IAM "get token" operation with grant-type=cr-token.
3434
// The resulting IAM access token is then added to outbound requests in an Authorization header
3535
// of the form:
3636
//
3737
// Authorization: Bearer <access-token>
3838
type ContainerAuthenticator struct {
3939
// [optional] The name of the file containing the injected CR token value (applies to
40-
// IKS-managed compute resources).
41-
// Default value: (1) "/var/run/secrets/tokens/vault-token" or (2) "/var/run/secrets/tokens/sa-token",
40+
// IKS-managed compute resources, a Code Engine compute resource always uses the third default from below).
41+
// Default value: (1) "/var/run/secrets/tokens/vault-token" or (2) "/var/run/secrets/tokens/sa-token" or (3) "/var/run/secrets/codeengine.cloud.ibm.com/compute-resource-token/token",
4242
// whichever is found first.
4343
CRTokenFilename string
4444

@@ -98,9 +98,10 @@ type ContainerAuthenticator struct {
9898
}
9999

100100
const (
101-
defaultCRTokenFilename1 = "/var/run/secrets/tokens/vault-token" // #nosec G101
102-
defaultCRTokenFilename2 = "/var/run/secrets/tokens/sa-token" // #nosec G101
103-
iamGrantTypeCRToken = "urn:ibm:params:oauth:grant-type:cr-token" // #nosec G101
101+
defaultCRTokenFilename1 = "/var/run/secrets/tokens/vault-token" // #nosec G101
102+
defaultCRTokenFilename2 = "/var/run/secrets/tokens/sa-token" // #nosec G101
103+
defaultCRTokenFilename3 = "/var/run/secrets/codeengine.cloud.ibm.com/compute-resource-token/token" // #nosec G101
104+
iamGrantTypeCRToken = "urn:ibm:params:oauth:grant-type:cr-token" // #nosec G101
104105
)
105106

106107
var craRequestTokenMutex sync.Mutex
@@ -504,6 +505,9 @@ func (authenticator *ContainerAuthenticator) retrieveCRToken() (crToken string,
504505
crToken, err = authenticator.readFile(defaultCRTokenFilename1)
505506
if err != nil {
506507
crToken, err = authenticator.readFile(defaultCRTokenFilename2)
508+
if err != nil {
509+
crToken, err = authenticator.readFile(defaultCRTokenFilename3)
510+
}
507511
}
508512
}
509513

0 commit comments

Comments
 (0)