Skip to content

Commit cf46ed0

Browse files
committed
GODRIVER-3397 Remove the MONGODB-CR auth mechanism.
1 parent 987b3ec commit cf46ed0

File tree

9 files changed

+37
-284
lines changed

9 files changed

+37
-284
lines changed

mongo/options/clientoptions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ type ContextDialer interface {
6868
// Credential can be used to provide authentication options when configuring a Client.
6969
//
7070
// AuthMechanism: the mechanism to use for authentication. Supported values include "SCRAM-SHA-256", "SCRAM-SHA-1",
71-
// "MONGODB-CR", "PLAIN", "GSSAPI", "MONGODB-X509", and "MONGODB-AWS". This can also be set through the "authMechanism"
71+
// "PLAIN", "GSSAPI", "MONGODB-X509", and "MONGODB-AWS". This can also be set through the "authMechanism"
7272
// URI option. (e.g. "authMechanism=PLAIN"). For more information, see
7373
// https://www.mongodb.com/docs/manual/core/authentication-mechanisms/.
7474
//

x/mongo/driver/auth/auth.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"errors"
1212
"fmt"
1313
"net/http"
14+
"strings"
1415

1516
"go.mongodb.org/mongo-driver/v2/mongo/address"
1617
"go.mongodb.org/mongo-driver/v2/x/mongo/driver"
@@ -31,7 +32,6 @@ func init() {
3132
RegisterAuthenticatorFactory("", newDefaultAuthenticator)
3233
RegisterAuthenticatorFactory(SCRAMSHA1, newScramSHA1Authenticator)
3334
RegisterAuthenticatorFactory(SCRAMSHA256, newScramSHA256Authenticator)
34-
RegisterAuthenticatorFactory(MONGODBCR, newMongoDBCRAuthenticator)
3535
RegisterAuthenticatorFactory(PLAIN, newPlainAuthenticator)
3636
RegisterAuthenticatorFactory(GSSAPI, newGSSAPIAuthenticator)
3737
RegisterAuthenticatorFactory(MongoDBX509, newMongoDBX509Authenticator)
@@ -41,6 +41,12 @@ func init() {
4141

4242
// CreateAuthenticator creates an authenticator.
4343
func CreateAuthenticator(name string, cred *Cred, httpClient *http.Client) (Authenticator, error) {
44+
// Return a custom error to indicate why auth mechanism "MONGODB-CR" is
45+
// missing, even though it was previously available.
46+
if strings.ToUpper(name) == "MONGODB-CR" {
47+
return nil, errors.New(`auth mechanism "MONGODB-CR" is not available in any supported version of MongoDB`)
48+
}
49+
4450
if f, ok := authFactories[name]; ok {
4551
return f(cred, httpClient)
4652
}

x/mongo/driver/auth/auth_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package auth_test
88

99
import (
1010
"context"
11+
"errors"
1112
"fmt"
1213
"net/http"
1314
"testing"
@@ -29,11 +30,12 @@ func TestCreateAuthenticator(t *testing.T) {
2930
name string
3031
source string
3132
auth auth.Authenticator
33+
err error
3234
}{
3335
{name: "", auth: &auth.DefaultAuthenticator{}},
3436
{name: "SCRAM-SHA-1", auth: &auth.ScramAuthenticator{}},
3537
{name: "SCRAM-SHA-256", auth: &auth.ScramAuthenticator{}},
36-
{name: "MONGODB-CR", auth: &auth.MongoDBCRAuthenticator{}},
38+
{name: "MONGODB-CR", err: errors.New(`auth mechanism "MONGODB-CR" is not available in any supported version of MongoDB`)},
3739
{name: "PLAIN", auth: &auth.PlainAuthenticator{}},
3840
{name: "MONGODB-X509", auth: &auth.MongoDBX509Authenticator{}},
3941
}
@@ -47,6 +49,10 @@ func TestCreateAuthenticator(t *testing.T) {
4749
}
4850

4951
a, err := auth.CreateAuthenticator(test.name, cred, &http.Client{})
52+
if test.err != nil {
53+
require.Equal(t, test.err, err)
54+
return
55+
}
5056
require.NoError(t, err)
5157
require.IsType(t, test.auth, a)
5258
})

x/mongo/driver/auth/default.go

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ func newDefaultAuthenticator(cred *Cred, httpClient *http.Client) (Authenticator
3232
}, nil
3333
}
3434

35-
// DefaultAuthenticator uses SCRAM-SHA-1 or MONGODB-CR depending
36-
// on the server version.
35+
// DefaultAuthenticator uses SCRAM-SHA-1 or SCRAM-SHA-256, depending on the
36+
// server's SASL supported mechanisms.
3737
type DefaultAuthenticator struct {
3838
Cred *Cred
3939

@@ -53,18 +53,20 @@ func (a *DefaultAuthenticator) CreateSpeculativeConversation() (SpeculativeConve
5353

5454
// Auth authenticates the connection.
5555
func (a *DefaultAuthenticator) Auth(ctx context.Context, cfg *driver.AuthConfig) error {
56-
var actual Authenticator
57-
var err error
58-
59-
switch chooseAuthMechanism(cfg) {
60-
case SCRAMSHA256:
61-
actual, err = newScramSHA256Authenticator(a.Cred, a.httpClient)
62-
case SCRAMSHA1:
63-
actual, err = newScramSHA1Authenticator(a.Cred, a.httpClient)
64-
default:
65-
actual, err = newMongoDBCRAuthenticator(a.Cred, a.httpClient)
66-
}
56+
actual, err := func() (Authenticator, error) {
57+
// If a server provides a list of supported mechanisms, we choose
58+
// SCRAM-SHA-256 if it exists or else MUST use SCRAM-SHA-1.
59+
// Otherwise, we decide based on what is supported.
60+
if saslSupportedMechs := cfg.HandshakeInfo.SaslSupportedMechs; saslSupportedMechs != nil {
61+
for _, v := range saslSupportedMechs {
62+
if v == SCRAMSHA256 {
63+
return newScramSHA256Authenticator(a.Cred, a.httpClient)
64+
}
65+
}
66+
}
6767

68+
return newScramSHA1Authenticator(a.Cred, a.httpClient)
69+
}()
6870
if err != nil {
6971
return newAuthError("error creating authenticator", err)
7072
}
@@ -76,18 +78,3 @@ func (a *DefaultAuthenticator) Auth(ctx context.Context, cfg *driver.AuthConfig)
7678
func (a *DefaultAuthenticator) Reauth(_ context.Context, _ *driver.AuthConfig) error {
7779
return newAuthError("DefaultAuthenticator does not support reauthentication", nil)
7880
}
79-
80-
// If a server provides a list of supported mechanisms, we choose
81-
// SCRAM-SHA-256 if it exists or else MUST use SCRAM-SHA-1.
82-
// Otherwise, we decide based on what is supported.
83-
func chooseAuthMechanism(cfg *driver.AuthConfig) string {
84-
if saslSupportedMechs := cfg.HandshakeInfo.SaslSupportedMechs; saslSupportedMechs != nil {
85-
for _, v := range saslSupportedMechs {
86-
if v == SCRAMSHA256 {
87-
return v
88-
}
89-
}
90-
}
91-
92-
return SCRAMSHA1
93-
}

x/mongo/driver/auth/mongodbcr.go

Lines changed: 0 additions & 120 deletions
This file was deleted.

x/mongo/driver/auth/mongodbcr_test.go

Lines changed: 0 additions & 120 deletions
This file was deleted.

x/mongo/driver/auth/plain_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,10 @@ func TestPlainAuthenticator_SucceedsBoolean(t *testing.T) {
197197
)
198198
compareResponses(t, <-c.Written, expectedCmd, "$external")
199199
}
200+
201+
func writeReplies(c chan []byte, docs ...bsoncore.Document) {
202+
for _, doc := range docs {
203+
reply := drivertest.MakeReply(doc)
204+
c <- reply
205+
}
206+
}

0 commit comments

Comments
 (0)