Skip to content

Commit

Permalink
handle database without any pool yet
Browse files Browse the repository at this point in the history
  • Loading branch information
chilagrow committed Feb 16, 2024
1 parent 470ca14 commit 40be66d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
9 changes: 8 additions & 1 deletion internal/backends/postgresql/metadata/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,14 @@ func (r *Registry) getPool(ctx context.Context) (*pgxpool.Pool, error) {

if connInfo.BypassBackendAuth {
if p = r.p.GetAny(); p == nil {
return nil, lazyerrors.New("no connection pool")
// no connection pool has been created yet and authentication
// is bypassed, attempt to use credentials to connect
username, password := connInfo.Auth()

var err error
if p, err = r.p.Get(username, password); err != nil {
return nil, lazyerrors.Error(err)
}
}
} else {
username, password := connInfo.Auth()
Expand Down
10 changes: 3 additions & 7 deletions internal/handler/msg_saslstart.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ func (h *Handler) MsgSASLStart(ctx context.Context, msg *wire.OpMsg) (*wire.OpMs
if err != nil {
return nil, err
}

if err = h.authenticate(ctx, msg); err != nil {
return nil, err
}
default:
msg := fmt.Sprintf("Unsupported authentication mechanism %q.\n", mechanism) +
"See https://docs.ferretdb.io/security/authentication/ for more details."
Expand All @@ -73,10 +69,10 @@ func (h *Handler) MsgSASLStart(ctx context.Context, msg *wire.OpMsg) (*wire.OpMs
if h.EnableNewAuth {
// If new auth is enabled and the database does not contain any user,
// backend authentication is bypassed.
//
// If a user connects with any credentials or no credentials at all,
// the authentication succeeds until the first user is created.
conninfo.Get(ctx).BypassBackendAuth = true
if err = h.authenticate(ctx, msg); err != nil {
return nil, err
}
} else {
conninfo.Get(ctx).SetAuth(username, password)
}
Expand Down

0 comments on commit 40be66d

Please sign in to comment.