Skip to content

Commit 55d7bcd

Browse files
authored
feat(ui): Implement ability to hide port from endpoint results via endpoints[].ui.hide-port (#1038)
* feat(ui): Implement ability to hide port from endpoint results via `endpoints[].ui.hide-port` Fixes #1036 * Add test
1 parent b79fb09 commit 55d7bcd

File tree

5 files changed

+24
-7
lines changed

5 files changed

+24
-7
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,9 @@ You can then configure alerts to be triggered when an endpoint is unhealthy once
278278
| `endpoints[].client` | [Client configuration](#client-configuration). | `{}` |
279279
| `endpoints[].ui` | UI configuration at the endpoint level. | `{}` |
280280
| `endpoints[].ui.hide-conditions` | Whether to hide conditions from the results. Note that this only hides conditions from results evaluated from the moment this was enabled. | `false` |
281-
| `endpoints[].ui.hide-hostname` | Whether to hide the hostname in the result. | `false` |
282-
| `endpoints[].ui.hide-url` | Whether to ensure the URL is not displayed in the results. Useful if the URL contains a token. | `false` |
281+
| `endpoints[].ui.hide-hostname` | Whether to hide the hostname from the results. | `false` |
282+
| `endpoints[].ui.hide-port` | Whether to hide the port from the results. | `false` |
283+
| `endpoints[].ui.hide-url` | Whether to hide the URL from the results. Useful if the URL contains a token. | `false` |
283284
| `endpoints[].ui.dont-resolve-failed-conditions` | Whether to resolve failed conditions for the UI. | `false` |
284285
| `endpoints[].ui.badge.response-time` | List of response time thresholds. Each time a threshold is reached, the badge has a different color. | `[50, 200, 300, 500, 750]` |
285286

config/endpoint/endpoint.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ import (
1313
"strings"
1414
"time"
1515

16-
"github.com/TwiN/gatus/v5/config/maintenance"
1716
"github.com/TwiN/gatus/v5/alerting/alert"
1817
"github.com/TwiN/gatus/v5/client"
1918
"github.com/TwiN/gatus/v5/config/endpoint/dns"
2019
sshconfig "github.com/TwiN/gatus/v5/config/endpoint/ssh"
2120
"github.com/TwiN/gatus/v5/config/endpoint/ui"
21+
"github.com/TwiN/gatus/v5/config/maintenance"
2222
"golang.org/x/crypto/ssh"
2323
)
2424

@@ -270,6 +270,7 @@ func (e *Endpoint) EvaluateHealth() *Result {
270270
result.AddError(err.Error())
271271
} else {
272272
result.Hostname = urlObject.Hostname()
273+
result.port = urlObject.Port()
273274
}
274275
}
275276
// Retrieve IP if necessary
@@ -307,7 +308,13 @@ func (e *Endpoint) EvaluateHealth() *Result {
307308
for errIdx, errorString := range result.Errors {
308309
result.Errors[errIdx] = strings.ReplaceAll(errorString, result.Hostname, "<redacted>")
309310
}
310-
result.Hostname = ""
311+
result.Hostname = "" // remove it from the result so it doesn't get exposed
312+
}
313+
if e.UIConfig.HidePort && len(result.port) > 0 {
314+
for errIdx, errorString := range result.Errors {
315+
result.Errors[errIdx] = strings.ReplaceAll(errorString, result.port, "<redacted>")
316+
}
317+
result.port = ""
311318
}
312319
if e.UIConfig.HideConditions {
313320
result.ConditionResults = nil

config/endpoint/endpoint_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ func TestEndpoint(t *testing.T) {
165165
Name: "endpoint-that-will-time-out-and-hidden-hostname",
166166
Endpoint: Endpoint{
167167
Name: "endpoint-that-will-time-out",
168-
URL: "https://twin.sh/health",
168+
URL: "https://twin.sh:9999/health",
169169
Conditions: []Condition{"[CONNECTED] == true"},
170-
UIConfig: &ui.Config{HideHostname: true},
170+
UIConfig: &ui.Config{HideHostname: true, HidePort: true},
171171
ClientConfig: &client.Config{Timeout: time.Millisecond},
172172
},
173173
ExpectedResult: &Result{
@@ -180,7 +180,7 @@ func TestEndpoint(t *testing.T) {
180180
// Because there's no [DOMAIN_EXPIRATION] condition, this is not resolved, so it should be 0.
181181
DomainExpiration: 0,
182182
// Because Endpoint.UIConfig.HideHostname is true, the hostname should be replaced by <redacted>.
183-
Errors: []string{`Get "https://<redacted>/health": context deadline exceeded (Client.Timeout exceeded while awaiting headers)`},
183+
Errors: []string{`Get "https://<redacted>:<redacted>/health": context deadline exceeded (Client.Timeout exceeded while awaiting headers)`},
184184
},
185185
MockRoundTripper: nil,
186186
},

config/endpoint/result.go

+5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ type Result struct {
4949
// Note that this field is not persisted in the storage.
5050
// It is used for health evaluation as well as debugging purposes.
5151
Body []byte `json:"-"`
52+
53+
///////////////////////////////////////////////////////////////////////
54+
// Below is used only for the UI and is not persisted in the storage //
55+
///////////////////////////////////////////////////////////////////////
56+
port string `yaml:"-"` // used for endpoints[].ui.hide-port
5257
}
5358

5459
// AddError adds an error to the result's list of errors.

config/endpoint/ui/ui.go

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ type Config struct {
1313
// HideURL whether to ensure the URL is not displayed in the results. Useful if the URL contains a token.
1414
HideURL bool `yaml:"hide-url"`
1515

16+
// HidePort whether to hide the port in the Result
17+
HidePort bool `yaml:"hide-port"`
18+
1619
// DontResolveFailedConditions whether to resolve failed conditions in the Result for display in the UI
1720
DontResolveFailedConditions bool `yaml:"dont-resolve-failed-conditions"`
1821

@@ -54,6 +57,7 @@ func GetDefaultConfig() *Config {
5457
return &Config{
5558
HideHostname: false,
5659
HideURL: false,
60+
HidePort: false,
5761
DontResolveFailedConditions: false,
5862
HideConditions: false,
5963
Badge: &Badge{

0 commit comments

Comments
 (0)