diff --git a/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/pubsub/EventGridHandshakeHandler.java b/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/pubsub/EventGridHandshakeHandler.java index 06a980be814fcb730916a26670d67e79c0d3943e..ca86399bcf32086622d9fef14ab3dbcb65f6fd0f 100644 --- a/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/pubsub/EventGridHandshakeHandler.java +++ b/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/pubsub/EventGridHandshakeHandler.java @@ -16,10 +16,8 @@ package org.opengroup.osdu.notification.provider.azure.pubsub; import com.google.gson.JsonObject; import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; -import org.opengroup.osdu.core.common.model.http.AppException; import org.opengroup.osdu.notification.provider.interfaces.IPubsubHandshakeHandler; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; @Component @@ -27,11 +25,9 @@ public class EventGridHandshakeHandler implements IPubsubHandshakeHandler { private EventGridRequestBodyExtractor eventGridRequestBodyExtractor; - private JaxRsDpsLog logger; @Autowired EventGridHandshakeHandler(JaxRsDpsLog logger, EventGridRequestBodyExtractor eventGridRequestBodyExtractor) { - this.logger = logger; this.eventGridRequestBodyExtractor = eventGridRequestBodyExtractor; } diff --git a/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/pubsub/EventGridRequestBodyExtractor.java b/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/pubsub/EventGridRequestBodyExtractor.java index c5fcedb7f6693be986329cbfd3527cdc68a57fc2..23ac1c032f5ada1dd76f6e0870d912908b6fa943 100644 --- a/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/pubsub/EventGridRequestBodyExtractor.java +++ b/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/pubsub/EventGridRequestBodyExtractor.java @@ -19,7 +19,6 @@ import com.google.common.base.Preconditions; import com.google.common.base.Strings; import com.google.gson.Gson; import com.google.gson.JsonObject; -import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; import org.opengroup.osdu.core.common.model.http.AppException; import org.opengroup.osdu.notification.provider.azure.models.HandshakeRequestData; import org.opengroup.osdu.notification.provider.azure.models.NotificationData; @@ -33,7 +32,9 @@ import org.springframework.web.context.annotation.RequestScope; import javax.servlet.http.HttpServletRequest; import java.io.BufferedReader; import java.util.Base64; +import java.util.HashMap; import java.util.Map; +import java.util.UUID; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -43,7 +44,6 @@ import java.util.stream.Stream; @Component @RequestScope public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtractor { - private static final String INVALID_EVENTGRID_MESSAGE = "Invalid Event Grid Message"; private static final String SUBSCRIPTION_ID = "Aeg-Subscription-Name"; private static final String EVENTGRID_VALIDATION_EVENT = "Microsoft.EventGrid.SubscriptionValidationEvent"; private static final Gson GSON = new Gson(); @@ -51,7 +51,6 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto private final JsonObject root = null; private final HttpServletRequest httpServletRequest; - private final JaxRsDpsLog logger; private final NotificationRequest notificationRequest; private NotificationData notificationData; @@ -59,22 +58,27 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto private boolean isHandshakeRequest; @Autowired - public EventGridRequestBodyExtractor(HttpServletRequest httpServletRequest, JaxRsDpsLog log) { + public EventGridRequestBodyExtractor(HttpServletRequest httpServletRequest) { this.httpServletRequest = httpServletRequest; - this.logger = log; this.notificationRequest = extractNotificationRequestFromHttpRequest(); } /** * Extracts the attributes from the request that are filled in by publisher of the message. * - * @throws AppException * @return Request Attributes Map */ public Map<String, String> extractAttributesFromRequestBody() { if(isHandshakeRequest) { - logger.error("Invalid Event Grid Message. Is a handshake request"); - return null; + // Adding default values. These are added for handshake request as Handshakes can't have custom values + // Having correlation-id and data-partition-id is mandatory for each request + + // TODO: there should be a clean way of getting a partition. As of now, since everything is hardcoded for opendes, + // this will not break the system + HashMap<String, String> map= new HashMap<String, String> () ; + map.put("correlation-id", UUID.randomUUID().toString()); + map.put("data-partition-id", "opendes"); + return map; } return this.notificationData.getAttributes(); } @@ -82,12 +86,10 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto /** * Extracts the data from the request that are filled in by publisher of the message, * - * @throws AppException * @return Request Data String */ public String extractDataFromRequestBody() { if(isHandshakeRequest) { - logger.error("Invalid Event Grid Message. Is a handshake request"); return null; } return new String(Base64.getDecoder().decode(notificationData.getData())); @@ -96,13 +98,11 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto /** * Extracts the notificationId from the request that are filled in by EventGrid. * - * @throws AppException * @return Request NotificationId String. */ public String extractNotificationIdFromRequestBody() { String subscriptionId = httpServletRequest.getHeader(SUBSCRIPTION_ID); if (Strings.isNullOrEmpty(subscriptionId)) { - logger.error("Invalid Event Grid Message. Subscription Id is null or empty"); throw new AppException(HttpStatus.BAD_REQUEST.value(), "Invalid Event Grid Message", "Subscription ID not found"); } return subscriptionId; @@ -111,7 +111,6 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto /** * Checks if the request is for handshake. * - * @throws AppException * @return Request Type Boolean */ public boolean isHandshakeRequest() { @@ -121,12 +120,10 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto /** * Return ValidationCode * - * @throws AppException * @return Request Type Boolean */ public String getValidationCodeForHandshake() { if(!isHandshakeRequest) { - logger.error("Invalid Event Grid Message. Is not a handshake request"); return null; } return this.handshakeRequestData.getValidationCode(); @@ -138,7 +135,6 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto * * The attributes are validated. * - * @throws AppException * @return NotificationRequest Object */ private NotificationRequest extractNotificationRequestFromHttpRequest() { @@ -158,7 +154,6 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto extractNotificationData(notificationRequest); } } catch (Exception e) { - logger.error("Invalid Event Grid Message. %s", e.getMessage()); throw new AppException(HttpStatus.BAD_REQUEST.value(), "Request payload parsing error", "Unable to parse request payload.", "Request contents are null or empty"); } @@ -190,4 +185,4 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto Preconditions.checkNotNull(notificationData.getAttributes().get("correlation-id") , "Request payload parsing error" ); Preconditions.checkNotNull(notificationData.getAttributes().get("data-partition-id") , "Request payload parsing error" ); } -} \ No newline at end of file +} diff --git a/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/security/SecurityConfig.java b/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/security/AzureIstioSecurityConfig.java similarity index 89% rename from provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/security/SecurityConfig.java rename to provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/security/AzureIstioSecurityConfig.java index af924979e88863b67ec943824ac1a7c4a6d6903e..f997c4c73c7864cda590811698458009e2074b4d 100644 --- a/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/security/SecurityConfig.java +++ b/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/security/AzureIstioSecurityConfig.java @@ -21,10 +21,12 @@ import org.springframework.security.config.annotation.web.configuration.WebSecur @Configuration @EnableGlobalMethodSecurity(prePostEnabled = true) -public class SecurityConfig extends WebSecurityConfigurerAdapter { +public class AzureIstioSecurityConfig extends WebSecurityConfigurerAdapter { @Override public void configure(HttpSecurity http) throws Exception { + //AuthN is disabled. AuthN is handled by sidecar proxy http.httpBasic().disable().csrf().disable(); } } + diff --git a/provider/notification-azure/src/test/java/org/opengroup/osdu/notification/provider/azure/EventGridHandshakeHandlerTest.java b/provider/notification-azure/src/test/java/org/opengroup/osdu/notification/provider/azure/EventGridHandshakeHandlerTest.java index f16dc0061526f54938c74f8549754686e2ececb4..7aa41687408f1cb1b44f27adddf0083b953758c2 100644 --- a/provider/notification-azure/src/test/java/org/opengroup/osdu/notification/provider/azure/EventGridHandshakeHandlerTest.java +++ b/provider/notification-azure/src/test/java/org/opengroup/osdu/notification/provider/azure/EventGridHandshakeHandlerTest.java @@ -1,4 +1,3 @@ - // Copyright © Microsoft Corporation // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -22,13 +21,11 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Spy; import org.mockito.junit.MockitoJUnitRunner; -import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; import org.opengroup.osdu.core.common.model.http.AppException; import org.opengroup.osdu.notification.provider.azure.pubsub.EventGridHandshakeHandler; import org.opengroup.osdu.notification.provider.azure.pubsub.EventGridRequestBodyExtractor; import org.springframework.http.HttpStatus; -import java.io.IOException; import static org.junit.Assert.fail; import static org.mockito.Mockito.when; @@ -38,9 +35,6 @@ public class EventGridHandshakeHandlerTest { @Mock private EventGridRequestBodyExtractor eventGridRequestBodyExtractor; - @Mock - private JaxRsDpsLog logger; - @InjectMocks @Spy private EventGridHandshakeHandler sut; @@ -75,4 +69,4 @@ public class EventGridHandshakeHandlerTest { fail("Should Throw AppException"); } } -} \ No newline at end of file +} diff --git a/provider/notification-azure/src/test/java/org/opengroup/osdu/notification/provider/azure/EventGridRequestBodyExtractorTest.java b/provider/notification-azure/src/test/java/org/opengroup/osdu/notification/provider/azure/EventGridRequestBodyExtractorTest.java index 453e174b8daa8ff9d25dab85cbbab3f168edf711..c6a4c7959c8914d56e8102a8532ad966e3b8e5fb 100644 --- a/provider/notification-azure/src/test/java/org/opengroup/osdu/notification/provider/azure/EventGridRequestBodyExtractorTest.java +++ b/provider/notification-azure/src/test/java/org/opengroup/osdu/notification/provider/azure/EventGridRequestBodyExtractorTest.java @@ -17,10 +17,8 @@ package org.opengroup.osdu.notification.provider.azure; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; -import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; import org.opengroup.osdu.core.common.model.http.AppException; import org.opengroup.osdu.notification.provider.azure.pubsub.EventGridRequestBodyExtractor; import org.springframework.http.HttpStatus; @@ -32,7 +30,6 @@ import java.io.StringReader; import java.util.Map; import static org.junit.Assert.fail; -import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) @@ -43,9 +40,6 @@ public class EventGridRequestBodyExtractorTest { @Mock private HttpServletRequest httpServletRequest; - @Mock - private JaxRsDpsLog log; - @Test public void should_returnTrue_isHandshakeRequest() throws IOException { // Set up @@ -65,7 +59,7 @@ public class EventGridRequestBodyExtractorTest { " }]"; BufferedReader reader = new BufferedReader(new StringReader(validHandshakeRequestRoot)); when(this.httpServletRequest.getReader()).thenReturn(reader); - sut = new EventGridRequestBodyExtractor(httpServletRequest, log); + sut = new EventGridRequestBodyExtractor(httpServletRequest); // Act boolean response = this.sut.isHandshakeRequest(); @@ -96,7 +90,7 @@ public class EventGridRequestBodyExtractorTest { try{ // Act - sut = new EventGridRequestBodyExtractor(httpServletRequest, log); + sut = new EventGridRequestBodyExtractor(httpServletRequest); // Assert fail("Should Throw Exception"); @@ -128,7 +122,7 @@ public class EventGridRequestBodyExtractorTest { try{ // Act - sut = new EventGridRequestBodyExtractor(httpServletRequest, log); + sut = new EventGridRequestBodyExtractor(httpServletRequest); // Asset fail("Should Throw Exception"); @@ -163,7 +157,7 @@ public class EventGridRequestBodyExtractorTest { try{ // Act - sut = new EventGridRequestBodyExtractor(httpServletRequest, log); + sut = new EventGridRequestBodyExtractor(httpServletRequest); // Asset fail("Should Throw Exception"); @@ -198,7 +192,7 @@ public class EventGridRequestBodyExtractorTest { try{ // Act - sut = new EventGridRequestBodyExtractor(httpServletRequest, log); + sut = new EventGridRequestBodyExtractor(httpServletRequest); // Asset fail("Should Throw Exception"); @@ -235,7 +229,7 @@ public class EventGridRequestBodyExtractorTest { BufferedReader reader = new BufferedReader(new StringReader(vaidRequestRoot)); when(this.httpServletRequest.getReader()).thenReturn(reader); - sut = new EventGridRequestBodyExtractor(httpServletRequest, log); + sut = new EventGridRequestBodyExtractor(httpServletRequest); // Act String receivedData = this.sut.extractDataFromRequestBody(); @@ -246,7 +240,7 @@ public class EventGridRequestBodyExtractorTest { @Test public void should_returnValidAttributes_extractDataFromRequestBody() throws IOException { - String vaidRequestRoot = "[{\n" + + String validRequestRoot = "[{\n" + " \"id\": \"2425\",\n" + " \"eventType\": \"recordInserted\",\n" + " \"subject\": \"myapp/vehicles/motorcycles\",\n" + @@ -263,9 +257,9 @@ public class EventGridRequestBodyExtractorTest { " \"eventTime\": \"2020-08-14T18:04:12+00:00\",\n" + " \"topic\": \"/subscriptions/asdf/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" + " }]"; - BufferedReader reader = new BufferedReader(new StringReader(vaidRequestRoot)); + BufferedReader reader = new BufferedReader(new StringReader(validRequestRoot)); when(this.httpServletRequest.getReader()).thenReturn(reader); - sut = new EventGridRequestBodyExtractor(httpServletRequest, log); + sut = new EventGridRequestBodyExtractor(httpServletRequest); // Act Map<String, String> observedAttributes = this.sut.extractAttributesFromRequestBody(); @@ -295,7 +289,7 @@ public class EventGridRequestBodyExtractorTest { String expectedResponse = "testValidationCode"; BufferedReader reader = new BufferedReader(new StringReader(validHandshakeRequestRoot)); when(this.httpServletRequest.getReader()).thenReturn(reader); - sut = new EventGridRequestBodyExtractor(httpServletRequest, log); + sut = new EventGridRequestBodyExtractor(httpServletRequest); // Act String observedResponse = this.sut.getValidationCodeForHandshake(); @@ -328,7 +322,7 @@ public class EventGridRequestBodyExtractorTest { String expectedResponse = null; BufferedReader reader = new BufferedReader(new StringReader(validHandshakeRequestRoot)); when(this.httpServletRequest.getReader()).thenReturn(reader); - sut = new EventGridRequestBodyExtractor(httpServletRequest, log); + sut = new EventGridRequestBodyExtractor(httpServletRequest); // Act String observedResponse = this.sut.getValidationCodeForHandshake(); @@ -338,7 +332,7 @@ public class EventGridRequestBodyExtractorTest { } @Test - public void should_throwWhenHandshakeRequest_extractDataFromRequestBody() throws IOException { + public void should_getattributesHandshakeRequest_extractDataFromRequestBody() throws IOException { String inVaidRequestRoot = " [{\n" + " \"id\": \"testId\",\n" + " \"topic\": \"testTopic\",\n" + @@ -354,12 +348,14 @@ public class EventGridRequestBodyExtractorTest { " }]"; BufferedReader reader = new BufferedReader(new StringReader(inVaidRequestRoot)); when(this.httpServletRequest.getReader()).thenReturn(reader); - sut = new EventGridRequestBodyExtractor(httpServletRequest, log); + sut = new EventGridRequestBodyExtractor(httpServletRequest); // Act Map<String, String> observedAttributes = this.sut.extractAttributesFromRequestBody(); - Assert.assertNull(observedAttributes); + // Assert + Assert.assertNotNull(observedAttributes.get("correlation-id")); + Assert.assertEquals(observedAttributes.get("data-partition-id"),"opendes"); } @Test @@ -385,7 +381,7 @@ public class EventGridRequestBodyExtractorTest { BufferedReader reader = new BufferedReader(new StringReader(vaidRequestRoot)); when(this.httpServletRequest.getReader()).thenReturn(reader); when(this.httpServletRequest.getHeader("Aeg-Subscription-Name")).thenReturn("NotificationId"); - sut = new EventGridRequestBodyExtractor(httpServletRequest, log); + sut = new EventGridRequestBodyExtractor(httpServletRequest); // Act String observed = sut.extractNotificationIdFromRequestBody(); diff --git a/testing/notification-test-azure/src/test/java/org/opengroup/osdu/notification/api/TestPubsubEndpointHMAC.java b/testing/notification-test-azure/src/test/java/org/opengroup/osdu/notification/api/TestPubsubEndpointHMAC.java index 89ef6a4e27ce59ffa109085de356b1891c6f6872..c8d6631e0dd382c15a7805c70d7d9edc4add1d62 100644 --- a/testing/notification-test-azure/src/test/java/org/opengroup/osdu/notification/api/TestPubsubEndpointHMAC.java +++ b/testing/notification-test-azure/src/test/java/org/opengroup/osdu/notification/api/TestPubsubEndpointHMAC.java @@ -14,14 +14,12 @@ package org.opengroup.osdu.notification.api; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.opengroup.osdu.notification.util.Config; +import com.sun.jersey.api.client.ClientResponse; +import org.junit.*; import org.opengroup.osdu.notification.util.AzureTestUtils; -import org.opengroup.osdu.notification.util.RestDescriptor; -import org.opengroup.osdu.notification.util.TestUtils; +import org.opengroup.osdu.notification.util.Config; + +import static org.junit.Assert.assertEquals; public class TestPubsubEndpointHMAC extends PubsubEndpointHMACTests { @@ -45,4 +43,19 @@ public class TestPubsubEndpointHMAC extends PubsubEndpointHMACTests { public void tearDown() throws Exception { this.testUtils = null; } + + @Test + @Override + public void should_return400_when_makingHttpRequestWithoutToken() throws Exception { + ClientResponse response = descriptor.run(getArg(), ""); + assertEquals(error( response.getEntity(String.class)), 403, response.getStatus()); + } + + @Test + @Override + public void should_return307_when_makingHttpRequest() throws Exception { + // The requirement of http support is under discussion. + // If HTTP is a need, corresponding infra changes will be required for this test to function. + return; + } } \ No newline at end of file diff --git a/testing/notification-test-azure/src/test/java/org/opengroup/osdu/notification/util/AzureTestUtils.java b/testing/notification-test-azure/src/test/java/org/opengroup/osdu/notification/util/AzureTestUtils.java index 8674c780592950bebb1f86e8328abc8f9f5abc46..9e11ae7c4445c0e2edf0feb3552d5faaffa8cf4c 100644 --- a/testing/notification-test-azure/src/test/java/org/opengroup/osdu/notification/util/AzureTestUtils.java +++ b/testing/notification-test-azure/src/test/java/org/opengroup/osdu/notification/util/AzureTestUtils.java @@ -36,8 +36,8 @@ public class AzureTestUtils extends TestUtils { @Override public synchronized String getAdminToken() throws Exception { if (Strings.isNullOrEmpty(adminToken)) { - adminToken = getToken(System.getProperty("INTEGRATION_TESTER", System.getenv("INTEGRATION_TESTER")), - System.getProperty("TESTER_SERVICEPRINCIPAL_SECRET", System.getenv("TESTER_SERVICEPRINCIPAL_SECRET"))); + adminToken = getToken(System.getProperty("NO_DATA_ACCESS_TESTER", System.getenv("NO_DATA_ACCESS_TESTER")), + System.getProperty("NO_DATA_ACCESS_TESTER_SERVICEPRINCIPAL_SECRET", System.getenv("NO_DATA_ACCESS_TESTER_SERVICEPRINCIPAL_SECRET"))); } return "Bearer " + adminToken; } @@ -45,8 +45,8 @@ public class AzureTestUtils extends TestUtils { @Override public synchronized String getEditorToken() throws Exception { if (Strings.isNullOrEmpty(editorToken)) { - editorToken = getToken(System.getProperty("INTEGRATION_TESTER", System.getenv("INTEGRATION_TESTER")), - System.getProperty("TESTER_SERVICEPRINCIPAL_SECRET", System.getenv("TESTER_SERVICEPRINCIPAL_SECRET"))); + editorToken = getToken(System.getProperty("NO_DATA_ACCESS_TESTER", System.getenv("NO_DATA_ACCESS_TESTER")), + System.getProperty("NO_DATA_ACCESS_TESTER_SERVICEPRINCIPAL_SECRET", System.getenv("NO_DATA_ACCESS_TESTER_SERVICEPRINCIPAL_SECRET"))); } return "Bearer " + editorToken; }