Skip to content

Commit 3b10003

Browse files
author
VarmaSANJAY
committed
C50-575 Add an option of SSL for mongodb connection
1 parent 9113573 commit 3b10003

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

server/app/model/file-upload/file-upload.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,42 @@ const dboptions = {
1010
host: process.env.DB_HOST || appConfig.db.host,
1111
port: process.env.DB_PORT || appConfig.db.port,
1212
dbName: process.env.DB_NAME || appConfig.db.dbName,
13-
ssl: process.env.DB_SSL || appConfig.db.ssl
13+
ssl: process.env.DB_SSL || appConfig.db.ssl,
14+
enable_ssl: process.env.ENABLE_SSL || appConfig.db.enable_ssl,
15+
enable_auth: process.env.ENABLE_AUTH || appConfig.db.enable_auth,
16+
ssl_config:{
17+
"CAFile": process.env.CAFILE || appConfig.db.ssl_config.CAFile,
18+
"PEMFile": process.env.PEMFILE || appConfig.db.ssl_config.PEMFile
19+
},
20+
auth_config:{
21+
"username":process.env.USERNAME || appConfig.db.auth_config.username,
22+
"password":process.env.PASSWORD || appConfig.db.auth_config.password,
23+
"authenticated":process.env.authenticated || appConfig.db.auth_config.authenticated
24+
}
1425
};
1526

16-
const connectionString = 'mongodb://' + dboptions.host + ':' + dboptions.port + '/' + dboptions.dbName + '?ssl=' + dboptions.ssl;
27+
var connectionString = 'mongodb://' + dboptions.host + ':' + dboptions.port + '/' + dboptions.dbName + '?ssl=' + dboptions.ssl;
28+
var mongoOptions = {};
29+
if(dboptions.enable_ssl){
30+
connectionString = 'mongodb://' + dboptions.host + ':' + dboptions.port + '/' + dboptions.dbName + '?ssl=' + dboptions.enable_ssl;
31+
var ca = [fs.readFileSync(dboptions.ssl_config.CAFile)];
32+
var cert = fs.readFileSync(dboptions.ssl_config.PEMFile);
33+
var key = fs.readFileSync(dboptions.ssl_config.PEMFile);
34+
mongoOptions.checkServerIdentity = false;
35+
mongoOptions.sslValidate = true;
36+
mongoOptions.sslCA = ca;
37+
mongoOptions.sslKey = key;
38+
mongoOptions.sslCert = cert;
39+
}
1740
logger.info(connectionString);
18-
19-
mongoDbClient.connect(connectionString, function (err, db) {
41+
mongoDbClient.connect(connectionString, {server : mongoOptions}, function (err, db) {
2042
if (err) {
2143
throw "unable to connect to mongodb"
2244
return;
45+
}else{
46+
gfs = Grid(db, mongoDbClient);
2347
}
24-
gfs = Grid(db, mongoDbClient);
48+
2549
});
2650

2751
var fileUpload = module.exports = {};
@@ -95,4 +119,4 @@ fileUpload.removeFileByFileId = function removeFileByFileId(fileId, callback) {
95119
callback(null, file);
96120
}
97121
});
98-
};
122+
};

0 commit comments

Comments
 (0)