Skip to content
This repository has been archived by the owner on Jun 5, 2023. It is now read-only.

Commit

Permalink
fix: reordered the auth methods
Browse files Browse the repository at this point in the history
  • Loading branch information
siredmar committed Sep 29, 2022
1 parent 0c94773 commit 06e37c8
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions pkg/certretrieval/certretrieval.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,32 +250,34 @@ func (cr *CertRetrieval) loginViaServiceAccount() (string, error) {
}

// readToken retrieves the Vault token from either the serviceaccount
// mechanism or the file system
// mechanism or the file system.
func (cr *CertRetrieval) readToken() (string, error) {
if cr.Token != "" {
klog.Infof("Using token from env variable")
return cr.Token, nil
}

if cr.Tokenfile != "" {
data, err := os.ReadFile(cr.Tokenfile)
if err != nil {
return "", err
}
return strings.TrimSpace(string(data)), nil
}

_, err := os.Stat(ServiceAccountPath)
if err == nil {
// Service account file exists, use it
token, err := cr.loginViaServiceAccount()
if err != nil {
return "", fmt.Errorf("failed to retrieve token via servic account: %v", err)
return "", fmt.Errorf("failed to retrieve token via service account: %v", err)
}
return token, nil
} else {
klog.Warningf("Cannot read service account file, continuing")
}

if cr.Token != "" {
klog.Infof("Using token from env variable")
return cr.Token, nil
}

data, err := os.ReadFile(cr.Tokenfile)
if err != nil {
return "", err
}
token := strings.TrimSpace(string(data))

return token, nil
return "", fmt.Errorf("Failed to retrieve the token from any source (Token, Tokenfile or Service Account)")
}

// retrieveCert executes the http request to retrieve a new certificate from vault
Expand Down

0 comments on commit 06e37c8

Please sign in to comment.