Skip to content

Commit f695c94

Browse files
committed
Renew on-demand rather than on a schedule
1 parent f5db0e9 commit f695c94

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

cmd/state-svc/internal/resolver/resolver.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ func New(cfg *config.Instance, an *sync.Client, auth *authentication.Auth) (*Res
7777

7878
mostRecentActivity := ptr.To(time.Now())
7979
pollAuth := poller.New(pollRateDuration, func() (interface{}, error) {
80-
if err := auth.MaybeRenew(time.Now().Add(pollRateDuration)); err != nil {
81-
return nil, errs.Wrap(err, "Could not renew auth")
80+
if auth.SyncRequired() {
81+
return nil, auth.Sync()
8282
}
8383
return nil, nil
8484
})
@@ -278,8 +278,8 @@ func (r *Resolver) GetProcessesInUse(ctx context.Context, execDir string) ([]*gr
278278
}
279279

280280
func (r *Resolver) GetJwt(ctx context.Context) (*graph.Jwt, error) {
281-
if r.auth.SyncRequired() {
282-
return nil, r.auth.Sync()
281+
if err := r.auth.MaybeRenew(); err != nil {
282+
return nil, errs.Wrap(err, "Could not renew auth token")
283283
}
284284

285285
if !r.auth.Authenticated() {

pkg/platform/authentication/auth.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ type ErrTokenRequired struct{ *locale.LocalizedError }
3636

3737
var errNotYetGranted = locale.NewInputError("err_auth_device_noauth")
3838

39-
// jwtKeepaliveDuration determines how long after the last state tool interaction we want to keep the JWT alive.
40-
const jwtKeepaliveDuration = (6 * time.Hour)
41-
4239
// jwtLifetime is the lifetime of the JWT. This is defined by the API, but the API doesn't communicate this.
4340
// We drop a minute from this to avoid race conditions with the API.
4441
const jwtLifetime = (1 * time.Hour) - (1 * time.Minute)
@@ -123,8 +120,9 @@ func (s *Auth) Sync() error {
123120
return nil
124121
}
125122

126-
// MaybeRenew will renew the JWT if it is set to expire before the provided cutoff
127-
func (s *Auth) MaybeRenew(cutoff time.Time) error {
123+
// MaybeRenew will renew the JWT if it has expired
124+
// This should only be called from the state-svc.
125+
func (s *Auth) MaybeRenew() error {
128126
// If we're out of sync then we should just always renew
129127
if s.SyncRequired() {
130128
err := s.Sync()
@@ -139,8 +137,8 @@ func (s *Auth) MaybeRenew(cutoff time.Time) error {
139137
return nil
140138
}
141139

142-
if s.cutoffReached(cutoff) {
143-
logging.Debug("Refreshing JWT as it will expire before the cutoff (%s)", cutoff.String())
140+
if s.cutoffReached(time.Now()) {
141+
logging.Debug("Refreshing JWT as has expired")
144142
return s.AuthenticateWithToken(s.AvailableAPIToken())
145143
}
146144

0 commit comments

Comments
 (0)