Skip to content

Commit

Permalink
OPENAPI: added redirectId feature to enable users more granular contr…
Browse files Browse the repository at this point in the history
…ol over rewriting. Additionally, fixed a bug in map ordering
  • Loading branch information
jacobm-splunk authored and daveshanley committed Jun 21, 2024
1 parent 1357ba0 commit 637c805
Show file tree
Hide file tree
Showing 9 changed files with 821 additions and 175 deletions.
19 changes: 13 additions & 6 deletions cmd/root_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/pb33f/harhar"
"github.com/pb33f/libopenapi"
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
"github.com/pb33f/libopenapi/orderedmap"
"github.com/pb33f/wiretap/har"
"github.com/pb33f/wiretap/shared"
"github.com/pterm/pterm"
Expand Down Expand Up @@ -345,13 +346,13 @@ var (
}

// paths
if len(config.PathConfigurations) > 0 || len(config.StaticPaths) > 0 || len(config.HARPathAllowList) > 0 || len(config.IgnorePathRewrite) > 0 {
if config.PathConfigurations.Len() > 0 || len(config.StaticPaths) > 0 || len(config.HARPathAllowList) > 0 || len(config.IgnorePathRewrite) > 0 {
config.CompilePaths()
if len(config.IgnorePathRewrite) > 0 {
printLoadedIgnorePathRewrite(config.IgnorePathRewrite)
}

if len(config.PathConfigurations) > 0 {
if config.PathConfigurations.Len() > 0 {
printLoadedPathConfigurations(config.PathConfigurations)
}
}
Expand Down Expand Up @@ -695,12 +696,13 @@ func printLoadedIgnorePathRewrite(ignoreRewritePaths []*shared.IgnoreRewriteConf
pterm.Println()
}

func printLoadedPathConfigurations(configs map[string]*shared.WiretapPathConfig) {
pterm.Info.Printf("Loaded %d path %s:\n", len(configs),
shared.Pluralize(len(configs), "configuration", "configurations"))
func printLoadedPathConfigurations(configs *orderedmap.Map[string, *shared.WiretapPathConfig]) {
pterm.Info.Printf("Loaded %d path %s:\n", configs.Len(),
shared.Pluralize(configs.Len(), "configuration", "configurations"))
pterm.Println()

for k, v := range configs {
for x := configs.First(); x != nil; x = x.Next() {
k, v := x.Key(), x.Value()
pterm.Printf("%s --> %s\n", pterm.LightMagenta(k), pterm.LightCyan(v.Target))
for ka, p := range v.PathRewrite {
pterm.Printf("✏️ '%s' re-written to '%s'\n", pterm.LightCyan(ka), pterm.LightGreen(p))
Expand All @@ -720,6 +722,11 @@ func printLoadedPathConfigurations(configs map[string]*shared.WiretapPathConfig)
if v.Auth != "" {
pterm.Printf("🔒 Basic authentication implemented for '%s'\n", pterm.LightMagenta(k))
}

if v.RewriteId != "" {
pterm.Printf("💳 Identifier '%s' registered for this configuration\n", pterm.LightCyan(v.RewriteId))
}

pterm.Println()
}
}
Expand Down
51 changes: 44 additions & 7 deletions config/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ import (
"fmt"
"github.com/pb33f/wiretap/shared"
"github.com/pterm/pterm"
"net/http"
"strings"
)

const (
RewriteIdHeader = "Rewrite-Id"
)

func FindPaths(path string, configuration *shared.WiretapConfiguration) []*shared.WiretapPathConfig {
var foundConfigurations []*shared.WiretapPathConfig
for key := range configuration.CompiledPaths {
if configuration.CompiledPaths[key].CompiledKey.Match(path) {
foundConfigurations = append(foundConfigurations, configuration.CompiledPaths[key].PathConfig)
for x := configuration.CompiledPaths.First(); x != nil; x = x.Next() {
compiledPath := x.Value()
if compiledPath.CompiledKey.Match(path) {
foundConfigurations = append(foundConfigurations, compiledPath.PathConfig)
}
}
return foundConfigurations
Expand Down Expand Up @@ -80,12 +86,43 @@ func rewriteTaget(path string, pathConfig *shared.WiretapPathConfig, configurati
return fmt.Sprintf("%s%s%s", scheme, target, path)
}

func RewritePath(path string, configuration *shared.WiretapConfiguration) string {
func FindPathWithRewriteId(paths []*shared.WiretapPathConfig, req *http.Request) *shared.WiretapPathConfig {

if req == nil {
return nil
}

if rewriteIdHeaderValues, ok := req.Header[RewriteIdHeader]; ok {
for _, pathRewriteConfig := range paths {

// Iterate through header values - since it's a multi-value field
for _, rewriteId := range rewriteIdHeaderValues {
if pathRewriteConfig.RewriteId == rewriteId {
return pathRewriteConfig
}
}

}
}

return nil
}

func RewritePath(path string, req *http.Request, configuration *shared.WiretapConfiguration) string {
paths := FindPaths(path, configuration)
var replaced string = path
var replaced = path
if len(paths) > 0 {
// extract first path
pathConfig := paths[0]

var pathConfig *shared.WiretapPathConfig

// Check if request headers have rewrite id; if so, we should try to find a matching rewrite config
pathConfig = FindPathWithRewriteId(paths, req)

// if rewriteId not specified in request or not found, extract first path
if pathConfig == nil {
pathConfig = paths[0]
}

replaced = ""

for _, globalIgnoreRewrite := range configuration.CompiledIgnorePathRewrite {
Expand Down
Loading

0 comments on commit 637c805

Please sign in to comment.