Skip to content

GODRIVER-3246: Support human flow for OIDC #1713

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 89 commits into from
Aug 2, 2024

Conversation

pmeredit
Copy link
Collaborator

GODRIVER-3246

Summary

Implements OIDC Human Flow

Background & Motivation

Needed for OIDC epic

pmeredit added 30 commits June 11, 2024 18:05
…r OIDC that is probably, maybe, possibly correct
… need to get down to Handshake instead of creating the Authenticator in Handshake as we do now
@pmeredit pmeredit requested a review from matthewdale July 24, 2024 20:37
@mongodb-drivers-pr-bot mongodb-drivers-pr-bot bot added the priority-3-low Low Priority PR for Review label Jul 24, 2024
Copy link
Contributor

API Change Report

./x/mongo/driver/auth

incompatible changes

SaslClient.Next: changed from func([]byte) ([]byte, error) to func(context.Context, []byte) ([]byte, error)

@pmeredit
Copy link
Collaborator Author

API Change Report

./x/mongo/driver/auth

incompatible changes

SaslClient.Next: changed from func([]byte) ([]byte, error) to func(context.Context, []byte) ([]byte, error)

I originally just stuck the context in the two step struct, but I know everyone says that's "bad form". I never agreed with that, but I definitely don't want to argue over idiomatic go 😂

@blink1073 blink1073 added priority-2-medium Medium Priority PR for Review and removed priority-3-low Low Priority PR for Review labels Jul 29, 2024
@blink1073
Copy link
Member

This build failure looks relevant:

[2024/07/25 12:17:06.147] # go.mongodb.org/mongo-driver/x/mongo/driver/auth [go.mongodb.org/mongo-driver/mongo/readpref.test]
[2024/07/25 12:17:06.147] x/mongo/driver/auth/gssapi.go:60:56: cannot use client (variable of type *gssapi.SaslClient) as SaslClient value in argument to ConductSaslConversation: *gssapi.SaslClient does not implement SaslClient (wrong type for method Next)
[2024/07/25 12:17:06.147] 		have Next([]byte) ([]byte, error)
[2024/07/25 12:17:06.147] 		want Next(context.Context, []byte) ([]byte, error)

@@ -19,7 +19,7 @@ import (
// SaslClient is the client piece of a sasl conversation.
type SaslClient interface {
Start() (string, []byte, error)
Next(challenge []byte) ([]byte, error)
Next(ctx context.Context, challenge []byte) ([]byte, error)
Copy link
Collaborator

Choose a reason for hiding this comment

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

The build for "gssapi" fails because Next needs to be updated in sspi.go and gss.go. Those files use build constraints, so are easy to miss because the compiler ignores them unless specific build tags are used.

Here's the build failure:

x/mongo/driver/auth/gssapi.go:60:56: cannot use client (variable of type *gssapi.SaslClient) as SaslClient value in argument to ConductSaslConversation: *gssapi.SaslClient does not implement SaslClient (wrong type for method Next)
	have Next([]byte) ([]byte, error)
	want Next(context.Context, []byte) ([]byte, error)

return nil, fmt.Errorf("error unmarshaling BSON document: %w", err)
}

accessToken, err := ots.oa.getAccessToken(ctx,
Copy link
Collaborator

Choose a reason for hiding this comment

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

err is never checked here.

Comment on lines 156 to 157
ret = createAllowedHostsPatterns(ret)
oa.allowedHosts = &ret
Copy link
Collaborator

Choose a reason for hiding this comment

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

Optional: Consider initializing allowedHosts in newOIDCAuthenticator, which would allow compiling the ALLOWED_HOSTS patterns as a slice of *regexp.Regexp there. That would also allow pre-compiling the default allowed hosts patterns, avoiding the CodeQL warnings and catching any regex compilation errors at initialization time.

For example:

func newOIDCAuthenticator(...) (Authenticator, error) {
	// ...
	var allowedHosts []*regexp.Regexp
	if v, ok := cred.Props[allowedHostsProp]; ok {
		patterns := strings.Split(v, ",")
		for _, pattern := range patterns {
			r, err := regexp.Compile(pattern)
			if err != nil {
				return nil, err
			}
			allowedHosts = append(allowedHosts, r)
		}
	} else {
		allowedHosts = defaultAllowedHosts
	}
	oa := &OIDCAuthenticator{
		userName:                cred.Username,
		httpClient:              httpClient,
		AuthMechanismProperties: cred.Props,
		OIDCMachineCallback:     cred.OIDCMachineCallback,
		OIDCHumanCallback:       cred.OIDCHumanCallback,
		allowedHosts:            allowedHosts,
	}
	return oa, nil
}

Then defaultAllowedHosts becomes:

var defaultAllowedHosts = []*regexp.Regexp{
	regexp.MustCompile("^.*[.]mongodb[.]net(:\\d+)?$"),
	regexp.MustCompile("^.*[.]mongodb-qa[.]net(:\\d+)?$"),
	regexp.MustCompile("^.*[.]mongodb-dev[.]net(:\\d+)?$"),
	regexp.MustCompile("^.*[.]mongodbgov[.]net(:\\d+)?$"),
	regexp.MustCompile("^localhost(:\\d+)?$"),
	regexp.MustCompile("^127[.]0[.]0[.]1(:\\d+)?$"),
	regexp.MustCompile("^::1(:\\d+)?$"),
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok, I've adapted most of this, but kept the logic abstracted to a separate method.

@pmeredit pmeredit requested a review from matthewdale August 2, 2024 16:12
Copy link
Collaborator

@matthewdale matthewdale left a comment

Choose a reason for hiding this comment

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

Looks good! 👍

@matthewdale matthewdale merged commit f0af593 into mongodb:v1 Aug 2, 2024
30 of 34 checks passed
pmeredit added a commit to pmeredit/mongo-go-driver that referenced this pull request Aug 6, 2024
blink1073 pushed a commit to blink1073/mongo-go-driver that referenced this pull request Aug 7, 2024
Co-authored-by: Matt Dale <[email protected]>
(cherry picked from commit f0af593)
blink1073 added a commit that referenced this pull request Aug 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority-2-medium Medium Priority PR for Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants