From 71600b76dfa302aa959e27852802f216511490c3 Mon Sep 17 00:00:00 2001 From: komakkar <komakkar@microsoft.com> Date: Fri, 4 Sep 2020 01:42:57 +0530 Subject: [PATCH] Added logs --- provider/notification-azure/pom.xml | 4 ---- .../azure/pubsub/EventGridHandshakeHandler.java | 5 +++++ .../pubsub/EventGridRequestBodyExtractor.java | 15 ++++++++++----- .../provider/azure/util/AppProperties.java | 6 ++++++ .../azure/util/AzureCosmosProperties.java | 7 +++++++ .../util/AzureServiceAccountValidatorImpl.java | 3 --- .../azure/util/GoogleServiceAccountImpl.java | 7 +++++++ .../util/ServiceAccountJwtAzureClientImpl.java | 16 +++++++++++----- 8 files changed, 46 insertions(+), 17 deletions(-) diff --git a/provider/notification-azure/pom.xml b/provider/notification-azure/pom.xml index 6051a4d29..a6ec6e277 100644 --- a/provider/notification-azure/pom.xml +++ b/provider/notification-azure/pom.xml @@ -65,10 +65,6 @@ <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-to-slf4j</artifactId> </exclusion> - <exclusion> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter-logging</artifactId> - </exclusion> </exclusions> </dependency> <dependency> diff --git a/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/pubsub/EventGridHandshakeHandler.java b/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/pubsub/EventGridHandshakeHandler.java index b3b2a861f..83ebf3dba 100644 --- a/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/pubsub/EventGridHandshakeHandler.java +++ b/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/pubsub/EventGridHandshakeHandler.java @@ -15,6 +15,7 @@ package org.opengroup.osdu.notification.provider.azure.pubsub; import com.google.gson.JsonObject; +import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; import org.opengroup.osdu.core.common.model.http.AppException; import org.opengroup.osdu.notification.provider.interfaces.IPubsubHandshakeHandler; import org.springframework.beans.factory.annotation.Autowired; @@ -27,6 +28,9 @@ public class EventGridHandshakeHandler implements IPubsubHandshakeHandler { @Autowired private EventGridRequestBodyExtractor eventGridRequestBodyExtractor; + @Autowired + JaxRsDpsLog logger; + /** * Extract Handshake response string form Handshake request. * TODO: Check if there is a need to verify subscription name with @@ -44,6 +48,7 @@ public class EventGridHandshakeHandler implements IPubsubHandshakeHandler { 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); } diff --git a/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/pubsub/EventGridRequestBodyExtractor.java b/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/pubsub/EventGridRequestBodyExtractor.java index 0d1a40336..c5fcedb7f 100644 --- a/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/pubsub/EventGridRequestBodyExtractor.java +++ b/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/pubsub/EventGridRequestBodyExtractor.java @@ -49,11 +49,11 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto private static final Gson GSON = new Gson(); private static final ObjectMapper objectMapper = new ObjectMapper(); - private JsonObject root = null; - private HttpServletRequest httpServletRequest; - private JaxRsDpsLog log; + private final JsonObject root = null; + private final HttpServletRequest httpServletRequest; + private final JaxRsDpsLog logger; - private NotificationRequest notificationRequest; + private final NotificationRequest notificationRequest; private NotificationData notificationData; private HandshakeRequestData handshakeRequestData; private boolean isHandshakeRequest; @@ -61,7 +61,7 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto @Autowired public EventGridRequestBodyExtractor(HttpServletRequest httpServletRequest, JaxRsDpsLog log) { this.httpServletRequest = httpServletRequest; - this.log = log; + this.logger = log; this.notificationRequest = extractNotificationRequestFromHttpRequest(); } @@ -73,6 +73,7 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto */ public Map<String, String> extractAttributesFromRequestBody() { if(isHandshakeRequest) { + logger.error("Invalid Event Grid Message. Is a handshake request"); return null; } return this.notificationData.getAttributes(); @@ -86,6 +87,7 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto */ public String extractDataFromRequestBody() { if(isHandshakeRequest) { + logger.error("Invalid Event Grid Message. Is a handshake request"); return null; } return new String(Base64.getDecoder().decode(notificationData.getData())); @@ -100,6 +102,7 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto public String extractNotificationIdFromRequestBody() { String subscriptionId = httpServletRequest.getHeader(SUBSCRIPTION_ID); if (Strings.isNullOrEmpty(subscriptionId)) { + logger.error("Invalid Event Grid Message. Subscription Id is null or empty"); throw new AppException(HttpStatus.BAD_REQUEST.value(), "Invalid Event Grid Message", "Subscription ID not found"); } return subscriptionId; @@ -123,6 +126,7 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto */ public String getValidationCodeForHandshake() { if(!isHandshakeRequest) { + logger.error("Invalid Event Grid Message. Is not a handshake request"); return null; } return this.handshakeRequestData.getValidationCode(); @@ -154,6 +158,7 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto extractNotificationData(notificationRequest); } } catch (Exception e) { + logger.error("Invalid Event Grid Message. %s", e.getMessage()); throw new AppException(HttpStatus.BAD_REQUEST.value(), "Request payload parsing error", "Unable to parse request payload.", "Request contents are null or empty"); } 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 59736e877..32f048a46 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 @@ -17,6 +17,7 @@ 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.opengroup.osdu.notification.provider.interfaces.IAppProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -49,6 +50,9 @@ public class AppProperties implements IAppProperties { @Autowired private SecretClient secretClient; + @Autowired + private JaxRsDpsLog logger; + private String authURL; private String authClientID; @@ -102,11 +106,13 @@ public class AppProperties implements IAppProperties { 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)); 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)); } diff --git a/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/AzureCosmosProperties.java b/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/AzureCosmosProperties.java index 22bb7ee41..81bca28a6 100644 --- a/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/AzureCosmosProperties.java +++ b/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/AzureCosmosProperties.java @@ -2,6 +2,8 @@ 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.stereotype.Component; @@ -17,6 +19,9 @@ public class AzureCosmosProperties { @Value("${azure.cosmosdb.database}") private String cosmosDBName; + @Autowired + private JaxRsDpsLog logger; + // TODO : Move away from Named beans. @Bean @Named("COSMOS_ENDPOINT") @@ -45,11 +50,13 @@ public class AzureCosmosProperties { public 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)); } 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 dc1a34518..e68b499c7 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,8 @@ package org.opengroup.osdu.notification.provider.azure.util; -import com.auth0.jwt.JWT; -import com.auth0.jwt.interfaces.DecodedJWT; import org.opengroup.osdu.notification.provider.interfaces.IServiceAccountValidator; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; @Service 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 9b0adf6c0..dc31ed83e 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,9 @@ 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; @@ -23,10 +25,15 @@ 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(); } } 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 5d6d99334..391637b2a 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 @@ -19,6 +19,7 @@ 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; @@ -43,10 +44,13 @@ public class ServiceAccountJwtAzureClientImpl implements IServiceAccountJwtClien @Autowired private IJwtCache tenantJwtCache; + @Autowired + JaxRsDpsLog logger; + public String getIdToken(String tenantName) { - // TODO : Add logs. TenantInfo tenant = 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"); } @@ -67,10 +71,12 @@ 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 e) { - throw e; - } catch (Exception e) { - throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Persistence error", "Error generating token", e); + } 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 { if(service != null) { service.shutdown(); -- GitLab