From 9c912b1e1d6e3cc235795a69240ce167c27954ad Mon Sep 17 00:00:00 2001 From: Jack Ussher-Smith Date: Thu, 17 Jun 2021 23:06:55 +0100 Subject: [PATCH] sso_*: rename 'options' package to 'validators' (#312) * rename options package * sso_proxy: "options" pkg is now "validators" * sso_auth: "options" pkg is now "validators" --- internal/auth/authenticator.go | 8 ++++---- internal/auth/authenticator_test.go | 14 +++++++------- internal/auth/mux.go | 10 +++++----- internal/auth/options.go | 4 ++-- .../email_address_validator.go | 2 +- .../email_address_validator_test.go | 2 +- .../email_domain_validator.go | 2 +- .../email_domain_validator_test.go | 2 +- .../email_group_validator.go | 2 +- .../pkg/{options => validators}/mock_validator.go | 2 +- internal/pkg/{options => validators}/validators.go | 2 +- internal/proxy/oauthproxy.go | 10 +++++----- internal/proxy/oauthproxy_test.go | 6 +++--- internal/proxy/options.go | 4 ++-- internal/proxy/proxy.go | 12 ++++++------ 15 files changed, 41 insertions(+), 41 deletions(-) rename internal/pkg/{options => validators}/email_address_validator.go (98%) rename internal/pkg/{options => validators}/email_address_validator_test.go (99%) rename internal/pkg/{options => validators}/email_domain_validator.go (99%) rename internal/pkg/{options => validators}/email_domain_validator_test.go (99%) rename internal/pkg/{options => validators}/email_group_validator.go (98%) rename internal/pkg/{options => validators}/mock_validator.go (95%) rename internal/pkg/{options => validators}/validators.go (97%) diff --git a/internal/auth/authenticator.go b/internal/auth/authenticator.go index a2ff6070..704e0e33 100644 --- a/internal/auth/authenticator.go +++ b/internal/auth/authenticator.go @@ -12,9 +12,9 @@ import ( "github.com/buzzfeed/sso/internal/auth/providers" "github.com/buzzfeed/sso/internal/pkg/aead" log "github.com/buzzfeed/sso/internal/pkg/logging" - "github.com/buzzfeed/sso/internal/pkg/options" "github.com/buzzfeed/sso/internal/pkg/sessions" "github.com/buzzfeed/sso/internal/pkg/templates" + "github.com/buzzfeed/sso/internal/pkg/validators" "github.com/datadog/datadog-go/statsd" "github.com/gorilla/mux" @@ -22,7 +22,7 @@ import ( // Authenticator stores all the information associated with proxying the request. type Authenticator struct { - Validators []options.Validator + Validators []validators.Validator EmailDomains []string ProxyRootDomains []string Host string @@ -228,7 +228,7 @@ func (p *Authenticator) authenticate(rw http.ResponseWriter, req *http.Request) } } - errors := options.RunValidators(p.Validators, session) + errors := validators.RunValidators(p.Validators, session) if len(errors) == len(p.Validators) { logger.WithUser(session.Email).Info( fmt.Sprintf("permission denied: unauthorized: %q", errors)) @@ -584,7 +584,7 @@ func (p *Authenticator) getOAuthCallback(rw http.ResponseWriter, req *http.Reque // - for p.Validator see validator.go#newValidatorImpl for more info // - for p.provider.ValidateGroup see providers/google.go#ValidateGroup for more info - errors := options.RunValidators(p.Validators, session) + errors := validators.RunValidators(p.Validators, session) if len(errors) == len(p.Validators) { tags := append(tags, "error:invalid_email") p.StatsdClient.Incr("application_error", tags, 1.0) diff --git a/internal/auth/authenticator_test.go b/internal/auth/authenticator_test.go index 2bb67e3f..9e02868a 100644 --- a/internal/auth/authenticator_test.go +++ b/internal/auth/authenticator_test.go @@ -17,10 +17,10 @@ import ( "github.com/buzzfeed/sso/internal/auth/providers" "github.com/buzzfeed/sso/internal/pkg/aead" - "github.com/buzzfeed/sso/internal/pkg/options" "github.com/buzzfeed/sso/internal/pkg/sessions" "github.com/buzzfeed/sso/internal/pkg/templates" "github.com/buzzfeed/sso/internal/pkg/testutil" + "github.com/buzzfeed/sso/internal/pkg/validators" ) func init() { @@ -418,7 +418,7 @@ func TestSignIn(t *testing.T) { t.Run(tc.name, func(t *testing.T) { config := testConfiguration(t) auth, err := NewAuthenticator(config, - SetValidators([]options.Validator{options.NewMockValidator(tc.validEmail)}), + SetValidators([]validators.Validator{validators.NewMockValidator(tc.validEmail)}), setMockSessionStore(tc.mockSessionStore), setMockTempl(), setMockRedirectURL(), @@ -565,7 +565,7 @@ func TestSignOutPage(t *testing.T) { provider.RevokeError = tc.RevokeError p, _ := NewAuthenticator(config, - SetValidators([]options.Validator{options.NewMockValidator(true)}), + SetValidators([]validators.Validator{validators.NewMockValidator(true)}), setMockSessionStore(tc.mockSessionStore), setMockTempl(), setTestProvider(provider), @@ -942,7 +942,7 @@ func TestGetProfile(t *testing.T) { t.Run(tc.name, func(t *testing.T) { config := testConfiguration(t) p, _ := NewAuthenticator(config, - SetValidators([]options.Validator{options.NewMockValidator(true)}), + SetValidators([]validators.Validator{validators.NewMockValidator(true)}), ) u, _ := url.Parse("http://example.com") testProvider := providers.NewTestProvider(u) @@ -1044,7 +1044,7 @@ func TestRedeemCode(t *testing.T) { config := testConfiguration(t) proxy, _ := NewAuthenticator(config, - SetValidators([]options.Validator{options.NewMockValidator(true)}), + SetValidators([]validators.Validator{validators.NewMockValidator(true)}), ) testURL, err := url.Parse("example.com") @@ -1433,7 +1433,7 @@ func TestOAuthCallback(t *testing.T) { t.Run(tc.name, func(t *testing.T) { config := testConfiguration(t) proxy, _ := NewAuthenticator(config, - SetValidators([]options.Validator{options.NewMockValidator(tc.validEmail)}), + SetValidators([]validators.Validator{validators.NewMockValidator(tc.validEmail)}), setMockCSRFStore(tc.csrfResp), setMockSessionStore(tc.sessionStore), ) @@ -1554,7 +1554,7 @@ func TestOAuthStart(t *testing.T) { provider := providers.NewTestProvider(nil) proxy, _ := NewAuthenticator(config, setTestProvider(provider), - SetValidators([]options.Validator{options.NewMockValidator(true)}), + SetValidators([]validators.Validator{validators.NewMockValidator(true)}), setMockRedirectURL(), setMockCSRFStore(&sessions.MockCSRFStore{}), ) diff --git a/internal/auth/mux.go b/internal/auth/mux.go index dbcefafa..f303896a 100644 --- a/internal/auth/mux.go +++ b/internal/auth/mux.go @@ -6,7 +6,7 @@ import ( "github.com/buzzfeed/sso/internal/pkg/hostmux" log "github.com/buzzfeed/sso/internal/pkg/logging" - "github.com/buzzfeed/sso/internal/pkg/options" + "github.com/buzzfeed/sso/internal/pkg/validators" "github.com/datadog/datadog-go/statsd" "github.com/gorilla/mux" @@ -19,11 +19,11 @@ type AuthenticatorMux struct { func NewAuthenticatorMux(config Configuration, statsdClient *statsd.Client) (*AuthenticatorMux, error) { logger := log.NewLogEntry() - validators := []options.Validator{} + v := []validators.Validator{} if len(config.AuthorizeConfig.EmailConfig.Addresses) != 0 { - validators = append(validators, options.NewEmailAddressValidator(config.AuthorizeConfig.EmailConfig.Addresses)) + v = append(v, validators.NewEmailAddressValidator(config.AuthorizeConfig.EmailConfig.Addresses)) } else { - validators = append(validators, options.NewEmailDomainValidator(config.AuthorizeConfig.EmailConfig.Domains)) + v = append(v, validators.NewEmailDomainValidator(config.AuthorizeConfig.EmailConfig.Domains)) } authenticators := []*Authenticator{} @@ -39,7 +39,7 @@ func NewAuthenticatorMux(config Configuration, statsdClient *statsd.Client) (*Au idpSlug := idp.Data().ProviderSlug authenticator, err := NewAuthenticator(config, - SetValidators(validators), + SetValidators(v), SetProvider(idp), SetCookieStore(config.SessionConfig, idpSlug), SetStatsdClient(statsdClient), diff --git a/internal/auth/options.go b/internal/auth/options.go index 6cc9598d..c5b75cf2 100644 --- a/internal/auth/options.go +++ b/internal/auth/options.go @@ -9,8 +9,8 @@ import ( "github.com/buzzfeed/sso/internal/auth/providers" "github.com/buzzfeed/sso/internal/pkg/aead" "github.com/buzzfeed/sso/internal/pkg/groups" - "github.com/buzzfeed/sso/internal/pkg/options" "github.com/buzzfeed/sso/internal/pkg/sessions" + "github.com/buzzfeed/sso/internal/pkg/validators" "github.com/datadog/datadog-go/statsd" ) @@ -115,7 +115,7 @@ func SetRedirectURL(serverConfig ServerConfig, slug string) func(*Authenticator) } // SetValidator sets the email validator -func SetValidators(validators []options.Validator) func(*Authenticator) error { +func SetValidators(validators []validators.Validator) func(*Authenticator) error { return func(a *Authenticator) error { a.Validators = validators return nil diff --git a/internal/pkg/options/email_address_validator.go b/internal/pkg/validators/email_address_validator.go similarity index 98% rename from internal/pkg/options/email_address_validator.go rename to internal/pkg/validators/email_address_validator.go index 02c6195e..050f5025 100644 --- a/internal/pkg/options/email_address_validator.go +++ b/internal/pkg/validators/email_address_validator.go @@ -1,4 +1,4 @@ -package options +package validators import ( "errors" diff --git a/internal/pkg/options/email_address_validator_test.go b/internal/pkg/validators/email_address_validator_test.go similarity index 99% rename from internal/pkg/options/email_address_validator_test.go rename to internal/pkg/validators/email_address_validator_test.go index aef92f75..d4a8af24 100644 --- a/internal/pkg/options/email_address_validator_test.go +++ b/internal/pkg/validators/email_address_validator_test.go @@ -1,4 +1,4 @@ -package options +package validators import ( "testing" diff --git a/internal/pkg/options/email_domain_validator.go b/internal/pkg/validators/email_domain_validator.go similarity index 99% rename from internal/pkg/options/email_domain_validator.go rename to internal/pkg/validators/email_domain_validator.go index 4afdf372..c5201059 100644 --- a/internal/pkg/options/email_domain_validator.go +++ b/internal/pkg/validators/email_domain_validator.go @@ -1,4 +1,4 @@ -package options +package validators import ( "errors" diff --git a/internal/pkg/options/email_domain_validator_test.go b/internal/pkg/validators/email_domain_validator_test.go similarity index 99% rename from internal/pkg/options/email_domain_validator_test.go rename to internal/pkg/validators/email_domain_validator_test.go index e2eb407f..31a6d1d4 100644 --- a/internal/pkg/options/email_domain_validator_test.go +++ b/internal/pkg/validators/email_domain_validator_test.go @@ -1,4 +1,4 @@ -package options +package validators import ( "testing" diff --git a/internal/pkg/options/email_group_validator.go b/internal/pkg/validators/email_group_validator.go similarity index 98% rename from internal/pkg/options/email_group_validator.go rename to internal/pkg/validators/email_group_validator.go index 14d1c6dd..e19a8c3e 100644 --- a/internal/pkg/options/email_group_validator.go +++ b/internal/pkg/validators/email_group_validator.go @@ -1,4 +1,4 @@ -package options +package validators import ( "errors" diff --git a/internal/pkg/options/mock_validator.go b/internal/pkg/validators/mock_validator.go similarity index 95% rename from internal/pkg/options/mock_validator.go rename to internal/pkg/validators/mock_validator.go index 87739be0..e98e237e 100644 --- a/internal/pkg/options/mock_validator.go +++ b/internal/pkg/validators/mock_validator.go @@ -1,4 +1,4 @@ -package options +package validators import ( "errors" diff --git a/internal/pkg/options/validators.go b/internal/pkg/validators/validators.go similarity index 97% rename from internal/pkg/options/validators.go rename to internal/pkg/validators/validators.go index 0b14e48f..b04d700a 100644 --- a/internal/pkg/options/validators.go +++ b/internal/pkg/validators/validators.go @@ -1,4 +1,4 @@ -package options +package validators import ( "errors" diff --git a/internal/proxy/oauthproxy.go b/internal/proxy/oauthproxy.go index b1f4983e..d2038c0c 100644 --- a/internal/proxy/oauthproxy.go +++ b/internal/proxy/oauthproxy.go @@ -13,8 +13,8 @@ import ( "github.com/buzzfeed/sso/internal/pkg/aead" log "github.com/buzzfeed/sso/internal/pkg/logging" - "github.com/buzzfeed/sso/internal/pkg/options" "github.com/buzzfeed/sso/internal/pkg/sessions" + "github.com/buzzfeed/sso/internal/pkg/validators" "github.com/buzzfeed/sso/internal/proxy/providers" "github.com/datadog/datadog-go/statsd" @@ -59,7 +59,7 @@ const statusInvalidHost = 421 // OAuthProxy stores all the information associated with proxying the request. type OAuthProxy struct { cookieSecure bool - Validators []options.Validator + Validators []validators.Validator redirectURL *url.URL // the url to receive requests at templates *template.Template @@ -87,7 +87,7 @@ type StateParameter struct { func NewOAuthProxy(sc SessionConfig, optFuncs ...func(*OAuthProxy) error) (*OAuthProxy, error) { p := &OAuthProxy{ cookieSecure: sc.CookieConfig.Secure, - Validators: []options.Validator{}, + Validators: []validators.Validator{}, redirectURL: &url.URL{Path: "/oauth2/callback"}, templates: getTemplates(), @@ -478,7 +478,7 @@ func (p *OAuthProxy) OAuthCallback(rw http.ResponseWriter, req *http.Request) { // // set cookie, or deny - errors := options.RunValidators(p.Validators, session) + errors := validators.RunValidators(p.Validators, session) if len(errors) == len(p.Validators) { tags = append(tags, "error:validation_failed") p.StatsdClient.Incr("application_error", tags, 1.0) @@ -718,7 +718,7 @@ func (p *OAuthProxy) Authenticate(rw http.ResponseWriter, req *http.Request) (er // To reduce strain on upstream identity providers we only revalidate email domains and // addresses on each request here. for _, v := range p.Validators { - _, EmailGroupValidator := v.(options.EmailGroupValidator) + _, EmailGroupValidator := v.(validators.EmailGroupValidator) if !EmailGroupValidator { err := v.Validate(session) diff --git a/internal/proxy/oauthproxy_test.go b/internal/proxy/oauthproxy_test.go index a089516d..b537858a 100644 --- a/internal/proxy/oauthproxy_test.go +++ b/internal/proxy/oauthproxy_test.go @@ -21,9 +21,9 @@ import ( "github.com/mccutchen/go-httpbin/httpbin" "github.com/buzzfeed/sso/internal/pkg/aead" - "github.com/buzzfeed/sso/internal/pkg/options" "github.com/buzzfeed/sso/internal/pkg/sessions" "github.com/buzzfeed/sso/internal/pkg/testutil" + "github.com/buzzfeed/sso/internal/pkg/validators" "github.com/buzzfeed/sso/internal/proxy/providers" ) @@ -121,7 +121,7 @@ func testNewOAuthProxy(t *testing.T, optFuncs ...func(*OAuthProxy) error) (*OAut statsdClient, _ := statsd.New("127.0.0.1:8125") standardOptFuncs := []func(*OAuthProxy) error{ - SetValidators([]options.Validator{options.NewMockValidator(true)}), + SetValidators([]validators.Validator{validators.NewMockValidator(true)}), SetProvider(provider), setSessionStore(&sessions.MockSessionStore{Session: testSession()}), SetUpstreamConfig(upstreamConfig), @@ -279,7 +279,7 @@ func TestAuthOnlyEndpoint(t *testing.T) { proxy, close := testNewOAuthProxy(t, setSessionStore(tc.sessionStore), - SetValidators([]options.Validator{options.NewMockValidator(tc.validEmail)}), + SetValidators([]validators.Validator{validators.NewMockValidator(tc.validEmail)}), SetProvider(tp), ) defer close() diff --git a/internal/proxy/options.go b/internal/proxy/options.go index 89d30e46..18acb36d 100644 --- a/internal/proxy/options.go +++ b/internal/proxy/options.go @@ -11,8 +11,8 @@ import ( "strings" log "github.com/buzzfeed/sso/internal/pkg/logging" - "github.com/buzzfeed/sso/internal/pkg/options" "github.com/buzzfeed/sso/internal/pkg/sessions" + "github.com/buzzfeed/sso/internal/pkg/validators" "github.com/buzzfeed/sso/internal/proxy/providers" "github.com/datadog/datadog-go/statsd" @@ -95,7 +95,7 @@ func SetProxyHandler(handler http.Handler) func(*OAuthProxy) error { } // SetValidator sets the email validator as a functional option -func SetValidators(validators []options.Validator) func(*OAuthProxy) error { +func SetValidators(validators []validators.Validator) func(*OAuthProxy) error { return func(op *OAuthProxy) error { op.Validators = validators return nil diff --git a/internal/proxy/proxy.go b/internal/proxy/proxy.go index db6402c9..5e467951 100644 --- a/internal/proxy/proxy.go +++ b/internal/proxy/proxy.go @@ -5,7 +5,7 @@ import ( "net/http" "github.com/buzzfeed/sso/internal/pkg/hostmux" - "github.com/buzzfeed/sso/internal/pkg/options" + "github.com/buzzfeed/sso/internal/pkg/validators" "github.com/datadog/datadog-go/statsd" ) @@ -45,17 +45,17 @@ func New(config Configuration, statsdClient *statsd.Client) (*SSOProxy, error) { return nil, err } - validators := []options.Validator{} + v := []validators.Validator{} if len(upstreamConfig.AllowedEmailAddresses) != 0 { - validators = append(validators, options.NewEmailAddressValidator(upstreamConfig.AllowedEmailAddresses)) + v = append(v, validators.NewEmailAddressValidator(upstreamConfig.AllowedEmailAddresses)) } if len(upstreamConfig.AllowedEmailDomains) != 0 { - validators = append(validators, options.NewEmailDomainValidator(upstreamConfig.AllowedEmailDomains)) + v = append(v, validators.NewEmailDomainValidator(upstreamConfig.AllowedEmailDomains)) } if len(upstreamConfig.AllowedGroups) != 0 { - validators = append(validators, options.NewEmailGroupValidator(provider, upstreamConfig.AllowedGroups)) + v = append(v, validators.NewEmailGroupValidator(provider, upstreamConfig.AllowedGroups)) } optFuncs = append(optFuncs, @@ -64,7 +64,7 @@ func New(config Configuration, statsdClient *statsd.Client) (*SSOProxy, error) { SetUpstreamConfig(upstreamConfig), SetProxyHandler(handler), SetStatsdClient(statsdClient), - SetValidators(validators), + SetValidators(v), ) oauthproxy, err := NewOAuthProxy(config.SessionConfig, optFuncs...)