Skip to content

Commit

Permalink
Merge pull request #26 from transcom/B-20564-MalformedData-Notification
Browse files Browse the repository at this point in the history
B-20564: Malformed Data Notification
  • Loading branch information
TevinAdams authored Sep 20, 2024
2 parents 61a78f5 + 7b0cf6e commit efcdd6c
Show file tree
Hide file tree
Showing 13 changed files with 279 additions and 293 deletions.
2 changes: 0 additions & 2 deletions .envrc
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ require TRDM_LAMBDA_MILMOVE_KEYPAIR_KEY "See 'DISABLE_AWS_VAULT_WRAPPER=1 AWS_RE
require TRDM_LAMBDA_MILMOVE_KEYPAIR_ALIAS "See 'DISABLE_AWS_VAULT_WRAPPER=1 AWS_REGION=us-gov-west-1 aws-vault exec transcom-gov-dev -- chamber read trdm-lambda-dev trdm_lambda_milmove_keypair_alias'"
require TRDM_LAMBDA_MILMOVE_KEYPAIR_TYPE "See 'DISABLE_AWS_VAULT_WRAPPER=1 AWS_REGION=us-gov-west-1 aws-vault exec transcom-gov-dev -- chamber read trdm-lambda-dev trdm_lambda_milmove_keypair_type'"
require TRDM_LAMBDA_MILMOVE_KEYPAIR_FILEPATH "See 'DISABLE_AWS_VAULT_WRAPPER=1 AWS_REGION=us-gov-west-1 aws-vault exec transcom-gov-dev -- chamber read trdm-lambda-dev trdm_lambda_milmove_keypair_filepath'"
require SES_SENDER_EMAIL "See 'DISABLE_AWS_VAULT_WRAPPER=1 AWS_REGION=us-gov-west-1 aws-vault exec transcom-gov-dev -- chamber read trdm-lambda-dev ses_sender_email'"
require SES_RECIPIENT "See 'DISABLE_AWS_VAULT_WRAPPER=1 AWS_REGION=us-gov-west-1 aws-vault exec transcom-gov-dev -- chamber read trdm-lambda-dev ses_recipient'"

##############################################
# Load Local Overrides and Check Environment #
Expand Down
29 changes: 20 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,23 @@
</parent>
<groupId>com.milmove.trdmlambda</groupId>
<artifactId>trdm-lambda</artifactId>
<version>1.0.3.15</version>
<version>1.0.3.16</version>
<name>trdm java spring interface</name>
<description>Project for deploying a Java TRDM interfacer for TGET data.</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.21.40</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
Expand All @@ -26,12 +37,6 @@
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>rds</artifactId>
<version>2.21.40</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>ses</artifactId>
<version>2.26.20</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down Expand Up @@ -95,12 +100,18 @@
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>aws-core</artifactId>
<version>2.21.40</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>ssm</artifactId>
<version>2.21.40</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>sns</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
</dependency>
</dependencies>

Expand Down
136 changes: 0 additions & 136 deletions src/main/java/com/milmove/trdmlambda/milmove/service/EmailService.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.milmove.trdmlambda.milmove.service;

import java.io.IOException;
import java.net.URISyntaxException;

import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import ch.qos.logback.classic.Logger;
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.core.ResponseInputStream;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
import software.amazon.awssdk.services.s3.model.S3Exception;

@Component
public class S3Service {

private Logger logger = (Logger) LoggerFactory.getLogger(S3Service.class);

private S3Client s3Client;
private final String trdmBucketName = "transcom-gov-milmove-stg-lambda-trdm-soap-us-gov-west-1";
private final String trdmTruststoreKeyName = "trdm_lambda_milmove_truststore_base64.txt";

public S3Service() throws URISyntaxException {
logger.info("S3Service::S3Service - starting initialization of S3 Service");

this.s3Client = S3Client.builder()
.region(Region.US_GOV_WEST_1)
.credentialsProvider(DefaultCredentialsProvider.create())
.build();

logger.info("S3Service::S3Service - finished initialization of S3 Service");
}

public String getTRDMTruststore() {
logger.info("S3Service::getTRDMTruststore - getting TRDM Truststore base64 string from S3");
String trdmTruststoreBase64String = getS3Object(trdmBucketName, trdmTruststoreKeyName);
logger.info("S3Service::getTRDMTruststore - returning TRDM Truststore string");
return trdmTruststoreBase64String;
}

private String getS3Object(String bucketName, String keyName) {
try {
GetObjectRequest objectRequest = GetObjectRequest.builder()
.key(keyName)
.bucket(bucketName)
.build();

ResponseInputStream<GetObjectResponse> object = this.s3Client.getObject(objectRequest);
String trdmTruststoreBase64 = new String(object.readAllBytes()).trim();

logger.info("successfully obtained object from bucket " + bucketName
+ " S3 object " + keyName);

object.close();
return trdmTruststoreBase64;
} catch (S3Exception ex) {
logger.error("S3Service::getS3ObjectBytes - error with S3 client" + ex);
} catch (IOException ex) {
logger.error("S3Service::getS3ObjectBytes - IO error with S3 client" + ex);
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.milmove.trdmlambda.milmove.service;

import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import ch.qos.logback.classic.Logger;
import jakarta.mail.MessagingException;
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sns.SnsClient;
import software.amazon.awssdk.services.sns.model.PublishRequest;
import software.amazon.awssdk.services.sns.model.PublishResponse;
import software.amazon.awssdk.services.sns.model.SnsException;
import java.net.URISyntaxException;
import java.util.ArrayList;

@Service
public class SNSService {

private Logger logger = (Logger) LoggerFactory.getLogger(SNSService.class);
private final String engGovTopicARN = "arn:aws-us-gov:sns:us-gov-west-1:015932076428:eng-gov-notification";

public SNSService() throws URISyntaxException {
logger.info("starting initialization of SNS Service");
logger.info("finished initializing SNS Service");
}

public void sendMalformedData(ArrayList<String> sysIds, String msgType)
throws MessagingException, URISyntaxException {

String msg = "";
String sysIdsListed = "";
String tacMsg = "Malformed TAC data has been detected when ingesting TGET Data. \n";
String tacSysIdsListHeader = "The following tacSysIds had malformed rows: \n";
String loaMsg = "Malformed LOA data has been detected when ingesting TGET Data. \n";
String loaSysIdsListHeader = "The following loaSysIds had malformed rows: \n";

if (msgType == "TAC") {
msg += tacMsg + tacSysIdsListHeader;
} else if (msgType == "LOA") {
msg += loaMsg + loaSysIdsListHeader;
}

for (String sysId : sysIds) {
sysIdsListed += sysId + "\n";
}

msg += sysIdsListed;

logger.info("sending malformed " + msgType + " SNS");
send(engGovTopicARN, msg);
logger.info("finished sending malformed " + msgType + " SNS");

}

private void send(String topicArn, String msg) throws MessagingException, URISyntaxException {
logger.info("sending SNS message");
try {
PublishRequest request = PublishRequest.builder()
.message(msg)
.topicArn(topicArn)
.build();

SnsClient snsClient = SnsClient.builder()
.region(Region.US_GOV_WEST_1)
.credentialsProvider(DefaultCredentialsProvider.create())
.build();

PublishResponse result = snsClient.publish(request);
logger.info("message successfully sent. With status code: "
+ result.sdkHttpResponse().statusCode() + " and Message Id: "
+ result.messageId());

snsClient.close();
logger.info("finished sending SNS message");
} catch (SnsException e) {
logger.error("SnsException executing sns notification cron job", e);
} catch (SdkClientException sdke) {
logger.error("SdkClientException: " + sdke.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.milmove.trdmlambda.milmove.service.S3Service;

import ch.qos.logback.classic.Logger;

@Component
public class DecodeTruststore {
private Logger logger = (Logger) LoggerFactory.getLogger(DecodeTruststore.class);

private String base64Content;
private String filepath;
private String password;

public DecodeTruststore(SecretFetcher secretFetcher) {
this.base64Content = secretFetcher.getSecret("trdm_lambda_milmove_truststore_base64");
public DecodeTruststore(SecretFetcher secretFetcher, S3Service s3Service) {
this.filepath = secretFetcher.getSecret("trdm_lambda_milmove_truststore_filepath");
this.password = secretFetcher.getSecret("trdm_lambda_milmove_truststore_password");

Expand All @@ -33,7 +33,8 @@ public DecodeTruststore(SecretFetcher secretFetcher) {
}

try {
byte[] decodedBytes = Base64.getDecoder().decode(base64Content);
String trdmTruststoreContent = s3Service.getTRDMTruststore();
byte[] decodedBytes = Base64.getDecoder().decode(trdmTruststoreContent);
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.write(decodedBytes);
}
Expand Down
Loading

0 comments on commit efcdd6c

Please sign in to comment.