From cd7488cc1b5b7274ee751f39b10d9bf1aeff0188 Mon Sep 17 00:00:00 2001 From: kohsuke Date: Fri, 12 Dec 2008 19:37:52 +0000 Subject: [PATCH] Added anonymous usage statistics tracking with opt-out. (discussion) git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@13533 71c3de6d-444a-0410-be80-ed276b4c234a --- core/src/main/java/hudson/model/Hudson.java | 12 ++ .../java/hudson/model/UsageStatistics.java | 110 ++++++++++++++++++ .../hudson/model/Hudson/configure.jelly | 4 + .../hudson/model/Hudson/configure.properties | 3 + .../hudson/model/UsageStatistics/footer.jelly | 14 +++ .../hudson/model/UsageStatisticsTest.java | 49 ++++++++ .../help/system-config/usage-statistics.html | 24 ++++ 7 files changed, 216 insertions(+) create mode 100644 core/src/main/java/hudson/model/UsageStatistics.java create mode 100644 core/src/main/resources/hudson/model/UsageStatistics/footer.jelly create mode 100644 core/src/test/java/hudson/model/UsageStatisticsTest.java create mode 100644 war/resources/help/system-config/usage-statistics.html diff --git a/core/src/main/java/hudson/model/Hudson.java b/core/src/main/java/hudson/model/Hudson.java index 03a24ae1f8..6dcfc5dfe5 100644 --- a/core/src/main/java/hudson/model/Hudson.java +++ b/core/src/main/java/hudson/model/Hudson.java @@ -319,6 +319,11 @@ public final class Hudson extends View implements ItemGroup, Node, private transient final UpdateCenter updateCenter = new UpdateCenter(); + /** + * True if the user opted out from the statistics tracking. We'll never send anything if this is true. + */ + private Boolean noUsageStatistics; + /** * HTTP proxy configuration. */ @@ -409,6 +414,7 @@ public final class Hudson extends View implements ItemGroup, Node, HudsonFilter.REMEMBER_ME_SERVICES_PROXY.setDelegate(rms); WindowsInstallerLink.registerIfApplicable(); + UsageStatistics.register(); } public TcpSlaveAgentListener getTcpSlaveAgentListener() { @@ -446,6 +452,10 @@ public final class Hudson extends View implements ItemGroup, Node, return updateCenter; } + public boolean isUsageStatisticsCollected() { + return noUsageStatistics==null || !noUsageStatistics; + } + /** * Returns a secret key that survives across container start/stop. *

@@ -1595,6 +1605,8 @@ public final class Hudson extends View implements ItemGroup, Node, authorizationStrategy = AuthorizationStrategy.UNSECURED; } + noUsageStatistics = json.has("usageStatisticsCollected") ? null : true; + { String v = req.getParameter("slaveAgentPortType"); if(!isUseSecurity() || v==null || v.equals("random")) diff --git a/core/src/main/java/hudson/model/UsageStatistics.java b/core/src/main/java/hudson/model/UsageStatistics.java new file mode 100644 index 0000000000..00dacd54c2 --- /dev/null +++ b/core/src/main/java/hudson/model/UsageStatistics.java @@ -0,0 +1,110 @@ +package hudson.model; + +import com.trilead.ssh2.crypto.Base64; +import hudson.Util; +import static hudson.util.TimeUnit2.DAYS; +import net.sf.json.JSONObject; +import org.apache.commons.io.output.ByteArrayOutputStream; + +import javax.crypto.Cipher; +import javax.crypto.CipherOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.security.GeneralSecurityException; +import java.security.KeyFactory; +import java.security.PublicKey; +import java.security.spec.X509EncodedKeySpec; +import java.util.zip.GZIPOutputStream; + +/** + * @author Kohsuke Kawaguchi + */ +public class UsageStatistics extends PageDecorator { + private final String keyImage; + + /** + * Lazily computed {@link PublicKey} representation of {@link #keyImage}. + */ + private volatile transient PublicKey key; + + /** + * When was the last time we asked a browser to send the usage stats for us? + */ + private volatile transient long lastAttempt = -1; + + public UsageStatistics() { + this(DEFAULT_KEY_BYTES); + } + + /** + * Creates an instance with a specific public key image. + */ + public UsageStatistics(String keyImage) { + super(UsageStatistics.class); + this.keyImage = keyImage; + load(); + } + + // register the instance + static { + PageDecorator.ALL.add(new UsageStatistics()); + } + + /** + * No-op, but calling this method makes sure that the static initializer is run. + */ + static void register() {} + + /** + * Returns true if it's time for us to check for new version. + */ + public boolean isDue() { + // user opted out. no data collection. + if(!Hudson.getInstance().isUsageStatisticsCollected()) return false; + + long now = System.currentTimeMillis(); + if(now - lastAttempt > DAY) { + lastAttempt = now; + return true; + } + return false; + } + + private Cipher getCipher() { + try { + if (key == null) { + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); + key = keyFactory.generatePublic(new X509EncodedKeySpec(Util.fromHexString(keyImage))); + } + + Cipher cipher = Cipher.getInstance("RSA"); + cipher.init(Cipher.ENCRYPT_MODE, key); + return cipher; + } catch (GeneralSecurityException e) { + throw new Error(e); // impossible + } + } + + /** + * Gets the encrypted usage stat data to be sent to the Hudson server. + */ + public String getStatData() throws IOException { + JSONObject o = new JSONObject(); + o.put("version",1); + + // json -> UTF-8 encode -> gzip -> encrypt -> base64 -> string + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + OutputStreamWriter w = new OutputStreamWriter(new GZIPOutputStream(new CipherOutputStream(baos,getCipher())), "UTF-8"); + o.write(w); + w.close(); + + return new String(Base64.encode(baos.toByteArray())); + } + + /** + * Public key to encrypt the usage statistics + */ + private static final String DEFAULT_KEY_BYTES = "30819f300d06092a864886f70d010101050003818d0030818902818100c14970473bd90fd1f2d20e4fa6e36ea21f7d46db2f4104a3a8f2eb097d6e26278dfadf3fe9ed05bbbb00a4433f4b7151e6683a169182e6ff2f6b4f2bb6490b2cddef73148c37a2a7421fc75f99fb0fadab46f191806599a208652f4829fd6f76e13195fb81ff3f2fce15a8e9a85ebe15c07c90b34ebdb416bd119f0d74105f3b0203010001"; + + private static final long DAY = DAYS.toMillis(1); +} diff --git a/core/src/main/resources/hudson/model/Hudson/configure.jelly b/core/src/main/resources/hudson/model/Hudson/configure.jelly index e666cea6b3..90c7db426b 100644 --- a/core/src/main/resources/hudson/model/Hudson/configure.jelly +++ b/core/src/main/resources/hudson/model/Hudson/configure.jelly @@ -52,6 +52,10 @@ + + diff --git a/core/src/main/resources/hudson/model/Hudson/configure.properties b/core/src/main/resources/hudson/model/Hudson/configure.properties index 909dda679c..819e2d21ea 100644 --- a/core/src/main/resources/hudson/model/Hudson/configure.properties +++ b/core/src/main/resources/hudson/model/Hudson/configure.properties @@ -2,4 +2,7 @@ slaves.description=\ List of slave nodes registered to this master Hudson. Jobs can be configured to \ execute on slave nodes to handle a large number of jobs. +statsBlurb=\ + Help make Hudson better by sending anonymous usage statistics and crash reports to the Hudson project. + no.such.JDK=No such JDK exists \ No newline at end of file diff --git a/core/src/main/resources/hudson/model/UsageStatistics/footer.jelly b/core/src/main/resources/hudson/model/UsageStatistics/footer.jelly new file mode 100644 index 0000000000..261f50943c --- /dev/null +++ b/core/src/main/resources/hudson/model/UsageStatistics/footer.jelly @@ -0,0 +1,14 @@ + + + + + + \ No newline at end of file diff --git a/core/src/test/java/hudson/model/UsageStatisticsTest.java b/core/src/test/java/hudson/model/UsageStatisticsTest.java new file mode 100644 index 0000000000..81eff72a88 --- /dev/null +++ b/core/src/test/java/hudson/model/UsageStatisticsTest.java @@ -0,0 +1,49 @@ +package hudson.model; + +import junit.framework.TestCase; + +import java.security.KeyFactory; +import java.security.PrivateKey; +import java.security.spec.PKCS8EncodedKeySpec; +import java.io.InputStreamReader; +import java.io.ByteArrayInputStream; +import java.util.zip.GZIPInputStream; + +import hudson.Util; + +import javax.crypto.Cipher; + +import com.trilead.ssh2.crypto.Base64; +import net.sf.json.JSONObject; +import org.apache.commons.io.IOUtils; + +/** + * @author Kohsuke Kawaguchi + */ +public class UsageStatisticsTest extends TestCase { + /** + * Makes sure that the stat data can be decrypted safely. + */ + public void testRoundtrip() throws Exception { + // key pair for testing + String privateKey = "30820276020100300d06092a864886f70d0101010500048202603082025c0201000281810084cababdb38040f659c2cb07a36d758f46e84ebc3d6ba39d967aedf1d396b0788ed3ab868d45ce280b1102b434c2a250ddc3254defe1785ab4f94d7038cf69ecca16753d2de3f6ad8976b3f74902d8634111d730982da74e1a6e3fc0bc3523bba53e45b8a8cbfd0321b94efc9f7fefbe66ad85281e3d0323d87f4426ec51204f0203010001028180784deaacdea8bd31f2d44578601954be3f714b93c2d977dbd76efb8f71303e249ad12dbeb2d2a1192a1d7923a6010768d7e06a3597b3df83de1d5688eb0f0e58c76070eddd696682730c93890dc727564c65dc8416bfbde5aad4eb7a97ed923efb55a291daf3c00810c0e43851298472fd539aab355af8cedcf1e9a0cbead661024100c498375102b068806c71dec838dc8dfa5624fb8a524a49cffadc19d10689a8c9c26db514faba6f96e50a605122abd3c9af16e82f2b7565f384528c9f31ea5947024100aceafd31d7f4872a873c7e5fe88f20c2fb086a053c6970026b3ce364768e2033100efb1ad8f2010fe53454a29decedc23a8a0c8df347742b1f13e11bd3a284b9024100931321470cd0f6cd24d4278bf8e61f9d69b6ef2bf3163a944aa340f91c7ffdf33aeea22b18cc43514af6714a21bb148d6cdca14530a8fa65acd7a8f62bfc9b5f024067452059f8438dc61466488336fce3f00ec483ad04db638dce45daf850e5a8cd5635dc39b87f2fab32940247ec5167ddabe06e870858104500967ac687aa73e102407e3b7997503e18d8d0f094d5e0bd5d57cb93cb39a2fc42cec1ea9a1562786438b61139e45813204d72c919f5397e139ad051d98e4d0f8a06d237f42c0d8440fb"; + String publicKey = "30819f300d06092a864886f70d010101050003818d003081890281810084cababdb38040f659c2cb07a36d758f46e84ebc3d6ba39d967aedf1d396b0788ed3ab868d45ce280b1102b434c2a250ddc3254defe1785ab4f94d7038cf69ecca16753d2de3f6ad8976b3f74902d8634111d730982da74e1a6e3fc0bc3523bba53e45b8a8cbfd0321b94efc9f7fefbe66ad85281e3d0323d87f4426ec51204f0203010001"; + + String data = new UsageStatistics(publicKey).getStatData(); + System.out.println(data); + + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); + PrivateKey priv = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(Util.fromHexString(privateKey))); + + Cipher cipher = Cipher.getInstance("RSA"); + cipher.init(Cipher.DECRYPT_MODE, priv); + + byte[] plain = cipher.doFinal(Base64.decode(data.toCharArray())); + InputStreamReader r = new InputStreamReader(new GZIPInputStream(new ByteArrayInputStream(plain)), "UTF-8"); + JSONObject o = JSONObject.fromObject(IOUtils.toString(r)); + System.out.println(o); + assertEquals(1,o.getInt("version")); + } + + +} diff --git a/war/resources/help/system-config/usage-statistics.html b/war/resources/help/system-config/usage-statistics.html new file mode 100644 index 0000000000..3c79c19d96 --- /dev/null +++ b/war/resources/help/system-config/usage-statistics.html @@ -0,0 +1,24 @@ +

+ Knowing how Hudson is used is a tremendous help in guiding the direction of the development, especially + for open-source projects where users are inheritenly hard to track down. When this option is enabled, + Hudson periodically send information about your usage of Hudson. + +

+ Specifically, it contains the following information: + +

    +
  • Your Hudson version +
  • For your master and each slave, JVM version, OS version, and # of executors +
  • Plugins that are installed and their versions +
  • Total number of jobs in your Hudson +
+ +

+ The information doesn't contain anything that identifies you or allows us to contact you + (except information inherently revealed by HTTP, such as the IP address.) Tabulated + data of these usage statistics submissions will be shared with the community. + +

+ You can read + the discussion of this mechanism in the community, and you are welcome to add your voice here. +

\ No newline at end of file