From 09c3a6c72bd13d94f2c798a77316bba01fab98c1 Mon Sep 17 00:00:00 2001 From: Kalissaac Date: Tue, 19 Jul 2022 10:15:41 -0700 Subject: [PATCH] fix(alerting): Reuse MatrixProviderConfig struct --- alerting/provider/matrix/matrix.go | 38 ++++--- alerting/provider/matrix/matrix_test.go | 128 +++++++++++++++--------- watchdog/alerting_test.go | 8 +- 3 files changed, 105 insertions(+), 69 deletions(-) diff --git a/alerting/provider/matrix/matrix.go b/alerting/provider/matrix/matrix.go index ca4d787e..c1171c25 100644 --- a/alerting/provider/matrix/matrix.go +++ b/alerting/provider/matrix/matrix.go @@ -16,12 +16,7 @@ import ( // AlertProvider is the configuration necessary for sending an alert using Matrix type AlertProvider struct { - // HomeserverURL is the custom homeserver to use (optional) - HomeserverURL string `yaml:"homeserver-url"` - // AccessToken is the bot user's access token to send messages - AccessToken string `yaml:"access-token"` - // InternalRoomID is the room that the bot user has permissions to send messages to - InternalRoomID string `yaml:"internal-room-id"` + MatrixProviderConfig `yaml:",inline"` // DefaultAlert is the default alert configuration to use for endpoints with an alert of the appropriate type DefaultAlert *alert.Alert `yaml:"default-alert,omitempty"` @@ -34,16 +29,19 @@ type AlertProvider struct { type Override struct { Group string `yaml:"group"` - HomeserverURL string `yaml:"homeserver-url"` - AccessToken string `yaml:"access-token"` - InternalRoomID string `yaml:"internal-room-id"` + MatrixProviderConfig `yaml:",inline"` } const defaultHomeserverURL = "https://matrix-client.matrix.org" -type matrixProviderConfig struct { - HomeserverURL string `yaml:"homeserver-url"` - AccessToken string `yaml:"access-token"` +type MatrixProviderConfig struct { + // ServerURL is the custom homeserver to use (optional) + ServerURL string `yaml:"server-url"` + + // AccessToken is the bot user's access token to send messages + AccessToken string `yaml:"access-token"` + + // InternalRoomID is the room that the bot user has permissions to send messages to InternalRoomID string `yaml:"internal-room-id"` } @@ -65,14 +63,14 @@ func (provider *AlertProvider) IsValid() bool { func (provider *AlertProvider) Send(endpoint *core.Endpoint, alert *alert.Alert, result *core.Result, resolved bool) error { buffer := bytes.NewBuffer([]byte(provider.buildRequestBody(endpoint, alert, result, resolved))) config := provider.getConfigForGroup(endpoint.Group) - if config.HomeserverURL == "" { - config.HomeserverURL = defaultHomeserverURL + if config.ServerURL == "" { + config.ServerURL = defaultHomeserverURL } txnId := randStringBytes(24) request, err := http.NewRequest( http.MethodPut, fmt.Sprintf("%s/_matrix/client/r0/rooms/%s/send/m.room.message/%s?access_token=%s", - config.HomeserverURL, + config.ServerURL, url.PathEscape(config.InternalRoomID), txnId, url.QueryEscape(config.AccessToken), @@ -156,20 +154,20 @@ func buildHTMLMessageBody(endpoint *core.Endpoint, alert *alert.Alert, result *c } // getConfigForGroup returns the appropriate configuration for a given group -func (provider *AlertProvider) getConfigForGroup(group string) matrixProviderConfig { +func (provider *AlertProvider) getConfigForGroup(group string) MatrixProviderConfig { if provider.Overrides != nil { for _, override := range provider.Overrides { if group == override.Group { - return matrixProviderConfig{ - HomeserverURL: override.HomeserverURL, + return MatrixProviderConfig{ + ServerURL: override.ServerURL, AccessToken: override.AccessToken, InternalRoomID: override.InternalRoomID, } } } } - return matrixProviderConfig{ - HomeserverURL: provider.HomeserverURL, + return MatrixProviderConfig{ + ServerURL: provider.ServerURL, AccessToken: provider.AccessToken, InternalRoomID: provider.InternalRoomID, } diff --git a/alerting/provider/matrix/matrix_test.go b/alerting/provider/matrix/matrix_test.go index 1c8cad00..12ba6042 100644 --- a/alerting/provider/matrix/matrix_test.go +++ b/alerting/provider/matrix/matrix_test.go @@ -12,15 +12,31 @@ import ( ) func TestAlertProvider_IsValid(t *testing.T) { - invalidProvider := AlertProvider{AccessToken: "", InternalRoomID: ""} + invalidProvider := AlertProvider{ + MatrixProviderConfig: MatrixProviderConfig{ + AccessToken: "", + InternalRoomID: "", + }, + } if invalidProvider.IsValid() { t.Error("provider shouldn't have been valid") } - validProvider := AlertProvider{AccessToken: "1", InternalRoomID: "!a:example.com"} + validProvider := AlertProvider{ + MatrixProviderConfig: MatrixProviderConfig{ + AccessToken: "1", + InternalRoomID: "!a:example.com", + }, + } if !validProvider.IsValid() { t.Error("provider should've been valid") } - validProviderWithHomeserver := AlertProvider{HomeserverURL: "https://example.com", AccessToken: "1", InternalRoomID: "!a:example.com"} + validProviderWithHomeserver := AlertProvider{ + MatrixProviderConfig: MatrixProviderConfig{ + ServerURL: "https://example.com", + AccessToken: "1", + InternalRoomID: "!a:example.com", + }, + } if !validProviderWithHomeserver.IsValid() { t.Error("provider with homeserver should've been valid") } @@ -30,9 +46,11 @@ func TestAlertProvider_IsValidWithOverride(t *testing.T) { providerWithInvalidOverrideGroup := AlertProvider{ Overrides: []Override{ { - AccessToken: "", - InternalRoomID: "", - Group: "", + Group: "", + MatrixProviderConfig: MatrixProviderConfig{ + AccessToken: "", + InternalRoomID: "", + }, }, }, } @@ -42,9 +60,11 @@ func TestAlertProvider_IsValidWithOverride(t *testing.T) { providerWithInvalidOverrideTo := AlertProvider{ Overrides: []Override{ { - AccessToken: "", - InternalRoomID: "", - Group: "group", + Group: "group", + MatrixProviderConfig: MatrixProviderConfig{ + AccessToken: "", + InternalRoomID: "", + }, }, }, } @@ -52,14 +72,18 @@ func TestAlertProvider_IsValidWithOverride(t *testing.T) { t.Error("provider integration key shouldn't have been valid") } providerWithValidOverride := AlertProvider{ - AccessToken: "1", - InternalRoomID: "!a:example.com", + MatrixProviderConfig: MatrixProviderConfig{ + AccessToken: "1", + InternalRoomID: "!a:example.com", + }, Overrides: []Override{ { - HomeserverURL: "https://example.com", - AccessToken: "1", - InternalRoomID: "!a:example.com", - Group: "group", + Group: "group", + MatrixProviderConfig: MatrixProviderConfig{ + ServerURL: "https://example.com", + AccessToken: "1", + InternalRoomID: "!a:example.com", + }, }, }, } @@ -208,19 +232,21 @@ func TestAlertProvider_getConfigForGroup(t *testing.T) { Name string Provider AlertProvider InputGroup string - ExpectedOutput matrixProviderConfig + ExpectedOutput MatrixProviderConfig }{ { Name: "provider-no-override-specify-no-group-should-default", Provider: AlertProvider{ - HomeserverURL: "https://example.com", - AccessToken: "1", - InternalRoomID: "!a:example.com", - Overrides: nil, + MatrixProviderConfig: MatrixProviderConfig{ + ServerURL: "https://example.com", + AccessToken: "1", + InternalRoomID: "!a:example.com", + }, + Overrides: nil, }, InputGroup: "", - ExpectedOutput: matrixProviderConfig{ - HomeserverURL: "https://example.com", + ExpectedOutput: MatrixProviderConfig{ + ServerURL: "https://example.com", AccessToken: "1", InternalRoomID: "!a:example.com", }, @@ -228,14 +254,16 @@ func TestAlertProvider_getConfigForGroup(t *testing.T) { { Name: "provider-no-override-specify-group-should-default", Provider: AlertProvider{ - HomeserverURL: "https://example.com", - AccessToken: "1", - InternalRoomID: "!a:example.com", - Overrides: nil, + MatrixProviderConfig: MatrixProviderConfig{ + ServerURL: "https://example.com", + AccessToken: "1", + InternalRoomID: "!a:example.com", + }, + Overrides: nil, }, InputGroup: "group", - ExpectedOutput: matrixProviderConfig{ - HomeserverURL: "https://example.com", + ExpectedOutput: MatrixProviderConfig{ + ServerURL: "https://example.com", AccessToken: "1", InternalRoomID: "!a:example.com", }, @@ -243,21 +271,25 @@ func TestAlertProvider_getConfigForGroup(t *testing.T) { { Name: "provider-with-override-specify-no-group-should-default", Provider: AlertProvider{ - HomeserverURL: "https://example.com", - AccessToken: "1", - InternalRoomID: "!a:example.com", + MatrixProviderConfig: MatrixProviderConfig{ + ServerURL: "https://example.com", + AccessToken: "1", + InternalRoomID: "!a:example.com", + }, Overrides: []Override{ { - Group: "group", - HomeserverURL: "https://example01.com", - AccessToken: "12", - InternalRoomID: "!a:example01.com", + Group: "group", + MatrixProviderConfig: MatrixProviderConfig{ + ServerURL: "https://example01.com", + AccessToken: "12", + InternalRoomID: "!a:example01.com", + }, }, }, }, InputGroup: "", - ExpectedOutput: matrixProviderConfig{ - HomeserverURL: "https://example.com", + ExpectedOutput: MatrixProviderConfig{ + ServerURL: "https://example.com", AccessToken: "1", InternalRoomID: "!a:example.com", }, @@ -265,21 +297,25 @@ func TestAlertProvider_getConfigForGroup(t *testing.T) { { Name: "provider-with-override-specify-group-should-override", Provider: AlertProvider{ - HomeserverURL: "https://example.com", - AccessToken: "1", - InternalRoomID: "!a:example.com", + MatrixProviderConfig: MatrixProviderConfig{ + ServerURL: "https://example.com", + AccessToken: "1", + InternalRoomID: "!a:example.com", + }, Overrides: []Override{ { - Group: "group", - HomeserverURL: "https://example01.com", - AccessToken: "12", - InternalRoomID: "!a:example01.com", + Group: "group", + MatrixProviderConfig: MatrixProviderConfig{ + ServerURL: "https://example01.com", + AccessToken: "12", + InternalRoomID: "!a:example01.com", + }, }, }, }, InputGroup: "group", - ExpectedOutput: matrixProviderConfig{ - HomeserverURL: "https://example01.com", + ExpectedOutput: MatrixProviderConfig{ + ServerURL: "https://example01.com", AccessToken: "12", InternalRoomID: "!a:example01.com", }, diff --git a/watchdog/alerting_test.go b/watchdog/alerting_test.go index bc8cbb01..b4e5d85b 100644 --- a/watchdog/alerting_test.go +++ b/watchdog/alerting_test.go @@ -318,9 +318,11 @@ func TestHandleAlertingWithProviderThatReturnsAnError(t *testing.T) { AlertType: alert.TypeMatrix, AlertingConfig: &alerting.Config{ Matrix: &matrix.AlertProvider{ - HomeserverURL: "https://example.com", - AccessToken: "1", - InternalRoomID: "!a:example.com", + MatrixProviderConfig: matrix.MatrixProviderConfig{ + ServerURL: "https://example.com", + AccessToken: "1", + InternalRoomID: "!a:example.com", + }, }, }, },