forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
timeoutvalidator.go
261 lines (221 loc) · 8.2 KB
/
timeoutvalidator.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package internaloauth
import (
"errors"
"time"
"github.com/golang/glog"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/runtime"
userv1 "github.com/openshift/api/user/v1"
"github.com/openshift/origin/pkg/oauth/apis/oauth"
oauthclient "github.com/openshift/origin/pkg/oauth/generated/internalclientset/typed/oauth/internalversion"
oauthclientlister "github.com/openshift/origin/pkg/oauth/generated/listers/oauth/internalversion"
"github.com/openshift/origin/pkg/util/rankedset"
)
var errTimedout = errors.New("token timed out")
// Implements rankedset.Item
var _ = rankedset.Item(&tokenData{})
type tokenData struct {
token *oauth.OAuthAccessToken
seen time.Time
}
func (a *tokenData) timeout() time.Time {
return a.token.CreationTimestamp.Time.Add(time.Duration(a.token.InactivityTimeoutSeconds) * time.Second)
}
func (a *tokenData) Key() string {
return a.token.Name
}
func (a *tokenData) Rank() int64 {
return a.timeout().Unix()
}
func timeoutAsDuration(timeout int32) time.Duration {
return time.Duration(timeout) * time.Second
}
// This interface is used to allow mocking time from unit tests
// NOTE: Never use time.Now() directly in the code, use this interface
// Now() function instead.
type internalTickerInterface interface {
Now() time.Time
NewTicker(d time.Duration)
C() <-chan time.Time
Stop()
}
type internalTicker struct {
ticker *time.Ticker
}
func (t *internalTicker) Now() time.Time {
return time.Now()
}
func (t *internalTicker) C() <-chan time.Time {
return t.ticker.C
}
func (t *internalTicker) Stop() {
t.ticker.Stop()
}
func (t *internalTicker) NewTicker(d time.Duration) {
t.ticker = time.NewTicker(d)
}
type TimeoutValidator struct {
oauthClients oauthclientlister.OAuthClientLister
tokens oauthclient.OAuthAccessTokenInterface
tokenChannel chan *tokenData
data *rankedset.RankedSet
defaultTimeout time.Duration
tickerInterval time.Duration
// fields that are used to have a deterministic order of events in unit tests
flushHandler func(flushHorizon time.Time) // allows us to decorate this func during unit tests
putTokenHandler func(td *tokenData) // allows us to decorate this func during unit tests
ticker internalTickerInterface // allows us to control time during unit tests
}
func NewTimeoutValidator(tokens oauthclient.OAuthAccessTokenInterface, oauthClients oauthclientlister.OAuthClientLister, defaultTimeout int32, minValidTimeout int32) *TimeoutValidator {
a := &TimeoutValidator{
oauthClients: oauthClients,
tokens: tokens,
tokenChannel: make(chan *tokenData),
data: rankedset.New(),
defaultTimeout: timeoutAsDuration(defaultTimeout),
tickerInterval: timeoutAsDuration(minValidTimeout / 3), // we tick at least 3 times within each timeout period
ticker: &internalTicker{},
}
a.flushHandler = a.flush
a.putTokenHandler = a.putToken
glog.V(5).Infof("Token Timeout Validator primed with defaultTimeout=%s tickerInterval=%s", a.defaultTimeout, a.tickerInterval)
return a
}
// Validate is called with a token when it is seen by an authenticator
// it touches only the tokenChannel so it is safe to call from other threads
func (a *TimeoutValidator) Validate(token *oauth.OAuthAccessToken, _ *userv1.User) error {
if token.InactivityTimeoutSeconds == 0 {
// We care only if the token was created with a timeout to start with
return nil
}
td := &tokenData{
token: token,
seen: a.ticker.Now(),
}
if td.timeout().Before(td.seen) {
return errTimedout
}
if token.ExpiresIn != 0 && token.ExpiresIn <= int64(token.InactivityTimeoutSeconds) {
// skip if the timeout is already larger than expiration deadline
return nil
}
// After a positive timeout check we need to update the timeout and
// schedule an update so that we can either set or update the Timeout
// we do that launching a micro goroutine to avoid blocking
go a.putTokenHandler(td)
return nil
}
func (a *TimeoutValidator) putToken(td *tokenData) {
a.tokenChannel <- td
}
func (a *TimeoutValidator) clientTimeout(name string) time.Duration {
oauthClient, err := a.oauthClients.Get(name)
if err != nil {
glog.V(5).Infof("Failed to fetch OAuthClient %q for timeout value: %v", name, err)
return a.defaultTimeout
}
if oauthClient.AccessTokenInactivityTimeoutSeconds == nil {
return a.defaultTimeout
}
return timeoutAsDuration(*oauthClient.AccessTokenInactivityTimeoutSeconds)
}
func (a *TimeoutValidator) update(td *tokenData) error {
// Obtain the timeout interval for this client
delta := a.clientTimeout(td.token.ClientName)
// if delta is 0 it means the OAuthClient has been changed to the
// no-timeout value. In this case we set newTimeout also to 0 so
// that the token will no longer timeout once updated.
newTimeout := int32(0)
if delta > 0 {
// InactivityTimeoutSeconds is the number of seconds since creation:
// InactivityTimeoutSeconds = Seen(Time) - CreationTimestamp(Time) + delta(Duration)
newTimeout = int32((td.seen.Sub(td.token.CreationTimestamp.Time) + delta) / time.Second)
}
// We need to get the token again here because it may have changed in the
// DB and we need to verify it is still worth updating
token, err := a.tokens.Get(td.token.Name, v1.GetOptions{})
if err != nil {
return err
}
if newTimeout != 0 && token.InactivityTimeoutSeconds >= newTimeout {
// if the token was already updated with a higher or equal timeout we
// do not have anything to do
return nil
}
token.InactivityTimeoutSeconds = newTimeout
_, err = a.tokens.Update(token)
return err
}
func (a *TimeoutValidator) flush(flushHorizon time.Time) {
// flush all tokens that are about to expire before the flushHorizon.
// Typically the flushHorizon is set to a time slightly past the next
// ticker interval, so that not token ends up timing out between flushes
glog.V(5).Infof("Flushing tokens timing out before %s", flushHorizon)
// grab all tokens that need to be update in this flush interval
// and remove them from the stored data, they either flush now or never
tokenList := a.data.LessThan(flushHorizon.Unix(), true)
var retryList []*tokenData
flushedTokens := 0
for _, item := range tokenList {
td := item.(*tokenData)
err := a.update(td)
// not logging the full errors here as it would leak the token.
switch {
case err == nil:
flushedTokens++
case apierrors.IsConflict(err) || apierrors.IsServerTimeout(err):
glog.V(5).Infof("Token update deferred for token belonging to %s",
td.token.UserName)
retryList = append(retryList, td)
default:
glog.V(5).Infof("Token timeout for user=%q client=%q scopes=%v was not updated",
td.token.UserName, td.token.ClientName, td.token.Scopes)
}
}
// we try once more and if it still fails we stop trying here and defer
// to a future regular update if the token is used again
for _, td := range retryList {
err := a.update(td)
if err != nil {
glog.V(5).Infof("Token timeout for user=%q client=%q scopes=%v was not updated",
td.token.UserName, td.token.ClientName, td.token.Scopes)
} else {
flushedTokens++
}
}
glog.V(5).Infof("Successfully flushed %d tokens out of %d",
flushedTokens, len(tokenList))
}
func (a *TimeoutValidator) nextTick() time.Time {
// Add a small safety Margin so flushes tend to
// overlap a little rather than have gaps
return a.ticker.Now().Add(a.tickerInterval + 10*time.Second)
}
func (a *TimeoutValidator) Run(stopCh <-chan struct{}) {
defer runtime.HandleCrash()
glog.V(5).Infof("Started Token Timeout Flush Handling thread!")
a.ticker.NewTicker(a.tickerInterval)
// make sure to kill the ticker when we exit
defer a.ticker.Stop()
nextTick := a.nextTick()
for {
select {
case <-stopCh:
// if channel closes terminate
return
case td := <-a.tokenChannel:
a.data.Insert(td)
// if this token is going to time out before the timer, flush now
tokenTimeout := td.timeout()
if tokenTimeout.Before(nextTick) {
glog.V(5).Infof("Timeout for user=%q client=%q scopes=%v falls before next ticker (%s < %s), forcing flush!",
td.token.UserName, td.token.ClientName, td.token.Scopes, tokenTimeout, nextTick)
a.flushHandler(nextTick)
}
case <-a.ticker.C():
nextTick = a.nextTick()
a.flushHandler(nextTick)
}
}
}