From ac7946e0176c86497c364310128cf14c3e4067bc Mon Sep 17 00:00:00 2001 From: Jacob Moore Date: Thu, 11 Apr 2024 17:33:04 -0400 Subject: [PATCH] fixed TODOs that were left from previous commit --- cmd/root_command.go | 29 ++++++++++++- config/paths.go | 8 ++-- config/paths_test.go | 94 +++++++++++++++++++++++++++++++++++++++- daemon/handle_request.go | 21 +++------ 4 files changed, 131 insertions(+), 21 deletions(-) diff --git a/cmd/root_command.go b/cmd/root_command.go index 6d00c60..cc7f90c 100644 --- a/cmd/root_command.go +++ b/cmd/root_command.go @@ -366,7 +366,12 @@ var ( if len(config.IgnoreValidation) > 0 { config.CompileIgnoreValidations() - // TODO: add print command + printLoadedIgnoreValidationPaths(config.IgnoreValidation) + } + + if len(config.ValidationAllowList) > 0 { + config.CompileValidationAllowList() + printLoadedValidationAllowList(config.ValidationAllowList) } // static headers @@ -725,7 +730,7 @@ func printLoadedIgnoreRedirectPaths(ignoreRedirects []string) { } func printLoadedRedirectAllowList(allowRedirects []string) { - pterm.Info.Printf("Loaded %d allows listed redirect %s:\n", len(allowRedirects), + pterm.Info.Printf("Loaded %d allow listed redirect %s:\n", len(allowRedirects), shared.Pluralize(len(allowRedirects), "path", "paths")) for _, x := range allowRedirects { @@ -742,3 +747,23 @@ func printLoadedWebsockets(websockets map[string]*shared.WiretapWebsocketConfig) } pterm.Println() } + +func printLoadedIgnoreValidationPaths(ignoreValidations []string) { + pterm.Info.Printf("Loaded %d %s to ignore validation:\n", len(ignoreValidations), + shared.Pluralize(len(ignoreValidations), "path", "paths")) + + for _, x := range ignoreValidations { + pterm.Printf("⚖️ Paths matching '%s' will not have requests validated\n", pterm.LightCyan(x)) + } + pterm.Println() +} + +func printLoadedValidationAllowList(validationAllowList []string) { + pterm.Info.Printf("Loaded %d allow listed validation paths %s :\n", len(validationAllowList), + shared.Pluralize(len(validationAllowList), "path", "paths")) + + for _, x := range validationAllowList { + pterm.Printf("👮 Paths matching '%s' will always have validation run, regardless of ignoreValidation settings\n", pterm.LightCyan(x)) + } + pterm.Println() +} diff --git a/config/paths.go b/config/paths.go index be3dd85..21ad90f 100644 --- a/config/paths.go +++ b/config/paths.go @@ -48,8 +48,8 @@ func PathRedirectAllowListed(path string, configuration *shared.WiretapConfigura } func IgnoreValidationOnPath(path string, configuration *shared.WiretapConfiguration) bool { - for _, redirectPath := range configuration.CompiledIgnoreValidations { - if redirectPath.CompiledPath.Match(path) { + for _, validationPath := range configuration.CompiledIgnoreValidations { + if validationPath.CompiledPath.Match(path) { return true } } @@ -57,8 +57,8 @@ func IgnoreValidationOnPath(path string, configuration *shared.WiretapConfigurat } func PathValidationAllowListed(path string, configuration *shared.WiretapConfiguration) bool { - for _, redirectPath := range configuration.CompiledIgnoreValidations { - if redirectPath.CompiledPath.Match(path) { + for _, validationPath := range configuration.CompiledValidationAllowList { + if validationPath.CompiledPath.Match(path) { return true } } diff --git a/config/paths_test.go b/config/paths_test.go index a9467f4..adb7f98 100644 --- a/config/paths_test.go +++ b/config/paths_test.go @@ -309,4 +309,96 @@ func TestRedirectAllowList_NoPathsRegistered(t *testing.T) { } -// TODO: add tests for new validation functions +func TestIgnoreValidation(t *testing.T) { + + config := `ignoreValidation: + - /pb33f/test/** + - /pb33f/cakes/123 + - /*/test/123` + + var c shared.WiretapConfiguration + _ = yaml.Unmarshal([]byte(config), &c) + + c.CompileIgnoreValidations() + + ignore := IgnoreValidationOnPath("/pb33f/test/burgers/fries?1234=no", &c) + assert.True(t, ignore) + + ignore = IgnoreValidationOnPath("/pb33f/cakes/123", &c) + assert.True(t, ignore) + + ignore = IgnoreValidationOnPath("/roastbeef/test/123", &c) + assert.True(t, ignore) + + ignore = IgnoreValidationOnPath("/not-registered", &c) + assert.False(t, ignore) + +} + +func TestIgnoreValidation_NoPathsRegistered(t *testing.T) { + + var c shared.WiretapConfiguration + _ = yaml.Unmarshal([]byte(""), &c) + + c.CompileIgnoreValidations() + + ignore := IgnoreValidationOnPath("/pb33f/test/burgers/fries?1234=no", &c) + assert.False(t, ignore) + + ignore = IgnoreValidationOnPath("/pb33f/cakes/123", &c) + assert.False(t, ignore) + + ignore = IgnoreValidationOnPath("/roastbeef/test/123", &c) + assert.False(t, ignore) + + ignore = IgnoreValidationOnPath("/not-registered", &c) + assert.False(t, ignore) + +} + +func TestValidationAllowList(t *testing.T) { + + config := `validationAllowList: + - /pb33f/test/** + - /pb33f/cakes/123 + - /*/test/123` + + var c shared.WiretapConfiguration + _ = yaml.Unmarshal([]byte(config), &c) + + c.CompileValidationAllowList() + + ignore := PathValidationAllowListed("/pb33f/test/burgers/fries?1234=no", &c) + assert.True(t, ignore) + + ignore = PathValidationAllowListed("/pb33f/cakes/123", &c) + assert.True(t, ignore) + + ignore = PathValidationAllowListed("/roastbeef/test/123", &c) + assert.True(t, ignore) + + ignore = PathValidationAllowListed("/not-registered", &c) + assert.False(t, ignore) + +} + +func TestValidationAllowList_NoPathsRegistered(t *testing.T) { + + var c shared.WiretapConfiguration + _ = yaml.Unmarshal([]byte(""), &c) + + c.CompileValidationAllowList() + + ignore := PathValidationAllowListed("/pb33f/test/burgers/fries?1234=no", &c) + assert.False(t, ignore) + + ignore = PathValidationAllowListed("/pb33f/cakes/123", &c) + assert.False(t, ignore) + + ignore = PathValidationAllowListed("/roastbeef/test/123", &c) + assert.False(t, ignore) + + ignore = PathValidationAllowListed("/not-registered", &c) + assert.False(t, ignore) + +} diff --git a/daemon/handle_request.go b/daemon/handle_request.go index 149596c..7e97412 100644 --- a/daemon/handle_request.go +++ b/daemon/handle_request.go @@ -131,29 +131,22 @@ func (ws *WiretapService) handleHttpRequest(request *model.Request) { ws.config.Logger.Info("[wiretap] handling API request", "url", request.HttpRequest.URL.String()) + // short-circuit if we're using mock mode, there is no API call to make. if ws.config.MockMode { - // TODO: Add logging - + ws.config.Logger.Info("MockMode enabled; skipping validation") + ws.handleMockRequest(request, config, newReq) + return } else if configModel.IgnoreValidationOnPath(apiRequest.URL.Path, ws.config) && !configModel.PathValidationAllowListed(apiRequest.URL.Path, ws.config) { - // TODO: Add logging - - // check if we're going to fail hard on validation errors. (default is to skip this) - } else if ws.config.HardErrors { - + ws.config.Logger.Info( + fmt.Sprintf("Request on validation ignored path: %s ; skipping validation", apiRequest.URL.Path)) + } else if ws.config.HardErrors { // check if we're going to fail hard on validation errors. (default is to skip this) // validate the request synchronously requestErrors = ws.ValidateRequest(request, newReq) - } else { // validate the request asynchronously go ws.ValidateRequest(request, newReq) } - // short-circuit if we're using mock mode, there is no API call to make. - if ws.config.MockMode { - ws.handleMockRequest(request, config, newReq) - return - } - // call the API being requested. returnedResponse, returnedError = ws.callAPI(apiRequest)