Skip to content

Commit

Permalink
Https validation for Notification channel webhooks (#192)
Browse files Browse the repository at this point in the history
* Add https validation while updating notification channel

* Add https validation for update notification channel flow
  • Loading branch information
pavan-traceable authored Dec 26, 2023
1 parent 66b1e14 commit 8695a54
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ public void validateUpdateNotificationChannelRequest(
validateNotificationChannelMutableData(request.getNotificationChannelMutableData());
validateWebhookConfigExclusionDomains(
request.getNotificationChannelMutableData(), notificationChannelConfig);
validateWebhookHttpSupport(
request.getNotificationChannelMutableData(), notificationChannelConfig);
}

private void validateNotificationChannelMutableData(NotificationChannelMutableData data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
import com.typesafe.config.ConfigFactory;
import com.typesafe.config.ConfigValueFactory;
import java.io.File;
import org.hypertrace.core.grpcutils.context.RequestContext;
import org.hypertrace.notification.config.service.v1.NotificationChannelMutableData;
import org.hypertrace.notification.config.service.v1.UpdateNotificationChannelRequest;
import org.hypertrace.notification.config.service.v1.WebhookChannelConfig;
import org.hypertrace.notification.config.service.v1.WebhookFormat;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -73,6 +76,17 @@ public void testValidateWebhookHttpsSupport() {
notificationChannelConfigServiceRequestValidator.validateWebhookHttpSupport(
notificationChannelMutableDataWithHttpsUrl, notificationChannelConfig);

// Http Webhook url while updating notification channel should throw exception
Assertions.assertThrows(
RuntimeException.class,
() -> {
notificationChannelConfigServiceRequestValidator.validateUpdateNotificationChannelRequest(
RequestContext.forTenantId("tenant1"),
getUpdateNotificationChannelRequestWithHttpUrl(),
notificationChannelConfig);
},
"RuntimeException was expected");

// Update config with http support enabled and verify no exceptions for http url
Config updatedNotificationChannelConfig =
config.withValue(WEBHOOK_HTTP_SUPPORT_ENABLED, ConfigValueFactory.fromAnyRef("true"));
Expand All @@ -81,12 +95,38 @@ public void testValidateWebhookHttpsSupport() {
getNotificationChannelMutableData("http://localhost:9000/test");
notificationChannelConfigServiceRequestValidator.validateWebhookHttpSupport(
notificationChannelMutableDataWithHttpUrl, updatedNotificationChannelConfig);

// Update config with http support enabled and verify no exceptions for http url
notificationChannelConfigServiceRequestValidator.validateUpdateNotificationChannelRequest(
RequestContext.forTenantId("tenant1"),
UpdateNotificationChannelRequest.newBuilder()
.setId("id1")
.setNotificationChannelMutableData(notificationChannelMutableDataWithHttpUrl)
.build(),
updatedNotificationChannelConfig);
}

private static UpdateNotificationChannelRequest getUpdateNotificationChannelRequestWithHttpUrl() {
return UpdateNotificationChannelRequest.newBuilder()
.setNotificationChannelMutableData(
NotificationChannelMutableData.newBuilder()
.setChannelName("channel1")
.addWebhookChannelConfig(
WebhookChannelConfig.newBuilder()
.setUrl("http://localhost:9000/url")
.setFormat(WebhookFormat.WEBHOOK_FORMAT_JSON)
.build())
.build())
.build();
}

private static NotificationChannelMutableData getNotificationChannelMutableData(String url) {
return NotificationChannelMutableData.newBuilder()
.setChannelName("testChannel")
.addWebhookChannelConfig(WebhookChannelConfig.newBuilder().setUrl(url))
.addWebhookChannelConfig(
WebhookChannelConfig.newBuilder()
.setUrl(url)
.setFormat(WebhookFormat.WEBHOOK_FORMAT_JSON))
.build();
}
}

0 comments on commit 8695a54

Please sign in to comment.