Mholloway has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/273026

Change subject: Use HTTPS only for requests to production sites
......................................................................

Use HTTPS only for requests to production sites

Requests to the beta cluster were failing since we had hard-coded the
https protocol into our request URLs and https isn't properly configured
there.

This updates the service to attach a protocol based on the request domain:
https if to production, and http otherwise.

Bug: T113542
Change-Id: Ia0a8d4af247ee9f0562a895ce3824db6e85b4ccc
---
M config.dev.yaml
M config.prod.yaml
M lib/mwapi.js
M lib/parsoid-access.js
M lib/util.js
M test/features/mobile-sections/pagecontent.js
6 files changed, 20 insertions(+), 8 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/mobileapps 
refs/changes/26/273026/1

diff --git a/config.dev.yaml b/config.dev.yaml
index cebe335..b2190ec 100644
--- a/config.dev.yaml
+++ b/config.dev.yaml
@@ -59,6 +59,6 @@
       #   - if-match
       #   - user-agent
       #   - x-request-id
-      restbase_uri: https://%DOMAIN%/api/rest_v1
+      restbase_uri: //%DOMAIN%/api/rest_v1
       # whether to print extra debug info
       debug: true
diff --git a/config.prod.yaml b/config.prod.yaml
index de5268c..3e37093 100644
--- a/config.prod.yaml
+++ b/config.prod.yaml
@@ -33,6 +33,6 @@
       port: 6927
       # interface: localhost # uncomment to only listen on localhost
       # more per-service config settings
-      restbase_uri: https://%DOMAIN%/api/rest_v1
+      restbase_uri: //%DOMAIN%/api/rest_v1
       # whether to print extra debug info
       debug: false
diff --git a/lib/mwapi.js b/lib/mwapi.js
index 2e7841a..0d53479 100644
--- a/lib/mwapi.js
+++ b/lib/mwapi.js
@@ -79,11 +79,10 @@
 function apiGet(domain, params) {
     // get the page from the MW API
     return preq.post({
-        uri: 'https://' + domain + '/w/api.php',
+        uri: (domain.match(sUtil.getProdDomainRegExp()) ? 'https://' : 
'http://')
+          + domain + '/w/api.php',
         body: params
     }).then(function(response) {
-        //console.log(JSON.stringify(response, null, 2));
-
         checkResponseStatus(response);
         return response;
     });
diff --git a/lib/parsoid-access.js b/lib/parsoid-access.js
index 645b743..f30bad9 100644
--- a/lib/parsoid-access.js
+++ b/lib/parsoid-access.js
@@ -74,8 +74,10 @@
  * @return {promise} a Promise, which if fulfilled, will return the Parsoid 
content of the given page.
  */
 function getContent(logger, restbase_uri, domain, title, revision, opts) {
-    var uri = restbase_uri.replace('%DOMAIN%',  domain.replace(/^(\w+\.)m\./, 
'$1'))
+    var uri = (domain.match(sUtil.getProdDomainRegExp()) ? 'https:' : 'http:')
+      + restbase_uri.replace('%DOMAIN%',  domain.replace(/^(\w+\.)m\./, '$1'))
         + '/page/html/' + encodeURIComponent(title);
+
     if (!opts && revision && revision.constructor === Object) {
         opts = revision;
         revision = undefined;
diff --git a/lib/util.js b/lib/util.js
index d590c7a..bb1fc52 100644
--- a/lib/util.js
+++ b/lib/util.js
@@ -7,6 +7,11 @@
 var uuid = require('cassandra-uuid');
 var bunyan = require('bunyan');
 
+var prod_domains = /[a-z]+\.(wikipedia|wikimedia|wiktionary)\.org/gi;
+
+function getProdDomainRegExp() {
+    return prod_domains;
+}
 
 /**
  * Error instance wrapping HTTP error responses
@@ -258,6 +263,6 @@
     initAndLogRequest: initAndLogRequest,
     wrapRouteHandlers: wrapRouteHandlers,
     setErrorHandler: setErrorHandler,
-    router: createRouter
+    router: createRouter,
+    getProdDomainRegExp: getProdDomainRegExp
 };
-
diff --git a/test/features/mobile-sections/pagecontent.js 
b/test/features/mobile-sections/pagecontent.js
index be665c5..d881bed 100644
--- a/test/features/mobile-sections/pagecontent.js
+++ b/test/features/mobile-sections/pagecontent.js
@@ -113,4 +113,10 @@
                 assert.deepEqual(res.status, 200);
             });
     });
+    it('Beta cluster request should load successfully', function() {
+        return preq.get({ uri: server.config.uri + 
'en.wikipedia.beta.wmflabs.org/v1/page/mobile-sections/Foobar' })
+            .then(function(res) {
+                assert.deepEqual(res.status, 200);
+            });
+    });
 });

-- 
To view, visit https://gerrit.wikimedia.org/r/273026
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia0a8d4af247ee9f0562a895ce3824db6e85b4ccc
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/mobileapps
Gerrit-Branch: master
Gerrit-Owner: Mholloway <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to