diff --git a/http/notification_endpoint_test.go b/http/notification_endpoint_test.go index 63bfe125cd5..1157efaa64a 100644 --- a/http/notification_endpoint_test.go +++ b/http/notification_endpoint_test.go @@ -75,7 +75,7 @@ func TestService_handleGetNotificationEndpoints(t *testing.T) { OrgID: influxTesting.MustIDBase16Ptr("50f7ba1150f7ba11"), Status: influxdb.Active, }, - URL: "http://example.com", + URL: "http://slack.com", }, &endpoint.HTTP{ Base: endpoint.Base{ @@ -84,7 +84,7 @@ func TestService_handleGetNotificationEndpoints(t *testing.T) { OrgID: influxTesting.MustIDBase16Ptr("7e55e118dbabb1ed"), Status: influxdb.Inactive, }, - URL: "example.com", + URL: "http://example.com", Username: influxdb.SecretField{Key: "http-user-key"}, Password: influxdb.SecretField{Key: "http-password-key"}, AuthMethod: "basic", @@ -152,11 +152,11 @@ func TestService_handleGetNotificationEndpoints(t *testing.T) { "type": "slack", "token": "", "updatedAt": "0001-01-01T00:00:00Z", - "url": "http://example.com" + "url": "http://slack.com" }, { "createdAt": "0001-01-01T00:00:00Z", - "url": "example.com", + "url": "http://example.com", "id": "c0175f0077a77005", "labels": [ { @@ -293,7 +293,7 @@ func TestService_handleGetNotificationEndpoint(t *testing.T) { Name: "hello", Status: influxdb.Active, }, - URL: "example.com", + URL: "http://example.com", Username: influxdb.SecretField{Key: "http-user-key"}, Password: influxdb.SecretField{Key: "http-password-key"}, AuthMethod: "basic", @@ -326,7 +326,7 @@ func TestService_handleGetNotificationEndpoint(t *testing.T) { "createdAt": "0001-01-01T00:00:00Z", "updatedAt": "0001-01-01T00:00:00Z", "id": "020f755c3c082000", - "url": "example.com", + "url": "http://example.com", "username": "secret: http-user-key", "password": "secret: http-password-key", "token":"", @@ -450,7 +450,7 @@ func TestService_handlePostNotificationEndpoint(t *testing.T) { "orgID": "6f626f7274697320", "description": "desc1", "status": "active", - "url": "example.com", + "url": "http://example.com", "username": "user1", "password": "password1", "authMethod": "basic", @@ -469,7 +469,7 @@ func TestService_handlePostNotificationEndpoint(t *testing.T) { "members": "/api/v2/notificationEndpoints/020f755c3c082000/members", "owners": "/api/v2/notificationEndpoints/020f755c3c082000/owners" }, - "url": "example.com", + "url": "http://example.com", "status": "active", "username": "secret: 020f755c3c082000-username", "password": "secret: 020f755c3c082000-password", @@ -489,6 +489,44 @@ func TestService_handlePostNotificationEndpoint(t *testing.T) { `, }, }, + { + name: "create a new notification endpoint with bad URL", + fields: fields{ + Secrets: map[string]string{}, + NotificationEndpointService: &mock.NotificationEndpointService{ + CreateNotificationEndpointF: func(ctx context.Context, edp influxdb.NotificationEndpoint, userID platform.ID) error { + edp.SetID(influxTesting.MustIDBase16("020f755c3c082000")) + edp.BackfillSecretKeys() + return nil + }, + }, + OrganizationService: &mock.OrganizationService{ + FindOrganizationF: func(ctx context.Context, f influxdb.OrganizationFilter) (*influxdb.Organization, error) { + return &influxdb.Organization{ID: influxTesting.MustIDBase16("6f626f7274697320")}, nil + }, + }, + }, + args: args{ + endpoint: map[string]interface{}{ + "name": "hello", + "type": "http", + "orgID": "6f626f7274697320", + "description": "desc1", + "status": "active", + "url": "example.com", + "username": "user1", + "password": "password1", + "authMethod": "basic", + "method": "POST", + "contentTemplate": "template", + }, + }, + wants: wants{ + statusCode: http.StatusBadRequest, + contentType: "application/json; charset=utf-8", + body: `{"code":"invalid","message":"the provided url should match the standard url schema"}`, + }, + }, } for _, tt := range tests { @@ -796,7 +834,7 @@ func TestService_handleUpdateNotificationEndpoint(t *testing.T) { "orgID": "020f755c3c082001", "type": "slack", "token": "", - "url": "example.com", + "url": "https://slack.com", }, }, wants: wants{ @@ -815,7 +853,7 @@ func TestService_handleUpdateNotificationEndpoint(t *testing.T) { "id": "020f755c3c082000", "orgID": "020f755c3c082001", "name": "example", - "url": "example.com", + "url": "https://slack.com", "type": "slack", "status": "active", "token": "", diff --git a/notification/endpoint/http.go b/notification/endpoint/http.go index 75cf1e66d09..d831f662988 100644 --- a/notification/endpoint/http.go +++ b/notification/endpoint/http.go @@ -133,6 +133,25 @@ func (s HTTP) MarshalJSON() ([]byte, error) { }) } +func (s *HTTP) UnmarshalJSON(b []byte) error { + type Alias HTTP + var a Alias + + if err := json.Unmarshal(b, &a); err != nil { + return err + } + + if a.URL != "" { + if u, err := url.Parse(a.URL); err != nil || u.Scheme == "" || u.Host == "" { + return fmt.Errorf("the provided url should match the standard url schema") + } + } + + *s = HTTP(a) + + return nil +} + // Type returns the type. func (s HTTP) Type() string { return HTTPType diff --git a/notification/endpoint/service/testing/service.go b/notification/endpoint/service/testing/service.go index 408b18d82bc..028f94f5804 100644 --- a/notification/endpoint/service/testing/service.go +++ b/notification/endpoint/service/testing/service.go @@ -228,7 +228,7 @@ func FindNotificationEndpointByID( UpdatedAt: timeGen2.Now(), }, }, - URL: "example-slack.com", + URL: "https://example-slack.com", Token: influxdb.SecretField{ Key: fmt.Sprintf("%s-token", oneID), }, @@ -276,7 +276,7 @@ func FindNotificationEndpointByID( UpdatedAt: timeGen2.Now(), }, }, - URL: "example-slack.com", + URL: "https://example-slack.com", Token: influxdb.SecretField{Key: fmt.Sprintf("%s-token", oneID)}, }, &endpoint.PagerDuty{ @@ -320,7 +320,7 @@ func FindNotificationEndpointByID( UpdatedAt: timeGen2.Now(), }, }, - URL: "example-slack.com", + URL: "https://example-slack.com", Token: influxdb.SecretField{Key: fmt.Sprintf("%s-token", oneID)}, }, &endpoint.PagerDuty{ @@ -422,7 +422,7 @@ func FindNotificationEndpoints( UpdatedAt: timeGen2.Now(), }, }, - URL: "example-slack.com", + URL: "https://example-slack.com", Token: influxdb.SecretField{Key: fmt.Sprintf("%s-token", oneID)}, }, &endpoint.PagerDuty{ @@ -454,7 +454,7 @@ func FindNotificationEndpoints( UpdatedAt: timeGen2.Now(), }, }, - URL: "example-slack.com", + URL: "https://example-slack.com", Token: influxdb.SecretField{Key: fmt.Sprintf("%s-token", oneID)}, }, &endpoint.PagerDuty{ @@ -495,7 +495,7 @@ func FindNotificationEndpoints( Status: influxdb.Active, Name: "edp1", }, - URL: "example-slack.com", + URL: "https://example-slack.com", Token: influxdb.SecretField{Key: fmt.Sprintf("%s-token", oneID)}, }, &endpoint.PagerDuty{ @@ -561,7 +561,7 @@ func FindNotificationEndpoints( Status: influxdb.Active, Name: "edp1", }, - URL: "example-slack.com", + URL: "https://example-slack.com", Token: influxdb.SecretField{Key: fmt.Sprintf("%s-token", oneID)}, }, &endpoint.HTTP{ @@ -571,7 +571,7 @@ func FindNotificationEndpoints( Status: influxdb.Active, Name: "edp2", }, - URL: "example-webhook.com", + URL: "http://example-webhook.com", Method: http.MethodGet, AuthMethod: "none", }, @@ -582,7 +582,7 @@ func FindNotificationEndpoints( Status: influxdb.Active, Name: "edp3", }, - ClientURL: "example-pagerduty.com", + ClientURL: "https://example-pagerduty.com", RoutingKey: influxdb.SecretField{Key: fmt.Sprintf("%s-routing-key", fourID)}, }, &endpoint.PagerDuty{ @@ -592,7 +592,7 @@ func FindNotificationEndpoints( Status: influxdb.Active, Name: "edp4", }, - ClientURL: "example-pagerduty.com", + ClientURL: "https://example-pagerduty.com", RoutingKey: influxdb.SecretField{Key: fmt.Sprintf("%s-routing-key", fourID)}, }, }, @@ -614,7 +614,7 @@ func FindNotificationEndpoints( Status: influxdb.Active, Name: "edp1", }, - URL: "example-slack.com", + URL: "https://example-slack.com", Token: influxdb.SecretField{Key: fmt.Sprintf("%s-token", oneID)}, }, &endpoint.HTTP{ @@ -624,7 +624,7 @@ func FindNotificationEndpoints( Status: influxdb.Active, Name: "edp2", }, - URL: "example-webhook.com", + URL: "http://example-webhook.com", Method: http.MethodGet, AuthMethod: "none", }, @@ -652,7 +652,7 @@ func FindNotificationEndpoints( Status: influxdb.Active, Name: "edp1", }, - URL: "example-slack.com", + URL: "https://example-slack.com", Token: influxdb.SecretField{Key: fmt.Sprintf("%s-token", oneID)}, }, &endpoint.HTTP{ @@ -662,7 +662,7 @@ func FindNotificationEndpoints( Status: influxdb.Active, Name: "edp2", }, - URL: "example-webhook.com", + URL: "http://example-webhook.com", Method: http.MethodGet, AuthMethod: "none", }, @@ -673,7 +673,7 @@ func FindNotificationEndpoints( Status: influxdb.Active, Name: "edp3", }, - ClientURL: "example-pagerduty.com", + ClientURL: "https://example-pagerduty.com", RoutingKey: influxdb.SecretField{Key: fmt.Sprintf("%s-routing-key", fourID)}, }, }, @@ -695,7 +695,7 @@ func FindNotificationEndpoints( Status: influxdb.Active, Name: "edp2", }, - URL: "example-webhook.com", + URL: "http://example-webhook.com", Method: http.MethodGet, AuthMethod: "none", }, @@ -706,7 +706,7 @@ func FindNotificationEndpoints( Status: influxdb.Active, Name: "edp3", }, - ClientURL: "example-pagerduty.com", + ClientURL: "https://example-pagerduty.com", RoutingKey: influxdb.SecretField{Key: fmt.Sprintf("%s-routing-key", fourID)}, }, }, @@ -733,7 +733,7 @@ func FindNotificationEndpoints( Status: influxdb.Active, Name: "edp1", }, - URL: "example-slack.com", + URL: "https://example-slack.com", Token: influxdb.SecretField{Key: fmt.Sprintf("%s-token", oneID)}, }, &endpoint.HTTP{ @@ -743,7 +743,7 @@ func FindNotificationEndpoints( Status: influxdb.Active, Name: "edp2", }, - URL: "example-webhook.com", + URL: "http://example-webhook.com", Method: http.MethodGet, AuthMethod: "none", }, @@ -754,7 +754,7 @@ func FindNotificationEndpoints( Status: influxdb.Active, Name: "edp3", }, - ClientURL: "example-pagerduty.com", + ClientURL: "https://example-pagerduty.com", RoutingKey: influxdb.SecretField{Key: fmt.Sprintf("%s-routing-key", fourID)}, }, }, @@ -777,7 +777,7 @@ func FindNotificationEndpoints( Status: influxdb.Active, Name: "edp2", }, - URL: "example-webhook.com", + URL: "http://example-webhook.com", Method: http.MethodGet, AuthMethod: "none", }, @@ -805,7 +805,7 @@ func FindNotificationEndpoints( Status: influxdb.Active, Name: "edp1", }, - URL: "example-slack.com", + URL: "https://example-slack.com", Token: influxdb.SecretField{Key: fmt.Sprintf("%s-token", oneID)}, }, &endpoint.HTTP{ @@ -815,7 +815,7 @@ func FindNotificationEndpoints( Status: influxdb.Active, Name: "edp2", }, - URL: "example-webhook.com", + URL: "http://example-webhook.com", Method: http.MethodGet, AuthMethod: "none", }, @@ -826,7 +826,7 @@ func FindNotificationEndpoints( Status: influxdb.Active, Name: "edp3", }, - ClientURL: "example-pagerduty.com", + ClientURL: "https://example-pagerduty.com", RoutingKey: influxdb.SecretField{Key: fmt.Sprintf("%s-routing-key", fourID)}, }, }, @@ -845,7 +845,7 @@ func FindNotificationEndpoints( Status: influxdb.Active, Name: "edp3", }, - ClientURL: "example-pagerduty.com", + ClientURL: "https://example-pagerduty.com", RoutingKey: influxdb.SecretField{Key: fmt.Sprintf("%s-routing-key", fourID)}, }, }, @@ -872,7 +872,7 @@ func FindNotificationEndpoints( Status: influxdb.Active, Name: "edp1", }, - URL: "example-slack.com", + URL: "https://example-slack.com", Token: influxdb.SecretField{Key: fmt.Sprintf("%s-token", oneID)}, }, &endpoint.HTTP{ @@ -882,7 +882,7 @@ func FindNotificationEndpoints( Status: influxdb.Active, Name: "edp2", }, - URL: "example-webhook.com", + URL: "http://example-webhook.com", Method: http.MethodGet, AuthMethod: "none", }, @@ -893,7 +893,7 @@ func FindNotificationEndpoints( Status: influxdb.Active, Name: "edp3", }, - ClientURL: "example-pagerduty.com", + ClientURL: "https://example-pagerduty.com", RoutingKey: influxdb.SecretField{Key: fmt.Sprintf("%s-routing-key", threeID)}, }, }, @@ -928,7 +928,7 @@ func FindNotificationEndpoints( Status: influxdb.Active, Name: "edp1", }, - URL: "example-slack.com", + URL: "https://example-slack.com", Token: influxdb.SecretField{Key: fmt.Sprintf("%s-token", oneID)}, }, &endpoint.HTTP{ @@ -938,7 +938,7 @@ func FindNotificationEndpoints( Status: influxdb.Active, Name: "edp2", }, - URL: "example-webhook.com", + URL: "http://example-webhook.com", Method: http.MethodGet, AuthMethod: "none", }, @@ -949,7 +949,7 @@ func FindNotificationEndpoints( Status: influxdb.Active, Name: "edp3", }, - ClientURL: "example-pagerduty.com", RoutingKey: influxdb.SecretField{Key: fmt.Sprintf("%s-routing-key", threeID)}, + ClientURL: "https://example-pagerduty.com", RoutingKey: influxdb.SecretField{Key: fmt.Sprintf("%s-routing-key", threeID)}, }, }, }, @@ -1019,7 +1019,7 @@ func UpdateNotificationEndpoint( UpdatedAt: timeGen2.Now(), }, }, - URL: "example-slack.com", + URL: "https://example-slack.com", Token: influxdb.SecretField{Key: fmt.Sprintf("%s-token", oneID)}, }, &endpoint.PagerDuty{ @@ -1075,7 +1075,7 @@ func UpdateNotificationEndpoint( UpdatedAt: timeGen2.Now(), }, }, - URL: "example-slack.com", + URL: "https://example-slack.com", Token: influxdb.SecretField{Key: fmt.Sprintf("%s-token", oneID)}, }, &endpoint.PagerDuty{ @@ -1257,7 +1257,7 @@ func PatchNotificationEndpoint( UpdatedAt: timeGen2.Now(), }, }, - URL: "example-slack.com", + URL: "https://example-slack.com", Token: influxdb.SecretField{Key: fmt.Sprintf("%s-token", oneID)}, }, &endpoint.PagerDuty{ @@ -1306,7 +1306,7 @@ func PatchNotificationEndpoint( UpdatedAt: timeGen2.Now(), }, }, - URL: "example-slack.com", + URL: "https://example-slack.com", Token: influxdb.SecretField{Key: fmt.Sprintf("%s-token", oneID)}, }, &endpoint.PagerDuty{ @@ -1411,7 +1411,7 @@ func DeleteNotificationEndpoint( UpdatedAt: timeGen2.Now(), }, }, - URL: "example-slack.com", + URL: "https://example-slack.com", Token: influxdb.SecretField{Key: fmt.Sprintf("%s-token", oneID)}, }, &endpoint.PagerDuty{ @@ -1452,7 +1452,7 @@ func DeleteNotificationEndpoint( UpdatedAt: timeGen2.Now(), }, }, - URL: "example-slack.com", + URL: "https://example-slack.com", Token: influxdb.SecretField{Key: fmt.Sprintf("%s-token", oneID)}, }, &endpoint.PagerDuty{ @@ -1486,7 +1486,7 @@ func DeleteNotificationEndpoint( UpdatedAt: timeGen2.Now(), }, }, - URL: "example-slack.com", + URL: "https://example-slack.com", Token: influxdb.SecretField{Key: fmt.Sprintf("%s-token", oneID)}, }, &endpoint.PagerDuty{ @@ -1526,7 +1526,7 @@ func DeleteNotificationEndpoint( UpdatedAt: timeGen2.Now(), }, }, - URL: "example-slack.com", + URL: "https://example-slack.com", Token: influxdb.SecretField{Key: fmt.Sprintf("%s-token", oneID)}, }, &endpoint.PagerDuty{ @@ -1561,7 +1561,7 @@ func DeleteNotificationEndpoint( UpdatedAt: timeGen2.Now(), }, }, - URL: "example-slack.com", + URL: "https://example-slack.com", Token: influxdb.SecretField{Key: fmt.Sprintf("%s-token", oneID)}, }, &endpoint.PagerDuty{ @@ -1602,7 +1602,7 @@ func DeleteNotificationEndpoint( UpdatedAt: timeGen2.Now(), }, }, - URL: "example-slack.com", + URL: "https://example-slack.com", Token: influxdb.SecretField{Key: fmt.Sprintf("%s-token", oneID)}, }, }, diff --git a/notification/endpoint/slack.go b/notification/endpoint/slack.go index f164475b00a..b5f69ad02db 100644 --- a/notification/endpoint/slack.go +++ b/notification/endpoint/slack.go @@ -77,6 +77,25 @@ func (s Slack) MarshalJSON() ([]byte, error) { }) } +func (s *Slack) UnmarshalJSON(b []byte) error { + type Alias Slack + var a Alias + + if err := json.Unmarshal(b, &a); err != nil { + return err + } + + if a.URL != "" { + if u, err := url.Parse(a.URL); err != nil || u.Scheme == "" || u.Host == "" { + return fmt.Errorf("the provided url should match the standard url schema") + } + } + + *s = Slack(a) + + return nil +} + // Type returns the type. func (s Slack) Type() string { return SlackType