Skip to content

Commit

Permalink
Fix missing token renew statistics (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
p53 authored Apr 8, 2021
1 parent 6a81b2b commit 645400c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
46 changes: 45 additions & 1 deletion middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,24 @@ func TestMetricsMiddleware(t *testing.T) {
cfg := newFakeKeycloakConfig()
cfg.EnableMetrics = true
cfg.LocalhostMetrics = true
cfg.EnableRefreshTokens = true
cfg.EnableEncryptedToken = true
cfg.EncryptionKey = testEncryptionKey
requests := []fakeRequest{
{
URI: fakeAuthAllURL,
HasLogin: true,
Redirects: true,
OnResponse: delay,
ExpectedProxy: true,
ExpectedCode: http.StatusOK,
},
{
URI: fakeAuthAllURL,
Redirects: false,
ExpectedProxy: true,
ExpectedCode: http.StatusOK,
},
{
URI: cfg.WithOAuthURI(metricsURL),
Headers: map[string]string{
Expand All @@ -407,8 +424,35 @@ func TestMetricsMiddleware(t *testing.T) {
ExpectedCode: http.StatusOK,
ExpectedContentContains: "proxy_request_status_total",
},
{
URI: cfg.WithOAuthURI(metricsURL),
ExpectedCode: http.StatusOK,
ExpectedContentContains: "action=\"issued\"",
},
{
URI: cfg.WithOAuthURI(metricsURL),
ExpectedCode: http.StatusOK,
ExpectedContentContains: "action=\"exchange\"",
},
{
URI: cfg.WithOAuthURI(metricsURL),
ExpectedCode: http.StatusOK,
ExpectedContentContains: "action=\"login\"",
},
{
URI: cfg.WithOAuthURI(metricsURL),
ExpectedCode: http.StatusOK,
ExpectedContentContains: "action=\"logout\"",
},
{
URI: cfg.WithOAuthURI(metricsURL),
ExpectedCode: http.StatusOK,
ExpectedContentContains: "action=\"renew\"",
},
}
newFakeProxy(cfg, &fakeAuthConfig{}).RunTests(t, requests)
p := newFakeProxy(cfg, &fakeAuthConfig{})
p.idp.setTokenExpiration(1000 * time.Millisecond)
p.RunTests(t, requests)
}

func TestOauthRequests(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func getRefreshedToken(conf *oauth2.Config, proxyConfig *Config, t string) (jwt.

defer cancel()

start := time.Now()
tkn, err := conf.TokenSource(ctx, &oauth2.Token{RefreshToken: t}).Token()

if err != nil {
Expand All @@ -83,6 +84,11 @@ func getRefreshedToken(conf *oauth2.Config, proxyConfig *Config, t string) (jwt.
}
return jwt.JSONWebToken{}, "", "", time.Time{}, time.Duration(0), err
}

taken := time.Since(start).Seconds()
oauthTokensMetric.WithLabelValues("renew").Inc()
oauthLatencyMetric.WithLabelValues("renew").Observe(taken)

refreshExpiresIn := time.Until(tkn.Expiry)
token, err := jwt.ParseSigned(tkn.AccessToken)

Expand Down

0 comments on commit 645400c

Please sign in to comment.