-
Notifications
You must be signed in to change notification settings - Fork 906
GODRIVER-3249: Handle all possible OIDC configuration errors #1734
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
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
48a8e24
GODRIVER-3246: Support human flow for OIDC (#1713)
pmeredit ed291d1
SQL-3249: Add configuration errors
pmeredit 9513150
SQL-3249: Add configuration error testing
pmeredit bc322b8
SQL-3249: Export AllowedHostsProp
pmeredit 50087cf
Merge branch 'v1' into GODRIVER-3249
pmeredit ed4f3eb
SQL-3249: Add comments for exported vars
pmeredit fa35dd1
SQL-3249: Lint
pmeredit b05f146
SQL-3249: Lint
pmeredit 0d59b56
SQL-3249: Add GODRIVER-3226 tests because it looks like the godriver …
pmeredit 33645f9
Update mongo/options/clientoptions.go
pmeredit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,14 +26,23 @@ import ( | |
// MongoDBOIDC is the string constant for the MONGODB-OIDC authentication mechanism. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are now public so they can be reused in configuration error checking to keep consistency if we ever need to change the values. |
||
const MongoDBOIDC = "MONGODB-OIDC" | ||
|
||
// const tokenResourceProp = "TOKEN_RESOURCE" | ||
const environmentProp = "ENVIRONMENT" | ||
const resourceProp = "TOKEN_RESOURCE" | ||
const allowedHostsProp = "ALLOWED_HOSTS" | ||
// EnvironmentProp is the property key name that specifies the environment for the OIDC authenticator. | ||
const EnvironmentProp = "ENVIRONMENT" | ||
|
||
const azureEnvironmentValue = "azure" | ||
const gcpEnvironmentValue = "gcp" | ||
const testEnvironmentValue = "test" | ||
// ResourceProp is the property key name that specifies the token resource for GCP and AZURE OIDC auth. | ||
const ResourceProp = "TOKEN_RESOURCE" | ||
|
||
// AllowedHostsProp is the property key name that specifies the allowed hosts for the OIDC authenticator. | ||
const AllowedHostsProp = "ALLOWED_HOSTS" | ||
|
||
// AzureEnvironmentValue is the value for the Azure environment. | ||
const AzureEnvironmentValue = "azure" | ||
|
||
// GCPEnvironmentValue is the value for the GCP environment. | ||
const GCPEnvironmentValue = "gcp" | ||
|
||
// TestEnvironmentValue is the value for the test environment. | ||
const TestEnvironmentValue = "test" | ||
|
||
const apiVersion = 1 | ||
const invalidateSleepTimeout = 100 * time.Millisecond | ||
|
@@ -104,18 +113,18 @@ func newOIDCAuthenticator(cred *Cred, httpClient *http.Client) (Authenticator, e | |
return nil, fmt.Errorf("password cannot be specified for %q", MongoDBOIDC) | ||
} | ||
if cred.Props != nil { | ||
if env, ok := cred.Props[environmentProp]; ok { | ||
if env, ok := cred.Props[EnvironmentProp]; ok { | ||
switch strings.ToLower(env) { | ||
case azureEnvironmentValue: | ||
case AzureEnvironmentValue: | ||
fallthrough | ||
case gcpEnvironmentValue: | ||
if _, ok := cred.Props[resourceProp]; !ok { | ||
return nil, fmt.Errorf("%q must be specified for %q %q", resourceProp, env, environmentProp) | ||
case GCPEnvironmentValue: | ||
if _, ok := cred.Props[ResourceProp]; !ok { | ||
return nil, fmt.Errorf("%q must be specified for %q %q", ResourceProp, env, EnvironmentProp) | ||
} | ||
fallthrough | ||
case testEnvironmentValue: | ||
case TestEnvironmentValue: | ||
if cred.OIDCMachineCallback != nil || cred.OIDCHumanCallback != nil { | ||
return nil, fmt.Errorf("OIDC callbacks are not allowed for %q %q", env, environmentProp) | ||
return nil, fmt.Errorf("OIDC callbacks are not allowed for %q %q", env, EnvironmentProp) | ||
} | ||
} | ||
} | ||
|
@@ -151,7 +160,8 @@ func (oa *OIDCAuthenticator) setAllowedHosts() error { | |
oa.allowedHosts = &defaultAllowedHosts | ||
return nil | ||
} | ||
allowedHosts, ok := oa.AuthMechanismProperties[allowedHostsProp] | ||
|
||
allowedHosts, ok := oa.AuthMechanismProperties[AllowedHostsProp] | ||
if !ok { | ||
oa.allowedHosts = &defaultAllowedHosts | ||
return nil | ||
|
@@ -168,18 +178,18 @@ func (oa *OIDCAuthenticator) setAllowedHosts() error { | |
func (oa *OIDCAuthenticator) validateConnectionAddressWithAllowedHosts(conn driver.Connection) error { | ||
if oa.allowedHosts == nil { | ||
// should be unreachable, but this is a safety check. | ||
return newAuthError(fmt.Sprintf("%q missing", allowedHostsProp), nil) | ||
return newAuthError(fmt.Sprintf("%q missing", AllowedHostsProp), nil) | ||
} | ||
allowedHosts := *oa.allowedHosts | ||
if len(allowedHosts) == 0 { | ||
return newAuthError(fmt.Sprintf("empty %q specified", allowedHostsProp), nil) | ||
return newAuthError(fmt.Sprintf("empty %q specified", AllowedHostsProp), nil) | ||
} | ||
for _, pattern := range allowedHosts { | ||
if pattern.MatchString(string(conn.Address())) { | ||
return nil | ||
} | ||
} | ||
return newAuthError(fmt.Sprintf("address %q not allowed by %q: %v", conn.Address(), allowedHostsProp, allowedHosts), nil) | ||
return newAuthError(fmt.Sprintf("address %q not allowed by %q: %v", conn.Address(), AllowedHostsProp, allowedHosts), nil) | ||
} | ||
|
||
type oidcOneStep struct { | ||
|
@@ -249,27 +259,27 @@ func (*oidcTwoStep) Completed() bool { | |
} | ||
|
||
func (oa *OIDCAuthenticator) providerCallback() (OIDCCallback, error) { | ||
env, ok := oa.AuthMechanismProperties[environmentProp] | ||
env, ok := oa.AuthMechanismProperties[EnvironmentProp] | ||
if !ok { | ||
return nil, nil | ||
} | ||
|
||
switch env { | ||
case azureEnvironmentValue: | ||
resource, ok := oa.AuthMechanismProperties[resourceProp] | ||
case AzureEnvironmentValue: | ||
resource, ok := oa.AuthMechanismProperties[ResourceProp] | ||
if !ok { | ||
return nil, newAuthError(fmt.Sprintf("%q must be specified for Azure OIDC", resourceProp), nil) | ||
return nil, newAuthError(fmt.Sprintf("%q must be specified for Azure OIDC", ResourceProp), nil) | ||
} | ||
return getAzureOIDCCallback(oa.userName, resource, oa.httpClient), nil | ||
case gcpEnvironmentValue: | ||
resource, ok := oa.AuthMechanismProperties[resourceProp] | ||
case GCPEnvironmentValue: | ||
resource, ok := oa.AuthMechanismProperties[ResourceProp] | ||
if !ok { | ||
return nil, newAuthError(fmt.Sprintf("%q must be specified for GCP OIDC", resourceProp), nil) | ||
return nil, newAuthError(fmt.Sprintf("%q must be specified for GCP OIDC", ResourceProp), nil) | ||
} | ||
return getGCPOIDCCallback(resource, oa.httpClient), nil | ||
} | ||
|
||
return nil, fmt.Errorf("%q %q not supported for MONGODB-OIDC", environmentProp, env) | ||
return nil, fmt.Errorf("%q %q not supported for MONGODB-OIDC", EnvironmentProp, env) | ||
} | ||
|
||
// getAzureOIDCCallback returns the callback for the Azure Identity Provider. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.