fix(@embark/core): Restart IPFS after CORS Update

When adding URLs to IPFS CORS that are not localhost, the IPFS daemon needed to be restarted after non-localhost CORS updates were added to the IPFS config. Without the restart, any non-localhost URLs added to the CORS were not being sent in the CORS header.

After this change, when the IPFS process is run, the following happens:
1. IPFS config is checked if the correct CORS settings are present in the config.
2. If not present, they are updated and IPFS is restarted.
3. If they are present, continue without restarting IPFS.
This commit is contained in:
emizzle 2018-11-28 12:46:47 +11:00 committed by Iuri Matias
parent 5928d134fc
commit 27babf0187
4 changed files with 66 additions and 30 deletions

View File

@ -51,7 +51,8 @@
},
"storage": {
"init": "init",
"initiated": "initiated"
"initiated": "initiated",
"restart": "restart"
},
"tests": {
"gasLimit": 6000000

View File

@ -62,40 +62,26 @@ class IPFSProcess extends ProcessWrapper {
else if (!self.readyCalled && data.indexOf('Daemon is ready') > -1) {
self.readyCalled = true;
// update IPFS cors before spawning a daemon (muhaha)
let ipfsCorsCmd = `${self.command} config --json API.HTTPHeaders.Access-Control-Allow-Origin "[\\"${self.cors.join('\\", \\"')}\\"]"`;
console.trace(`Updating IPFS CORS using command: ${ipfsCorsCmd}`);
child_process.exec(ipfsCorsCmd, {silent: true}, (err, stdout, _stderr) => {
// check cors config before updating if needed
self.getCorsConfig((err, config) => {
if(err){
err = err.toString();
console.error('IPFS CORS update error: ', err);
return console.error('Error getting IPFS CORS config: ', err);
}
if(_stderr){
_stderr = _stderr.toString();
console.error(`IPFS CORS update error: ${_stderr}`);
}
child_process.exec(self.command + ' config --json API.HTTPHeaders.Access-Control-Allow-Credentials "[\\"true\\"]"', {silent: true}, (err, stdout, _stderr) => {
if(err){
err = err.toString();
console.error('IPFS CORS update error: ', err);
}
if(_stderr){
_stderr = _stderr.toString();
console.error(`IPFS CORS update error: ${_stderr}`);
}
child_process.exec(self.command + ' config --json API.HTTPHeaders.Access-Control-Allow-Methods "[\\"PUT\\", \\"POST\\", \\"GET\\"]"', {silent: true}, (err, stdout, _stderr) => {
let corsConfig = new Set(JSON.parse(config));
// test to ensure we have all cors needed
const needsUpdate = !self.cors.every(address => corsConfig.has(address));
if(needsUpdate){
// update IPFS cors config
return self.updateCorsConfig(err => {
if(err){
err = err.toString();
console.error('IPFS CORS update error: ', err);
}
if(_stderr){
_stderr = _stderr.toString();
console.error(`IPFS CORS update error: ${_stderr}`);
}
self.send({result: constants.storage.initiated});
self.send({result: constants.storage.restart}, () => {
childProcess.kill();
});
});
});
}
self.send({result: constants.storage.initiated});
});
}
console.log('IPFS: ' + data);
@ -107,6 +93,43 @@ class IPFSProcess extends ProcessWrapper {
});
}
getCorsConfig(cb){
let ipfsCorsCmd = `${this.command} config API.HTTPHeaders.Access-Control-Allow-Origin`;
child_process.exec(ipfsCorsCmd, {silent: true}, (err, stdout, stderr) => {
if(err || stderr){
err = (err || stderr).toString();
return cb(err);
}
cb(null, stdout);
});
}
updateCorsConfig(cb){
// update IPFS cors before spawning a daemon (muhaha)
let ipfsCorsCmd = `${this.command} config --json API.HTTPHeaders.Access-Control-Allow-Origin "[\\"${this.cors.join('\\", \\"')}\\"]"`;
console.trace(`Updating IPFS CORS using command: ${ipfsCorsCmd}`);
child_process.exec(ipfsCorsCmd, {silent: true}, (err, _stdout, stderr) => {
if(err || stderr){
err = (err || stderr).toString();
return cb(err);
}
child_process.exec(this.command + ' config --json API.HTTPHeaders.Access-Control-Allow-Credentials "[\\"true\\"]"', {silent: true}, (err, _stdout, stderr) => {
if(err || stderr){
err = (err || stderr).toString();
return cb(err);
}
child_process.exec(this.command + ' config --json API.HTTPHeaders.Access-Control-Allow-Methods "[\\"PUT\\", \\"POST\\", \\"GET\\"]"', {silent: true}, (err, stdout, stderr) => {
if(err || stderr){
err = (err || stderr).toString();
return cb(err);
}
cb(null, stdout);
});
});
});
}
startIPFSDaemon() {
const self = this;

View File

@ -21,6 +21,7 @@ class StorageProcessesLauncher {
this.embark = options.embark;
this.processes = {};
this.corsParts = options.corsParts || [];
this.restartCalled = false;
this.cors = this.buildCors();
@ -81,6 +82,10 @@ class StorageProcessesLauncher {
}
processExited(storageName, code) {
if(this.restartCalled){
this.restartCalled = false;
return this.launchProcess(storageName, () => {});
}
this.logger.error(__(`Storage process for {{storageName}} ended before the end of this process. Code: {{code}}`, {storageName, code}));
}
@ -145,6 +150,13 @@ class StorageProcessesLauncher {
callback();
});
self.processes[storageName].on('result', constants.storage.restart, (_msg) => {
self.restartCalled = true;
self.logger.info(__(`Restarting ${storageName} process...`).cyan);
self.processes[storageName].kill();
delete this.processes[storageName];
});
self.events.on('logs:swarm:enable', () => {
self.processes[storageName].silent = false;
});

View File

@ -62,7 +62,7 @@ function httpsGet(url, callback) {
function httpGetJson(url, callback) {
httpGetRequest(http, url, function (err, body) {
try {
let parsed = JSON.parse(body);
let parsed = body && JSON.parse(body);
return callback(err, parsed);
} catch (e) {
return callback(e);