Skip to content

Commit

Permalink
Merge pull request kubernetes#18917 from yifan-gu/add_close_oidc_authn
Browse files Browse the repository at this point in the history
auth: Add Close() for OIDC authenticator.
  • Loading branch information
gmarek committed Dec 23, 2015
2 parents 8be7999 + 04db432 commit 3872c7e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
20 changes: 15 additions & 5 deletions plugin/pkg/auth/authenticator/token/oidc/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ var (
)

type OIDCAuthenticator struct {
clientConfig oidc.ClientConfig
client *oidc.Client
usernameClaim string
clientConfig oidc.ClientConfig
client *oidc.Client
usernameClaim string
stopSyncProvider chan struct{}
}

// New creates a new OpenID Connect client with the given issuerURL and clientID.
Expand Down Expand Up @@ -113,9 +114,9 @@ func New(issuerURL, clientID, caFile, usernameClaim string) (*OIDCAuthenticator,
// SyncProviderConfig will start a goroutine to periodically synchronize the provider config.
// The synchronization interval is set by the expiration length of the config, and has a mininum
// and maximum threshold.
client.SyncProviderConfig(issuerURL)
stop := client.SyncProviderConfig(issuerURL)

return &OIDCAuthenticator{ccfg, client, usernameClaim}, nil
return &OIDCAuthenticator{ccfg, client, usernameClaim, stop}, nil
}

// AuthenticateToken decodes and verifies a JWT using the OIDC client, if the verification succeeds,
Expand Down Expand Up @@ -156,3 +157,12 @@ func (a *OIDCAuthenticator) AuthenticateToken(value string) (user.Info, bool, er
// TODO(yifan): Add UID and Group, also populate the issuer to upper layer.
return &user.DefaultInfo{Name: username}, true, nil
}

// Close closes the OIDC authenticator, this will close the provider sync goroutine.
func (a *OIDCAuthenticator) Close() {
// This assumes the s.stopSyncProvider is an unbuffered channel.
// So instead of closing the channel, we send am empty struct here.
// This guarantees that when this function returns, there is no flying requests,
// because a send to an unbuffered channel happens after the receive from the channel.
a.stopSyncProvider <- struct{}{}
}
1 change: 1 addition & 0 deletions plugin/pkg/auth/authenticator/token/oidc/oidc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,5 +391,6 @@ func TestOIDCAuthentication(t *testing.T) {
if !reflect.DeepEqual(tt.userInfo, user) {
t.Errorf("#%d: Expecting: %v, but got: %v", i, tt.userInfo, user)
}
client.Close()
}
}

0 comments on commit 3872c7e

Please sign in to comment.