Skip to content

GODRIVER-3215 Fix default auth source for auth specified via ClientOptions. #1764

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

Merged
merged 4 commits into from
Aug 28, 2024

Conversation

matthewdale
Copy link
Collaborator

@matthewdale matthewdale commented Aug 20, 2024

GODRIVER-3215

Summary

  • Move logic for creating an authenticator from ClientOptions to a new function topology.NewAuthenticator. Use it everywhere that needs to create an authenticator from ClientOptions.
    • Requires moving convertOIDCArgs into the topology package.
  • Move the logic for setting the default auth source into each individual authenticator type.
    • The current logic for setting the default auth source appears to be in topology.NewConfigWithAuthenticator, but actually has no effect currently, so no default auth sources are being set.
  • Update PLAIN authenticator to support auth sources other than "$external".
    • Using the database name from the connection string as the auth source is currently supported in connstring, but is ignored by the PLAIN authenticator. Using database name from the connection string is also described in the spec.
  • Correct MONGODB-OIDC connection string logic for setting auth source (it should be identical to MONGODB-AWS and MONGODB-X509).

Background & Motivation

Currently, if auth mechanism "MONGODB-AWS" is set using ClientOptions.SetAuth, the default auth source is set to "admin" instead of "$external". The result is a confusing error message

MONGODB-AWS source must be empty or $external

There was a further regression (that hasn't been released yet) caused by #1678, which effectively skips all of the default auth source logic. Refactor the authenticator creation logic and the default auth source logic to make similar regressions more obvious.

@mongodb-drivers-pr-bot mongodb-drivers-pr-bot bot added the priority-3-low Low Priority PR for Review label Aug 20, 2024
Copy link
Contributor

mongodb-drivers-pr-bot bot commented Aug 20, 2024

API Change Report

./x/mongo/driver/topology

compatible changes

ConvertCreds: added

@matthewdale matthewdale force-pushed the godriver3215-default-external branch from 1643840 to 2422048 Compare August 21, 2024 01:43
@matthewdale matthewdale force-pushed the godriver3215-default-external branch from 2422048 to d6a25ad Compare August 21, 2024 16:17
@matthewdale matthewdale changed the title GODRIVER-3215 Fix default auth source for auth specified via ClientOp… GODRIVER-3215 Fix default auth source for auth specified via ClientOptions. Aug 21, 2024
@matthewdale matthewdale marked this pull request as ready for review August 21, 2024 16:49
@blink1073 blink1073 added priority-1-high High Priority PR for Review and removed priority-3-low Low Priority PR for Review labels Aug 26, 2024

// NewAuthenticator returns a [driver.Authenticator] configured with the given
// credential and HTTP client. It returns nil if cred is nil.
func NewAuthenticator(cred *options.Credential, httpClient *http.Client) (driver.Authenticator, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest repurposing this function with the goal of converting options.Credential to auth.Cred. And constructing the Authenticator using the auth package where needed. For example,

authCreds := topology.ConvertCreds(clientOpt.Auth)

client.authenticator, err = auth.CreateAuthenticator(clientOpt.Auth.AuthMechanism, authCreds, clientOpt.HTTPClient)

This way we limit the number of functions that return the Authenticator interface, making a future refactor to correct this anti-pattern more straightforward: GODRIVER-3316

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable, will try that refactor.


// Test that convertOIDCArgs exhaustively copies all fields of a driver.OIDCArgs
// into an options.OIDCArgs.
func TestConvertOIDCArgs(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest parallelizing the entire test

@@ -26,6 +25,7 @@ func TestPlainAuthenticator_Fails(t *testing.T) {
authenticator := PlainAuthenticator{
Username: "user",
Password: "pencil",
Source: "$external",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use this so much, what are your thoughts on constantizing?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tradeoff is more indirection for lower typo risk. Since the source of truth is the server APIs, there is no risk of out-of-sync constants in the Go Driver. Seems like a good idea overall, will update 👍

prestonvasquez
prestonvasquez previously approved these changes Aug 26, 2024
Copy link
Member

@prestonvasquez prestonvasquez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, see suggestions 👍

prestonvasquez
prestonvasquez previously approved these changes Aug 27, 2024
@blink1073
Copy link
Member

drivers-pr-bot please backport to master

@matthewdale matthewdale merged commit 18d1b19 into mongodb:v1 Aug 28, 2024
31 of 33 checks passed
@blink1073
Copy link
Member

drivers-pr-bot please backport to master

This didn't quite work yet, I'll follow up.

@blink1073
Copy link
Member

I don't think I have enough golang-foo to cherry-pick this one myself.

blink1073 added a commit to blink1073/mongo-go-driver that referenced this pull request Sep 10, 2024
…tions. (mongodb#1764)

Co-authored-by: Preston Vasquez <[email protected]>
Co-authored-by: Steven Silvester <[email protected]>
(cherry picked from commit 18d1b19)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority-1-high High Priority PR for Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants