Skip to content

Commit

Permalink
fixed TODOs that were left from previous commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobm-splunk committed Apr 12, 2024
1 parent 0006b70 commit ae26159
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 21 deletions.
29 changes: 27 additions & 2 deletions cmd/root_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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()
}
8 changes: 4 additions & 4 deletions config/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ 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
}
}
return false
}

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
}
}
Expand Down
94 changes: 93 additions & 1 deletion config/paths_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}
21 changes: 7 additions & 14 deletions daemon/handle_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit ae26159

Please sign in to comment.