Skip to content

Commit 8c778c7

Browse files
author
Shun Fan
committed
Add comments to cloudstorage
Add comments to datastore, and truncate user ip Add comments to cloudsql, and truncate user ip
1 parent cc8f13a commit 8c778c7

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
import java.io.IOException;
2020
import java.io.PrintWriter;
21+
import java.net.Inet4Address;
22+
import java.net.Inet6Address;
23+
import java.net.InetAddress;
2124
import java.sql.Connection;
2225
import java.sql.DriverManager;
2326
import java.sql.PreparedStatement;
@@ -40,6 +43,15 @@ public class CloudSqlServlet extends HttpServlet {
4043
@Override
4144
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException,
4245
ServletException {
46+
// store only the first two octets of a users ip address
47+
String userIp = req.getRemoteAddr();
48+
InetAddress address = InetAddress.getByName(userIp);
49+
if (address instanceof Inet6Address) {
50+
userIp = userIp.substring(0, userIp.indexOf(":", 2)) + ":*:*:*:*:*:*";
51+
} else if (address instanceof Inet4Address) {
52+
userIp = userIp.substring(0, userIp.indexOf(".", 2)) + ".*.*";
53+
}
54+
4355
final String createTableSql = "CREATE TABLE IF NOT EXISTS visits ( visit_id INT NOT NULL "
4456
+ "AUTO_INCREMENT, user_ip VARCHAR(46) NOT NULL, timestamp DATETIME NOT NULL, "
4557
+ "PRIMARY KEY (visit_id) )";
@@ -49,18 +61,20 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOExc
4961
PrintWriter out = resp.getWriter();
5062
resp.setContentType("text/plain");
5163
String url = System.getenv("SQL_DATABASE_URL");
64+
5265
try (Connection conn = DriverManager.getConnection(url);
5366
PreparedStatement statementCreateVisit = conn.prepareStatement(createVisitSql)) {
5467
conn.createStatement().executeUpdate(createTableSql);
55-
statementCreateVisit.setString(1, req.getRemoteAddr());
68+
statementCreateVisit.setString(1, userIp);
5669
statementCreateVisit.setTimestamp(2, new Timestamp(new Date().getTime()));
5770
statementCreateVisit.executeUpdate();
71+
5872
try (ResultSet rs = conn.prepareStatement(selectSql).executeQuery()) {
5973
out.print("Last 10 visits:\n");
6074
while (rs.next()) {
61-
String userIp = rs.getString("user_ip");
75+
String savedIp = rs.getString("user_ip");
6276
String timeStamp = rs.getString("timestamp");
63-
out.print("Time: " + timeStamp + " Addr: " + userIp + "\n");
77+
out.print("Time: " + timeStamp + " Addr: " + savedIp + "\n");
6478
}
6579
}
6680
} catch (SQLException e) {

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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
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>
@@ -62,7 +62,11 @@
6262
<failsOnError>true</failsOnError>
6363
</configuration>
6464
<executions>
65-
<execution><goals><goal>check</goal></goals></execution>
65+
<execution>
66+
<goals>
67+
<goal>check</goal>
68+
</goals>
69+
</execution>
6670
</executions>
6771
</plugin>
6872
<plugin>

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929

3030
import java.io.IOException;
3131
import java.io.PrintWriter;
32+
import java.net.Inet4Address;
33+
import java.net.Inet6Address;
34+
import java.net.InetAddress;
3235

3336
import javax.servlet.ServletException;
3437
import javax.servlet.annotation.WebServlet;
@@ -44,17 +47,29 @@ public class DatastoreServlet extends HttpServlet {
4447
@Override
4548
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException,
4649
ServletException {
50+
// store only the first two octets of a users ip address
51+
String userIp = req.getRemoteAddr();
52+
InetAddress address = InetAddress.getByName(userIp);
53+
if (address instanceof Inet6Address) {
54+
userIp = userIp.substring(0, userIp.indexOf(":", 2)) + ":*:*:*:*:*:*";
55+
} else if (address instanceof Inet4Address) {
56+
userIp = userIp.substring(0, userIp.indexOf(".", 2)) + ".*.*";
57+
}
58+
4759
Datastore datastore = DatastoreOptions.defaultInstance().service();
4860
KeyFactory keyFactory = datastore.newKeyFactory().kind("visit");
4961
IncompleteKey key = keyFactory.kind("visit").newKey();
62+
5063
// Record a visit to the datastore, storing the IP and timestamp.
5164
FullEntity<IncompleteKey> curVisit = FullEntity.builder(key)
52-
.set("user_ip", req.getRemoteAddr()).set("timestamp", DateTime.now()).build();
65+
.set("user_ip", userIp).set("timestamp", DateTime.now()).build();
5366
datastore.add(curVisit);
67+
5468
// Retrieve the last 10 visits from the datastore, ordered by timestamp.
5569
Query<Entity> query = Query.entityQueryBuilder().kind("visit")
5670
.orderBy(StructuredQuery.OrderBy.desc("timestamp")).limit(10).build();
5771
QueryResults<Entity> results = datastore.run(query);
72+
5873
resp.setContentType("text/plain");
5974
PrintWriter out = resp.getWriter();
6075
out.print("Last 10 visits:\n");

0 commit comments

Comments
 (0)