Skip to content
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

Configurable query parameters runtime data structures. #873

Merged
merged 13 commits into from
Feb 16, 2022
41 changes: 22 additions & 19 deletions api/disabled_parameters.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"fmt"
"github.com/getkin/kin-openapi/openapi3"
"github.com/labstack/echo/v4"
)
Expand Down Expand Up @@ -73,29 +74,31 @@ func NewDisabledMapFromOA3(swag *openapi3.Swagger) *DisabledMap {
return rval
}

// VerifyRC is the return code for the Verify function
type VerifyRC int
// ErrVerifyFailedEndpoint an error that signifies that the entire endpoint is disabled
var ErrVerifyFailedEndpoint error = fmt.Errorf("endpoint is disabled")

const (
verifyIsGood VerifyRC = iota
verifyFailedEndpoint
verifyFailedParameter
)
// ErrVerifyFailedParameter an error that signifies that a parameter was provided when it was disabled
type ErrVerifyFailedParameter struct {
ParameterName string
}

func (evfp ErrVerifyFailedParameter) Error() string {
return fmt.Sprintf("provided disabled parameter: %s", evfp.ParameterName)
}

// DisabledParameterErrorReporter defines an error reporting interface
// for the Verify functions
type DisabledParameterErrorReporter interface {
Errorf(format string, args ...interface{})
}

// Verify returns verifyIsGood if the function can continue (i.e. the parameters are valid and disabled
// parameters are not supplied), otherwise verifyFailedEndpoint if the endpoint failed and
// verifyFailedParameter if a disabled parameter was provided. If verifyFailedParameter is returned
// then the variable name that caused it is provided. In all other cases, it is empty
func Verify(dm *DisabledMap, nameOfHandlerFunc string, ctx echo.Context, log DisabledParameterErrorReporter) (VerifyRC, string) {
// Verify returns nil if the function can continue (i.e. the parameters are valid and disabled
// parameters are not supplied), otherwise VerifyFailedEndpoint if the endpoint failed and
// VerifyFailedParameter if a disabled parameter was provided.
func Verify(dm *DisabledMap, nameOfHandlerFunc string, ctx echo.Context, log DisabledParameterErrorReporter) error {

if dm == nil || dm.Data == nil {
return verifyIsGood, ""
return nil
}

AlgoStephenAkiki marked this conversation as resolved.
Show resolved Hide resolved
if val, ok := dm.Data[nameOfHandlerFunc]; ok {
Expand All @@ -105,13 +108,13 @@ func Verify(dm *DisabledMap, nameOfHandlerFunc string, ctx echo.Context, log Dis
// If the function name wasn't in the map something got messed up....
log.Errorf("verify function could not find name of handler function in map: %s", nameOfHandlerFunc)
// We want to fail-safe to not stop the indexer
return verifyIsGood, ""
return nil
}

func (ec *EndpointConfig) verify(ctx echo.Context, log DisabledParameterErrorReporter) (VerifyRC, string) {
func (ec *EndpointConfig) verify(ctx echo.Context, log DisabledParameterErrorReporter) error {

if ec.EndpointDisabled {
return verifyFailedEndpoint, ""
return ErrVerifyFailedEndpoint
}

queryParams := ctx.QueryParams()
Expand All @@ -127,7 +130,7 @@ func (ec *EndpointConfig) verify(ctx echo.Context, log DisabledParameterErrorRep
queryValue := queryParams.Get(paramName)
if queryValue != "" {
// If the query value is non-zero, and it was disabled, we should return false
return verifyFailedParameter, paramName
return ErrVerifyFailedParameter{paramName}
}

if formErr != nil {
Expand All @@ -137,9 +140,9 @@ func (ec *EndpointConfig) verify(ctx echo.Context, log DisabledParameterErrorRep
formValue := formParams.Get(paramName)
AlgoStephenAkiki marked this conversation as resolved.
Show resolved Hide resolved
if formValue != "" {
// If the query value is non-zero, and it was disabled, we should return false
return verifyFailedParameter, paramName
return ErrVerifyFailedParameter{paramName}
}
}

return verifyIsGood, ""
return nil
}
31 changes: 13 additions & 18 deletions api/disabled_parameters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ func TestFailingParam(t *testing.T) {
type testingStruct struct {
name string
setFormValues func(*url.Values)
expectedRval VerifyRC
expectedRstr string
expectedError error
expectedErrorCount int
mimeType string
}
Expand All @@ -28,19 +27,19 @@ func TestFailingParam(t *testing.T) {
"non-disabled param provided",
func(f *url.Values) {
f.Set("3", "Provided")
}, verifyIsGood, "", 0, echo.MIMEApplicationForm,
}, nil, 0, echo.MIMEApplicationForm,
},
{
"disabled param provided but empty",
func(f *url.Values) {
f.Set("1", "")
}, verifyIsGood, "", 0, echo.MIMEApplicationForm,
}, nil, 0, echo.MIMEApplicationForm,
},
{
"disabled param provided",
func(f *url.Values) {
f.Set("1", "Provided")
}, verifyFailedParameter, "1", 0, echo.MIMEApplicationForm,
}, ErrVerifyFailedParameter{"1"}, 0, echo.MIMEApplicationForm,
},
}

Expand All @@ -49,7 +48,7 @@ func TestFailingParam(t *testing.T) {
"Error encountered for Form Params",
func(f *url.Values) {
f.Set("1", "Provided")
}, verifyIsGood, "", 1, echo.MIMEMultipartForm,
}, nil, 1, echo.MIMEMultipartForm,
},
}

Expand Down Expand Up @@ -83,11 +82,10 @@ func TestFailingParam(t *testing.T) {

logger, hook := test.NewNullLogger()

rval, rstr := Verify(dm, "K1", *ctx, logger)
err := Verify(dm, "K1", *ctx, logger)

require.Equal(t, tstruct.expectedRval, rval)
require.Equal(t, tstruct.expectedError, err)
require.Equal(t, tstruct.expectedErrorCount, len(hook.AllEntries()))
AlgoStephenAkiki marked this conversation as resolved.
Show resolved Hide resolved
require.Equal(t, tstruct.expectedRstr, rstr)
}

for _, test := range tests {
Expand Down Expand Up @@ -126,11 +124,10 @@ func TestFailingEndpoint(t *testing.T) {

logger, hook := test.NewNullLogger()

rval, rstr := Verify(dm, "K1", ctx, logger)
err := Verify(dm, "K1", ctx, logger)

require.Equal(t, verifyFailedEndpoint, rval)
require.Equal(t, ErrVerifyFailedEndpoint, err)
require.Equal(t, 0, len(hook.AllEntries()))
require.Empty(t, rstr)
}

// TestVerifyNonExistentHandler tests that nonexistent endpoint is logged
Expand All @@ -150,17 +147,15 @@ func TestVerifyNonExistentHandler(t *testing.T) {

logger, hook := test.NewNullLogger()

rval, rstr := Verify(dm, "DoesntExist", ctx, logger)
err := Verify(dm, "DoesntExist", ctx, logger)

require.Equal(t, verifyIsGood, rval)
require.Equal(t, nil, err)
require.Equal(t, 1, len(hook.AllEntries()))
require.Empty(t, rstr)

hook.Reset()

rval, rstr = Verify(dm, "K1", ctx, logger)
err = Verify(dm, "K1", ctx, logger)

require.Equal(t, verifyIsGood, rval)
require.Equal(t, nil, err)
require.Equal(t, 0, len(hook.AllEntries()))
require.Empty(t, rstr)
}
Loading