From 5807d76c2f728ceba4e5084dff5d7857f901538c Mon Sep 17 00:00:00 2001 From: asymness Date: Fri, 17 Jun 2022 02:53:03 +0500 Subject: [PATCH] feat(ui): Implement parameter to hide URL from results (#294) * Add support for HideURL UI config parameter * Redact whole URL when hide-url parameter is set to true * Add integration test for hide-url functionality * Document the hide-url config parameter in README * Apply suggestions from code review Co-authored-by: TwiN * Update test to have client config with 1ms timeout * Re-align README tables * Update core/endpoint_test.go * Update core/endpoint_test.go Co-authored-by: TwiN --- README.md | 3 ++- core/endpoint.go | 5 +++++ core/endpoint_test.go | 25 +++++++++++++++++++++++++ core/ui/ui.go | 3 +++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 414bebc0..3ddd534e 100644 --- a/README.md +++ b/README.md @@ -173,7 +173,8 @@ If you want to test it locally, see [Docker](#docker). | `endpoints[].alerts[].description` | Description of the alert. Will be included in the alert sent. | `""` | | `endpoints[].client` | [Client configuration](#client-configuration). | `{}` | | `endpoints[].ui` | UI configuration at the endpoint level. | `{}` | -| `endpoints[].ui.hide-hostname` | Whether to include the hostname in the result. | `false` | +| `endpoints[].ui.hide-hostname` | Whether to hide the hostname in the result. | `false` | +| `endpoints[].ui.hide-url` | Whether to ensure the URL is not displayed in the results. Useful if the URL contains a token. | `false` | | `endpoints[].ui.dont-resolve-failed-conditions` | Whether to resolve failed conditions for the UI. | `false` | | `alerting` | [Alerting configuration](#alerting). | `{}` | | `security` | [Security configuration](#security). | `{}` | diff --git a/core/endpoint.go b/core/endpoint.go index 006da2b9..626c89b8 100644 --- a/core/endpoint.go +++ b/core/endpoint.go @@ -224,6 +224,11 @@ func (endpoint *Endpoint) EvaluateHealth() *Result { // No need to keep the body after the endpoint has been evaluated result.body = nil // Clean up parameters that we don't need to keep in the results + if endpoint.UIConfig.HideURL { + for errIdx, errorString := range result.Errors { + result.Errors[errIdx] = strings.ReplaceAll(errorString, endpoint.URL, "") + } + } if endpoint.UIConfig.HideHostname { for errIdx, errorString := range result.Errors { result.Errors[errIdx] = strings.ReplaceAll(errorString, result.Hostname, "") diff --git a/core/endpoint_test.go b/core/endpoint_test.go index 33b67f90..93c0df6b 100644 --- a/core/endpoint_test.go +++ b/core/endpoint_test.go @@ -396,6 +396,31 @@ func TestIntegrationEvaluateHealthWithError(t *testing.T) { } } +func TestIntegrationEvaluateHealthWithErrorAndHideURL(t *testing.T) { + endpoint := Endpoint{ + Name: "invalid-url", + URL: "https://httpstat.us/200?sleep=100", + Conditions: []Condition{Condition("[STATUS] == 200")}, + ClientConfig: &client.Config{ + Timeout: 1 * time.Millisecond, + }, + UIConfig: &ui.Config{ + HideURL: true, + }, + } + endpoint.ValidateAndSetDefaults() + result := endpoint.EvaluateHealth() + if result.Success { + t.Error("Because one of the conditions was invalid, result.Success should have been false") + } + if len(result.Errors) == 0 { + t.Error("There should've been an error") + } + if !strings.Contains(result.Errors[0], "") || strings.Contains(result.Errors[0], endpoint.URL) { + t.Error("result.Errors[0] should've had the URL redacted because ui.hide-url is set to true") + } +} + func TestIntegrationEvaluateHealthForDNS(t *testing.T) { conditionSuccess := Condition("[DNS_RCODE] == NOERROR") conditionBody := Condition("[BODY] == 93.184.216.34") diff --git a/core/ui/ui.go b/core/ui/ui.go index aeb4e1a4..9346b658 100644 --- a/core/ui/ui.go +++ b/core/ui/ui.go @@ -4,6 +4,8 @@ package ui type Config struct { // HideHostname whether to hide the hostname in the Result HideHostname bool `yaml:"hide-hostname"` + // HideURL whether to ensure the URL is not displayed in the results. Useful if the URL contains a token. + HideURL bool `yaml:"hide-url"` // DontResolveFailedConditions whether to resolve failed conditions in the Result for display in the UI DontResolveFailedConditions bool `yaml:"dont-resolve-failed-conditions"` } @@ -12,6 +14,7 @@ type Config struct { func GetDefaultConfig() *Config { return &Config{ HideHostname: false, + HideURL: false, DontResolveFailedConditions: false, } }