2017-02-10 60 views
4

要antecipate的問題:我需要得到SSL支持在Heroku上,以建立使用SSL Heroku的阿特拉斯的MongoDB雲之間的連接? (TSL/SSL連接是requirement來訪問Atlas MongoDB雲服務)。連接的Heroku應用到阿特拉斯的MongoDB雲服務


我想我的Heroku應用程序,寫在node.js中,連接到阿特拉斯的MongoDB雲託管集羣。

我現在的數據庫是在MLAB託管(作爲Heroku的附加),以及MongoDB的URI用於訪問通過貓鼬羣集(使用XXX省略信息保密):

MONGODB_URI="mongodb://xxx:[email protected]:23266,xxx-a1.mlab.com:xxx/xxx?replicaSet=rs-xxx" 

現在,我已經遷移從MLAB我的數據阿特拉斯MongoDB的雲,我目前訪問使用URI集羣:

MONGODB_URI="mongodb://xxx:[email protected]:xxx,cluster0-shard-xxx.mongodb.net:xxx,cluster0-shard-xxx.mongodb.net:xxx/xxx?replicaSet=xxx&ssl=true&authSource=admin" 

當我運行應用程序的Heroku本地在我的機器,我可以沒有問題訪問數據庫。我也可以使用mongo shell連接到羣集。

但是,在Heroku中運行應用程序時,無法建立連接。在瀏覽器JS控制檯中,我收到503服務不可用消息。在heroku中,我收到錯誤:

no primary found in replica set 

我知道Atlas MongoDB Cloud需要SSL連接,與mLab不同。在我的本地機器中,我想使用自簽名證書來成功連接到羣集。

我的問題是:我是否需要在Heroku中獲得SSL支持才能訪問建立Heroku和MongoDB Atlas之間的安全連接?或者Heroku中的SSL支持只需要客戶端/ Heroku安全連接?

回答

4

我認爲可能解決您的問題

免責聲明:我已經使用Heroku上沒有MongoDB的也不阿特拉斯,但我期待到他們。

根據我發現的Github問題[1],如果您沒有將MongoDB Atlas中的服務器IP地址列入白名單,您將收到該錯誤消息。

閱讀MongoDB Atlas docs [2],我看到與Heroku dynos結合使用的唯一方法是將0.0.0.0/0(即所有地址)添加到您的MongoDB Atlas白名單。

試一試,請回報您是否可以實例化連接。

在SSL

試圖回覆SSL的問題,我不認爲你需要根據我讀,使其能夠在Heroku上,雖然我不能完全確定。

如果MongoDB服務器執行了證書驗證,則連接到它的Node.js代碼必須如下所示(取自節點。JS驅動程序文檔[3]):

var MongoClient = require('mongodb').MongoClient, 
    f = require('util').format, 
    fs = require('fs'); 

// Read the certificates 
var ca = [fs.readFileSync(__dirname + "/ssl/ca.pem")]; 
var cert = fs.readFileSync(__dirname + "/ssl/client.pem"); 
var key = fs.readFileSync(__dirname + "/ssl/client.pem"); 

// Connect validating the returned certificates from the server 
MongoClient.connect("mongodb://localhost:27017/test?ssl=true", { 
    server: { 
     sslValidate:true 
    , sslCA:ca 
    , sslKey:key 
    , sslCert:cert 
    , sslPass:'10gen' 
    } 
}, function(err, db) { 
    db.close(); 
}); 

如果MongoDB服務器不檢查任何SSL證書,你可以簡單地使用如下代碼(也Node.js的驅動程序文檔拍攝[3]):

var MongoClient = require('mongodb').MongoClient; 

MongoClient.connect("mongodb://localhost:27017/test?ssl=true", function(err, db) { 
    db.close(); 
}); 

鑑於阿特拉斯文檔[4]中包含用於連接到它的Node.js下面的示例代碼,我認爲你做必須在Heroku上啓用SSL:

var MongoClient = require('mongodb').MongoClient; 

var uri = "mongodb://kay:[email protected]ongodb.net:27017,mycluster0-shard-00-01-wpeiv.mongodb.net:27017,mycluster0-shard-00-02-wpeiv.mongodb.net:27017/admin?ssl=true&replicaSet=Mycluster0-shard-0&authSource=admin"; 
MongoClient.connect(uri, function(err, db) { 
    db.close(); 
}); 

[1] https://github.com/meteor/meteor/issues/7492#issuecomment-236562860 
[2] https://docs.atlas.mongodb.com/security-whitelist/ 
[3] https://mongodb.github.io/node-mongodb-native/2.2/tutorials/connect/ssl/ 
[4] https://docs.atlas.mongodb.com/driver-connection/#node-js-driver-example 
+2

尼克拉斯,你是對的!問題在於白名單訪問。 關於SSL問題,我想你是對的,因爲我從Herkou支持得到了以下回復:「(...)使用SSL的應用程序是完全獨立於mongo連接,所以我不認爲它會這是一個問題。「 –

+4

**更新([source](https://kb.heroku.com/i-need-to-whitelist-heroku-dynos-what-are-ip-address-ranges-in-use-at-heroku)) :**作爲Heroku dynos使用AWS EC2實例的IP範圍子集,可以添加[AWS IP範圍](http://docs.aws.amazon.com/general/latest/gr/aws-ip-ranges .html)添加到Cloud Atlas的白名單,或者獲取附加組件以提供靜態出站IP地址,或通過TLS使用安全通信。 –

+1

太棒了!謝謝你回報,布魯諾。 :) –