Commit e0ca8e25 authored by Komal Makkar's avatar Komal Makkar
Browse files

intermediate changes

parent 1ca2d5f3
......@@ -65,6 +65,12 @@
<groupId>org.opengroup.osdu</groupId>
<artifactId>core-lib-azure</artifactId>
<version>0.0.17</version>
<!--<exclusions>
<exclusion>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
</exclusion>
</exclusions>-->
</dependency>
<dependency>
......
......@@ -25,11 +25,15 @@ import org.springframework.stereotype.Component;
@Component
public class EventGridHandshakeHandler implements IPubsubHandshakeHandler {
@Autowired
private EventGridRequestBodyExtractor eventGridRequestBodyExtractor;
private JaxRsDpsLog logger;
@Autowired
JaxRsDpsLog logger;
EventGridHandshakeHandler(JaxRsDpsLog logger, EventGridRequestBodyExtractor eventGridRequestBodyExtractor) {
this.logger = logger;
this.eventGridRequestBodyExtractor = eventGridRequestBodyExtractor;
}
/**
* Extract Handshake response string form Handshake request.
......@@ -39,20 +43,10 @@ public class EventGridHandshakeHandler implements IPubsubHandshakeHandler {
*/
@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) {
logger.error(exception.getMessage());
throw new AppException(HttpStatus.BAD_REQUEST.value(), "Request payload parsing error",
"Unable to parse request payload.", exception);
}
String validationCode = this.eventGridRequestBodyExtractor.getValidationCodeForHandshake();
JsonObject jsonResponse = new JsonObject();
jsonResponse.addProperty("ValidationResponse", validationCode);
return response;
return jsonResponse.toString();
}
}
......@@ -103,7 +103,7 @@ public class AppProperties implements IAppProperties {
return maxCacheSize;
}
public String getKeyVaultSecret(SecretClient kv, String secretName) {
private String getKeyVaultSecret(SecretClient kv, String secretName) {
KeyVaultSecret secret = kv.getSecret(secretName);
if (secret == null) {
logger.error(String.format("Secret unexpectedly missing from KeyVault response for secret with name %s", secretName));
......
......@@ -2,12 +2,9 @@ package org.opengroup.osdu.notification.provider.azure.util;
import com.azure.security.keyvault.secrets.SecretClient;
import com.azure.security.keyvault.secrets.models.KeyVaultSecret;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import javax.inject.Named;
......@@ -20,9 +17,6 @@ public class AzureCosmosProperties {
@Value("${azure.cosmosdb.database}")
private String cosmosDBName;
@Autowired
private JaxRsDpsLog logger;
// TODO : Move away from Named beans.
@Bean
@Named("COSMOS_ENDPOINT")
......@@ -48,16 +42,14 @@ public class AzureCosmosProperties {
return cosmosDBName;
}
public String getKeyVaultSecret(SecretClient kv, String secretName) {
private String getKeyVaultSecret(SecretClient kv, String secretName) {
KeyVaultSecret secret = kv.getSecret(secretName);
if (secret == null) {
logger.error(String.format("No secret found with name %s", secretName));
throw new IllegalStateException(String.format("No secret found with name %s", secretName));
}
String secretValue = secret.getValue();
if (secretValue == null) {
logger.error(String.format("Secret unexpectedly missing from KeyVault response for secret with name %s", secretName));
throw new IllegalStateException(String.format(
"Secret unexpectedly missing from KeyVault response for secret with name %s", secretName));
}
......
......@@ -15,9 +15,7 @@
package org.opengroup.osdu.notification.provider.azure.util;
import lombok.SneakyThrows;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.notification.provider.interfaces.IGoogleServiceAccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.naming.AuthenticationNotSupportedException;
......@@ -25,14 +23,10 @@ import javax.naming.AuthenticationNotSupportedException;
@Component
public class GoogleServiceAccountImpl implements IGoogleServiceAccount {
@Autowired
JaxRsDpsLog logger;
@SneakyThrows
@Override
public String getIdToken(String keyString, String audience) {
// TODO : Check if it is to be supported
logger.error("GSA tokens are not supported.");
throw new AuthenticationNotSupportedException();
}
......
......@@ -19,7 +19,6 @@ import com.microsoft.aad.adal4j.AuthenticationContext;
import com.microsoft.aad.adal4j.AuthenticationResult;
import com.microsoft.aad.adal4j.ClientCredential;
import org.apache.http.HttpStatus;
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;
......@@ -44,13 +43,9 @@ public class ServiceAccountJwtAzureClientImpl implements IServiceAccountJwtClien
@Autowired
private IJwtCache tenantJwtCache;
@Autowired
JaxRsDpsLog logger;
public String getIdToken(String tenantName) {
TenantInfo tenant = tenantInfoServiceProvider.getTenantInfo(tenantName);
TenantInfo tenant = this.tenantInfoServiceProvider.getTenantInfo(tenantName);
if (tenant == null) {
logger.error(String.format("Invalid tenant name %s", tenantName));
throw new AppException(HttpStatus.SC_BAD_REQUEST, "Invalid tenant Name", "Invalid tenant Name from azure");
}
......@@ -59,7 +54,7 @@ public class ServiceAccountJwtAzureClientImpl implements IServiceAccountJwtClien
try {
// TODO : Refactor to move ID token form Common.Core.model.search to Common.core
IdToken cachedToken = (IdToken) tenantJwtCache.get(tenant.getName());
IdToken cachedToken = (IdToken) this.tenantJwtCache.get(tenant.getName());
if ((cachedToken != null) && !IdToken.refreshToken(cachedToken)) {
return cachedToken.getTokenValue();
......@@ -70,14 +65,8 @@ public class ServiceAccountJwtAzureClientImpl implements IServiceAccountJwtClien
ACCESS_TOKEN = getAccessToken(service);
IdToken idToken = IdToken.builder().tokenValue(ACCESS_TOKEN).expirationTimeMillis(JWT.decode(ACCESS_TOKEN).getExpiresAt().getTime()).build();
tenantJwtCache.put(tenant.getName(), idToken);
} catch (AppException appException) {
logger.error(String.format("Could not get a token %s", appException.getMessage()));
throw appException;
} catch (Exception exception) {
logger.error(String.format("Could not get a token %s", exception.getMessage()));
throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Persistence error", "Error generating token", exception);
} finally {
this.tenantJwtCache.put(tenant.getName(), idToken);
} finally {
if(service != null) {
service.shutdown();
}
......
......@@ -45,3 +45,4 @@ spring.application.name=notification-azure
# Logging configuration
logging.transaction.enabled=true
logging.slf4jlogger.enabled=true
logging.mdccontext.enabled=true
\ No newline at end of file
......@@ -36,10 +36,10 @@ import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class EventGridHandshakeHandlerTest {
@Mock
EventGridRequestBodyExtractor eventGridRequestBodyExtractor;
private EventGridRequestBodyExtractor eventGridRequestBodyExtractor;
@Mock
JaxRsDpsLog logger;
private JaxRsDpsLog logger;
@InjectMocks
@Spy
......@@ -61,7 +61,7 @@ public class EventGridHandshakeHandlerTest {
@Test
public void should_throw_getHandshakeResponse() {
// Set up
when(sut.getHandshakeResponse())
when(eventGridRequestBodyExtractor.getValidationCodeForHandshake())
.thenThrow(new AppException(HttpStatus.BAD_REQUEST.value(), "Request payload parsing error", "" ));
try{
// Act
......@@ -71,7 +71,6 @@ public class EventGridHandshakeHandlerTest {
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");
}
......
......@@ -54,7 +54,7 @@ public class AppPropertiesTests {
// Act
IllegalStateException exception = assertThrows(IllegalStateException.class, () ->{
sut.getKeyVaultSecret(kv, "secret-name");
sut.getAuthClientSecret();
});
// Assert
......@@ -69,7 +69,7 @@ public class AppPropertiesTests {
// Act
IllegalStateException exception = assertThrows(IllegalStateException.class, () ->{
sut.getKeyVaultSecret(kv, "secret-name");
sut.getAuthClientSecret();
});
// Assert
......
......@@ -54,26 +54,11 @@ public class AzureCosmosPropertiesTest {
// Act
IllegalStateException exception = assertThrows(IllegalStateException.class, () ->{
sut.getKeyVaultSecret(kv, "secret-name");
sut.cosmosKey(kv);
});
// Assert
assertEquals("No secret found with name secret-name", exception.getMessage());
}
@Test
public void should_throwWhenSecretIsMissing_getKeyVaultSecret() {
// Set-Up
doReturn(null).when(secret).getValue();
doReturn(secret).when(kv).getSecret("secret-name");
// Act
IllegalStateException exception = assertThrows(IllegalStateException.class, () ->{
sut.getKeyVaultSecret(kv, "secret-name");
});
// Assert
assertEquals("Secret unexpectedly missing from KeyVault response for secret with name secret-name", exception.getMessage());
assertEquals("No secret found with name cosmos-primary-key", exception.getMessage());
}
@Test
......
......@@ -121,22 +121,6 @@ public class ServiceAccountClientImplTest {
Assert.assertEquals(validToken, returnedToken);
}
@Test
public void should_throw500ForInvalidAccessTokenResponse_getIdToken() {
try {
// Act
this.sut.getIdToken(tenantName);
// Assert
fail("Should throw exception");
} catch (AppException e) {
Assert.assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getError().getCode());
Assert.assertEquals("Error generating token", e.getError().getMessage());
} catch (Exception e) {
fail("Should not throw this exception" + e.getMessage());
}
}
@Test
public void should_return403GivenInvalidApplicationProperties_getAccessToken() {
when(appProperties.getAuthURL()).thenReturn("https://login.microsoftonline.com/s/oauth2/token/");
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment