-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Attempt to refresh credentials if we get access exceptions (#2890)
* restore the old refreshing step * try a mechanism to refresh credentials * widen the scope a little
- Loading branch information
Showing
4 changed files
with
142 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package refreshable | ||
|
||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the Apache License 2.0. | ||
|
||
import ( | ||
"github.com/Azure/go-autorest/autorest" | ||
|
||
"github.com/Azure/ARO-RP/pkg/env" | ||
) | ||
|
||
type Authorizer interface { | ||
autorest.Authorizer | ||
Rebuild() error | ||
} | ||
|
||
type authorizer struct { | ||
auth autorest.Authorizer | ||
env env.Interface | ||
tenantID string | ||
} | ||
|
||
func (a *authorizer) Rebuild() error { | ||
auth, err := a.env.FPAuthorizer(a.tenantID, a.env.Environment().ResourceManagerScope) | ||
if err != nil { | ||
return err | ||
} | ||
a.auth = auth | ||
return nil | ||
} | ||
|
||
func (a *authorizer) WithAuthorization() autorest.PrepareDecorator { | ||
return a.auth.WithAuthorization() | ||
} | ||
|
||
// NewAuthorizer creates an Authorizer that can be rebuilt when needed to force | ||
// token recreation. | ||
func NewAuthorizer(_env env.Interface, tenantID string) (Authorizer, error) { | ||
a := &authorizer{ | ||
env: _env, | ||
tenantID: tenantID, | ||
} | ||
err := a.Rebuild() | ||
return a, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package steps | ||
|
||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the Apache License 2.0. | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/sirupsen/logrus" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
|
||
"github.com/Azure/ARO-RP/pkg/util/azureerrors" | ||
"github.com/Azure/ARO-RP/pkg/util/refreshable" | ||
) | ||
|
||
var ErrWantRefresh = errors.New("want refresh") | ||
|
||
// AuthorizationRefreshingAction returns a wrapper Step which will refresh | ||
// `authorizer` if the step returns an Azure AuthenticationError and rerun it. | ||
// The step will be retried until `retryTimeout` is hit. Any other error will be | ||
// returned directly. | ||
func AuthorizationRetryingAction(r refreshable.Authorizer, action actionFunction) Step { | ||
return &authorizationRefreshingActionStep{ | ||
auth: r, | ||
f: action, | ||
} | ||
} | ||
|
||
type authorizationRefreshingActionStep struct { | ||
f actionFunction | ||
auth refreshable.Authorizer | ||
retryTimeout time.Duration | ||
pollInterval time.Duration | ||
} | ||
|
||
func (s *authorizationRefreshingActionStep) run(ctx context.Context, log *logrus.Entry) error { | ||
var pollInterval time.Duration | ||
var retryTimeout time.Duration | ||
|
||
// ARM role caching can be 5 minutes | ||
if s.retryTimeout == time.Duration(0) { | ||
retryTimeout = 10 * time.Minute | ||
} else { | ||
retryTimeout = s.retryTimeout | ||
} | ||
|
||
// If no pollInterval has been set, use a default | ||
if s.pollInterval == time.Duration(0) { | ||
pollInterval = 30 * time.Second | ||
} else { | ||
pollInterval = s.pollInterval | ||
} | ||
|
||
timeoutCtx, cancel := context.WithTimeout(ctx, retryTimeout) | ||
defer cancel() | ||
|
||
// Run the step immediately. If an Azure authorization error is returned and | ||
// we have not hit the retry timeout, the authorizer is refreshed and the | ||
// step is called again after runner.pollInterval. If we have timed out or | ||
// any other error is returned, the error from the step is returned | ||
// directly. | ||
return wait.PollImmediateUntil(pollInterval, func() (bool, error) { | ||
// We use the outer context, not the timeout context, as we do not want | ||
// to time out the condition function itself, only stop retrying once | ||
// timeoutCtx's timeout has fired. | ||
err := s.f(ctx) | ||
|
||
// If we haven't timed out and there is an error that is either an | ||
// unauthorized client (AADSTS700016) or "AuthorizationFailed" (likely | ||
// role propagation delay) then refresh and retry. | ||
if timeoutCtx.Err() == nil && err != nil && | ||
(azureerrors.IsUnauthorizedClientError(err) || | ||
azureerrors.HasAuthorizationFailedError(err)) { | ||
log.Printf("auth error, refreshing and retrying: %v", err) | ||
// Try refreshing auth. | ||
err = s.auth.Rebuild() | ||
return false, err // retry step | ||
} | ||
return true, err | ||
}, timeoutCtx.Done()) | ||
} | ||
|
||
func (s *authorizationRefreshingActionStep) String() string { | ||
return fmt.Sprintf("[AuthorizationRetryingAction %s]", FriendlyName(s.f)) | ||
} | ||
|
||
func (s *authorizationRefreshingActionStep) metricsName() string { | ||
return fmt.Sprintf("authorizationretryingaction.%s", shortName(FriendlyName(s.f))) | ||
} |