Skip to content

Commit b3fd0ff

Browse files
committed
chore: Expire token 30 seconds early to mitigate mid-reconcile expirations
1 parent 9fc4378 commit b3fd0ff

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

controllers/client/grafana_client.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ type JWTCache struct {
3737

3838
var jwtCache *JWTCache
3939

40+
// Revoke tokens early expecting them to be rotated hourly, see 'ExpirationSeconds' in KEP1205
41+
// Should mitigate mid-reconcile expiration
42+
const tokenExpirationCompensation = -30 * time.Second
43+
4044
// getBearerToken will read JWT token from given file and cache it until it expires.
4145
// accepts filepath arg for testing
4246
func getBearerToken(bearerTokenPath string) (string, error) {
@@ -72,13 +76,13 @@ func getBearerToken(bearerTokenPath string) (string, error) {
7276
}
7377

7478
tokenExpiration := claims.Expiry.Time()
75-
if tokenExpiration.Before(time.Now()) {
76-
return "", fmt.Errorf("token expired at %s, expected %s to be rotated", tokenExpiration.String(), bearerTokenPath)
79+
if tokenExpiration.Add(tokenExpirationCompensation).Before(time.Now()) {
80+
return "", fmt.Errorf("token expired at %s, expected %s to be renewed. Tokens are considered expired 30 seconds early", tokenExpiration.String(), bearerTokenPath)
7781
}
7882

7983
jwtCache = &JWTCache{
8084
Token: token,
81-
Expiration: tokenExpiration,
85+
Expiration: tokenExpiration.Add(tokenExpirationCompensation),
8286
}
8387

8488
return token, nil

controllers/client/grafana_client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ func TestGetBearerToken(t *testing.T) {
330330
Audience: jwt.Audience{"https://grafana.operator.com"},
331331
IssuedAt: jwt.NewNumericDate(now),
332332
NotBefore: jwt.NewNumericDate(now),
333-
Expiry: jwt.NewNumericDate(now.Add(time.Duration(30 * float64(time.Second)))),
333+
Expiry: jwt.NewNumericDate(now.Add(time.Duration(60 * float64(time.Second)))),
334334
}
335335

336336
// Generate key

0 commit comments

Comments
 (0)