Skip to content

Commit ceabd4a

Browse files
author
Shun Fan
committed
Add comments to cloudstorage
Add comments to datastore, and encrypt user ip Add comments to cloudsql, and encrpyt user ip
1 parent cc8f13a commit ceabd4a

File tree

5 files changed

+42
-6
lines changed

5 files changed

+42
-6
lines changed

managed_vms/cloudsql/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
<version>5.1.38</version>
2222
</dependency>
2323
<!-- [END dependencies] -->
24+
<dependency>
25+
<groupId>org.jasypt</groupId>
26+
<artifactId>jasypt</artifactId>
27+
<version>1.9.2</version>
28+
</dependency>
2429
</dependencies>
2530
<build>
2631
<!-- for hot reload of the web application -->

managed_vms/cloudsql/src/main/java/com/example/managedvms/cloudsql/CloudSqlServlet.java

+11-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.example.managedvms.cloudsql;
1818

19+
import org.jasypt.util.text.BasicTextEncryptor;
20+
1921
import java.io.IOException;
2022
import java.io.PrintWriter;
2123
import java.sql.Connection;
@@ -40,27 +42,33 @@ public class CloudSqlServlet extends HttpServlet {
4042
@Override
4143
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException,
4244
ServletException {
45+
// encrypt user ip using PBEWithMD5AndDES
46+
BasicTextEncryptor encryptor = new BasicTextEncryptor();
47+
encryptor.setPassword(Double.toString(10000*Math.random()));
48+
String userIp = encryptor.encrypt(req.getRemoteAddr());
49+
4350
final String createTableSql = "CREATE TABLE IF NOT EXISTS visits ( visit_id INT NOT NULL "
4451
+ "AUTO_INCREMENT, user_ip VARCHAR(46) NOT NULL, timestamp DATETIME NOT NULL, "
4552
+ "PRIMARY KEY (visit_id) )";
4653
final String createVisitSql = "INSERT INTO visits (user_ip, timestamp) VALUES (?, ?)";
4754
final String selectSql = "SELECT user_ip, timestamp FROM visits ORDER BY timestamp DESC "
4855
+ "LIMIT 10";
56+
4957
PrintWriter out = resp.getWriter();
5058
resp.setContentType("text/plain");
5159
String url = System.getenv("SQL_DATABASE_URL");
5260
try (Connection conn = DriverManager.getConnection(url);
5361
PreparedStatement statementCreateVisit = conn.prepareStatement(createVisitSql)) {
5462
conn.createStatement().executeUpdate(createTableSql);
55-
statementCreateVisit.setString(1, req.getRemoteAddr());
63+
statementCreateVisit.setString(1, userIp);
5664
statementCreateVisit.setTimestamp(2, new Timestamp(new Date().getTime()));
5765
statementCreateVisit.executeUpdate();
5866
try (ResultSet rs = conn.prepareStatement(selectSql).executeQuery()) {
5967
out.print("Last 10 visits:\n");
6068
while (rs.next()) {
61-
String userIp = rs.getString("user_ip");
69+
String savedIp = rs.getString("user_ip");
6270
String timeStamp = rs.getString("timestamp");
63-
out.print("Time: " + timeStamp + " Addr: " + userIp + "\n");
71+
out.print("Time: " + timeStamp + " Addr: " + savedIp + "\n");
6472
}
6573
}
6674
} catch (SQLException e) {

managed_vms/cloudstorage/src/main/java/com/example/managedvms/cloudstorage/UploadServlet.java

+4
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOEx
5252
ServletException {
5353
final Part filePart = req.getPart("file");
5454
final String fileName = filePart.getSubmittedFileName();
55+
56+
// Modify access list to allow all users with link to read file
5557
List<Acl> acls = new ArrayList<>();
5658
acls.add(Acl.of(Acl.User.ofAllUsers(), Acl.Role.READER));
5759
// the inputstream is closed by default, so we don't need to close it here
@@ -60,6 +62,8 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOEx
6062
BlobInfo.builder(BUCKET_NAME, fileName).acl(acls).build(),
6163
filePart.getInputStream());
6264
blobInfo = storage.get(BUCKET_NAME, fileName);
65+
66+
// return the public download link
6367
resp.getWriter().print(blobInfo.mediaLink());
6468
}
6569
}

managed_vms/datastore/pom.xml

+11-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,18 @@
1414
<type>jar</type>
1515
<scope>provided</scope>
1616
</dependency>
17-
<!-- [START dependencies] -->
17+
<!-- [START dependencies] -->
1818
<dependency>
1919
<groupId>com.google.gcloud</groupId>
2020
<artifactId>gcloud-java-datastore</artifactId>
2121
<version>0.1.3</version>
2222
</dependency>
2323
<!-- [END dependencies] -->
24+
<dependency>
25+
<groupId>org.jasypt</groupId>
26+
<artifactId>jasypt</artifactId>
27+
<version>1.9.2</version>
28+
</dependency>
2429
</dependencies>
2530
<build>
2631
<!-- for hot reload of the web application -->
@@ -62,7 +67,11 @@
6267
<failsOnError>true</failsOnError>
6368
</configuration>
6469
<executions>
65-
<execution><goals><goal>check</goal></goals></execution>
70+
<execution>
71+
<goals>
72+
<goal>check</goal>
73+
</goals>
74+
</execution>
6675
</executions>
6776
</plugin>
6877
<plugin>

managed_vms/datastore/src/main/java/com/example/managedvms/datastore/DatastoreServlet.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import com.google.gcloud.datastore.QueryResults;
2828
import com.google.gcloud.datastore.StructuredQuery;
2929

30+
import org.jasypt.util.text.BasicTextEncryptor;
31+
3032
import java.io.IOException;
3133
import java.io.PrintWriter;
3234

@@ -44,17 +46,25 @@ public class DatastoreServlet extends HttpServlet {
4446
@Override
4547
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException,
4648
ServletException {
49+
// encrypt user ip using PBEWithMD5AndDES
50+
BasicTextEncryptor encryptor = new BasicTextEncryptor();
51+
encryptor.setPassword(Double.toString(10000*Math.random()));
52+
String userIp = encryptor.encrypt(req.getRemoteAddr());
53+
4754
Datastore datastore = DatastoreOptions.defaultInstance().service();
4855
KeyFactory keyFactory = datastore.newKeyFactory().kind("visit");
4956
IncompleteKey key = keyFactory.kind("visit").newKey();
57+
5058
// Record a visit to the datastore, storing the IP and timestamp.
5159
FullEntity<IncompleteKey> curVisit = FullEntity.builder(key)
52-
.set("user_ip", req.getRemoteAddr()).set("timestamp", DateTime.now()).build();
60+
.set("user_ip", userIp).set("timestamp", DateTime.now()).build();
5361
datastore.add(curVisit);
62+
5463
// Retrieve the last 10 visits from the datastore, ordered by timestamp.
5564
Query<Entity> query = Query.entityQueryBuilder().kind("visit")
5665
.orderBy(StructuredQuery.OrderBy.desc("timestamp")).limit(10).build();
5766
QueryResults<Entity> results = datastore.run(query);
67+
5868
resp.setContentType("text/plain");
5969
PrintWriter out = resp.getWriter();
6070
out.print("Last 10 visits:\n");

0 commit comments

Comments
 (0)