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

add the ability to specify additional fallback relay configs #45

Merged
merged 3 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,20 @@ outbound:
would result in requests addressed to http://localhost:8080/relay/test being relayed to https://httpbin.org/anything as long as the result of the jsonpath query `$.foo` executed on the request body results in the string `bar`.

Check out an example [here](./examples/github-pr-comment-relay.yaml) for how to use the relay for GitHub PR comments.

You can also define additional relay mappings via the `additionalConfigs` field:

```yaml
outbound:
listenPort: 8080
relay:
test:
destinationUrl: https://httpbin.org/anything
jsonPath: "$.foo"
equals:
- bar
additionalConfigs:
- destinationUrl: htttps://example.com/fallback
```

The example above would relay traffic to https://httpbin.org/anything if the request body contains `{"foo": "bar"}`, otherwise, it'd relay traffic to `htttps://example.com/fallback`.
11 changes: 6 additions & 5 deletions pkg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,12 @@ type InboundProxyConfig struct {
}

type FilteredRelayConfig struct {
DestinationURL string `mapstructure:"destinationUrl"`
JSONPath string `mapstructure:"jsonPath"`
Contains []string `mapstructure:"contains"`
Equals []string `mapstructure:"equals"`
HasPrefix []string `mapstructure:"hasPrefix"`
DestinationURL string `mapstructure:"destinationUrl"`
JSONPath string `mapstructure:"jsonPath"`
Contains []string `mapstructure:"contains"`
Equals []string `mapstructure:"equals"`
HasPrefix []string `mapstructure:"hasPrefix"`
AdditionalConfigs []FilteredRelayConfig `mapstructure:"additionalConfigs"` // this is awful, but we can refactor this in the near future
}

type OutboundProxyConfig struct {
Expand Down
32 changes: 20 additions & 12 deletions pkg/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,42 +32,50 @@ func GetRequestBodyJSON(body io.Reader) (map[string]interface{}, error) {
return value, nil
}

func (config *FilteredRelayConfig) Matches(value map[string]interface{}) (bool, error) {
func (config *FilteredRelayConfig) FindMatch(value map[string]interface{}) (*FilteredRelayConfig, bool, error) {
if config.JSONPath == "" {
return true, nil
return config, true, nil
}

result, err := jsonpath.Get(config.JSONPath, value)

if err != nil {
if strings.HasPrefix(err.Error(), "unknown key ") {
return false, nil
return config, false, nil
}
return false, fmt.Errorf("error evaluating jsonpath: %v", err)
return config, false, fmt.Errorf("error evaluating jsonpath: %v", err)
}

if reflect.TypeOf(result).Kind() != reflect.String {
return false, fmt.Errorf("jsonpath result is not a string")
return config, false, fmt.Errorf("jsonpath result is not a string")
}

resultStr := result.(string)

for _, val := range config.Equals {
if resultStr == val {
return true, nil
return config, true, nil
}
}
for _, val := range config.HasPrefix {
if strings.HasPrefix(resultStr, val) {
return true, nil
return config, true, nil
}
}
for _, val := range config.Contains {
if strings.Contains(resultStr, val) {
return true, nil
return config, true, nil
}
}
return false, nil

for i := range config.AdditionalConfigs {
inner_config, inner_match, inner_err := config.AdditionalConfigs[i].FindMatch(value)
if inner_match || inner_err != nil {
return inner_config, inner_match, inner_err
}
}

return config, false, nil
}

func (config *OutboundProxyConfig) Start() error {
Expand Down Expand Up @@ -120,7 +128,7 @@ func (config *OutboundProxyConfig) Start() error {
logger.WithError(err).Warn("relay.parse_json")
}

match, err := relayConfig.Matches(obj)
config, match, err := relayConfig.FindMatch(obj)
if err != nil {
logger.WithError(err).Info("relay.match_err")
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("matching error: %v", err)})
Expand All @@ -133,9 +141,9 @@ func (config *OutboundProxyConfig) Start() error {
return
}

logger = logger.WithField("destinationUrl", relayConfig.DestinationURL)
logger = logger.WithField("destinationUrl", config.DestinationURL)

destinationUrl, err := url.Parse(relayConfig.DestinationURL) // TODO: precompute this
destinationUrl, err := url.Parse(config.DestinationURL) // TODO: precompute this
if err != nil {
logger.WithError(err).Warn("relay.destination_url_parse")
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("url parser error: %v", err)})
Expand Down