Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v13] Fix NewHeadlessAuthenticationWatcher prevents Auth initialization when the backend is unavailable #27298

Merged
merged 2 commits into from
Jun 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/auth/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,9 @@ func NewTestAuthServer(cfg TestAuthServerConfig) (*TestAuthServer, error) {
if err != nil {
return nil, trace.Wrap(err)
}
if err := headlessAuthenticationWatcher.WaitInit(ctx); err != nil {
return nil, trace.Wrap(err)
}
srv.AuthServer.SetHeadlessAuthenticationWatcher(headlessAuthenticationWatcher)

srv.Authorizer, err = authz.NewAuthorizer(authz.AuthorizerOpts{
Expand Down
10 changes: 6 additions & 4 deletions lib/services/local/headlessauthn_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,16 @@ func NewHeadlessAuthenticationWatcher(ctx context.Context, cfg HeadlessAuthentic

go h.runWatchLoop(ctx)

// Wait for the watch loop to initialize before returning.
return h, nil
}

// WaitInit waits for the watch loop to initialize.
func (h *HeadlessAuthenticationWatcher) WaitInit(ctx context.Context) error {
select {
case <-h.running:
case <-ctx.Done():
return nil, trace.Wrap(ctx.Err())
}

return h, nil
return trace.Wrap(ctx.Err())
}

// Done returns a channel that's closed when the watcher is closed.
Expand Down
4 changes: 4 additions & 0 deletions lib/services/local/headlessauthn_watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func newHeadlessAuthenticationWatcherTestEnv(t *testing.T, clock clockwork.Clock
})
require.NoError(t, err)

ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
require.NoError(t, w.WaitInit(ctx))

return &headlessAuthenticationWatcherTestEnv{
watcher: w,
watcherCancel: watcherCancel,
Expand Down