From ebbc0a1f53b593756faf1a0286e235cc295e4efc Mon Sep 17 00:00:00 2001 From: Neelesh Thakur Date: Thu, 13 Aug 2020 11:02:00 -0500 Subject: [PATCH 01/14] refactor --- .../auth/AuthorizationService.java | 23 ------------------- .../interfaces}/IAppProperties.java | 1 + .../interfaces}/IGoogleServiceAccount.java | 0 .../interfaces}/ServiceAccountValidator.java | 1 + 4 files changed, 2 insertions(+), 23 deletions(-) delete mode 100644 notification-core/src/main/java/org/opengroup/osdu/notification/auth/AuthorizationService.java rename notification-core/src/main/java/org/opengroup/osdu/notification/{utils => provider/interfaces}/IAppProperties.java (98%) rename notification-core/src/main/java/org/opengroup/osdu/notification/{utils => provider/interfaces}/IGoogleServiceAccount.java (100%) rename notification-core/src/main/java/org/opengroup/osdu/notification/{utils => provider/interfaces}/ServiceAccountValidator.java (99%) diff --git a/notification-core/src/main/java/org/opengroup/osdu/notification/auth/AuthorizationService.java b/notification-core/src/main/java/org/opengroup/osdu/notification/auth/AuthorizationService.java deleted file mode 100644 index 24b05695..00000000 --- a/notification-core/src/main/java/org/opengroup/osdu/notification/auth/AuthorizationService.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright 2017-2020, Schlumberger - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.opengroup.osdu.notification.auth; - -import org.opengroup.osdu.core.common.model.http.DpsHeaders; - -public interface AuthorizationService { - String authorizeAny(DpsHeaders headers, String... roles); -} diff --git a/notification-core/src/main/java/org/opengroup/osdu/notification/utils/IAppProperties.java b/notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/IAppProperties.java similarity index 98% rename from notification-core/src/main/java/org/opengroup/osdu/notification/utils/IAppProperties.java rename to notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/IAppProperties.java index 16049cc1..855d3512 100644 --- a/notification-core/src/main/java/org/opengroup/osdu/notification/utils/IAppProperties.java +++ b/notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/IAppProperties.java @@ -1,6 +1,7 @@ package org.opengroup.osdu.notification.utils; public interface IAppProperties { + String getAuthorizeAPI(); String getRegisterAPI(); diff --git a/notification-core/src/main/java/org/opengroup/osdu/notification/utils/IGoogleServiceAccount.java b/notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/IGoogleServiceAccount.java similarity index 100% rename from notification-core/src/main/java/org/opengroup/osdu/notification/utils/IGoogleServiceAccount.java rename to notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/IGoogleServiceAccount.java diff --git a/notification-core/src/main/java/org/opengroup/osdu/notification/utils/ServiceAccountValidator.java b/notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/ServiceAccountValidator.java similarity index 99% rename from notification-core/src/main/java/org/opengroup/osdu/notification/utils/ServiceAccountValidator.java rename to notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/ServiceAccountValidator.java index 1491eba4..f733ea72 100644 --- a/notification-core/src/main/java/org/opengroup/osdu/notification/utils/ServiceAccountValidator.java +++ b/notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/ServiceAccountValidator.java @@ -17,6 +17,7 @@ package org.opengroup.osdu.notification.utils; public interface ServiceAccountValidator { + boolean isValidPublisherServiceAccount(String jwt); boolean isValidServiceAccount(String jwt, String userIdentity, String... audiences); -- GitLab From 698a128adfdc29e91ea0a289e169e1f4b6c3e2e7 Mon Sep 17 00:00:00 2001 From: Neelesh Thakur Date: Thu, 13 Aug 2020 11:03:01 -0500 Subject: [PATCH 02/14] refactor --- .../opengroup/osdu/notification/api/PubsubEndpoint.java | 2 +- .../osdu/notification/auth/AuthorizationFilter.java | 2 +- .../osdu/notification/auth/EntitlementsClientFactory.java | 2 +- .../osdu/notification/di/CredentialHeadersProvider.java | 1 - .../osdu/notification/di/SubscriptionClientFactory.java | 8 ++------ .../notification/provider/interfaces/IAppProperties.java | 2 +- .../provider/interfaces/IGoogleServiceAccount.java | 2 +- .../provider/interfaces/ServiceAccountValidator.java | 4 ++-- .../osdu/notification/api/PubsubEndpointTests.java | 2 +- .../osdu/notification/auth/AuthorizationFilterTest.java | 2 +- .../opengroup/osdu/notification/di/RequestInfoTest.java | 2 +- .../notification/provider/gcp/util/AppProperties.java | 2 +- .../provider/gcp/util/GoogleServiceAccountImpl.java | 2 +- .../gcp/util/GoogleServiceAccountValidatorImpl.java | 2 +- 14 files changed, 15 insertions(+), 20 deletions(-) diff --git a/notification-core/src/main/java/org/opengroup/osdu/notification/api/PubsubEndpoint.java b/notification-core/src/main/java/org/opengroup/osdu/notification/api/PubsubEndpoint.java index 975d6695..89a3376a 100644 --- a/notification-core/src/main/java/org/opengroup/osdu/notification/api/PubsubEndpoint.java +++ b/notification-core/src/main/java/org/opengroup/osdu/notification/api/PubsubEndpoint.java @@ -36,7 +36,7 @@ import org.opengroup.osdu.core.common.notification.SubscriptionException; import org.opengroup.osdu.notification.di.SubscriptionCacheFactory; import org.opengroup.osdu.notification.pubsub.PubsubRequestBodyExtractor; import org.opengroup.osdu.notification.utils.Config; -import org.opengroup.osdu.notification.utils.IGoogleServiceAccount; +import org.opengroup.osdu.notification.provider.interfaces.IGoogleServiceAccount; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; diff --git a/notification-core/src/main/java/org/opengroup/osdu/notification/auth/AuthorizationFilter.java b/notification-core/src/main/java/org/opengroup/osdu/notification/auth/AuthorizationFilter.java index c720b2d5..e646b877 100644 --- a/notification-core/src/main/java/org/opengroup/osdu/notification/auth/AuthorizationFilter.java +++ b/notification-core/src/main/java/org/opengroup/osdu/notification/auth/AuthorizationFilter.java @@ -23,7 +23,7 @@ import org.opengroup.osdu.core.common.model.http.DpsHeaders; import org.opengroup.osdu.core.common.provider.interfaces.IAuthorizationService; import org.opengroup.osdu.notification.di.RequestInfoExt; import org.opengroup.osdu.notification.utils.Config; -import org.opengroup.osdu.notification.utils.ServiceAccountValidator; +import org.opengroup.osdu.notification.provider.interfaces.ServiceAccountValidator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.context.annotation.RequestScope; diff --git a/notification-core/src/main/java/org/opengroup/osdu/notification/auth/EntitlementsClientFactory.java b/notification-core/src/main/java/org/opengroup/osdu/notification/auth/EntitlementsClientFactory.java index efd7a908..53a5427d 100644 --- a/notification-core/src/main/java/org/opengroup/osdu/notification/auth/EntitlementsClientFactory.java +++ b/notification-core/src/main/java/org/opengroup/osdu/notification/auth/EntitlementsClientFactory.java @@ -19,7 +19,7 @@ package org.opengroup.osdu.notification.auth; import org.opengroup.osdu.core.common.entitlements.EntitlementsAPIConfig; import org.opengroup.osdu.core.common.entitlements.EntitlementsFactory; import org.opengroup.osdu.core.common.entitlements.IEntitlementsFactory; -import org.opengroup.osdu.notification.utils.IAppProperties; +import org.opengroup.osdu.notification.provider.interfaces.IAppProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.AbstractFactoryBean; import org.springframework.stereotype.Component; diff --git a/notification-core/src/main/java/org/opengroup/osdu/notification/di/CredentialHeadersProvider.java b/notification-core/src/main/java/org/opengroup/osdu/notification/di/CredentialHeadersProvider.java index f27e7815..24b6646c 100644 --- a/notification-core/src/main/java/org/opengroup/osdu/notification/di/CredentialHeadersProvider.java +++ b/notification-core/src/main/java/org/opengroup/osdu/notification/di/CredentialHeadersProvider.java @@ -18,7 +18,6 @@ package org.opengroup.osdu.notification.di; import org.opengroup.osdu.core.common.model.http.DpsHeaders; import org.opengroup.osdu.core.common.util.IServiceAccountJwtClient; import org.opengroup.osdu.notification.pubsub.PubsubRequestBodyExtractor; -import org.opengroup.osdu.notification.utils.IAppProperties; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Primary; diff --git a/notification-core/src/main/java/org/opengroup/osdu/notification/di/SubscriptionClientFactory.java b/notification-core/src/main/java/org/opengroup/osdu/notification/di/SubscriptionClientFactory.java index b367f7fc..feda29e9 100644 --- a/notification-core/src/main/java/org/opengroup/osdu/notification/di/SubscriptionClientFactory.java +++ b/notification-core/src/main/java/org/opengroup/osdu/notification/di/SubscriptionClientFactory.java @@ -16,17 +16,13 @@ package org.opengroup.osdu.notification.di; +import org.opengroup.osdu.core.common.notification.ISubscriptionFactory; import org.opengroup.osdu.core.common.notification.SubscriptionAPIConfig; import org.opengroup.osdu.core.common.notification.SubscriptionFactory; -import org.opengroup.osdu.notification.utils.IAppProperties; +import org.opengroup.osdu.notification.provider.interfaces.IAppProperties; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.config.AbstractFactoryBean; -import org.springframework.beans.factory.config.ConfigurableBeanFactory; -import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; -import org.opengroup.osdu.core.common.notification.ISubscriptionFactory; -import org.opengroup.osdu.core.common.model.notification.*; @Component public class SubscriptionClientFactory extends AbstractFactoryBean { diff --git a/notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/IAppProperties.java b/notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/IAppProperties.java index 855d3512..188ca8c2 100644 --- a/notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/IAppProperties.java +++ b/notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/IAppProperties.java @@ -1,4 +1,4 @@ -package org.opengroup.osdu.notification.utils; +package org.opengroup.osdu.notification.provider.interfaces; public interface IAppProperties { diff --git a/notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/IGoogleServiceAccount.java b/notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/IGoogleServiceAccount.java index ae067a8a..33628ba3 100644 --- a/notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/IGoogleServiceAccount.java +++ b/notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/IGoogleServiceAccount.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.opengroup.osdu.notification.utils; +package org.opengroup.osdu.notification.provider.interfaces; public interface IGoogleServiceAccount { diff --git a/notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/ServiceAccountValidator.java b/notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/ServiceAccountValidator.java index f733ea72..fc364898 100644 --- a/notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/ServiceAccountValidator.java +++ b/notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/ServiceAccountValidator.java @@ -14,10 +14,10 @@ * limitations under the License. */ -package org.opengroup.osdu.notification.utils; +package org.opengroup.osdu.notification.provider.interfaces; public interface ServiceAccountValidator { - + boolean isValidPublisherServiceAccount(String jwt); boolean isValidServiceAccount(String jwt, String userIdentity, String... audiences); diff --git a/notification-core/src/test/java/org/opengroup/osdu/notification/api/PubsubEndpointTests.java b/notification-core/src/test/java/org/opengroup/osdu/notification/api/PubsubEndpointTests.java index 67b9a4a8..d3155489 100644 --- a/notification-core/src/test/java/org/opengroup/osdu/notification/api/PubsubEndpointTests.java +++ b/notification-core/src/test/java/org/opengroup/osdu/notification/api/PubsubEndpointTests.java @@ -37,7 +37,7 @@ import org.opengroup.osdu.core.common.notification.SubscriptionService; import org.opengroup.osdu.notification.di.CredentialHeadersProvider; import org.opengroup.osdu.notification.di.SubscriptionCacheFactory; import org.opengroup.osdu.notification.pubsub.PubsubRequestBodyExtractor; -import org.opengroup.osdu.notification.utils.IGoogleServiceAccount; +import org.opengroup.osdu.notification.provider.interfaces.IGoogleServiceAccount; import org.powermock.modules.junit4.PowerMockRunner; import org.springframework.http.ResponseEntity; diff --git a/notification-core/src/test/java/org/opengroup/osdu/notification/auth/AuthorizationFilterTest.java b/notification-core/src/test/java/org/opengroup/osdu/notification/auth/AuthorizationFilterTest.java index c73e87d1..6becc37e 100644 --- a/notification-core/src/test/java/org/opengroup/osdu/notification/auth/AuthorizationFilterTest.java +++ b/notification-core/src/test/java/org/opengroup/osdu/notification/auth/AuthorizationFilterTest.java @@ -28,7 +28,7 @@ import org.opengroup.osdu.core.common.provider.interfaces.IAuthorizationService; import org.opengroup.osdu.notification.di.RequestInfoExt; import org.opengroup.osdu.notification.pubsub.PubsubRequestBodyExtractor; import org.opengroup.osdu.notification.utils.Config; -import org.opengroup.osdu.notification.utils.ServiceAccountValidator; +import org.opengroup.osdu.notification.provider.interfaces.ServiceAccountValidator; import org.powermock.modules.junit4.PowerMockRunner; import java.util.HashMap; diff --git a/notification-core/src/test/java/org/opengroup/osdu/notification/di/RequestInfoTest.java b/notification-core/src/test/java/org/opengroup/osdu/notification/di/RequestInfoTest.java index 2518ce8e..a0a7d481 100644 --- a/notification-core/src/test/java/org/opengroup/osdu/notification/di/RequestInfoTest.java +++ b/notification-core/src/test/java/org/opengroup/osdu/notification/di/RequestInfoTest.java @@ -20,7 +20,7 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; -import org.opengroup.osdu.notification.utils.IAppProperties; +import org.opengroup.osdu.notification.provider.interfaces.IAppProperties; import org.powermock.modules.junit4.PowerMockRunner; import javax.servlet.http.HttpServletRequest; diff --git a/provider/notification-gcp/src/main/java/org/opengroup/osdu/notification/provider/gcp/util/AppProperties.java b/provider/notification-gcp/src/main/java/org/opengroup/osdu/notification/provider/gcp/util/AppProperties.java index 19789adf..1aa85c59 100644 --- a/provider/notification-gcp/src/main/java/org/opengroup/osdu/notification/provider/gcp/util/AppProperties.java +++ b/provider/notification-gcp/src/main/java/org/opengroup/osdu/notification/provider/gcp/util/AppProperties.java @@ -16,7 +16,7 @@ package org.opengroup.osdu.notification.provider.gcp.util; -import org.opengroup.osdu.notification.utils.IAppProperties; +import org.opengroup.osdu.notification.provider.interfaces.IAppProperties; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; diff --git a/provider/notification-gcp/src/main/java/org/opengroup/osdu/notification/provider/gcp/util/GoogleServiceAccountImpl.java b/provider/notification-gcp/src/main/java/org/opengroup/osdu/notification/provider/gcp/util/GoogleServiceAccountImpl.java index 7dcf3ba6..1555ab18 100644 --- a/provider/notification-gcp/src/main/java/org/opengroup/osdu/notification/provider/gcp/util/GoogleServiceAccountImpl.java +++ b/provider/notification-gcp/src/main/java/org/opengroup/osdu/notification/provider/gcp/util/GoogleServiceAccountImpl.java @@ -19,7 +19,7 @@ package org.opengroup.osdu.notification.provider.gcp.util; import lombok.SneakyThrows; import org.apache.http.impl.client.CloseableHttpClient; import org.opengroup.osdu.core.gcp.GoogleIdToken.IGoogleIdTokenFactory; -import org.opengroup.osdu.notification.utils.IGoogleServiceAccount; +import org.opengroup.osdu.notification.provider.interfaces.IGoogleServiceAccount; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; diff --git a/provider/notification-gcp/src/main/java/org/opengroup/osdu/notification/provider/gcp/util/GoogleServiceAccountValidatorImpl.java b/provider/notification-gcp/src/main/java/org/opengroup/osdu/notification/provider/gcp/util/GoogleServiceAccountValidatorImpl.java index 2d70b3d3..eaac68d6 100644 --- a/provider/notification-gcp/src/main/java/org/opengroup/osdu/notification/provider/gcp/util/GoogleServiceAccountValidatorImpl.java +++ b/provider/notification-gcp/src/main/java/org/opengroup/osdu/notification/provider/gcp/util/GoogleServiceAccountValidatorImpl.java @@ -21,7 +21,7 @@ import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.jackson2.JacksonFactory; import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; -import org.opengroup.osdu.notification.utils.ServiceAccountValidator; +import org.opengroup.osdu.notification.provider.interfaces.ServiceAccountValidator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -- GitLab From 1596ddcd8759a630d37b55ff4d0191ff97fb99ca Mon Sep 17 00:00:00 2001 From: Neelesh Thakur Date: Thu, 13 Aug 2020 11:09:44 -0500 Subject: [PATCH 03/14] add MR template --- .gitlab/merge_request_templates/default.md | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 .gitlab/merge_request_templates/default.md diff --git a/.gitlab/merge_request_templates/default.md b/.gitlab/merge_request_templates/default.md new file mode 100644 index 00000000..30b57282 --- /dev/null +++ b/.gitlab/merge_request_templates/default.md @@ -0,0 +1,25 @@ +## Type of change +- [ ] Bug Fix +- [ ] Feature + +**Please provide link to gitlab issue or ADR(Architecture Decision Record)** + +## Does this introduce a change in the core logic? +- [YES/NO] + +## Does this introduce a change in the cloud provider implementation, if so which cloud? +- [ ] AWS +- [ ] Azure +- [ ] GCP +- [ ] IBM + +## Does this introduce a breaking change? +- [YES/NO] + +## What is the current behavior? + +## What is the new/expected behavior? + +## Have you added/updated Unit Tests and Integration Tests? + +## Any other useful information -- GitLab From 604601878710341399d655ad1212392121c98994 Mon Sep 17 00:00:00 2001 From: Neelesh Thakur Date: Thu, 13 Aug 2020 13:56:42 -0500 Subject: [PATCH 04/14] comments --- .../notification/auth/AuthorizationFilter.java | 4 ++-- .../provider/interfaces/IAppProperties.java | 16 ++++++++++++++++ ...idator.java => IServiceAccountValidator.java} | 2 +- .../auth/AuthorizationFilterTest.java | 4 ++-- .../util/GoogleServiceAccountValidatorImpl.java | 4 ++-- 5 files changed, 23 insertions(+), 7 deletions(-) rename notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/{ServiceAccountValidator.java => IServiceAccountValidator.java} (94%) diff --git a/notification-core/src/main/java/org/opengroup/osdu/notification/auth/AuthorizationFilter.java b/notification-core/src/main/java/org/opengroup/osdu/notification/auth/AuthorizationFilter.java index e646b877..36bfc803 100644 --- a/notification-core/src/main/java/org/opengroup/osdu/notification/auth/AuthorizationFilter.java +++ b/notification-core/src/main/java/org/opengroup/osdu/notification/auth/AuthorizationFilter.java @@ -23,7 +23,7 @@ import org.opengroup.osdu.core.common.model.http.DpsHeaders; import org.opengroup.osdu.core.common.provider.interfaces.IAuthorizationService; import org.opengroup.osdu.notification.di.RequestInfoExt; import org.opengroup.osdu.notification.utils.Config; -import org.opengroup.osdu.notification.provider.interfaces.ServiceAccountValidator; +import org.opengroup.osdu.notification.provider.interfaces.IServiceAccountValidator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.context.annotation.RequestScope; @@ -40,7 +40,7 @@ public class AuthorizationFilter { @Autowired private RequestInfoExt requestInfoExt; @Autowired - private ServiceAccountValidator validator; + private IServiceAccountValidator validator; public boolean hasAnyPermission(String... requiredRoles) { DpsHeaders dpsHeaders = requestInfoExt.getHeaders(); diff --git a/notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/IAppProperties.java b/notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/IAppProperties.java index 188ca8c2..c5bf0112 100644 --- a/notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/IAppProperties.java +++ b/notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/IAppProperties.java @@ -1,3 +1,19 @@ +/* + * Copyright 2017-2020, Schlumberger + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.opengroup.osdu.notification.provider.interfaces; public interface IAppProperties { diff --git a/notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/ServiceAccountValidator.java b/notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/IServiceAccountValidator.java similarity index 94% rename from notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/ServiceAccountValidator.java rename to notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/IServiceAccountValidator.java index fc364898..3b36bb1d 100644 --- a/notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/ServiceAccountValidator.java +++ b/notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/IServiceAccountValidator.java @@ -16,7 +16,7 @@ package org.opengroup.osdu.notification.provider.interfaces; -public interface ServiceAccountValidator { +public interface IServiceAccountValidator { boolean isValidPublisherServiceAccount(String jwt); diff --git a/notification-core/src/test/java/org/opengroup/osdu/notification/auth/AuthorizationFilterTest.java b/notification-core/src/test/java/org/opengroup/osdu/notification/auth/AuthorizationFilterTest.java index 6becc37e..50a46790 100644 --- a/notification-core/src/test/java/org/opengroup/osdu/notification/auth/AuthorizationFilterTest.java +++ b/notification-core/src/test/java/org/opengroup/osdu/notification/auth/AuthorizationFilterTest.java @@ -28,7 +28,7 @@ import org.opengroup.osdu.core.common.provider.interfaces.IAuthorizationService; import org.opengroup.osdu.notification.di.RequestInfoExt; import org.opengroup.osdu.notification.pubsub.PubsubRequestBodyExtractor; import org.opengroup.osdu.notification.utils.Config; -import org.opengroup.osdu.notification.provider.interfaces.ServiceAccountValidator; +import org.opengroup.osdu.notification.provider.interfaces.IServiceAccountValidator; import org.powermock.modules.junit4.PowerMockRunner; import java.util.HashMap; @@ -57,7 +57,7 @@ public class AuthorizationFilterTest { @Mock private IAuthorizationService authorizationService; @Mock - private ServiceAccountValidator validator; + private IServiceAccountValidator validator; @Mock private PubsubRequestBodyExtractor extractor; @InjectMocks diff --git a/provider/notification-gcp/src/main/java/org/opengroup/osdu/notification/provider/gcp/util/GoogleServiceAccountValidatorImpl.java b/provider/notification-gcp/src/main/java/org/opengroup/osdu/notification/provider/gcp/util/GoogleServiceAccountValidatorImpl.java index eaac68d6..3a1736a9 100644 --- a/provider/notification-gcp/src/main/java/org/opengroup/osdu/notification/provider/gcp/util/GoogleServiceAccountValidatorImpl.java +++ b/provider/notification-gcp/src/main/java/org/opengroup/osdu/notification/provider/gcp/util/GoogleServiceAccountValidatorImpl.java @@ -21,12 +21,12 @@ import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.jackson2.JacksonFactory; import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; -import org.opengroup.osdu.notification.provider.interfaces.ServiceAccountValidator; +import org.opengroup.osdu.notification.provider.interfaces.IServiceAccountValidator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service -public class GoogleServiceAccountValidatorImpl implements ServiceAccountValidator { +public class GoogleServiceAccountValidatorImpl implements IServiceAccountValidator { private final NetHttpTransport netHttpTransport = new NetHttpTransport(); private final JacksonFactory jacksonFactory = new JacksonFactory(); -- GitLab From cb2e2f9d9ab4277d692d17312923f5e1d92e84eb Mon Sep 17 00:00:00 2001 From: Neelesh Thakur Date: Thu, 20 Aug 2020 18:18:55 -0500 Subject: [PATCH 05/14] merge from master --- .../osdu/notification/provider/azure/util/AppProperties.java | 2 +- .../provider/azure/util/AzureServiceAccountValidatorImpl.java | 4 ++-- .../provider/azure/util/GoogleServiceAccountImpl.java | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/AppProperties.java b/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/AppProperties.java index 10bac0d1..8d751e08 100644 --- a/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/AppProperties.java +++ b/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/AppProperties.java @@ -19,7 +19,7 @@ import com.azure.cosmos.CosmosClient; import com.azure.security.keyvault.secrets.SecretClient; import com.azure.security.keyvault.secrets.models.KeyVaultSecret; import org.opengroup.osdu.azure.KeyVaultFacade; -import org.opengroup.osdu.notification.utils.IAppProperties; +import org.opengroup.osdu.notification.provider.interfaces.IAppProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; diff --git a/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/AzureServiceAccountValidatorImpl.java b/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/AzureServiceAccountValidatorImpl.java index 7f153b36..971666dd 100644 --- a/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/AzureServiceAccountValidatorImpl.java +++ b/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/AzureServiceAccountValidatorImpl.java @@ -14,11 +14,11 @@ package org.opengroup.osdu.notification.provider.azure.util; -import org.opengroup.osdu.notification.utils.ServiceAccountValidator; +import org.opengroup.osdu.notification.provider.interfaces.IServiceAccountValidator; import org.springframework.stereotype.Component; @Component -public class AzureServiceAccountValidatorImpl implements ServiceAccountValidator { +public class AzureServiceAccountValidatorImpl implements IServiceAccountValidator { @Override public boolean isValidPublisherServiceAccount(String jwt) { diff --git a/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/GoogleServiceAccountImpl.java b/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/GoogleServiceAccountImpl.java index ed463192..9b0adf6c 100644 --- a/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/GoogleServiceAccountImpl.java +++ b/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/GoogleServiceAccountImpl.java @@ -15,7 +15,7 @@ package org.opengroup.osdu.notification.provider.azure.util; import lombok.SneakyThrows; -import org.opengroup.osdu.notification.utils.IGoogleServiceAccount; +import org.opengroup.osdu.notification.provider.interfaces.IGoogleServiceAccount; import org.springframework.stereotype.Component; import javax.naming.AuthenticationNotSupportedException; -- GitLab From 8b5625c51c16975e88356c9aff0382b94c8f04f1 Mon Sep 17 00:00:00 2001 From: komakkar Date: Thu, 3 Sep 2020 04:17:39 +0530 Subject: [PATCH 06/14] cleaning stray changes --- .../api/PubsubEndpointHMACDescriptor.java | 28 ++++++------ .../api/TestPubsubEndpointHMAC.java | 14 ++++++ .../notification/util/AzureTestUtils.java | 43 +++++++++++-------- .../osdu/notification/util/Config.java | 3 +- 4 files changed, 53 insertions(+), 35 deletions(-) diff --git a/testing/notification-test-azure/src/test/java/org/opengroup/osdu/notification/api/PubsubEndpointHMACDescriptor.java b/testing/notification-test-azure/src/test/java/org/opengroup/osdu/notification/api/PubsubEndpointHMACDescriptor.java index 2e8d73c9..55c7ba57 100644 --- a/testing/notification-test-azure/src/test/java/org/opengroup/osdu/notification/api/PubsubEndpointHMACDescriptor.java +++ b/testing/notification-test-azure/src/test/java/org/opengroup/osdu/notification/api/PubsubEndpointHMACDescriptor.java @@ -1,18 +1,16 @@ -/* - * Copyright 2017-2020, Schlumberger - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright © Microsoft Corporation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package org.opengroup.osdu.notification.api; 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 a18328c3..89ef6a4e 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 @@ -1,3 +1,17 @@ +// Copyright © Microsoft Corporation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package org.opengroup.osdu.notification.api; import org.junit.After; 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 93bedd57..8674c780 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 @@ -1,3 +1,17 @@ +// Copyright © Microsoft Corporation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package org.opengroup.osdu.notification.util; import com.google.common.base.Strings; @@ -13,51 +27,42 @@ public class AzureTestUtils extends TestUtils { @Override public synchronized String getOpsToken() throws Exception { if (Strings.isNullOrEmpty(opsToken)) { - noAccessToken = getToken("c4167fb6-c724-40e8-be11-2e20d1b7a92c",//System.getProperty("NO_DATA_ACCESS_TESTER", System.getenv("NO_DATA_ACCESS_TESTER")), - "muwCHobjuKlVzQwdUgMcta4w7mfQzAiW");//System.getProperty("NO_DATA_ACCESS_TESTER_SERVICEPRINCIPAL_SECRET", System.getenv("NO_DATA_ACCESS_TESTER_SERVICEPRINCIPAL_SECRET"))); - - /*opsToken = getToken(System.getProperty("INTEGRATION_TESTER", System.getenv("INTEGRATION_TESTER")), + opsToken = getToken(System.getProperty("INTEGRATION_TESTER", System.getenv("INTEGRATION_TESTER")), System.getProperty("TESTER_SERVICEPRINCIPAL_SECRET", System.getenv("TESTER_SERVICEPRINCIPAL_SECRET"))); - */} + } return "Bearer " + opsToken; } @Override public synchronized String getAdminToken() throws Exception { if (Strings.isNullOrEmpty(adminToken)) { - noAccessToken = getToken("c4167fb6-c724-40e8-be11-2e20d1b7a92c",//System.getProperty("NO_DATA_ACCESS_TESTER", System.getenv("NO_DATA_ACCESS_TESTER")), - "muwCHobjuKlVzQwdUgMcta4w7mfQzAiW");//System.getProperty("NO_DATA_ACCESS_TESTER_SERVICEPRINCIPAL_SECRET", System.getenv("NO_DATA_ACCESS_TESTER_SERVICEPRINCIPAL_SECRET"))); - - /*adminToken = getToken(System.getProperty("INTEGRATION_TESTER", System.getenv("INTEGRATION_TESTER")), + adminToken = getToken(System.getProperty("INTEGRATION_TESTER", System.getenv("INTEGRATION_TESTER")), System.getProperty("TESTER_SERVICEPRINCIPAL_SECRET", System.getenv("TESTER_SERVICEPRINCIPAL_SECRET"))); - */} + } return "Bearer " + adminToken; } @Override public synchronized String getEditorToken() throws Exception { if (Strings.isNullOrEmpty(editorToken)) { - noAccessToken = getToken("c4167fb6-c724-40e8-be11-2e20d1b7a92c",//System.getProperty("NO_DATA_ACCESS_TESTER", System.getenv("NO_DATA_ACCESS_TESTER")), - "muwCHobjuKlVzQwdUgMcta4w7mfQzAiW");//System.getProperty("NO_DATA_ACCESS_TESTER_SERVICEPRINCIPAL_SECRET", System.getenv("NO_DATA_ACCESS_TESTER_SERVICEPRINCIPAL_SECRET"))); - - /*editorToken = getToken(System.getProperty("INTEGRATION_TESTER", System.getenv("INTEGRATION_TESTER")), + editorToken = getToken(System.getProperty("INTEGRATION_TESTER", System.getenv("INTEGRATION_TESTER")), System.getProperty("TESTER_SERVICEPRINCIPAL_SECRET", System.getenv("TESTER_SERVICEPRINCIPAL_SECRET"))); - */} + } return "Bearer " + editorToken; } @Override public synchronized String getNoAccessToken() throws Exception { if (Strings.isNullOrEmpty(noAccessToken)) { - noAccessToken = getToken("c4167fb6-c724-40e8-be11-2e20d1b7a92c",//System.getProperty("NO_DATA_ACCESS_TESTER", System.getenv("NO_DATA_ACCESS_TESTER")), - "muwCHobjuKlVzQwdUgMcta4w7mfQzAiW");//System.getProperty("NO_DATA_ACCESS_TESTER_SERVICEPRINCIPAL_SECRET", System.getenv("NO_DATA_ACCESS_TESTER_SERVICEPRINCIPAL_SECRET"))); + noAccessToken = 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 " + noAccessToken; } private String getToken(String sp_id, String sp_secret) throws Exception { - String tenant_id = "58975fd3-4977-44d0-bea8-37af0baac100";//System.getProperty("AZURE_AD_TENANT_ID", System.getenv("AZURE_AD_TENANT_ID")); - String app_resource_id = "dbf53e4d-f088-4379-87c3-d9b1496e77f8";//System.getProperty("AZURE_AD_APP_RESOURCE_ID", System.getenv("AZURE_AD_APP_RESOURCE_ID")); + String tenant_id = System.getProperty("AZURE_AD_TENANT_ID", System.getenv("AZURE_AD_TENANT_ID")); + String app_resource_id = System.getProperty("AZURE_AD_APP_RESOURCE_ID", System.getenv("AZURE_AD_APP_RESOURCE_ID")); return new AzureServicePrincipal().getIdToken(sp_id, sp_secret, tenant_id, app_resource_id); } diff --git a/testing/notification-test-core/src/main/java/org/opengroup/osdu/notification/util/Config.java b/testing/notification-test-core/src/main/java/org/opengroup/osdu/notification/util/Config.java index 81dc130b..7812c54c 100644 --- a/testing/notification-test-core/src/main/java/org/opengroup/osdu/notification/util/Config.java +++ b/testing/notification-test-core/src/main/java/org/opengroup/osdu/notification/util/Config.java @@ -37,13 +37,14 @@ public class Config { config.IntegrationAudience = "245464679631-ktfdfpl147m1mjpbutl00b3cmffissgq.apps.googleusercontent.com"; config.OsduTenant = "opendes"; config.Topic = "records-changed"; + config.hmacSecretValue = System.getProperty("HMAC_SECRET", System.getenv("HMAC_SECRET")); if (env.equalsIgnoreCase("LOCAL")) { //make sure to run register service on a different port. You can also choose to point to Register service that is running in cloud String registerUrl = "http://localhost:8081/api/register/v1"; //must have notification and register services running on different ports - config.HostUrl = "http://localhost:8082/"; + config.HostUrl = "http://localhost:8080/"; config.GSAPushUrl = registerUrl+"/test/gsa-challenge/"; config.HMACPushUrl = registerUrl+"/test/challenge/"; -- GitLab From 758740a31d033c72fcf69e54d51fce9ccd1c958f Mon Sep 17 00:00:00 2001 From: komakkar Date: Fri, 4 Sep 2020 19:07:13 +0530 Subject: [PATCH 07/14] UT updated --- .../notification/util/ServiceAccountClientImplTest.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/provider/notification-azure/src/test/java/org/opengroup/osdu/notification/util/ServiceAccountClientImplTest.java b/provider/notification-azure/src/test/java/org/opengroup/osdu/notification/util/ServiceAccountClientImplTest.java index 5153d620..8e5db06d 100644 --- a/provider/notification-azure/src/test/java/org/opengroup/osdu/notification/util/ServiceAccountClientImplTest.java +++ b/provider/notification-azure/src/test/java/org/opengroup/osdu/notification/util/ServiceAccountClientImplTest.java @@ -97,24 +97,26 @@ public class ServiceAccountClientImplTest { public void should_getTokenFromCache_getIdTokenTest() { // SetUp when(tenantJwtCacheMock.get(any())).thenReturn(idToken); + String expectedToken = "Bearer " +idToken.getTokenValue(); // Act String returnedIdToken = sut.getIdToken(tenantName); // Assert - Assert.assertEquals(idToken.getTokenValue(), returnedIdToken); + Assert.assertEquals(expectedToken, returnedIdToken); } @Test public void should_updateCache_getIdTokenTest() { // Set up when(tenantJwtCacheMock.get(any())).thenReturn(idToken); + String expectedToken = "Bearer " +idToken.getTokenValue(); // Act String returnedToken = this.sut.getIdToken(tenantName); // Assert - Assert.assertEquals(validToken, returnedToken); + Assert.assertEquals(expectedToken, returnedToken); } @Test -- GitLab From 9f649d565f266e32c465baaee21bedb9583b98e3 Mon Sep 17 00:00:00 2001 From: komakkar Date: Fri, 4 Sep 2020 21:16:55 +0530 Subject: [PATCH 08/14] minor changes --- .../osdu/notification/api/PubsubEndpointHMACDescriptor.java | 2 +- .../osdu/notification/api/PubsubEndpointHMACDescriptor.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/testing/notification-test-azure/src/test/java/org/opengroup/osdu/notification/api/PubsubEndpointHMACDescriptor.java b/testing/notification-test-azure/src/test/java/org/opengroup/osdu/notification/api/PubsubEndpointHMACDescriptor.java index 55c7ba57..e41c5666 100644 --- a/testing/notification-test-azure/src/test/java/org/opengroup/osdu/notification/api/PubsubEndpointHMACDescriptor.java +++ b/testing/notification-test-azure/src/test/java/org/opengroup/osdu/notification/api/PubsubEndpointHMACDescriptor.java @@ -56,7 +56,7 @@ public class PubsubEndpointHMACDescriptor extends RestDescriptor { } @Override - public Map getOsduTenantHeaders(){ + public Map getOsduTenantHeaders() { Map headers = new HashMap<>(); headers.put("Aeg-Subscription-Name", arg()); headers.put("data-partition-id", Config.Instance().OsduTenant); diff --git a/testing/notification-test-core/src/main/java/org/opengroup/osdu/notification/api/PubsubEndpointHMACDescriptor.java b/testing/notification-test-core/src/main/java/org/opengroup/osdu/notification/api/PubsubEndpointHMACDescriptor.java index ea4553e8..60791c28 100644 --- a/testing/notification-test-core/src/main/java/org/opengroup/osdu/notification/api/PubsubEndpointHMACDescriptor.java +++ b/testing/notification-test-core/src/main/java/org/opengroup/osdu/notification/api/PubsubEndpointHMACDescriptor.java @@ -53,7 +53,7 @@ public class PubsubEndpointHMACDescriptor extends RestDescriptor { @Override public Map getOsduTenantHeaders(){ Map headers = new HashMap<>(); - headers.put("data-partition-id", Config.Instance().ClientTenant); + headers.put("data-partition-id", Config.Instance().OsduTenant); return headers; } -- GitLab From 20950a67737deb77b59b56fe3d34e7feab0d532f Mon Sep 17 00:00:00 2001 From: komakkar Date: Sun, 6 Sep 2020 19:27:03 +0530 Subject: [PATCH 09/14] Changed version in core and removed the Azure implementation for PubsubEndpointHMACDescriptor.java --- .../api/PubsubEndpointHMACDescriptor.java | 75 ------------------- testing/notification-test-core/pom.xml | 2 +- .../api/PubsubEndpointGSADescriptor.java | 2 +- 3 files changed, 2 insertions(+), 77 deletions(-) delete mode 100644 testing/notification-test-azure/src/test/java/org/opengroup/osdu/notification/api/PubsubEndpointHMACDescriptor.java diff --git a/testing/notification-test-azure/src/test/java/org/opengroup/osdu/notification/api/PubsubEndpointHMACDescriptor.java b/testing/notification-test-azure/src/test/java/org/opengroup/osdu/notification/api/PubsubEndpointHMACDescriptor.java deleted file mode 100644 index e41c5666..00000000 --- a/testing/notification-test-azure/src/test/java/org/opengroup/osdu/notification/api/PubsubEndpointHMACDescriptor.java +++ /dev/null @@ -1,75 +0,0 @@ -// Copyright © Microsoft Corporation -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package org.opengroup.osdu.notification.api; - -import org.opengroup.osdu.notification.util.Config; -import org.opengroup.osdu.notification.util.RestDescriptor; -import org.opengroup.osdu.notification.util.TestUtils; - -import java.util.HashMap; -import java.util.Map; - -public class PubsubEndpointHMACDescriptor extends RestDescriptor { - - @Override - public String getPath() { - return "push-handlers/records-changed"; - } - - @Override - public String getHttpMethod() { - return "POST"; - } - - @Override - public String getValidBody() { - return "[{\n" + - " \"id\": \"2425\",\n" + - " \"eventType\": \"recordInserted\",\n" + - " \"subject\": \"myapp/vehicles/motorcycles\",\n" + - " \"data\": {\n" + - " \"attributes\": {\n" + - " \"correlation-id\": \"39137f49-67d6-4001-a6aa-15521ef4f49e\",\n" + - " \"data-partition-id\": \"" + TestUtils.getOsduTenant() + "\"\n" + - " },\n" + - " \"data\": \"W3sia2luZCI6InRlc3RraW5kIiwiaWQiOiJ0ZXN0aWQiLCJvcGVyYXRpb250eXBlIjoiY3JlYXRlIn0seyJraW5kIjoidGVzdGtpbmQyIiwiaWQiOiJ0ZXN0aWQyIiwib3BlcmF0aW9udHlwZSI6InVwZGF0ZSJ9XQ\",\n" + - " \"messageId\": \"136969346945\"\n" + - " },\n" + - " \"dataVersion\": \"1.0\",\n" + - " \"metadataVersion\": \"1\",\n" + - " \"eventTime\": \"2020-08-14T18:04:12+00:00\",\n" + - " \"topic\": \"/subscriptions/c99e2bf3-1777-412b-baba-d823676589c2/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" + - " }]"; - - } - - @Override - public Map getOsduTenantHeaders() { - Map headers = new HashMap<>(); - headers.put("Aeg-Subscription-Name", arg()); - headers.put("data-partition-id", Config.Instance().OsduTenant); - return headers; - } - - @Override - public Map getCustomerTenantHeaders(){ - Map headers = new HashMap<>(); - headers.put("Aeg-Subscription-Name", arg()); - headers.put("data-partition-id", Config.Instance().ClientTenant); - return headers; - } - - -} diff --git a/testing/notification-test-core/pom.xml b/testing/notification-test-core/pom.xml index 5d4a558b..e2779a05 100644 --- a/testing/notification-test-core/pom.xml +++ b/testing/notification-test-core/pom.xml @@ -21,7 +21,7 @@ 4.0.0 org.opengroup.osdu.notification notification-test-core - 1.0-SNAPSHOT + 1.1-SNAPSHOT notification-test-core Integration tests core for notification jar diff --git a/testing/notification-test-core/src/main/java/org/opengroup/osdu/notification/api/PubsubEndpointGSADescriptor.java b/testing/notification-test-core/src/main/java/org/opengroup/osdu/notification/api/PubsubEndpointGSADescriptor.java index 8c4f5a53..476c1b68 100644 --- a/testing/notification-test-core/src/main/java/org/opengroup/osdu/notification/api/PubsubEndpointGSADescriptor.java +++ b/testing/notification-test-core/src/main/java/org/opengroup/osdu/notification/api/PubsubEndpointGSADescriptor.java @@ -51,7 +51,7 @@ public class PubsubEndpointGSADescriptor extends RestDescriptor { @Override public Map getOsduTenantHeaders(){ Map headers = new HashMap<>(); - headers.put("data-partition-id", Config.Instance().ClientTenant); + headers.put("data-partition-id", Config.Instance().OsduTenant); return headers; } -- GitLab From 602f01a6eb5d20bc17935cfac67a977bb1f631ba Mon Sep 17 00:00:00 2001 From: komakkar Date: Mon, 7 Sep 2020 12:52:05 +0530 Subject: [PATCH 10/14] added testing pom to govern build order. --- .../api/PubsubEndpointHMACDescriptor.java | 73 +++++++++++++++++++ testing/notification-test-core/pom.xml | 2 +- testing/pom.xml | 58 +++++++++++++++ 3 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 testing/notification-test-azure/src/test/java/org/opengroup/osdu/notification/api/PubsubEndpointHMACDescriptor.java create mode 100644 testing/pom.xml diff --git a/testing/notification-test-azure/src/test/java/org/opengroup/osdu/notification/api/PubsubEndpointHMACDescriptor.java b/testing/notification-test-azure/src/test/java/org/opengroup/osdu/notification/api/PubsubEndpointHMACDescriptor.java new file mode 100644 index 00000000..915558b3 --- /dev/null +++ b/testing/notification-test-azure/src/test/java/org/opengroup/osdu/notification/api/PubsubEndpointHMACDescriptor.java @@ -0,0 +1,73 @@ +// Copyright © Microsoft Corporation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.opengroup.osdu.notification.api; + +import org.opengroup.osdu.notification.util.Config; +import org.opengroup.osdu.notification.util.RestDescriptor; +import org.opengroup.osdu.notification.util.TestUtils; + +import java.util.HashMap; +import java.util.Map; + +public class PubsubEndpointHMACDescriptor extends RestDescriptor { + + @Override + public String getPath() { + return "push-handlers/records-changed"; + } + + @Override + public String getHttpMethod() { + return "POST"; + } + + @Override + public String getValidBody() { + return "[{\n" + + " \"id\": \"2425\",\n" + + " \"eventType\": \"recordInserted\",\n" + + " \"subject\": \"myapp/vehicles/motorcycles\",\n" + + " \"data\": {\n" + + " \"attributes\": {\n" + + " \"correlation-id\": \"39137f49-67d6-4001-a6aa-15521ef4f49e\",\n" + + " \"data-partition-id\": \"" + TestUtils.getOsduTenant() + "\"\n" + + " },\n" + + " \"data\": \"W3sia2luZCI6InRlc3RraW5kIiwiaWQiOiJ0ZXN0aWQiLCJvcGVyYXRpb250eXBlIjoiY3JlYXRlIn0seyJraW5kIjoidGVzdGtpbmQyIiwiaWQiOiJ0ZXN0aWQyIiwib3BlcmF0aW9udHlwZSI6InVwZGF0ZSJ9XQ\",\n" + + " \"messageId\": \"136969346945\"\n" + + " },\n" + + " \"dataVersion\": \"1.0\",\n" + + " \"metadataVersion\": \"1\",\n" + + " \"eventTime\": \"2020-08-14T18:04:12+00:00\",\n" + + " \"topic\": \"/subscriptions/c99e2bf3-1777-412b-baba-d823676589c2/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" + + " }]"; + + } + + @Override + public Map getOsduTenantHeaders() { + Map headers = new HashMap<>(); + headers.put("Aeg-Subscription-Name", arg()); + headers.put("data-partition-id", Config.Instance().OsduTenant); + return headers; + } + + @Override + public Map getCustomerTenantHeaders(){ + Map headers = new HashMap<>(); + headers.put("Aeg-Subscription-Name", arg()); + headers.put("data-partition-id", Config.Instance().ClientTenant); + return headers; + } +} diff --git a/testing/notification-test-core/pom.xml b/testing/notification-test-core/pom.xml index e2779a05..5d4a558b 100644 --- a/testing/notification-test-core/pom.xml +++ b/testing/notification-test-core/pom.xml @@ -21,7 +21,7 @@ 4.0.0 org.opengroup.osdu.notification notification-test-core - 1.1-SNAPSHOT + 1.0-SNAPSHOT notification-test-core Integration tests core for notification jar diff --git a/testing/pom.xml b/testing/pom.xml new file mode 100644 index 00000000..98ce8276 --- /dev/null +++ b/testing/pom.xml @@ -0,0 +1,58 @@ + + + + 4.0.0 + org.opengroup.osdu + os-notification-testing + 0.0.5-SNAPSHOT + Root Notification Service project + pom + + + + Apache License, Version 2.0 + https://www.apache.org/licenses/LICENSE-2.0.txt + repo + + + + notification-test-core + notification-test-azure + notification-test-gcp + + + + + ${gitlab-server} + https://community.opengroup.org/api/v4/groups/17/-/packages/maven + + + + + + ${gitlab-server} + https://community.opengroup.org/api/v4/projects/44/packages/maven + + + ${gitlab-server} + https://community.opengroup.org/api/v4/projects/44/packages/maven + + + + -- GitLab From a97a1b667415351a5af6ce793d004b4638ee2a63 Mon Sep 17 00:00:00 2001 From: komakkar Date: Mon, 7 Sep 2020 14:39:32 +0530 Subject: [PATCH 11/14] undoing stray changes. --- .../pubsub/EventGridHandshakeHandler.java | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/pubsub/EventGridHandshakeHandler.java 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 new file mode 100644 index 00000000..686636d8 --- /dev/null +++ b/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/pubsub/EventGridHandshakeHandler.java @@ -0,0 +1,53 @@ +// Copyright © Microsoft Corporation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.opengroup.osdu.notification.provider.azure.pubsub; + +import com.google.gson.JsonObject; +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 +public class EventGridHandshakeHandler implements IPubsubHandshakeHandler { + + @Autowired + private EventGridRequestBodyExtractor eventGridRequestBodyExtractor; + + /** + * Extract Handshake response string form Handshake request. + * TODO: Check if there is a need to verify subscription name with + * registration service, before verifying the endpoint. + * @return validation string + */ + @Override + public String getHandshakeResponse() { + String response; + + try { + String validationCode = this.eventGridRequestBodyExtractor.getValidationCodeForHandshake(); + JsonObject jsonResponse = new JsonObject(); + jsonResponse.addProperty("ValidationResponse", validationCode); + + response = jsonResponse.toString(); + } catch (Exception exception) { + throw new AppException(HttpStatus.BAD_REQUEST.value(), "Request payload parsing error", + "Unable to parse request payload.", exception); + } + + return response; + } +} \ No newline at end of file -- GitLab From 557624c07353e56db23d3d2a391cfc5dbb33043b Mon Sep 17 00:00:00 2001 From: komakkar Date: Mon, 7 Sep 2020 15:37:28 +0530 Subject: [PATCH 12/14] undoing stray changes. --- .../pubsub/EventGridHandshakeHandler.java | 53 ------------------- 1 file changed, 53 deletions(-) delete mode 100644 provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/pubsub/EventGridHandshakeHandler.java 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 deleted file mode 100644 index 686636d8..00000000 --- a/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/pubsub/EventGridHandshakeHandler.java +++ /dev/null @@ -1,53 +0,0 @@ -// Copyright © Microsoft Corporation -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package org.opengroup.osdu.notification.provider.azure.pubsub; - -import com.google.gson.JsonObject; -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 -public class EventGridHandshakeHandler implements IPubsubHandshakeHandler { - - @Autowired - private EventGridRequestBodyExtractor eventGridRequestBodyExtractor; - - /** - * Extract Handshake response string form Handshake request. - * TODO: Check if there is a need to verify subscription name with - * registration service, before verifying the endpoint. - * @return validation string - */ - @Override - public String getHandshakeResponse() { - String response; - - try { - String validationCode = this.eventGridRequestBodyExtractor.getValidationCodeForHandshake(); - JsonObject jsonResponse = new JsonObject(); - jsonResponse.addProperty("ValidationResponse", validationCode); - - response = jsonResponse.toString(); - } catch (Exception exception) { - throw new AppException(HttpStatus.BAD_REQUEST.value(), "Request payload parsing error", - "Unable to parse request payload.", exception); - } - - return response; - } -} \ No newline at end of file -- GitLab From b34eee9610979d2800be6d9eb4dba69a5b910bf3 Mon Sep 17 00:00:00 2001 From: komakkar Date: Mon, 7 Sep 2020 19:18:05 +0530 Subject: [PATCH 13/14] undoing stray changes --- .../pubsub/EventGridHandshakeHandler.java | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/pubsub/EventGridHandshakeHandler.java 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 new file mode 100644 index 00000000..686636d8 --- /dev/null +++ b/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/pubsub/EventGridHandshakeHandler.java @@ -0,0 +1,53 @@ +// Copyright © Microsoft Corporation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.opengroup.osdu.notification.provider.azure.pubsub; + +import com.google.gson.JsonObject; +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 +public class EventGridHandshakeHandler implements IPubsubHandshakeHandler { + + @Autowired + private EventGridRequestBodyExtractor eventGridRequestBodyExtractor; + + /** + * Extract Handshake response string form Handshake request. + * TODO: Check if there is a need to verify subscription name with + * registration service, before verifying the endpoint. + * @return validation string + */ + @Override + public String getHandshakeResponse() { + String response; + + try { + String validationCode = this.eventGridRequestBodyExtractor.getValidationCodeForHandshake(); + JsonObject jsonResponse = new JsonObject(); + jsonResponse.addProperty("ValidationResponse", validationCode); + + response = jsonResponse.toString(); + } catch (Exception exception) { + throw new AppException(HttpStatus.BAD_REQUEST.value(), "Request payload parsing error", + "Unable to parse request payload.", exception); + } + + return response; + } +} \ No newline at end of file -- GitLab From f92f8d2a7e4a65e1b815111108828962126823c8 Mon Sep 17 00:00:00 2001 From: komakkar Date: Mon, 7 Sep 2020 19:23:50 +0530 Subject: [PATCH 14/14] undoing stray changes --- .../azure/EventGridHandshakeHandlerTest.java | 75 +++++ .../EventGridRequestBodyExtractorTest.java | 304 ++++++++++++++++++ 2 files changed, 379 insertions(+) create mode 100644 provider/notification-azure/src/test/java/org/opengroup/osdu/notification/provider/azure/EventGridHandshakeHandlerTest.java create mode 100644 provider/notification-azure/src/test/java/org/opengroup/osdu/notification/provider/azure/EventGridRequestBodyExtractorTest.java 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 new file mode 100644 index 00000000..b58451b9 --- /dev/null +++ b/provider/notification-azure/src/test/java/org/opengroup/osdu/notification/provider/azure/EventGridHandshakeHandlerTest.java @@ -0,0 +1,75 @@ + +// Copyright © Microsoft Corporation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +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.Spy; +import org.mockito.junit.MockitoJUnitRunner; +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; + +@RunWith(MockitoJUnitRunner.class) +public class EventGridHandshakeHandlerTest { + @Mock + EventGridRequestBodyExtractor eventGridRequestBodyExtractor; + + @InjectMocks + @Spy + private EventGridHandshakeHandler sut; + + @Test + public void should_returnValidResponse_getHandshakeResponse() throws IOException { + // Set up + when(sut.getHandshakeResponse()).thenReturn("testValidationCode"); + String expectedResponse = "{\"ValidationResponse\":\"testValidationCode\"}"; + + // Act + String observedResponse = this.sut.getHandshakeResponse(); + + // Assert + Assert.assertEquals(observedResponse, expectedResponse); + } + + @Test + public void should_throw_getHandshakeResponse() throws IOException { + // Set up + when(sut.getHandshakeResponse()) + .thenThrow(new AppException(HttpStatus.BAD_REQUEST.value(), "Request payload parsing error", "" )); + try{ + // Act + String observedResponse = this.sut.getHandshakeResponse(); + + // Assert + fail("Should Throw Exception"); + } catch (AppException appException){ + Assert.assertEquals(HttpStatus.BAD_REQUEST.value(), appException.getError().getCode()); + Assert.assertEquals("Unable to parse request payload.", appException.getError().getMessage()); + } catch (Exception exception) { + 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 new file mode 100644 index 00000000..bce964c1 --- /dev/null +++ b/provider/notification-azure/src/test/java/org/opengroup/osdu/notification/provider/azure/EventGridRequestBodyExtractorTest.java @@ -0,0 +1,304 @@ +// Copyright © Microsoft Corporation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.opengroup.osdu.notification.provider.azure; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +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; + +import javax.servlet.http.HttpServletRequest; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.StringReader; +import java.util.Map; + +import static org.junit.Assert.fail; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class EventGridRequestBodyExtractorTest { + + private EventGridRequestBodyExtractor sut; + + @Mock + private HttpServletRequest httpServletRequest; + + @Mock + private JaxRsDpsLog log; + + @Test + public void should_returnTrue_isHandshakeRequest() throws IOException { + // Set up + String validHandshakeRequestRoot = + " [{\n" + + " \"id\": \"testId\",\n" + + " \"topic\": \"testTopic\",\n" + + " \"subject\": \"\",\n" + + " \"data\": {\n" + + " \"validationCode\": \"testValidationCode\",\n" + + " \"validationUrl\": \"testURL\"\n" + + " },\n" + + " \"eventType\": \"Microsoft.EventGrid.SubscriptionValidationEvent\",\n" + + " \"eventTime\": \"2020-08-14T11:18:55.9278057Z\",\n" + + " \"metadataVersion\": \"1\",\n" + + " \"dataVersion\": \"2\"\n" + + " }]"; + BufferedReader reader = new BufferedReader(new StringReader(validHandshakeRequestRoot)); + when(this.httpServletRequest.getReader()).thenReturn(reader); + sut = new EventGridRequestBodyExtractor(httpServletRequest, log); + + // Act + boolean response = this.sut.isHandshakeRequest(); + + // Assert + Assert.assertTrue(response); + } + + @Test + public void shouldThrow_whenRequestTypeIsNotright_isHandshakeRequest() throws IOException { + //SetUp + String invalidHandshakeRequestRoot = + " [{\n" + + " \"id\": \"testId\",\n" + + " \"topic\": \"testTopic\",\n" + + " \"subject\": \"\",\n" + + " \"data\": {\n" + + " \"validationCode\": \"testValidationCode\",\n" + + " \"validationUrl\": \"testURL\"\n" + + " },\n" + + " \"eventType\": \"SubscriptionValidationEvent\",\n" + + " \"eventTime\": \"2020-08-14T11:18:55.9278057Z\",\n" + + " \"metadataVersion\": \"1\",\n" + + " \"dataVersion\": \"2\"\n" + + " }]"; + BufferedReader reader = new BufferedReader(new StringReader(invalidHandshakeRequestRoot)); + when(this.httpServletRequest.getReader()).thenReturn(reader); + + try{ + // Act + sut = new EventGridRequestBodyExtractor(httpServletRequest, log); + + // Assert + fail("Should Throw Exception"); + } catch (AppException appException){ + Assert.assertEquals(HttpStatus.BAD_REQUEST.value(), appException.getError().getCode()); + Assert.assertEquals("Unable to parse request payload.", appException.getError().getMessage()); + } catch (Exception exception) { + fail("Should Throw AppException"); + } + } + + @Test + public void should_throwWhenAttributesAreMissing_extractDataFromRequestBody() throws IOException { + String requestRootWithoutAttributes = "[{\n" + + " \"id\": \"2425\",\n" + + " \"eventType\": \"recordInserted\",\n" + + " \"subject\": \"myapp/vehicles/motorcycles\",\n" + + " \"data\": {\n" + + " \"data\": \"W3sia2luZCI6InRlc3RraW5kIiwiaWQiOiJ0ZXN0aWQiLCJvcGVyYXRpb250eXBlIjoiY3JlYXRlIn0seyJraW5kIjoidGVzdGtpbmQyIiwiaWQiOiJ0ZXN0aWQyIiwib3BlcmF0aW9udHlwZSI6InVwZGF0ZSJ9XQ\",\n" + + " \"messageId\": \"136969346945\"\n" + + " },\n" + + " \"dataVersion\": \"1.0\",\n" + + " \"metadataVersion\": \"1\",\n" + + " \"eventTime\": \"2020-08-14T18:04:12+00:00\",\n" + + " \"topic\": \"/subscriptions/c99e2bf3-1777-412b-baba-d823676589c2/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" + + " }]"; + BufferedReader reader = new BufferedReader(new StringReader(requestRootWithoutAttributes)); + when(this.httpServletRequest.getReader()).thenReturn(reader); + + try{ + // Act + sut = new EventGridRequestBodyExtractor(httpServletRequest, log); + + // Asset + fail("Should Throw Exception"); + } catch (AppException appException){ + Assert.assertEquals(HttpStatus.BAD_REQUEST.value(), appException.getError().getCode()); + Assert.assertEquals("Unable to parse request payload.", appException.getError().getMessage()); + } catch (Exception exception) { + fail("Should Throw AppException"); + } + } + + @Test + public void should_throwWhenDataFiledIsMissing_extractDataFromRequestBody() throws IOException { + String requestRootWithoutData = "[{\n" + + " \"id\": \"2425\",\n" + + " \"eventType\": \"recordInserted\",\n" + + " \"subject\": \"myapp/vehicles/motorcycles\",\n" + + " \"data\": {\n" + + " \"attributes\": {\n" + + " \"correlation-id\": \"39137f49-67d6-4001-a6aa-15521ef4f49e\",\n" + + " \"data-partition-id\": \"opendes \"\n" + + " },\n" + + " \"messageId\": \"136969346945\"\n" + + " },\n" + + " \"dataVersion\": \"1.0\",\n" + + " \"metadataVersion\": \"1\",\n" + + " \"eventTime\": \"2020-08-14T18:04:12+00:00\",\n" + + " \"topic\": \"/subscriptions/c99e2bf3-1777-412b-baba-d823676589c2/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" + + " }]"; + BufferedReader reader = new BufferedReader(new StringReader(requestRootWithoutData)); + when(this.httpServletRequest.getReader()).thenReturn(reader); + + try{ + // Act + sut = new EventGridRequestBodyExtractor(httpServletRequest, log); + + // Asset + fail("Should Throw Exception"); + } catch (AppException appException){ + Assert.assertEquals(HttpStatus.BAD_REQUEST.value(), appException.getError().getCode()); + Assert.assertEquals("Unable to parse request payload.", appException.getError().getMessage()); + } catch (Exception exception) { + fail("Should Throw AppException"); + } + } + + @Test + public void should_throwWhenPartitionIdIsMissing_extractDataFromRequestBody() throws IOException { + String requestRootWithoutDataPartitionId = "[{\n" + + " \"id\": \"2425\",\n" + + " \"eventType\": \"recordInserted\",\n" + + " \"subject\": \"myapp/vehicles/motorcycles\",\n" + + " \"data\": {\n" + + " \"attributes\": {\n" + + " \"correlation-id\": \"39137f49-67d6-4001-a6aa-15521ef4f49e\"\n" + + " },\n" + + " \"data\": \"W3sia2luZCI6InRlc3RraW5kIiwiaWQiOiJ0ZXN0aWQiLCJvcGVyYXRpb250eXBlIjoiY3JlYXRlIn0seyJraW5kIjoidGVzdGtpbmQyIiwiaWQiOiJ0ZXN0aWQyIiwib3BlcmF0aW9udHlwZSI6InVwZGF0ZSJ9XQ\",\n"+ + " \"messageId\": \"136969346945\"\n" + + " },\n" + + " \"dataVersion\": \"1.0\",\n" + + " \"metadataVersion\": \"1\",\n" + + " \"eventTime\": \"2020-08-14T18:04:12+00:00\",\n" + + " \"topic\": \"/subscriptions/c99e2bf3-1777-412b-baba-d823676589c2/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" + + " }]"; + BufferedReader reader = new BufferedReader(new StringReader(requestRootWithoutDataPartitionId)); + when(this.httpServletRequest.getReader()).thenReturn(reader); + + try{ + // Act + sut = new EventGridRequestBodyExtractor(httpServletRequest, log); + + // Asset + fail("Should Throw Exception"); + } catch (AppException appException){ + Assert.assertEquals(HttpStatus.BAD_REQUEST.value(), appException.getError().getCode()); + Assert.assertEquals("Unable to parse request payload.", appException.getError().getMessage()); + } catch (Exception exception) { + fail("Should Throw AppException"); + } + } + + @Test + public void should_returnValidData_extractDataFromRequestBody() throws IOException { + String vaidRequestRoot = "[{\n" + + " \"id\": \"2425\",\n" + + " \"eventType\": \"recordInserted\",\n" + + " \"subject\": \"myapp/vehicles/motorcycles\",\n" + + " \"data\": {\n" + + " \"attributes\": {\n" + + " \"correlation-id\": \"39137f49-67d6-4001-a6aa-15521ef4f49e\",\n" + + " \"data-partition-id\": \"opendes \"\n" + + " },\n" + + " \"data\": \"W3sia2luZCI6InRlc3RraW5kIiwiaWQiOiJ0ZXN0aWQiLCJvcGVyYXRpb250eXBlIjoiY3JlYXRlIn0seyJraW5kIjoidGVzdGtpbmQyIiwiaWQiOiJ0ZXN0aWQyIiwib3BlcmF0aW9udHlwZSI6InVwZGF0ZSJ9XQ\",\n" + + " \"messageId\": \"136969346945\"\n" + + " },\n" + + " \"dataVersion\": \"1.0\",\n" + + " \"metadataVersion\": \"1\",\n" + + " \"eventTime\": \"2020-08-14T18:04:12+00:00\",\n" + + " \"topic\": \"/subscriptions/c99e2bf3-1777-412b-baba-d823676589c2/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" + + " }]"; + + String expectedData = "[{\"kind\":\"testkind\",\"id\":\"testid\",\"operationtype\":\"create\"},{\"kind\":\"testkind2\",\"id\":\"testid2\",\"operationtype\":\"update\"}]"; + + BufferedReader reader = new BufferedReader(new StringReader(vaidRequestRoot)); + when(this.httpServletRequest.getReader()).thenReturn(reader); + + sut = new EventGridRequestBodyExtractor(httpServletRequest, log); + + // Act + String receivedData = this.sut.extractDataFromRequestBody(); + + // Asset + Assert.assertEquals(expectedData,receivedData); + } + + @Test + public void should_returnValidAttributes_extractDataFromRequestBody() throws IOException { + String vaidRequestRoot = "[{\n" + + " \"id\": \"2425\",\n" + + " \"eventType\": \"recordInserted\",\n" + + " \"subject\": \"myapp/vehicles/motorcycles\",\n" + + " \"data\": {\n" + + " \"attributes\": {\n" + + " \"correlation-id\": \"39137f49-67d6-4001-a6aa-15521ef4f49e\",\n" + + " \"data-partition-id\": \"opendes\"\n" + + " },\n" + + " \"data\": \"W3sia2luZCI6InRlc3RraW5kIiwiaWQiOiJ0ZXN0aWQiLCJvcGVyYXRpb250eXBlIjoiY3JlYXRlIn0seyJraW5kIjoidGVzdGtpbmQyIiwiaWQiOiJ0ZXN0aWQyIiwib3BlcmF0aW9udHlwZSI6InVwZGF0ZSJ9XQ\",\n" + + " \"messageId\": \"136969346945\"\n" + + " },\n" + + " \"dataVersion\": \"1.0\",\n" + + " \"metadataVersion\": \"1\",\n" + + " \"eventTime\": \"2020-08-14T18:04:12+00:00\",\n" + + " \"topic\": \"/subscriptions/c99e2bf3-1777-412b-baba-d823676589c2/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" + + " }]"; + BufferedReader reader = new BufferedReader(new StringReader(vaidRequestRoot)); + when(this.httpServletRequest.getReader()).thenReturn(reader); + sut = new EventGridRequestBodyExtractor(httpServletRequest, log); + + // Act + Map observedAttributes = this.sut.extractAttributesFromRequestBody(); + + // Asset + Assert.assertEquals(observedAttributes.get("correlation-id"),"39137f49-67d6-4001-a6aa-15521ef4f49e"); + Assert.assertEquals(observedAttributes.get("data-partition-id"),"opendes"); + } + + @Test + public void should_returnValidResponse_getHandshakeResponse() throws IOException { + // Set up + String validHandshakeRequestRoot = + " [{\n" + + " \"id\": \"testId\",\n" + + " \"topic\": \"testTopic\",\n" + + " \"subject\": \"\",\n" + + " \"data\": {\n" + + " \"validationCode\": \"testValidationCode\",\n" + + " \"validationUrl\": \"testURL\"\n" + + " },\n" + + " \"eventType\": \"Microsoft.EventGrid.SubscriptionValidationEvent\",\n" + + " \"eventTime\": \"2020-08-14T11:18:55.9278057Z\",\n" + + " \"metadataVersion\": \"1\",\n" + + " \"dataVersion\": \"2\"\n" + + " }]"; + String expectedResponse = "testValidationCode"; + BufferedReader reader = new BufferedReader(new StringReader(validHandshakeRequestRoot)); + when(this.httpServletRequest.getReader()).thenReturn(reader); + sut = new EventGridRequestBodyExtractor(httpServletRequest, log); + + // Act + String observedResponse = this.sut.getValidationCodeForHandshake(); + + // Assert + Assert.assertEquals(observedResponse, expectedResponse); + } +} -- GitLab