Bug 1359127 - Initial Mozharness support for mitmproxy draft
authorArmen Zambrano G. <armenzg@mozilla.com>
Mon, 01 May 2017 14:58:01 -0400
changeset 572700 a427d2825ddfa1e33807e14520961a67f4a3b983
parent 572699 096d499ae1c8678a9c45e406a4c1d99e46c40773
child 627091 c71eac70b644b5c823c49f9290c4c3190e1652c0
push id57146
push userarmenzg@mozilla.com
push dateThu, 04 May 2017 14:23:10 +0000
bugs1359127
milestone55.0a1
Bug 1359127 - Initial Mozharness support for mitmproxy This change allows a script to have mitmproxy set up support. This will make a Firefox installation to import the cert from the mitmproxy and selecting it as a proxy. MozReview-Commit-ID: FweyOCzWyN9
testing/mozharness/mozharness/mozilla/mitmproxy.py
new file mode 100644
--- /dev/null
+++ b/testing/mozharness/mozharness/mozilla/mitmproxy.py
@@ -0,0 +1,47 @@
+'''This helps loading mitmproxy's cert and change proxy settings for Firefox.'''
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+import os
+from mozharness.mozilla.firefox.autoconfig import write_autoconfig_files
+
+DEFAULT_CERT_PATH = os.path.join(os.getenv('HOME'),
+                                 '.mitmproxy', 'mitmproxy-ca-cert.cer')
+MITMPROXY_SETTINGS = '''// Start with a comment
+// Load up mitmproxy cert
+var Cc = Components.classes;
+var Ci = Components.interfaces;
+var certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(Ci.nsIX509CertDB);
+var certdb2 = certdb;
+
+try {
+certdb2 = Cc["@mozilla.org/security/x509certdb;1"].getService(Ci.nsIX509CertDB2);
+} catch (e) {}
+
+cert = "%(cert)s";
+certdb2.addCertFromBase64(cert, "C,C,C", "");
+
+// Use mitmdump as the proxy
+// Manual proxy configuration
+pref("network.proxy.type", 1);
+pref("network.proxy.http", "127.0.0.1");
+pref("network.proxy.http_port", 8080);
+pref("network.proxy.ssl", "127.0.0.1");
+pref("network.proxy.ssl_port", 8080);
+'''
+
+
+def configure_mitmproxy(fx_install_dir, certificate_path=DEFAULT_CERT_PATH):
+        certificate = _read_certificate(certificate_path)
+        write_autoconfig_files(fx_install_dir=fx_install_dir,
+                               cfg_contents=MITMPROXY_SETTINGS % {
+                                  'cert': certificate})
+
+
+def _read_certificate(certificate_path):
+    ''' Return the certificate's hash from the certificate file.'''
+    # NOTE: mitmproxy's certificates do not exist until one of its binaries
+    #       has been executed once on the host
+    with open(certificate_path, 'r') as fd:
+        contents = fd.read()
+    return ''.join(contents.splitlines()[1:-1])