Skip to content

Commit

Permalink
Java V2 Updated mysql item tracker to use Secrets Manager (awsdocs#4904)
Browse files Browse the repository at this point in the history
  • Loading branch information
scmacdon authored Jun 7, 2023
1 parent 0b635c6 commit f35a0a2
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 104 deletions.
172 changes: 126 additions & 46 deletions javav2/usecases/Creating_rds_item_tracker/Readme.md

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 14 additions & 36 deletions javav2/usecases/Creating_rds_item_tracker/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<version>2.6.4</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<java.version>17</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.17.146</version>
<version>2.20.45</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand All @@ -39,6 +39,15 @@
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>secretsmanager</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand Down Expand Up @@ -84,11 +93,6 @@
<artifactId>jakarta.mail</artifactId>
<version>1.6.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
Expand All @@ -97,7 +101,7 @@
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand All @@ -117,32 +121,6 @@
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<configLocation>checkstyle.xml</configLocation>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<linkXRef>false</linkXRef>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
</configuration>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,13 @@

public class ConnectionHelper {

private static ConnectionHelper instance;
private String url;

private ConnectionHelper() {
url = "jdbc:mysql://<Enter URL>:3306/mydb?useSSL=false";
}

public static Connection getConnection() throws SQLException {
if (instance == null) {
instance = new ConnectionHelper();
}
public static Connection getConnection(String host, String user, String password) throws SQLException {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
return DriverManager.getConnection(instance.url, "root","root1234");
Class.forName("com.mysql.cj.jdbc.Driver");
String url2 = "jdbc:mysql://"+host+":3306/mydb?useSSL=false";
return DriverManager.getConnection(url2, user, password);

} catch (SQLException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
} catch (SQLException | ClassNotFoundException e) {
e.getStackTrace();
}
return null;
Expand All @@ -42,4 +33,4 @@ public static void close(Connection connection) {
e.printStackTrace();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@

package com.aws.rest;

import com.google.gson.Gson;
import org.springframework.stereotype.Component;
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient;
import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueRequest;
import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueResponse;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
Expand All @@ -22,13 +28,38 @@
@Component
public class DatabaseService {

private SecretsManagerClient getSecretClient() {
Region region = Region.US_WEST_2;
return SecretsManagerClient.builder()
.region(region)
.credentialsProvider(ProfileCredentialsProvider.create())
.build();
}

private String getSecretValues() {
// Get the Amazon RDS creds from Secrets Manager.
SecretsManagerClient secretClient = getSecretClient();
String secretName = "itemtracker/mysql";

GetSecretValueRequest valueRequest = GetSecretValueRequest.builder()
.secretId(secretName)
.build();

GetSecretValueResponse valueResponse = secretClient.getSecretValue(valueRequest);
return valueResponse.secretString();
}

// Set the specified item to archive.
public void flipItemArchive(String id) {
Connection c = null;
String query;
// Get the Amazon RDS credentials from AWS Secrets Manager.
Gson gson = new Gson();
User user = gson.fromJson(String.valueOf(getSecretValues()), User.class);
try {
c = ConnectionHelper.getConnection();
c = ConnectionHelper.getConnection(user.getHost(), user.getUsername(), user.getPassword());
query = "update work set archive = ? where idwork ='" +id + "' ";
assert c != null;
PreparedStatement updateForm = c.prepareStatement(query);
updateForm.setBoolean(1, true);
updateForm.execute();
Expand All @@ -48,29 +79,35 @@ public List<WorkItem> getItemsDataSQLReport(int flag) {
String username = "user";
WorkItem item;

// Get the Amazon RDS credentials from AWS Secrets Manager.
Gson gson = new Gson();
User user = gson.fromJson(String.valueOf(getSecretValues()), User.class);
try {
c = ConnectionHelper.getConnection();
c = ConnectionHelper.getConnection(user.getHost(), user.getUsername(), user.getPassword());
ResultSet rs = null;
PreparedStatement pstmt = null;
if (flag == 0) {
// Retrieves active data from the MySQL database
int arch = 0;
query = "Select idwork,username,date,description,guide,status,archive FROM work where username=? and archive=?;";
assert c != null;
pstmt = c.prepareStatement(query);
pstmt.setString(1, username);
pstmt.setInt(2, arch);
rs = pstmt.executeQuery();
}else if (flag == 1) {
// Retrieves archive data from the MySQL database
int arch = 1;
query = "Select idwork,username,date,description,guide,status, archive FROM work where username=? and archive=?;";
query = "Select idwork,username,date,description,guide,status,archive FROM work where username=? and archive=?;";
assert c != null;
pstmt = c.prepareStatement(query);
pstmt.setString(1, username);
pstmt.setInt(2, arch);
rs = pstmt.executeQuery();
} else {
// Retrieves all data from the MySQL database
query = "Select idwork,username,date,description,guide,status, archive FROM work";
assert c != null;
pstmt = c.prepareStatement(query);
rs = pstmt.executeQuery();
}
Expand Down Expand Up @@ -101,8 +138,11 @@ public List<WorkItem> getItemsDataSQLReport(int flag) {
// Inject a new submission.
public void injestNewSubmission(WorkItem item) {
Connection c = null;
// Get the Amazon RDS credentials from AWS Secrets Manager.
Gson gson = new Gson();
User user = gson.fromJson(String.valueOf(getSecretValues()), User.class);
try {
c = ConnectionHelper.getConnection();
c = ConnectionHelper.getConnection(user.getHost(), user.getUsername(), user.getPassword());
PreparedStatement ps;

// Convert rev to int.
Expand All @@ -124,6 +164,7 @@ public void injestNewSubmission(WorkItem item) {

// Inject an item into the system.
String insert = "INSERT INTO work (idwork, username,date,description, guide, status, archive) VALUES(?,?, ?,?,?,?,?);";
assert c != null;
ps = c.prepareStatement(insert);
ps.setString(1, workId);
ps.setString(2, name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package com.aws.rest;

import com.google.gson.Gson;
import jxl.write.WriteException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
Expand All @@ -14,6 +15,7 @@
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

@CrossOrigin(origins = "*")
Expand All @@ -38,14 +40,16 @@ public class ReportController {

@PostMapping("")
public String sendReport(@RequestBody Map<String, String> body) {
var list = dbService.getItemsDataSQLReport(0);
List<WorkItem> list = dbService.getItemsDataSQLReport(0);
try {
InputStream is = writeExcel.write(list);
sm.sendReport(is, body.get("email"));
return "Report generated & sent";
Gson gson = new Gson();
return gson.toJson("ok");

} catch (IOException | WriteException e) {
e.printStackTrace();
}
return "Failed to generate report";
return gson.toJson("error");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.aws.rest;

public class User {

private String username;
private String password;

private String host;


//getter
String getUsername(){
return this.username;
}

String getPassword(){
return this.password;
}

String getHost(){
return this.host;
}

}

0 comments on commit f35a0a2

Please sign in to comment.