-
Notifications
You must be signed in to change notification settings - Fork 188
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
Refactor provider options #76
Refactor provider options #76
Conversation
Ready for review. |
Thanks for starting on this @sporkmonger, I think I had a deeper refactor in mind for this issue, which is to remove all provider logic from options validation. I would approach this starting off with removing the |
I hadn't ever seen that article before, thanks for that. That's super-cool. |
@shrayolacrayon I started on making the changes, but given the order in which options are processed, I'm not sure how this would work in a Lines 21 to 33 in 17427ad
I was thinking, e.g. opts, err := auth.NewOptions(auth.GoogleProvider("[email protected]", "credentials.json")) But the provider's not known at the point where you call |
Hmm I had in mind doing something like this, where we validate the non-provider options in
|
The pattern made more sense to me as a way to minimize the complexity of the provider switch opts.Provider {
case "azure":
providerConfig = authenticator.AzureProvider(opts.AzureTenantID)
case "google":
providerConfig = authenticator.GoogleProvider(opts.GoogleAdminEmail, opts.GoogleServiceAccountJSON)
}
authenticator, err := NewAuthenticator(options, providerConfig, ....) Writing it as |
The
var (
GoogleProvider = "google"
AzureProvider = "azure"
)
func NewProvider(opts) (providers.Provider, error) {
switch opts.Provider {
case GoogleProvider:
return providers.NewGoogleProvider(opts)
case AzureProvider:
return providers.NewAzureProvider(opts)
default:
return nil, fmt.Errorf("unsupported provider in options: %s", opts.Provider)
}
} I think assigning the provider in a functional option could be useful for some test cases and is a pattern that we have been using for mocking in tests, but I'm not attached to the idea of using it for the providers. FWIW there are already functional options being passed into |
OK, cool, I think that gives me enough to work with. |
It might also be a good pattern for configuring statsd vs prometheus & open tracing too, FWIW. |
OK, I removed the |
internal/auth/options.go
Outdated
msgs = append(msgs, "invalid Google credentials file: "+o.GoogleServiceAccountJSON) | ||
var singleFlightProvider providers.Provider | ||
switch o.Provider { | ||
default: // Google |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we make the default return an error here rather than the google provider and add a separate switch case for google? We can for now set the provider in NewOptions()
to be google.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. I actually had it that way initially but it broke half the test suite, but it's probably not a bad idea to make the tests more explicit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is coming together, thanks for making those changes. In addition to the comments I left, I think we'd want to move the Google provider specific validation out of the options.Validate()
and into probably providers.NewGoogleProvider()
which should return an error if they aren't valid config vals.
This reasoning is because I think it ultimately makes sense to get rid of the Validate function in options completely and validate config values on initialization of the objects, which will make it easier to consolidate common logic between auth and proxy without having to rely on changing logic in the different options configs.
internal/auth/options.go
Outdated
@@ -227,42 +227,74 @@ func (o *Options) Validate() error { | |||
} | |||
|
|||
func parseProviderInfo(o *Options, msgs []string) []string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this function does not really parse provider info anymore, I think it could be named to something like validateEndpoints
} | ||
|
||
func TestGoogleGroupInvalidFile(t *testing.T) { | ||
opts := testOpts("abced", "testtest") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the file doesn't exist in NewGoogleProvider
this panics right now, and this test will not catch that. We can either have NewGoogleProvider
return an error instead (which might be easier to reason about behavior) or recover the panic in the test as we do below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd lean towards error rather than panic.
14091dd
to
3b439f6
Compare
internal/auth/providers/providers.go
Outdated
@@ -24,6 +24,23 @@ var ( | |||
ErrServiceUnavailable = errors.New("SERVICE_UNAVAILABLE") | |||
) | |||
|
|||
const ( | |||
// AzureProviderName identifies the Azure AD provider | |||
AzureProviderName = "azure" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if we need all of these constants yet, might be good to just keep Google for now and add them as the providers have been implemented as to not confuse anyone that they have been.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, that seems fine. I'm planning to do these next-ish, but they can go into another PR instead, that's fine.
This looks good to me! Can you squash and we can merge? |
1453c2d
to
5a35c4f
Compare
Should be all set! |
Thanks for doing this @sporkmonger! |
Sure thing! |
Introduces a simple
switch
to initialize the provider to allow for new providers to be subsequently added.I did consider removing the
TestGoogleGroupInvalidFile
function and including it inTestNewOptions
, since it no longer requires apanic
recovery, but that seemed like maybe the wrong solution once multiple providers are introduced.Fixes #6.