|
3 | 3 | import static org.hypertrace.config.validation.GrpcValidatorUtils.validateNonDefaultPresenceOrThrow; |
4 | 4 | import static org.hypertrace.config.validation.GrpcValidatorUtils.validateRequestContextOrThrow; |
5 | 5 |
|
| 6 | +import com.typesafe.config.Config; |
6 | 7 | import io.grpc.Status; |
| 8 | +import java.net.MalformedURLException; |
| 9 | +import java.net.URL; |
| 10 | +import java.util.List; |
7 | 11 | import org.hypertrace.core.grpcutils.context.RequestContext; |
8 | 12 | import org.hypertrace.notification.config.service.v1.AwsS3BucketChannelConfig; |
9 | 13 | import org.hypertrace.notification.config.service.v1.AwsS3BucketChannelConfig.WebIdentityAuthenticationCredential; |
|
20 | 24 |
|
21 | 25 | public class NotificationChannelConfigServiceRequestValidator { |
22 | 26 |
|
| 27 | + public static final String WEBHOOK_EXCLUSION_DOMAINS = "webhook.exclusion.domains"; |
| 28 | + public static final String WEBHOOK_HTTP_SUPPORT_ENABLED = "webhook.http.support.enabled"; |
| 29 | + |
23 | 30 | public void validateCreateNotificationChannelRequest( |
24 | | - RequestContext requestContext, CreateNotificationChannelRequest request) { |
| 31 | + RequestContext requestContext, |
| 32 | + CreateNotificationChannelRequest request, |
| 33 | + Config notificationChannelConfig) { |
25 | 34 | validateRequestContextOrThrow(requestContext); |
26 | 35 | validateNotificationChannelMutableData(request.getNotificationChannelMutableData()); |
| 36 | + validateWebhookConfigExclusionDomains( |
| 37 | + request.getNotificationChannelMutableData(), notificationChannelConfig); |
| 38 | + validateWebhookHttpSupport( |
| 39 | + request.getNotificationChannelMutableData(), notificationChannelConfig); |
| 40 | + } |
| 41 | + |
| 42 | + void validateWebhookHttpSupport( |
| 43 | + NotificationChannelMutableData notificationChannelMutableData, |
| 44 | + Config notificationChannelConfig) { |
| 45 | + if (notificationChannelConfig == null |
| 46 | + || notificationChannelMutableData.getWebhookChannelConfigList().isEmpty()) { |
| 47 | + return; |
| 48 | + } |
| 49 | + for (WebhookChannelConfig webhookChannelConfig : |
| 50 | + notificationChannelMutableData.getWebhookChannelConfigList()) { |
| 51 | + if (notificationChannelConfig.hasPath(WEBHOOK_HTTP_SUPPORT_ENABLED) |
| 52 | + && notificationChannelConfig.getBoolean(WEBHOOK_HTTP_SUPPORT_ENABLED)) { |
| 53 | + continue; |
| 54 | + } |
| 55 | + validateHttpsUrl(webhookChannelConfig.getUrl()); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private void validateHttpsUrl(String urlString) { |
| 60 | + try { |
| 61 | + URL url = new URL(urlString); |
| 62 | + String protocol = url.getProtocol(); |
| 63 | + if (!protocol.equals("https")) { |
| 64 | + throw Status.INVALID_ARGUMENT |
| 65 | + .withDescription("URL configured in webhook is not https ") |
| 66 | + .asRuntimeException(); |
| 67 | + } |
| 68 | + } catch (MalformedURLException e) { |
| 69 | + throw Status.INVALID_ARGUMENT |
| 70 | + .withDescription("URL configured in webhook is malformed ") |
| 71 | + .asRuntimeException(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + void validateWebhookConfigExclusionDomains( |
| 76 | + NotificationChannelMutableData notificationChannelMutableData, |
| 77 | + Config notificationChannelConfig) { |
| 78 | + if (notificationChannelConfig == null |
| 79 | + || notificationChannelMutableData.getWebhookChannelConfigList().isEmpty()) { |
| 80 | + return; |
| 81 | + } |
| 82 | + if (notificationChannelConfig.hasPath(WEBHOOK_EXCLUSION_DOMAINS)) { |
| 83 | + List<String> exclusionDomains = |
| 84 | + notificationChannelConfig.getStringList(WEBHOOK_EXCLUSION_DOMAINS); |
| 85 | + for (WebhookChannelConfig webhookChannelConfig : |
| 86 | + notificationChannelMutableData.getWebhookChannelConfigList()) { |
| 87 | + for (String exclusionDomain : exclusionDomains) { |
| 88 | + if (webhookChannelConfig.getUrl().contains(exclusionDomain)) { |
| 89 | + throw Status.INVALID_ARGUMENT |
| 90 | + .withDescription("URL configured in webhook contains excluded domain") |
| 91 | + .asRuntimeException(); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
27 | 96 | } |
28 | 97 |
|
29 | 98 | public void validateUpdateNotificationChannelRequest( |
30 | | - RequestContext requestContext, UpdateNotificationChannelRequest request) { |
| 99 | + RequestContext requestContext, |
| 100 | + UpdateNotificationChannelRequest request, |
| 101 | + Config notificationChannelConfig) { |
31 | 102 | validateRequestContextOrThrow(requestContext); |
32 | 103 | validateNonDefaultPresenceOrThrow(request, UpdateNotificationChannelRequest.ID_FIELD_NUMBER); |
33 | 104 | validateNotificationChannelMutableData(request.getNotificationChannelMutableData()); |
| 105 | + validateWebhookConfigExclusionDomains( |
| 106 | + request.getNotificationChannelMutableData(), notificationChannelConfig); |
34 | 107 | } |
35 | 108 |
|
36 | 109 | private void validateNotificationChannelMutableData(NotificationChannelMutableData data) { |
|
0 commit comments