-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathrefreshing.go
94 lines (79 loc) · 2.95 KB
/
refreshing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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) ||
azureerrors.IsInvalidSecretError(err) ||
err == ErrWantRefresh) {
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)))
}