Skip to content

Add comments to cloudstorage #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions managed_vms/cloudsql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
<version>5.1.38</version>
</dependency>
<!-- [END dependencies] -->
<dependency>
<groupId>org.jasypt</groupId>
<artifactId>jasypt</artifactId>
<version>1.9.2</version>
</dependency>
</dependencies>
<build>
<!-- for hot reload of the web application -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.example.managedvms.cloudsql;

import org.jasypt.util.text.BasicTextEncryptor;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
Expand All @@ -40,12 +42,18 @@ public class CloudSqlServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException,
ServletException {
// encrypt user ip using PBEWithMD5AndDES
BasicTextEncryptor encryptor = new BasicTextEncryptor();
encryptor.setPassword(Double.toString(10000*Math.random()));
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe add a disclaimer comment that Math.random is not a secure way to generate encryption keys / passwords.

String userIp = encryptor.encrypt(req.getRemoteAddr());

final String createTableSql = "CREATE TABLE IF NOT EXISTS visits ( visit_id INT NOT NULL "
+ "AUTO_INCREMENT, user_ip VARCHAR(46) NOT NULL, timestamp DATETIME NOT NULL, "
+ "PRIMARY KEY (visit_id) )";
final String createVisitSql = "INSERT INTO visits (user_ip, timestamp) VALUES (?, ?)";
final String selectSql = "SELECT user_ip, timestamp FROM visits ORDER BY timestamp DESC "
+ "LIMIT 10";

PrintWriter out = resp.getWriter();
resp.setContentType("text/plain");
String url = System.getenv("SQL_DATABASE_URL");
Expand All @@ -58,7 +66,7 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOExc
try (ResultSet rs = conn.prepareStatement(selectSql).executeQuery()) {
out.print("Last 10 visits:\n");
while (rs.next()) {
String userIp = rs.getString("user_ip");
userIp = rs.getString("user_ip");
String timeStamp = rs.getString("timestamp");
out.print("Time: " + timeStamp + " Addr: " + userIp + "\n");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOEx
ServletException {
final Part filePart = req.getPart("file");
final String fileName = filePart.getSubmittedFileName();

// Modify access list to allow all users with link to read file
List<Acl> acls = new ArrayList<>();
acls.add(Acl.of(Acl.User.ofAllUsers(), Acl.Role.READER));
// the inputstream is closed by default, so we don't need to close it here
Expand All @@ -60,6 +62,8 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOEx
BlobInfo.builder(BUCKET_NAME, fileName).acl(acls).build(),
filePart.getInputStream());
blobInfo = storage.get(BUCKET_NAME, fileName);

// return the public download link
resp.getWriter().print(blobInfo.mediaLink());
}
}
Expand Down
13 changes: 11 additions & 2 deletions managed_vms/datastore/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@
<type>jar</type>
<scope>provided</scope>
</dependency>
<!-- [START dependencies] -->
<!-- [START dependencies] -->
<dependency>
<groupId>com.google.gcloud</groupId>
<artifactId>gcloud-java-datastore</artifactId>
<version>0.1.3</version>
</dependency>
<!-- [END dependencies] -->
<dependency>
<groupId>org.jasypt</groupId>
<artifactId>jasypt</artifactId>
<version>1.9.2</version>
</dependency>
</dependencies>
<build>
<!-- for hot reload of the web application -->
Expand Down Expand Up @@ -62,7 +67,11 @@
<failsOnError>true</failsOnError>
</configuration>
<executions>
<execution><goals><goal>check</goal></goals></execution>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import com.google.gcloud.datastore.QueryResults;
import com.google.gcloud.datastore.StructuredQuery;

import org.jasypt.util.text.BasicTextEncryptor;

import java.io.IOException;
import java.io.PrintWriter;

Expand All @@ -44,17 +46,25 @@ public class DatastoreServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException,
ServletException {
// encrypt user ip using PBEWithMD5AndDES
BasicTextEncryptor encryptor = new BasicTextEncryptor();
encryptor.setPassword(Double.toString(10000*Math.random()));
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto. Not very secure to use Math.random. We should either use a secure random number generator or at least add a "don't do this" disclaimer. http://stackoverflow.com/a/11052736/101923

String userIp = encryptor.encrypt(req.getRemoteAddr());

Datastore datastore = DatastoreOptions.defaultInstance().service();
KeyFactory keyFactory = datastore.newKeyFactory().kind("visit");
IncompleteKey key = keyFactory.kind("visit").newKey();

// Record a visit to the datastore, storing the IP and timestamp.
FullEntity<IncompleteKey> curVisit = FullEntity.builder(key)
.set("user_ip", req.getRemoteAddr()).set("timestamp", DateTime.now()).build();
.set("user_ip", userIp).set("timestamp", DateTime.now()).build();
datastore.add(curVisit);

// Retrieve the last 10 visits from the datastore, ordered by timestamp.
Query<Entity> query = Query.entityQueryBuilder().kind("visit")
.orderBy(StructuredQuery.OrderBy.desc("timestamp")).limit(10).build();
QueryResults<Entity> results = datastore.run(query);

resp.setContentType("text/plain");
PrintWriter out = resp.getWriter();
out.print("Last 10 visits:\n");
Expand Down