Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ coverage
test/definitions/policy-loader/token
env.yaml
gw_skel
config/*.pem
18 changes: 0 additions & 18 deletions config/cert.pem

This file was deleted.

15 changes: 0 additions & 15 deletions config/key.pem

This file was deleted.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"main": "index.js",
"scripts": {
"lint": "jscs ./",
"postinstall": "node utils/genkeycert",
"pretest": "eslint ./",
"start": "node .",
"test": "mocha -t 600000"
Expand Down Expand Up @@ -135,6 +136,7 @@
"type-is": "^1.6.11",
"uid-safe": "^2.1.1",
"wlpn-password": "^1.0.0",
"pem": "^1.8.3",
"yamljs": "^0.2.4"
},
"APIConnectGateway": "1.0.0"
Expand Down
80 changes: 80 additions & 0 deletions utils/genkeycert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env node

// Copyright IBM Corp. 2016. All Rights Reserved.
// Node module: microgateway
// US Government Users Restricted Rights - Use, duplication or disclosure
// restricted by GSA ADP Schedule Contract with IBM Corp.

var pem = require('pem');
var fs = require('fs');
var path = require('path');

var keyPath = path.resolve(__dirname, '../config/key.pem');
var certPath = path.resolve(__dirname, '../config/cert.pem');

function checkServiceKey(cb) {
try {
fs.statSync(keyPath);
var serviceKey = fs.readFileSync(keyPath);
cb(null, serviceKey);
} catch (e) {
if (e.code === 'ENOENT') {
// if key.pem not exist, generate the private key
console.log("Create the service key 'config/key.pem'");
pem.createPrivateKey(2048, function(e, keyData) {
if (e) {
cb(e);
} else {
fs.writeFile(keyPath, keyData.key, function(e) {
cb(e, e || { newKey: true, keyData: keyData.key });
});
}
});
} else {
cb(e);
}
}
}

function createCertificate(keyData, cb) {
pem.createCertificate({ serviceKey: keyData }, function(e, certResult) {
if (e) {
cb(e);
} else {
fs.writeFile(certPath, certResult.certificate, cb);
}
});
}

function createCertificateCB(err) {
if (err) {
console.log("Error when creating the cert file 'config/cert.pem': " + err);
process.exit(1);
}
}

checkServiceKey(function(err, result) {
if (err) {
console.log("Error when checking/creating the service key file 'config/key.pem': " + err);
process.exit(1);
}
var certFileExist = false;
try {
fs.statSync(certPath);
certFileExist = true;
} catch (e) {
if (e.code !== 'ENOENT') {
console.log("Error when checking the cert file 'config/cert.pem': " + e);
process.exit(1);
}
}
if (certFileExist) {
if (result.newKey) {
console.log("Recreate the cert file 'config/cert.pem'");
createCertificate(result.keyData, createCertificateCB);
}
} else {
console.log("Create the cert file 'config/cert.pem'");
createCertificate(result.keyData, createCertificateCB);
}
});