Skip to content

GODRIVER-3429 Revert internal-only "AuthenticateToAnything". #2025

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

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions internal/options/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (C) MongoDB, Inc. 2025-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

package options

// Options stores internal options.
type Options struct {
values map[string]any
}

// WithValue sets an option value with the associated key.
func WithValue(opts Options, key string, option any) Options {
if opts.values == nil {
opts.values = make(map[string]any)
}
opts.values[key] = option
return opts
}

// Value returns the value associated with the options for key.
func Value(opts Options, key string) any {
if opts.values == nil {
return nil
}
if val, ok := opts.values[key]; ok {
return val
}
return nil
}

// Equal compares two Options instances for equality.
func Equal(opts1, opts2 Options) bool {
if len(opts1.values) != len(opts2.values) {
return false
}
for key, val1 := range opts1.values {
if val2, ok := opts2.values[key]; !ok || val1 != val2 {
return false
}
}
return true
}
7 changes: 7 additions & 0 deletions mongo/options/clientoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/event"
"go.mongodb.org/mongo-driver/v2/internal/httputil"
"go.mongodb.org/mongo-driver/v2/internal/options"
"go.mongodb.org/mongo-driver/v2/mongo/readconcern"
"go.mongodb.org/mongo-driver/v2/mongo/readpref"
"go.mongodb.org/mongo-driver/v2/mongo/writeconcern"
Expand Down Expand Up @@ -295,6 +296,12 @@ type ClientOptions struct {
// release.
Deployment driver.Deployment

// Custom specifies internal options for the new Client.
//
// Deprecated: This option is for internal use only and should not be set. It may be changed or removed in any
// release.
Custom options.Options

connString *connstring.ConnString
err error
}
Expand Down
3 changes: 3 additions & 0 deletions mongo/options/clientoptions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"go.mongodb.org/mongo-driver/v2/event"
"go.mongodb.org/mongo-driver/v2/internal/assert"
"go.mongodb.org/mongo-driver/v2/internal/httputil"
"go.mongodb.org/mongo-driver/v2/internal/options"
"go.mongodb.org/mongo-driver/v2/internal/ptrutil"
"go.mongodb.org/mongo-driver/v2/mongo/readconcern"
"go.mongodb.org/mongo-driver/v2/mongo/readpref"
Expand Down Expand Up @@ -156,6 +157,7 @@ func TestClientOptions(t *testing.T) {
cmp.Comparer(func(r1, r2 *bson.Registry) bool { return r1 == r2 }),
cmp.Comparer(func(cfg1, cfg2 *tls.Config) bool { return cfg1 == cfg2 }),
cmp.Comparer(func(fp1, fp2 *event.PoolMonitor) bool { return fp1 == fp2 }),
cmp.Comparer(options.Equal),
cmp.AllowUnexported(ClientOptions{}),
cmpopts.IgnoreFields(http.Client{}, "Transport"),
); diff != "" {
Expand Down Expand Up @@ -1253,6 +1255,7 @@ func TestApplyURI(t *testing.T) {
cmp.Comparer(func(r1, r2 *bson.Registry) bool { return r1 == r2 }),
cmp.Comparer(compareTLSConfig),
cmp.Comparer(compareErrors),
cmp.Comparer(options.Equal),
cmpopts.SortSlices(stringLess),
cmpopts.IgnoreFields(connstring.ConnString{}, "SSLClientCertificateKeyPassword"),
cmpopts.IgnoreFields(http.Client{}, "Transport"),
Expand Down
45 changes: 45 additions & 0 deletions x/mongo/driver/options/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (C) MongoDB, Inc. 2025-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

package options

import (
"fmt"

internalOptions "go.mongodb.org/mongo-driver/v2/internal/options"
"go.mongodb.org/mongo-driver/v2/mongo/options"
"go.mongodb.org/mongo-driver/v2/x/mongo/driver"
)

// SetInternalClientOptions sets internal options for ClientOptions.
//
// Deprecated: This function is for internal use only. It may be changed or removed in any release.
func SetInternalClientOptions(opts *options.ClientOptions, key string, option any) error {
const typeErr = "unexpected type for %s"
switch key {
case "crypt":
c, ok := option.(driver.Crypt)
if !ok {
return fmt.Errorf(typeErr, key)
}
opts.Crypt = c
case "deployment":
d, ok := option.(driver.Deployment)
if !ok {
return fmt.Errorf(typeErr, key)
}
opts.Deployment = d
case "authenticateToAnything":
b, ok := option.(bool)
if !ok {
return fmt.Errorf(typeErr, key)
}
opts.Custom = internalOptions.WithValue(opts.Custom, key, b)
default:
return fmt.Errorf("unsupported option: %s", key)
}
return nil
}
10 changes: 10 additions & 0 deletions x/mongo/driver/topology/topology_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import (

"go.mongodb.org/mongo-driver/v2/event"
"go.mongodb.org/mongo-driver/v2/internal/logger"
internalOptions "go.mongodb.org/mongo-driver/v2/internal/options"
"go.mongodb.org/mongo-driver/v2/mongo/options"
"go.mongodb.org/mongo-driver/v2/x/mongo/driver"
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/auth"
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/description"
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/ocsp"
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/operation"
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/session"
Expand Down Expand Up @@ -270,6 +272,14 @@ func NewConfigFromOptionsWithAuthenticator(opts *options.ClientOptions, clock *s
// Required for SASL mechanism negotiation during handshake
handshakeOpts.DBUser = opts.Auth.AuthSource + "." + opts.Auth.Username
}
if a := internalOptions.Value(opts.Custom, "authenticateToAnything"); a != nil {
if v, ok := a.(bool); ok && v {
// Authenticate arbiters
handshakeOpts.PerformAuthentication = func(_ description.Server) bool {
return true
}
}
}

handshaker = func(driver.Handshaker) driver.Handshaker {
return auth.Handshaker(nil, handshakeOpts)
Expand Down
Loading