Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,8 @@ The IAM access token is added to each outbound request in the `Authorization` he
### Properties

- CRTokenFilename: (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.
If not specified, then the authenticator will first try `/var/run/secrets/tokens/vault-token`
and then `/var/run/secrets/tokens/sa-token` as the default value (first file found is used).
The application must have `read` permissions on the file containing the CR token value.

- IAMProfileName: (optional) the name of the linked trusted IAM profile to be used when obtaining the
Expand Down
42 changes: 30 additions & 12 deletions core/container_authenticator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package core

// (C) Copyright IBM Corp. 2021.
// (C) Copyright IBM Corp. 2021, 2023..
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -39,7 +39,8 @@ type ContainerAuthenticator struct {

// [optional] The name of the file containing the injected CR token value (applies to
// IKS-managed compute resources).
// Default value: "/var/run/secrets/tokens/vault-token"
// Default value: (1) "/var/run/secrets/tokens/vault-token" or (2) "/var/run/secrets/tokens/sa-token",
// whichever is found first.
CRTokenFilename string

// [optional] The name of the linked trusted IAM profile to be used when obtaining the IAM access token.
Expand Down Expand Up @@ -94,8 +95,9 @@ type ContainerAuthenticator struct {
}

const (
defaultCRTokenFilename = "/var/run/secrets/tokens/vault-token" // #nosec G101
iamGrantTypeCRToken = "urn:ibm:params:oauth:grant-type:cr-token" // #nosec G101
defaultCRTokenFilename1 = "/var/run/secrets/tokens/vault-token" // #nosec G101
defaultCRTokenFilename2 = "/var/run/secrets/tokens/sa-token" // #nosec G101
iamGrantTypeCRToken = "urn:ibm:params:oauth:grant-type:cr-token" // #nosec G101
)

var craRequestTokenMutex sync.Mutex
Expand Down Expand Up @@ -479,25 +481,41 @@ func (authenticator *ContainerAuthenticator) RequestToken() (*IamTokenServerResp
// retrieveCRToken tries to read the CR token value from the local file system.
func (authenticator *ContainerAuthenticator) retrieveCRToken() (crToken string, err error) {

// Use the default filename if one wasn't supplied by the user.
crTokenFilename := authenticator.CRTokenFilename
if crTokenFilename == "" {
crTokenFilename = defaultCRTokenFilename
if authenticator.CRTokenFilename != "" {
// Use the file specified by the user.
crToken, err = authenticator.readFile(authenticator.CRTokenFilename)
} else {
// If the user didn't specify a filename, try our two defaults.
crToken, err = authenticator.readFile(defaultCRTokenFilename1)
if err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is perfectly fine here, but a question came to my mind and can't let it go. We would like to try to read the 2nd file, even if the error is not a FileNotFound but - let's say - a permission issue, right? Really just asking... :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question... yes, I thought about the various errors that might occur and whether we should treat a permissions issue differently than a "file doesn't exist at all" issue.
I concluded that we should try to keep this simple and treat any error while trying to read the file the same... i.e. we couldn't read it (whatever the reason) so move on to the next one to try. I think it's extremely unlikely that we'd encounter a scenario where both default files (vault-token and sa-token) exist and we end up using "sa-token" by mistake because "vault-token" is not readable by the application.

crToken, err = authenticator.readFile(defaultCRTokenFilename2)
}
}

if err != nil {
err = fmt.Errorf(ERRORMSG_UNABLE_RETRIEVE_CRTOKEN, err.Error())
GetLogger().Debug(err.Error())
return
}

GetLogger().Debug("Attempting to read CR token from file: %s\n", crTokenFilename)
return
}

// readFile attempts to read the specified cr token file and return its contents as a string.
func (authenticator *ContainerAuthenticator) readFile(filename string) (crToken string, err error) {
GetLogger().Debug("Attempting to read CR token from file: %s\n", filename)

// Read the entire file into a byte slice, then convert to string.
var bytes []byte
bytes, err = os.ReadFile(crTokenFilename) // #nosec G304
bytes, err = os.ReadFile(filename) // #nosec G304
if err != nil {
err = fmt.Errorf(ERRORMSG_UNABLE_RETRIEVE_CRTOKEN, err.Error())
GetLogger().Debug(err.Error())
return
}

GetLogger().Debug("Successfully read CR token from file: %s\n", filename)

crToken = string(bytes)
GetLogger().Debug("Successfully read CR token from file: %s\n", crTokenFilename)

return
}