From e448b152c1ba60e26f6a1fa5746e2f69f36311fc Mon Sep 17 00:00:00 2001 From: komakkar <komakkar@microsoft.com> Date: Mon, 18 Jan 2021 22:53:42 +0530 Subject: [PATCH] adding auth mechanism for pubsub request --- .../ServiceAccountJwtAzureClientImpl.java | 13 +++----- .../AzureServiceAccountValidatorImplTest.java | 3 -- .../util/ServiceAccountClientImplTest.java | 30 +------------------ 3 files changed, 5 insertions(+), 41 deletions(-) diff --git a/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/ServiceAccountJwtAzureClientImpl.java b/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/ServiceAccountJwtAzureClientImpl.java index b1db3272e..934dd8672 100644 --- a/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/ServiceAccountJwtAzureClientImpl.java +++ b/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/ServiceAccountJwtAzureClientImpl.java @@ -21,15 +21,16 @@ import com.microsoft.aad.adal4j.ClientCredential; import org.apache.http.HttpStatus; import org.opengroup.osdu.core.common.model.http.AppException; import org.opengroup.osdu.core.common.model.search.IdToken; -import org.opengroup.osdu.core.common.model.tenant.TenantInfo; import org.opengroup.osdu.core.common.provider.interfaces.IJwtCache; -import org.opengroup.osdu.core.common.provider.interfaces.ITenantFactory; import org.opengroup.osdu.core.common.util.IServiceAccountJwtClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.net.MalformedURLException; -import java.util.concurrent.*; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; @Component public class ServiceAccountJwtAzureClientImpl implements IServiceAccountJwtClient { @@ -37,15 +38,10 @@ public class ServiceAccountJwtAzureClientImpl implements IServiceAccountJwtClien @Autowired private AppProperties config; - @Autowired - private ITenantFactory tenantInfoServiceProvider; - @Autowired private IJwtCache tenantJwtCache; public String getIdToken(String tenantName) { - - String ACCESS_TOKEN = ""; ExecutorService service = null; @@ -97,4 +93,3 @@ public class ServiceAccountJwtAzureClientImpl implements IServiceAccountJwtClien return ACCESS_TOKEN; } } - diff --git a/provider/notification-azure/src/test/java/org/opengroup/osdu/notification/util/AzureServiceAccountValidatorImplTest.java b/provider/notification-azure/src/test/java/org/opengroup/osdu/notification/util/AzureServiceAccountValidatorImplTest.java index 65206d2ae..2124bff88 100644 --- a/provider/notification-azure/src/test/java/org/opengroup/osdu/notification/util/AzureServiceAccountValidatorImplTest.java +++ b/provider/notification-azure/src/test/java/org/opengroup/osdu/notification/util/AzureServiceAccountValidatorImplTest.java @@ -19,11 +19,9 @@ import com.auth0.jwt.algorithms.Algorithm; import org.junit.Assert; import org.junit.Before; 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.notification.provider.azure.util.AppProperties; import org.opengroup.osdu.notification.provider.azure.util.AzureServiceAccountValidatorImpl; @@ -34,7 +32,6 @@ import static org.mockito.MockitoAnnotations.initMocks; public class AzureServiceAccountValidatorImplTest { private static String eventGridId = "eventgridid"; - private static String invalidJWT = "invalidJWT"; @Mock private AppProperties appProperties; 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 f567cd985..e7caf1d3b 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 @@ -26,8 +26,6 @@ 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.core.common.model.search.IdToken; -import org.opengroup.osdu.core.common.model.tenant.TenantInfo; -import org.opengroup.osdu.core.common.provider.interfaces.ITenantFactory; import org.opengroup.osdu.notification.provider.azure.cache.JwtCache; import org.opengroup.osdu.notification.provider.azure.util.AppProperties; import org.opengroup.osdu.notification.provider.azure.util.ServiceAccountJwtAzureClientImpl; @@ -36,8 +34,8 @@ import java.util.concurrent.ExecutorService; import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.MockitoAnnotations.initMocks; import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; @RunWith(MockitoJUnitRunner.class) public class ServiceAccountClientImplTest { @@ -45,12 +43,6 @@ public class ServiceAccountClientImplTest { final String tenantName = "Test Tenant"; final String validToken = "validToken"; - @Mock - private ITenantFactory tenantInfoServiceProvider; - - @Mock - private TenantInfo tenantInfo; - @Mock private IdToken idToken; @@ -73,29 +65,9 @@ public class ServiceAccountClientImplTest { @Before public void setup() { initMocks(this); - when(tenantInfo.getName()).thenReturn(tenantName); - when(this.tenantInfoServiceProvider.getTenantInfo(tenantName)).thenReturn(tenantInfo); idToken = IdToken.builder().tokenValue(validToken).expirationTimeMillis(System.currentTimeMillis() + 10000000L).build(); } - @Test - public void should_throwAppException_getIdTokenTest() { - // Set up - when(this.tenantInfoServiceProvider.getTenantInfo(tenantName)).thenReturn(null); - try { - // Act - sut.getIdToken(tenantName); - - // Assert - fail("Should throw exception"); - } catch (AppException e) { - Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, e.getError().getCode()); - Assert.assertEquals("Invalid tenant Name from azure", e.getError().getMessage()); - } catch (Exception e) { - fail("Should not throw this exception" + e.getMessage()); - } - } - @Test public void should_getTokenFromCache_getIdTokenTest() { // SetUp -- GitLab