Skip to content

Commit

Permalink
Token file path should take precedence over token env var
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaddalozzo committed Sep 5, 2024
1 parent 8b0ec54 commit 5a0ea83
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions plugins/rest/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,18 +278,22 @@ func (cs *awsMetadataCredentialService) refreshFromService(ctx context.Context)
// if using the AWS_CONTAINER_CREDENTIALS_FULL_URI variable, we need to associate the token
// to the request
if _, useFullPath := os.LookupEnv(ecsFullPathEnvVar); useFullPath {
token, tokenExists := os.LookupEnv(ecsAuthorizationTokenEnvVar)
// If token doesn't exist as an env var check if it exists on the file system (e.g. for pod identities)
if !tokenExists {
tokenFilePath, tokenFilePathExists := os.LookupEnv(ecsAuthorizationTokenFileEnvVar)
if !tokenFilePathExists {
return errors.New("unable to get ECS metadata authorization token")
}
var token string
tokenFilePath, tokenFilePathExists := os.LookupEnv(ecsAuthorizationTokenFileEnvVar)

if tokenFilePathExists {
tokenBytes, err := os.ReadFile(tokenFilePath)
if err != nil {
return errors.New("failed to read ECS metadata authorization token from file: " + err.Error())
}
token = string(tokenBytes)
// If token doesn't exist as a file check if it exists as an environment variable
} else {
var tokenExists bool
token, tokenExists = os.LookupEnv(ecsAuthorizationTokenEnvVar)
if !tokenExists {
return errors.New("unable to get ECS metadata authorization token")
}
}
req.Header.Set("Authorization", token)
}
Expand Down

0 comments on commit 5a0ea83

Please sign in to comment.