Skip to content

Commit

Permalink
added support for a base URL / base path
Browse files Browse the repository at this point in the history
Signed-off-by: quobix <dave@quobix.com>
  • Loading branch information
daveshanley committed Nov 18, 2023
1 parent bca05ad commit 0fe90f6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
27 changes: 25 additions & 2 deletions cmd/load_specification.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ package cmd
import (
"fmt"
"github.com/pb33f/libopenapi"
"github.com/pb33f/libopenapi/datamodel"
"github.com/pterm/pterm"
"io"
"log/slog"
"net/http"
"net/url"
"os"
"strings"
)

func loadOpenAPISpec(contract string) (libopenapi.Document, error) {
func loadOpenAPISpec(contract, base string) (libopenapi.Document, error) {
var specBytes []byte

if strings.HasPrefix(contract, "http://") || strings.HasPrefix(contract, "https://") {
Expand Down Expand Up @@ -47,5 +49,26 @@ func loadOpenAPISpec(contract string) (libopenapi.Document, error) {
if len(specBytes) <= 0 {
return nil, fmt.Errorf("no bytes in OpenAPI Specification")
}
return libopenapi.NewDocument(specBytes)

docConfig := datamodel.NewDocumentConfiguration()
docConfig.AllowFileReferences = true
docConfig.AllowRemoteReferences = true
if base != "" {
if strings.HasPrefix(base, "http") {
u, _ := url.Parse(base)
if u != nil {
pterm.Info.Printf("Setting OpenAPI reference base URL to: '%s'\n", u.String())
docConfig.BaseURL = u
}
} else {
pterm.Info.Printf("Setting OpenAPI reference base path to: '%s'\n", base)
docConfig.BasePath = base
}
}

handler := pterm.NewSlogHandler(&pterm.DefaultLogger)
docConfig.Logger = slog.New(handler)
pterm.DefaultLogger.Level = pterm.LogLevelError

return libopenapi.NewDocumentWithConfiguration(specBytes, docConfig)
}
19 changes: 12 additions & 7 deletions cmd/root_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ var (
FS embed.FS

rootCmd = &cobra.Command{
SilenceUsage: true,
SilenceErrors: true,
Use: "wiretap",
Short: "wiretap is a tool for detecting API compliance against an OpenAPI contract, by sniffing network traffic.",
Long: `wiretap is a tool for detecting API compliance against an OpenAPI contract, by sniffing network traffic.`,
SilenceUsage: true,
Use: "wiretap",
Short: "wiretap is a tool for detecting API compliance against an OpenAPI contract, by sniffing network traffic.",
Long: `wiretap is a tool for detecting API compliance against an OpenAPI contract, by sniffing network traffic.`,
RunE: func(cmd *cobra.Command, args []string) error {

PrintBanner()
Expand Down Expand Up @@ -66,7 +65,7 @@ var (
if keyFlag != "" {
certKey = keyFlag
}

base, _ := cmd.Flags().GetString("base")
mockMode, _ = cmd.Flags().GetBool("mock-mode")
hardError, _ = cmd.Flags().GetBool("hard-validation")
hardErrorCode, _ = cmd.Flags().GetInt("hard-validation-code")
Expand Down Expand Up @@ -159,6 +158,9 @@ var (
config.MockMode = true
}
}
if base != config.Base {
config.Base = base
}

} else {

Expand All @@ -167,6 +169,9 @@ var (
if mockMode {
config.MockMode = true
}
if base != "" {
config.Base = base
}
}

if spec == "" {
Expand Down Expand Up @@ -359,9 +364,9 @@ func Execute(version, commit, date string, fs embed.FS) {
rootCmd.Flags().IntP("hard-validation-code", "q", 400, "Set a custom http error code for non-compliant requests when using the hard-error flag")
rootCmd.Flags().IntP("hard-validation-return-code", "y", 502, "Set a custom http error code for non-compliant responses when using the hard-error flag")
rootCmd.Flags().BoolP("mock-mode", "x", false, "Run in mock mode, responses are mocked and no traffic is sent to the target API (requires OpenAPI spec)")

rootCmd.Flags().StringP("config", "c", "",
"Location of wiretap configuration file to use (default is .wiretap in current directory)")
rootCmd.Flags().StringP("base", "b", "", "Set a base path to resolve relative file references from, or a overriding base URL to resolve remote references from")

if err := rootCmd.Execute(); err != nil {
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion cmd/run_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func runWiretapService(wiretapConfig *shared.WiretapConfiguration) (server.Platf
var err error
// load the openapi spec
if wiretapConfig.Contract != "" {
doc, err = loadOpenAPISpec(wiretapConfig.Contract)
doc, err = loadOpenAPISpec(wiretapConfig.Contract, wiretapConfig.Base)
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions shared/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type WiretapConfiguration struct {
PathDelays map[string]int `json:"pathDelays,omitempty" yaml:"pathDelays,omitempty"`
MockMode bool `json:"mockMode,omitempty" yaml:"mockMode,omitempty"`
MockModePretty bool `json:"mockModePretty,omitempty" yaml:"mockModePretty,omitempty"`
Base string `json:"base,omitempty" yaml:"base,omitempty"`
CompiledPathDelays map[string]*CompiledPathDelay `json:"-" yaml:"-"`
CompiledVariables map[string]*CompiledVariable `json:"-" yaml:"-"`
Version string `json:"-" yaml:"-"`
Expand Down

0 comments on commit 0fe90f6

Please sign in to comment.