Skip to content

Commit

Permalink
Fix MSI token refresh
Browse files Browse the repository at this point in the history
Signed-off-by: Philip Laine <philip.laine@xenit.se>
  • Loading branch information
Philip Laine committed Aug 30, 2021
1 parent 702752a commit b1fb8d4
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions pkg/objstore/azure/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,31 @@ func getAzureStorageCredentials(conf Config) (blob.Credential, error) {
if conf.MSIResource != "" {
msiConfig := auth.NewMSIConfig()
msiConfig.Resource = conf.MSIResource

azureServicePrincipalToken, err := msiConfig.ServicePrincipalToken()
if err != nil {
return nil, err
}

// Get a new token.
var tokenRefresher blob.TokenRefresher = func(credential blob.TokenCredential) time.Duration {
err := azureServicePrincipalToken.Refresh()
if err != nil {
// Retry later as the error can be related to API throttling
return 30 * time.Second
}
token := azureServicePrincipalToken.Token()
credential.SetToken(token.AccessToken)
// Refresh token two minutes before actual expiration
exp := token.Expires().Sub(time.Now().Add(2 * time.Minute))
return exp
}

err = azureServicePrincipalToken.Refresh()
if err != nil {
return nil, err
}
token := azureServicePrincipalToken.Token()
initialToken := azureServicePrincipalToken.Token()

return blob.NewTokenCredential(token.AccessToken, nil), nil
return blob.NewTokenCredential(initialToken.AccessToken, tokenRefresher), nil
}

credential, err := blob.NewSharedKeyCredential(conf.StorageAccountName, conf.StorageAccountKey)
Expand Down

0 comments on commit b1fb8d4

Please sign in to comment.