Skip to content

Commit 0df304e

Browse files
author
Isabella Siu
committed
GODRIVER-803 add AuthenticateArbiter client option to allow authenticating with arbiter
Change-Id: I580d2801c0ed0e35b20650fc32a5f7ae66362d7b
1 parent 33a07ee commit 0df304e

File tree

5 files changed

+45
-19
lines changed

5 files changed

+45
-19
lines changed

mongo/client.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,16 +268,27 @@ func (c *Client) configure(opts *options.ClientOptions) error {
268268
return err
269269
}
270270

271-
opts := &auth.HandshakeOptions{
271+
handshakeOpts := &auth.HandshakeOptions{
272272
AppName: appName,
273273
Authenticator: authenticator,
274274
Compressors: compressors,
275275
}
276276
if mechanism == "" {
277277
// Required for SASL mechanism negotiation during handshake
278-
opts.DBUser = cred.Source + "." + cred.Username
278+
handshakeOpts.DBUser = cred.Source + "." + cred.Username
279279
}
280-
handshaker = auth.Handshaker(nil, opts)
280+
if opts.AuthenticateArbiter != nil && *opts.AuthenticateArbiter {
281+
// Authenticate arbiters
282+
handshakeOpts.PerformAuthentication = func(serv description.Server) bool {
283+
return serv.Kind == description.RSPrimary ||
284+
serv.Kind == description.RSSecondary ||
285+
serv.Kind == description.Mongos ||
286+
serv.Kind == description.Standalone ||
287+
serv.Kind == description.RSArbiter
288+
}
289+
}
290+
291+
handshaker = auth.Handshaker(nil, handshakeOpts)
281292
}
282293
connOpts = append(connOpts, connection.WithHandshaker(
283294
func(connection.Handshaker) connection.Handshaker { return handshaker },

mongo/options/clientoptions.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ type Credential struct {
6161
type ClientOptions struct {
6262
AppName *string
6363
Auth *Credential
64+
AuthenticateArbiter *bool
6465
ConnectTimeout *time.Duration
6566
Compressors []string
6667
Dialer ContextDialer
@@ -287,6 +288,13 @@ func (c *ClientOptions) SetCompressors(comps []string) *ClientOptions {
287288
return c
288289
}
289290

291+
// SetAuthenticateArbiter specifies whether or not the driver should authenticate arbiters. By
292+
// default, they are not authenticated.
293+
func (c *ClientOptions) SetAuthenticateArbiter(b bool) *ClientOptions {
294+
c.AuthenticateArbiter = &b
295+
return c
296+
}
297+
290298
// SetConnectTimeout specifies the timeout for an initial connection to a server.
291299
// If a custom Dialer is used, this method won't be set and the user is
292300
// responsible for setting the ConnectTimeout for connections on the dialer
@@ -435,6 +443,9 @@ func MergeClientOptions(opts ...*ClientOptions) *ClientOptions {
435443
if opt.Auth != nil {
436444
c.Auth = opt.Auth
437445
}
446+
if opt.AuthenticateArbiter != nil {
447+
c.AuthenticateArbiter = opt.AuthenticateArbiter
448+
}
438449
if opt.Compressors != nil {
439450
c.Compressors = opt.Compressors
440451
}

x/mongo/driver/auth/auth.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,11 @@ func RegisterAuthenticatorFactory(name string, factory AuthenticatorFactory) {
9595
// function. DBUser is optional but must be of the form <dbname.username>;
9696
// if non-empty, then the connection will do SASL mechanism negotiation.
9797
type HandshakeOptions struct {
98-
AppName string
99-
Authenticator Authenticator
100-
Compressors []string
101-
DBUser string
98+
AppName string
99+
Authenticator Authenticator
100+
Compressors []string
101+
DBUser string
102+
PerformAuthentication func(description.Server) bool
102103
}
103104

104105
// Handshaker creates a connection handshaker for the given authenticator.
@@ -114,9 +115,21 @@ func Handshaker(h connection.Handshaker, options *HandshakeOptions) connection.H
114115
return description.Server{}, newAuthError("handshake failure", err)
115116
}
116117

117-
err = options.Authenticator.Auth(ctx, desc, rw)
118-
if err != nil {
119-
return description.Server{}, newAuthError("auth error", err)
118+
performAuth := options.PerformAuthentication
119+
if performAuth == nil {
120+
performAuth = func(serv description.Server) bool {
121+
return serv.Kind == description.RSPrimary ||
122+
serv.Kind == description.RSSecondary ||
123+
serv.Kind == description.Mongos ||
124+
serv.Kind == description.Standalone
125+
}
126+
}
127+
if performAuth(desc) && options.Authenticator != nil {
128+
err = options.Authenticator.Auth(ctx, desc, rw)
129+
if err != nil {
130+
return description.Server{}, newAuthError("auth error", err)
131+
}
132+
120133
}
121134
if h == nil {
122135
return desc, nil

x/mongo/driver/auth/mongodbcr.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,6 @@ type MongoDBCRAuthenticator struct {
4747
// The MONGODB-CR authentication mechanism is deprecated in MongoDB 4.0.
4848
func (a *MongoDBCRAuthenticator) Auth(ctx context.Context, desc description.Server, rw wiremessage.ReadWriter) error {
4949

50-
// Arbiters cannot be authenticated
51-
if desc.Kind == description.RSArbiter {
52-
return nil
53-
}
54-
5550
db := a.DB
5651
if db == "" {
5752
db = defaultAuthDB

x/mongo/driver/auth/sasl.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ type SaslClientCloser interface {
3131

3232
// ConductSaslConversation handles running a sasl conversation with MongoDB.
3333
func ConductSaslConversation(ctx context.Context, desc description.Server, rw wiremessage.ReadWriter, db string, client SaslClient) error {
34-
// Arbiters cannot be authenticated
35-
if desc.Kind == description.RSArbiter {
36-
return nil
37-
}
3834

3935
if db == "" {
4036
db = defaultAuthDB

0 commit comments

Comments
 (0)