Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/smdclient/SMDclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type SMDClientInterface interface {

// SMDClient is a client for SMD
type SMDClient struct {
refreshLock sync.Mutex
clusterName string
smdClient *http.Client
smdBaseURL string
Expand Down
53 changes: 44 additions & 9 deletions internal/smdclient/oidc.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package smdclient

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)

// Structure of a token reponse from OIDC server
Expand All @@ -19,23 +22,53 @@ type oidcTokenData struct {
// authorization grant. Support for said grant should probably be implemented
// at some point.
func (s *SMDClient) RefreshToken() error {
s.accessTokenMutex.Lock()
defer s.accessTokenMutex.Unlock()
return s.refreshTokenLocked()
// Serialize refresh to avoid concurrent token fetches.
s.refreshLock.Lock()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think s.refreshLock doesn't exist in this branch.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, somehow did not get committed (fe17047). I also somehow missed running go build to catch that...

defer s.refreshLock.Unlock()
ctx, cancel := context.WithTimeout(context.Background(), defaultRefreshTimeout)
defer cancel()
return s.refreshTokenWithContext(ctx)
}

const defaultRefreshTimeout = 10 * time.Second

func (s *SMDClient) refreshTokenIfCurrent(rejectedToken string) error {
// Fast path: if token already different, nothing to do.
s.accessTokenMutex.Lock()
if s.accessToken != rejectedToken {
s.accessTokenMutex.Unlock()
return nil
}
s.accessTokenMutex.Unlock()

// Serialize refresh to avoid concurrent token fetches.
s.refreshLock.Lock()
defer s.refreshLock.Unlock()

// Re-check token after acquiring lock (it may have been refreshed by another goroutine).
s.accessTokenMutex.Lock()
defer s.accessTokenMutex.Unlock()
if s.accessToken != rejectedToken {
s.accessTokenMutex.Unlock()
return nil
}
return s.refreshTokenLocked()
s.accessTokenMutex.Unlock()

// Acquire new token with timeout.
ctx, cancel := context.WithTimeout(context.Background(), defaultRefreshTimeout)
defer cancel()
return s.refreshTokenWithContext(ctx)
}

func (s *SMDClient) refreshTokenLocked() error {
// Request new token from OIDC server
r, err := http.Get(s.tokenEndpoint)
func (s *SMDClient) refreshTokenWithContext(ctx context.Context) error {
// Request new token from OIDC server using the provided context.
req, err := http.NewRequestWithContext(ctx, "GET", s.tokenEndpoint, nil)
if err != nil {
return err
}
if s.smdClient == nil {
return fmt.Errorf("SMD HTTP client was nil (was NewSMDClient() run?)")
}
r, err := s.smdClient.Do(req)
if err != nil {
return err
}
Expand All @@ -49,7 +82,9 @@ func (s *SMDClient) refreshTokenLocked() error {
if err = json.Unmarshal(body, &tokenResp); err != nil {
return err
}
// Extract and store the JWT itself
// Store the JWT safely.
s.accessTokenMutex.Lock()
s.accessToken = tokenResp.Access_token
s.accessTokenMutex.Unlock()
return nil
}
Loading