Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add webhook domain exclusion config for notification channels #172

Merged
merged 5 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public List<GrpcPlatformService> buildServices(
localChannel, config, configChangeEventGenerator),
new EventConditionConfigServiceImpl(localChannel, configChangeEventGenerator),
new NotificationRuleConfigServiceImpl(localChannel, configChangeEventGenerator),
new NotificationChannelConfigServiceImpl(localChannel, configChangeEventGenerator),
new NotificationChannelConfigServiceImpl(
localChannel, config, configChangeEventGenerator),
SpanProcessingConfigServiceFactory.build(localChannel, config))
.map(GrpcPlatformService::new)
.collect(Collectors.toUnmodifiableList());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.hypertrace.notification.config.service;

import com.typesafe.config.Config;
import io.grpc.Channel;
import io.grpc.Status;
import io.grpc.stub.StreamObserver;
Expand Down Expand Up @@ -27,11 +28,18 @@
public class NotificationChannelConfigServiceImpl
extends NotificationChannelConfigServiceGrpc.NotificationChannelConfigServiceImplBase {

static final String NOTIFICATION_CHANNEL_CONFIG_SERVICE_CONFIG =
"notification.channel.config.service";

private final NotificationChannelStore notificationChannelStore;
private final NotificationChannelConfigServiceRequestValidator validator;
private Config notificationChannelConfig = null;

public NotificationChannelConfigServiceImpl(
Channel channel, ConfigChangeEventGenerator configChangeEventGenerator) {
Channel channel, Config config, ConfigChangeEventGenerator configChangeEventGenerator) {
if (config.hasPath(NOTIFICATION_CHANNEL_CONFIG_SERVICE_CONFIG)) {
this.notificationChannelConfig = config.getConfig(NOTIFICATION_CHANNEL_CONFIG_SERVICE_CONFIG);
}
this.notificationChannelStore =
new NotificationChannelStore(channel, configChangeEventGenerator);
this.validator = new NotificationChannelConfigServiceRequestValidator();
Expand All @@ -43,7 +51,8 @@ public void createNotificationChannel(
StreamObserver<CreateNotificationChannelResponse> responseObserver) {
try {
RequestContext requestContext = RequestContext.CURRENT.get();
validator.validateCreateNotificationChannelRequest(requestContext, request);
validator.validateCreateNotificationChannelRequest(
requestContext, request, notificationChannelConfig);
NotificationChannel.Builder builder =
NotificationChannel.newBuilder()
.setId(UUID.randomUUID().toString())
Expand All @@ -66,7 +75,8 @@ public void updateNotificationChannel(
StreamObserver<UpdateNotificationChannelResponse> responseObserver) {
try {
RequestContext requestContext = RequestContext.CURRENT.get();
validator.validateUpdateNotificationChannelRequest(requestContext, request);
validator.validateUpdateNotificationChannelRequest(
requestContext, request, notificationChannelConfig);
responseObserver.onNext(
UpdateNotificationChannelResponse.newBuilder()
.setNotificationChannel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import static org.hypertrace.config.validation.GrpcValidatorUtils.validateNonDefaultPresenceOrThrow;
import static org.hypertrace.config.validation.GrpcValidatorUtils.validateRequestContextOrThrow;

import com.typesafe.config.Config;
import io.grpc.Status;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import org.hypertrace.core.grpcutils.context.RequestContext;
import org.hypertrace.notification.config.service.v1.AwsS3BucketChannelConfig;
import org.hypertrace.notification.config.service.v1.AwsS3BucketChannelConfig.WebIdentityAuthenticationCredential;
Expand All @@ -20,17 +24,86 @@

public class NotificationChannelConfigServiceRequestValidator {

public static final String WEBHOOK_EXCLUSION_DOMAINS = "webhook.exclusion.domains";
public static final String WEBHOOK_HTTP_SUPPORT_ENABLED = "webhook.http.support.enabled";

public void validateCreateNotificationChannelRequest(
RequestContext requestContext, CreateNotificationChannelRequest request) {
RequestContext requestContext,
CreateNotificationChannelRequest request,
Config notificationChannelConfig) {
validateRequestContextOrThrow(requestContext);
validateNotificationChannelMutableData(request.getNotificationChannelMutableData());
validateWebhookConfigExclusionDomains(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add unit tests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done. Added validation for https support

request.getNotificationChannelMutableData(), notificationChannelConfig);
validateWebhookHttpSupport(
request.getNotificationChannelMutableData(), notificationChannelConfig);
}

void validateWebhookHttpSupport(
NotificationChannelMutableData notificationChannelMutableData,
Config notificationChannelConfig) {
if (notificationChannelConfig == null
|| notificationChannelMutableData.getWebhookChannelConfigList().isEmpty()) {
return;
}
for (WebhookChannelConfig webhookChannelConfig :
notificationChannelMutableData.getWebhookChannelConfigList()) {
if (notificationChannelConfig.hasPath(WEBHOOK_HTTP_SUPPORT_ENABLED)
&& notificationChannelConfig.getBoolean(WEBHOOK_HTTP_SUPPORT_ENABLED)) {
continue;
}
validateHttpsUrl(webhookChannelConfig.getUrl());
}
}

private void validateHttpsUrl(String urlString) {
try {
URL url = new URL(urlString);
String protocol = url.getProtocol();
if (!protocol.equals("https")) {
throw Status.INVALID_ARGUMENT
.withDescription("URL configured in webhook is not https ")
.asRuntimeException();
}
} catch (MalformedURLException e) {
throw Status.INVALID_ARGUMENT
.withDescription("URL configured in webhook is malformed ")
.asRuntimeException();
}
}

void validateWebhookConfigExclusionDomains(
NotificationChannelMutableData notificationChannelMutableData,
Config notificationChannelConfig) {
if (notificationChannelConfig == null
|| notificationChannelMutableData.getWebhookChannelConfigList().isEmpty()) {
return;
}
if (notificationChannelConfig.hasPath(WEBHOOK_EXCLUSION_DOMAINS)) {
List<String> exclusionDomains =
notificationChannelConfig.getStringList(WEBHOOK_EXCLUSION_DOMAINS);
for (WebhookChannelConfig webhookChannelConfig :
notificationChannelMutableData.getWebhookChannelConfigList()) {
for (String exclusionDomain : exclusionDomains) {
if (webhookChannelConfig.getUrl().contains(exclusionDomain)) {
throw Status.INVALID_ARGUMENT
.withDescription("URL configured in webhook contains excluded domain")
.asRuntimeException();
}
}
}
}
}

public void validateUpdateNotificationChannelRequest(
RequestContext requestContext, UpdateNotificationChannelRequest request) {
RequestContext requestContext,
UpdateNotificationChannelRequest request,
Config notificationChannelConfig) {
validateRequestContextOrThrow(requestContext);
validateNonDefaultPresenceOrThrow(request, UpdateNotificationChannelRequest.ID_FIELD_NUMBER);
validateNotificationChannelMutableData(request.getNotificationChannelMutableData());
validateWebhookConfigExclusionDomains(
request.getNotificationChannelMutableData(), notificationChannelConfig);
}

private void validateNotificationChannelMutableData(NotificationChannelMutableData data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import com.google.protobuf.Value;
import com.google.protobuf.util.JsonFormat;
import com.typesafe.config.Config;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
Expand Down Expand Up @@ -38,7 +39,7 @@ void beforeEach() {
mockGenericConfigService
.addService(
new NotificationChannelConfigServiceImpl(
mockGenericConfigService.channel(), configChangeEventGenerator))
mockGenericConfigService.channel(), mock(Config.class), configChangeEventGenerator))
.start();

channelStub =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package org.hypertrace.notification.config.service;

import static org.hypertrace.notification.config.service.NotificationChannelConfigServiceImpl.NOTIFICATION_CHANNEL_CONFIG_SERVICE_CONFIG;
import static org.hypertrace.notification.config.service.NotificationChannelConfigServiceRequestValidator.WEBHOOK_HTTP_SUPPORT_ENABLED;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import com.typesafe.config.ConfigValueFactory;
import java.io.File;
import org.hypertrace.notification.config.service.v1.NotificationChannelMutableData;
import org.hypertrace.notification.config.service.v1.WebhookChannelConfig;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class NotificationChannelConfigServiceRequestValidatorTest {
@Test
public void testValidateWebhookExclusions() {
NotificationChannelConfigServiceRequestValidator
notificationChannelConfigServiceRequestValidator =
new NotificationChannelConfigServiceRequestValidator();
File configFile = new File(ClassLoader.getSystemResource("application.conf").getPath());
Config config = ConfigFactory.parseFile(configFile);
NotificationChannelMutableData notificationChannelMutableDataWithExcludedDomain =
getNotificationChannelMutableData("http://localhost:9000/test");
Assertions.assertThrows(
RuntimeException.class,
() -> {
notificationChannelConfigServiceRequestValidator.validateWebhookConfigExclusionDomains(
notificationChannelMutableDataWithExcludedDomain,
config.getConfig(NOTIFICATION_CHANNEL_CONFIG_SERVICE_CONFIG));
},
"RuntimeException was expected");
NotificationChannelMutableData notificationChannelMutableDataWithValidDomain =
getNotificationChannelMutableData("http://testHost:9000/test");
notificationChannelConfigServiceRequestValidator.validateWebhookConfigExclusionDomains(
notificationChannelMutableDataWithValidDomain,
config.getConfig(NOTIFICATION_CHANNEL_CONFIG_SERVICE_CONFIG));
}

@Test
public void testValidateWebhookHttpsSupport() {
NotificationChannelConfigServiceRequestValidator
notificationChannelConfigServiceRequestValidator =
new NotificationChannelConfigServiceRequestValidator();
File configFile = new File(ClassLoader.getSystemResource("application.conf").getPath());
Config config = ConfigFactory.parseFile(configFile);
Config notificationChannelConfig = config.getConfig(NOTIFICATION_CHANNEL_CONFIG_SERVICE_CONFIG);
NotificationChannelMutableData notificationChannelWithHttpUrl =
getNotificationChannelMutableData("http://localhost:9000/test");
// As http support disabled RuntimeException should be thrown.
Assertions.assertThrows(
RuntimeException.class,
() -> {
notificationChannelConfigServiceRequestValidator.validateWebhookHttpSupport(
notificationChannelWithHttpUrl, notificationChannelConfig);
},
"RuntimeException was expected");

// In valid URl not accepted
NotificationChannelMutableData notificationChannelWithInvalidUrl =
getNotificationChannelMutableData("localhost");
Assertions.assertThrows(
RuntimeException.class,
() -> {
notificationChannelConfigServiceRequestValidator.validateWebhookHttpSupport(
notificationChannelWithInvalidUrl, notificationChannelConfig);
},
"RuntimeException was expected");

// Valid webhook config with https url.
NotificationChannelMutableData notificationChannelMutableDataWithHttpsUrl =
getNotificationChannelMutableData("https://localhost:9000/test");
notificationChannelConfigServiceRequestValidator.validateWebhookHttpSupport(
notificationChannelMutableDataWithHttpsUrl, notificationChannelConfig);

// Update config with http support enabled and verify no exceptions for http url
Config updatedNotificationChannelConfig =
config.withValue(WEBHOOK_HTTP_SUPPORT_ENABLED, ConfigValueFactory.fromAnyRef("true"));

NotificationChannelMutableData notificationChannelMutableDataWithHttpUrl =
getNotificationChannelMutableData("http://localhost:9000/test");
notificationChannelConfigServiceRequestValidator.validateWebhookHttpSupport(
notificationChannelMutableDataWithHttpUrl, updatedNotificationChannelConfig);
}

private static NotificationChannelMutableData getNotificationChannelMutableData(String url) {
return NotificationChannelMutableData.newBuilder()
.setChannelName("testChannel")
.addWebhookChannelConfig(WebhookChannelConfig.newBuilder().setUrl(url))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
notification.channel.config.service {
webhook.exclusion.domains = ["localhost"]
webhook.http.support.enabled = false
}