Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions Sample4.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;

import javax.servlet.http.HttpServletRequest;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Sample4 {

public String accessKeyId="ADSFASDFESDFEFEDFECF";
public String secretAccessKey="asdfadf34ffsdfds4SDDSF4sdfsdf34df356DFDFSDFSFassdfdsf";

public boolean getS3BucketExists() {
AmazonS3 s3client = getAmazonS3Client();
return s3client.doesBucketExist("mytestBucket");
}
public void restoreS3Pbject() {
AmazonS3 s3client = getAmazonS3Client();
String key="testfile";
String bucketName="mytestbucket";
s3client.restoreObject( bucketName, key, 20);
}

private AmazonS3 getAmazonS3Client() {
AWSCredentials awsCredentials = new BasicAWSCredentials(accessKeyId, secretAccessKey);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommendation generated by Amazon CodeGuru Reviewer. Leave feedback on this recommendation by replying to the comment or by reacting to the comment using emoji.

If possible, use temporary security credentials (IAM roles) instead of long-term access keys.
Long-term access keys, such as those associated with IAM users and AWS account root users, remain valid until you manually revoke them. However, temporary security credentials obtained through IAM roles and other features of the AWS Security Token Service expire after a short period of time. Use temporary security credentials to help reduce your risk in case credentials are accidentally exposed.
Learn more about best practices for managing AWS access keys.

return AmazonS3ClientBuilder
.standard()
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
.withRegion(Regions.US_EAST_1)
.build();
}

public Map<String, String> sqlInjection(DataSource ds, HttpServletRequest request) {
Map<String, String> nameValueMAp = new HashMap<String, String>();
try {
Connection connection = ds.getConnection();
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommendation generated by Amazon CodeGuru Reviewer. Leave feedback on this recommendation by replying to the comment or by reacting to the comment using emoji.

Problem
This line of code might contain a resource leak. Resource leaks can cause your system to slow down or crash.

Fix
Consider closing the following resource: connection. Currently, there are execution paths that do not contain closure statements. Either a) close connection in a try-finally block or b) close the resource by declaring connection in a try-with-resources block.

More info
View resource management guidelines at oracle.com (external link).

String sql="select name, title from employee where empId="+request.getParameter("empID");
ResultSet rs= connection.createStatement().executeQuery(sql);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommendation generated by Amazon CodeGuru Reviewer. Leave feedback on this recommendation by replying to the comment or by reacting to the comment using emoji.

Problem
This line of code might contain a resource leak. Resource leaks can cause your system to slow down or crash.

Fix
Consider closing the following resource: rs. Currently, there are execution paths that do not contain closure statements. Either a) close rs in a try-finally block or b) close the resource by declaring rs in a try-with-resources block.

More info
View resource management guidelines at oracle.com (external link).

while (rs.next()) {
String name = rs.getString("name");
String value = rs.getString("value");
nameValueMAp.put(name,value);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return nameValueMAp;
}
class Customer{
public String customerName;
public String customerAddress;
public String ssn;
public String phoneNumber;
public String password;

}
public List<Customer> piiData(DataSource ds) {
List<Customer> customers = new ArrayList<Customer>();
try {
Connection connection = ds.getConnection();
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommendation generated by Amazon CodeGuru Reviewer. Leave feedback on this recommendation by replying to the comment or by reacting to the comment using emoji.

Problem
This line of code might contain a resource leak. Resource leaks can cause your system to slow down or crash.

Fix
Consider closing the following resource: connection. Currently, there are execution paths that do not contain closure statements. Either a) close connection in a try-finally block or b) close the resource by declaring connection in a try-with-resources block.

More info
View resource management guidelines at oracle.com (external link).

String sql="select name, address, ssn, phoneNumber, password from customer";
ResultSet rs= connection.createStatement().executeQuery(sql);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommendation generated by Amazon CodeGuru Reviewer. Leave feedback on this recommendation by replying to the comment or by reacting to the comment using emoji.

Problem
This line of code might contain a resource leak. Resource leaks can cause your system to slow down or crash.

Fix
Consider closing the following resource: rs. Currently, there are execution paths that do not contain closure statements. Either a) close rs in a try-finally block or b) close the resource by declaring rs in a try-with-resources block.

More info
View resource management guidelines at oracle.com (external link).

while (rs.next()) {
Customer customer = new Customer();
customer.customerName = rs.getString("name");
customer.customerAddress = rs.getString("value");
customer.ssn = rs.getString("ssn");
customer.phoneNumber = rs.getString("phoneNumber");
customer.password = rs.getString("password");
customers.add(customer);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return customers;
}
}