From 16d7373800189a8fb551e4657adef08d3d6c8641 Mon Sep 17 00:00:00 2001 From: Winston Li Date: Mon, 5 Jan 2015 02:11:57 +0000 Subject: [PATCH] Added ssl enabled option for postback url construction (#4, #2) and removed api key setting (#3). --- services/git-bridge/README.md | 7 +++-- services/git-bridge/bin/config.json | 6 ++-- .../ac/ic/wlgitbridge/application/Config.java | 6 ++++ .../ic/wlgitbridge/application/SSLConfig.java | 28 +++++++++++++++++++ .../application/WLGitBridgeServer.java | 3 ++ .../src/uk/ac/ic/wlgitbridge/util/Util.java | 20 +++++++++++++ .../writelatex/WLDirectoryNodeSnapshot.java | 8 +++++- 7 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 services/git-bridge/src/uk/ac/ic/wlgitbridge/application/SSLConfig.java diff --git a/services/git-bridge/README.md b/services/git-bridge/README.md index cbbb04af77..1ab77cbf39 100644 --- a/services/git-bridge/README.md +++ b/services/git-bridge/README.md @@ -29,11 +29,14 @@ The configuration file is in `.json` format. There is an example at `bin/config. { "port" (int): the port number, "rootGitDirectory" (string): the directory in which to store git repos and the db/atts, - "apiKey" (string): currently does nothing, "apiBaseUrl" (string): base url for the snapshot api, "username" (string, optional): username for http basic auth, "password" (string, optional): password for http basic auth, - "serviceName" (string): current name of writeLaTeX in case it ever changes ;) + "serviceName" (string): current name of writeLaTeX in case it ever changes, + "hostname": (string): the public hostname of the server, for postback, + "ssl": { (object): ssl configuration + "enabled": (boolean): decides on http or https for the postback url + } } You have to restart the server for configuration changes to take effect. diff --git a/services/git-bridge/bin/config.json b/services/git-bridge/bin/config.json index 3cc83dd2d1..5649ba2e0c 100644 --- a/services/git-bridge/bin/config.json +++ b/services/git-bridge/bin/config.json @@ -1,10 +1,12 @@ { "port": 80, "rootGitDirectory": "/var/wlgb/git", - "apiKey": "", "apiBaseUrl": "https://radiant-wind-3058.herokuapp.com/api/v0", "username": "staging", "password": "6kUfbv0R", "serviceName": "Overleaf", - "hostname": "git.overleaf.com" + "hostname": "git.overleaf.com", + "ssl": { + "enabled": false + } } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/Config.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/Config.java index c861122107..75d118c5cb 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/Config.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/Config.java @@ -22,6 +22,7 @@ public class Config implements JSONSource { private String apiBaseURL; private String serviceName; private String hostname; + private SSLConfig ssl; public Config(String configFilePath) throws InvalidConfigFileException, IOException { try { @@ -45,6 +46,7 @@ public class Config implements JSONSource { this.apiBaseURL = apiBaseURL; serviceName = getElement(configObject, "serviceName").getAsString(); hostname = getOptionalString(configObject, "hostname"); + ssl = new SSLConfig(getElement(configObject, "ssl").getAsJsonObject()); } public int getPort() { @@ -91,4 +93,8 @@ public class Config implements JSONSource { return hostname; } + public SSLConfig getSSL() { + return ssl; + } + } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/SSLConfig.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/SSLConfig.java new file mode 100644 index 0000000000..242cca81f7 --- /dev/null +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/SSLConfig.java @@ -0,0 +1,28 @@ +package uk.ac.ic.wlgitbridge.application; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import uk.ac.ic.wlgitbridge.writelatex.api.request.base.JSONSource; + +/** + * Created by Winston on 05/01/15. + */ +public class SSLConfig implements JSONSource { + + private boolean enabled; + + public SSLConfig(JsonObject ssl) { + fromJSON(ssl); + } + + @Override + public void fromJSON(JsonElement json) { + JsonObject obj = json.getAsJsonObject(); + enabled = obj.get("enabled").getAsJsonPrimitive().getAsBoolean(); + } + + public boolean isEnabled() { + return enabled; + } + +} diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/WLGitBridgeServer.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/WLGitBridgeServer.java index e8b7dda108..88745e9771 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/WLGitBridgeServer.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/WLGitBridgeServer.java @@ -55,6 +55,8 @@ public class WLGitBridgeServer { SnapshotAPIRequest.setBaseURL(writeLatexHostname); Util.setServiceName(config.getServiceName()); Util.setHostname(config.getHostname()); + Util.setSSL(config.getSSL()); + Util.setPort(config.getPort()); } /** @@ -67,6 +69,7 @@ public class WLGitBridgeServer { System.out.println(Util.getServiceName() + "-Git Bridge server started"); System.out.println("Hostname: " + Util.getHostname()); System.out.println("Listening on port: " + port); + System.out.println("SSL enabled: " + Util.getSSLConfig().isEnabled()); System.out.println("Bridged to: " + writeLatexHostname); System.out.println("Root git directory path: " + rootGitDirectoryPath); } catch (BindException e) { diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/util/Util.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/util/Util.java index 1289284bae..ac75733cd0 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/util/Util.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/util/Util.java @@ -1,5 +1,7 @@ package uk.ac.ic.wlgitbridge.util; +import uk.ac.ic.wlgitbridge.application.SSLConfig; + import java.io.BufferedReader; import java.io.IOException; @@ -10,6 +12,8 @@ public class Util { private static String SERVICE_NAME; private static String HOSTNAME; + private static SSLConfig SSL_CONFIG; + private static int PORT; public static String entries(int entries) { if (entries == 1) { @@ -73,4 +77,20 @@ public class Util { return HOSTNAME; } + public static SSLConfig getSSLConfig() { + return SSL_CONFIG; + } + + public static void setSSL(SSLConfig ssl) { + SSL_CONFIG = ssl; + } + + public static int getPort() { + return PORT; + } + + public static void setPort(int port) { + PORT = port; + } + } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/WLDirectoryNodeSnapshot.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/WLDirectoryNodeSnapshot.java index 3af31c0f5a..1fdc62a5de 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/WLDirectoryNodeSnapshot.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/WLDirectoryNodeSnapshot.java @@ -5,6 +5,7 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import uk.ac.ic.wlgitbridge.bridge.CandidateSnapshot; import uk.ac.ic.wlgitbridge.bridge.CandidateSnapshotCallback; +import uk.ac.ic.wlgitbridge.util.Util; import uk.ac.ic.wlgitbridge.writelatex.filestore.node.FileNode; import uk.ac.ic.wlgitbridge.writelatex.filestore.node.WLDirectoryNode; import uk.ac.ic.wlgitbridge.writelatex.model.WLProject; @@ -24,7 +25,12 @@ public class WLDirectoryNodeSnapshot implements CandidateSnapshot { public WLDirectoryNodeSnapshot(WLProject project, WLDirectoryNode directoryNode, String hostname, String postbackKey, CandidateSnapshotCallback callback) { previousVersionID = project.getLatestSnapshotID(); projectName = project.getName(); - projectURL = "http://" + hostname + "/" + projectName; + String protocol = "http"; + if (Util.getSSLConfig().isEnabled()) { + protocol += "s"; + } + projectURL = protocol + "://" + hostname + ":" + Util.getPort() + "/" + projectName; + this.directoryNode = directoryNode; this.postbackKey = postbackKey; this.callback = callback;