Skip to content

Commit

Permalink
Fixes inability to authenticate through the older cmdQuery mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
Evengard committed Feb 12, 2024
1 parent 282c8e1 commit d66a77f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
10 changes: 9 additions & 1 deletion internal/handler/cmd_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"strings"

"github.com/FerretDB/FerretDB/internal/clientconn/conninfo"
"github.com/FerretDB/FerretDB/internal/handler/common"
"github.com/FerretDB/FerretDB/internal/handler/handlererrors"
"github.com/FerretDB/FerretDB/internal/types"
Expand All @@ -40,7 +41,14 @@ func (h *Handler) CmdQuery(ctx context.Context, query *wire.OpQuery) (*wire.OpRe

// database name typically is either "$external" or "admin"

if cmd == "saslStart" && strings.HasSuffix(collection, ".$cmd") {
if cmd == "saslStart" && strings.HasSuffix(collection, ".$cmd") && query.Query().Mechanism() == "PLAIN" {
username, password, err := saslStartPlain(query.Query())
if err != nil {
return nil, err
}

conninfo.Get(ctx).SetAuth(username, password)

var emptyPayload types.Binary
reply := wire.OpReply{
NumberReturned: 1,
Expand Down
20 changes: 20 additions & 0 deletions internal/types/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,26 @@ func (d *Document) Command() string {
return d.fields[0].key
}

// Command returns the mechanism key. This is used for authentication.
// It returns an empty string if document is nil or empty, or if the key doesn't exist.
func (d *Document) Mechanism() string {
if d == nil || len(d.fields) == 0 {
return ""
}

val, err := d.Get("mechanism")
if err != nil {
return ""
}

stringVal, ok := val.(string)
if ok != true {
return ""
}

return stringVal
}

// Has returns true if the given key is present in the document.
func (d *Document) Has(key string) bool {
_, ok := d.keys[key]
Expand Down

0 comments on commit d66a77f

Please sign in to comment.