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
20 changes: 10 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ jobs:
script:
- sshpass -p $PROD_MACHINE_PASSWORD ssh -o StrictHostKeyChecking=no $PROD_MACHINE_USERNAME@$PROD_MACHINE_IP "cd /Controller; NODE_ENV=production node src/main.js stop; git pull; npm i; NODE_ENV=production node src/main.js start"
- stage: release
before_install:
- git clone "https://github.com/$TRAVIS_REPO_SLUG.git" "$TRAVIS_REPO_SLUG";
- cd "$TRAVIS_REPO_SLUG";
- git checkout -qf "$TRAVIS_COMMIT";
- git checkout master
before_deploy:
- git config --global user.name "${GH_USERNAME}";
- git config credential.helper "store --file=.git/credentials";
- echo "https://${GH_TOKEN}:@github.com" > .git/credentials;
- npm run automatic-release
#before_install:
#- git clone "https://github.com/$TRAVIS_REPO_SLUG.git" "$TRAVIS_REPO_SLUG";
#- cd "$TRAVIS_REPO_SLUG";
#- git checkout -qf "$TRAVIS_COMMIT";
#- git checkout master
#before_deploy:
#- git config --global user.name "${GH_USERNAME}";
#- git config credential.helper "store --file=.git/credentials";
#- echo "https://${GH_TOKEN}:@github.com" > .git/credentials;
#- npm run automatic-release
deploy:
skip_cleanup: true
provider: npm
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "iofogcontroller",
"version": "1.0.29",
"version": "1.0.31",
"description": "ioFog Controller project for Eclipse IoFog @ iofog.org \\nCopyright (c) 2018 Edgeworx, Inc.",
"main": "./src/main.js",
"author": "Saeid Baghbidi",
Expand Down
190 changes: 150 additions & 40 deletions scripts/postinstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,57 +17,49 @@ const fs = require('fs');
const semver = require('semver');
const currentVersion = require('../package').version;

const rootDir = `${__dirname}/../`;
const rootDir = `${__dirname}/..`;
let installationVariablesFileName = 'iofogcontroller_install_variables';
let installationVariablesFile;
let tempDir;

if (os.type() === 'Linux') {
tempDir = '/tmp/';
} else if (os.type() === 'Darwin') {
tempDir = '/tmp/';
} else if (os.type() === 'Windows_NT') {
tempDir = `${process.env.APPDATA}/`;
} else {
throw new Error("Unsupported OS found: " + os.type());
}

installationVariablesFile = tempDir + installationVariablesFileName;
let tempDir = getTempDirLocation();
const installationVariablesFile = tempDir + '/' + installationVariablesFileName;

const devDbBackup = `${tempDir}dev_database.sqlite`;
//restore all files
const devDbBackup = `${tempDir}/dev_database.sqlite`;
const devDb = `${rootDir}/src/sequelize/dev_database.sqlite`;
if (fs.existsSync(devDbBackup)) {
fs.renameSync(devDbBackup, devDb);
}
moveFileIfExists(devDbBackup, devDb);

const prodDbBackup = `${tempDir}prod_database.sqlite`;
const prodDbBackup = `${tempDir}/prod_database.sqlite`;
const prodDb = `${rootDir}/src/sequelize/prod_database.sqlite`;
if (fs.existsSync(prodDbBackup)) {
fs.renameSync(prodDbBackup, prodDb);
}
moveFileIfExists(prodDbBackup, prodDb);

const defConfigBackup = `${tempDir}/default_iofog_backup.json`;
const defConfig = `${rootDir}/src/config/default.json`;
moveFileIfExists(defConfigBackup, defConfig);

const prodConfigBackup = `${tempDir}/production_iofog_backup.json`;
const prodConfig = `${rootDir}/src/config/production.json`;
moveFileIfExists(prodConfigBackup, prodConfig);

const devConfigBackup = `${tempDir}/development_iofog_backup.json`;
const devConfig = `${rootDir}/src/config/development.json`;
moveFileIfExists(devConfigBackup, devConfig);

//process migrations
try {
const instalationVarsStr = fs.readFileSync(installationVariablesFile);
const instalationVars = JSON.parse(instalationVarsStr);
const prevVersion = instalationVars.prevVer;
const installationVarsStr = fs.readFileSync(installationVariablesFile);
const installationVars = JSON.parse(installationVarsStr);
const prevVersion = installationVars.prevVer;

console.log(`previous version - ${prevVersion}`);
console.log(`new version - ${currentVersion}`);

if (semver.satisfies(prevVersion, '<=1.0.0')) {
console.log('upgrading from version <=1.0.0 :');
console.log(' inserting seeds meta info in db');
const options = {
env: {
"PATH": process.env.PATH
},
stdio: [process.stdin, process.stdout, process.stderr]
};

execSync(`sqlite3 ${prodDb} "insert into SequelizeMeta (name) values ('20180928110125-insert-registry.js');"`, options);
execSync(`sqlite3 ${prodDb} "insert into SequelizeMeta (name) values ('20180928111532-insert-catalog-item.js');"`, options);
execSync(`sqlite3 ${prodDb} "insert into SequelizeMeta (name) values ('20180928112152-insert-iofog-type.js');"`, options);
execSync(`sqlite3 ${prodDb} "insert into SequelizeMeta (name) values ('20180928121334-insert-catalog-item-image.js');"`, options);
console.log('upgrading from version <= 1.0.0 :');
insertSeeds();
}

if (semver.satisfies(prevVersion, '<=1.0.30')) {
console.log('upgrading from version <= 1.0.30 :');
updateEncryptionMethod();
}

fs.unlinkSync(installationVariablesFile);
Expand All @@ -84,4 +76,122 @@ const options = {
stdio: [process.stdin, process.stdout, process.stderr]
};

execSync('node ./src/main.js init', options);
execSync('node ./src/main.js init', options);

//other functions definitions

function getTempDirLocation() {
let tempDir;
if (os.type() === 'Linux') {
tempDir = '/tmp';
} else if (os.type() === 'Darwin') {
tempDir = '/tmp';
} else if (os.type() === 'Windows_NT') {
tempDir = `${process.env.APPDATA}`;
} else {
throw new Error("Unsupported OS found: " + os.type());
}
return tempDir;
}

function moveFileIfExists(from, to) {
if (fs.existsSync(from)) {
fs.renameSync(from, to);
}
}

function insertSeeds() {
console.log(' inserting seeds meta info in db');
const options = {
env: {
"PATH": process.env.PATH
},
stdio: [process.stdin, process.stdout, process.stderr]
};

execSync(`sqlite3 ${prodDb} "insert into SequelizeMeta (name) values ('20180928110125-insert-registry.js');"`, options);
execSync(`sqlite3 ${prodDb} "insert into SequelizeMeta (name) values ('20180928111532-insert-catalog-item.js');"`, options);
execSync(`sqlite3 ${prodDb} "insert into SequelizeMeta (name) values ('20180928112152-insert-iofog-type.js');"`, options);
execSync(`sqlite3 ${prodDb} "insert into SequelizeMeta (name) values ('20180928121334-insert-catalog-item-image.js');"`, options);
}

function updateEncryptionMethodForUsersPassword(decryptionFunc) {
const options = {
env: {
"PATH": process.env.PATH
}
};

const usersOutput = execSync(`sqlite3 ${prodDb} "select id, email, password from Users;"`, options).toString();
const usersLines = usersOutput.match(/[^\r\n]+/g);
for (let line of usersLines) {
let id, email, oldEncryptedPassword;
try {
const vals = line.split('|');
id = vals[0];
email = vals[1];
oldEncryptedPassword = vals[2];

const decryptedPassword = decryptionFunc(oldEncryptedPassword, email);

const AppHelper = require('../src/helpers/app-helper');
const newEncryptedPassword = AppHelper.encryptText(decryptedPassword, email);

const options = {
env: {
"PATH": process.env.PATH
},
stdio: [process.stdin, process.stdout, process.stderr]
};
execSync(`sqlite3 ${prodDb} "update Users set password='${newEncryptedPassword}' where id=${id};"`, options);
} catch (e) {
console.log('db problem');
}
}
}

function updateEncryptionMethodForEmailService(configFile, decryptionFunc) {
console.log(configFile);
if (!configFile) {
return
}
const configObj = JSON.parse(fs.readFileSync(configFile, 'utf8'));
console.log(configObj);
if (!configObj || !configObj.Email || !configObj.Email.Address || !configObj.Email.Password) {
return
}

const email = configObj.Email.Address;
const oldEncryptedPassword = configObj.Email.Password;

const decryptedPassword = decryptionFunc(oldEncryptedPassword, email);

const AppHelper = require('../src/helpers/app-helper');
configObj.Email.Password = AppHelper.encryptText(decryptedPassword, email);

console.log(configObj);
try {
fs.writeFileSync(configFile, JSON.stringify(configObj, null, 2));
} catch (e) {
console.log(e)
}
}

function updateEncryptionMethod() {
console.log(' updating encryption method for old users');

function decryptTextVer30(text, salt) {
const crypto = require('crypto');
const ALGORITHM = 'aes-256-ctr';

const decipher = crypto.createDecipher(ALGORITHM, salt);
let dec = decipher.update(text, 'hex', 'utf8');
dec += decipher.final('utf8');
return dec
}

updateEncryptionMethodForUsersPassword(decryptTextVer30);
updateEncryptionMethodForEmailService(defConfig, decryptTextVer30);
updateEncryptionMethodForEmailService(devConfig, decryptTextVer30);
updateEncryptionMethodForEmailService(prodConfig, decryptTextVer30);
}
29 changes: 22 additions & 7 deletions scripts/preuninstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ const execSync = require('child_process').execSync;
const fs = require('fs');
const version = require('../package').version;

const rootDir = `${__dirname}/../`;
const rootDir = `${__dirname}/..`;
let installationVariablesFileName = 'iofogcontroller_install_variables';
let installationVariablesFile;
let tempDir;

if (os.type() === 'Linux') {
tempDir = '/tmp/';
tempDir = '/tmp';
} else if (os.type() === 'Darwin') {
tempDir = '/tmp/';
tempDir = '/tmp';
} else if (os.type() === 'Windows_NT') {
tempDir = `${process.env.APPDATA}/`;
tempDir = `${process.env.APPDATA}`;
} else {
throw new Error("Unsupported OS found: " + os.type());
}

installationVariablesFile = tempDir + installationVariablesFileName;
installationVariablesFile = tempDir + '/' + installationVariablesFileName;

const instalationVars = {
prevVer: version
Expand All @@ -41,11 +41,26 @@ fs.writeFileSync(installationVariablesFile, JSON.stringify(instalationVars));

const devDb = `${rootDir}/src/sequelize/dev_database.sqlite`;
if (fs.existsSync(devDb)) {
fs.renameSync(devDb, `${tempDir}dev_database.sqlite`)
fs.renameSync(devDb, `${tempDir}/dev_database.sqlite`)
}

const prodDb = `${rootDir}/src/sequelize/prod_database.sqlite`;
if (fs.existsSync(prodDb)) {
fs.renameSync(prodDb, `${tempDir}prod_database.sqlite`)
fs.renameSync(prodDb, `${tempDir}/prod_database.sqlite`)
}

const defConfig = `${rootDir}/src/config/default.json`;
if (fs.existsSync(defConfig)) {
fs.renameSync(defConfig, `${tempDir}/default_iofog_backup.json`)
}

const devConfig = `${rootDir}/src/config/development.json`;
if (fs.existsSync(devConfig)) {
fs.renameSync(devConfig, `${tempDir}/development_iofog_backup.json`)
}

const prodConfig = `${rootDir}/src/config/production.json`;
if (fs.existsSync(prodConfig)) {
fs.renameSync(prodConfig, `${tempDir}/production_iofog_backup.json`)
}

4 changes: 2 additions & 2 deletions src/cli/diagnostics.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ const _changeMicroserviceStraceState = async function (obj) {
const isEnable = AppHelper.validateBooleanCliOptions(obj.enable, obj.disable);
await DiagnosticService.changeMicroserviceStraceState(obj.microserviceUuid, {enable: isEnable}, {}, true);
const msg = isEnable ? 'Microservice strace has been enabled' : 'Microservice strace has been disabled';

logger.info(msg);
};

const _getMicroserviceStraceData = async function (obj) {
logger.info(JSON.stringify(obj));

const result = await DiagnosticService.getMicroserviceStraceData(obj.microserviceUuid, {format: obj.format}, {}, true);
logger.info(JSON.stringify(result, null, 2));
logger.info('Strace data:');
logger.info(result.data);
logger.info('Microservice strace data has been retrieved successfully.');
};

Expand Down
11 changes: 8 additions & 3 deletions src/cli/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ class User extends BaseCLIHandler {
name: 'password', alias: 'p', type: String,
description: 'User\'s password',
group: [constants.CMD_ADD, constants.CMD_UPDATE],
},
{
name: 'force', alias: 'F', type: Boolean,
description: 'User\'s force delete',
group: [constants.CMD_REMOVE],
}
];
this.commands = {
Expand Down Expand Up @@ -136,10 +141,10 @@ const _updateUserDetails = async function (userDetails, user) {
logger.info('User updated successfully.');
};

const _deleteUser = async function (emailObj, user) {
logger.info(JSON.stringify(emailObj));
const _deleteUser = async function (obj, user) {
logger.info(JSON.stringify(obj));

await UserService.deleteUser(user, true);
await UserService.deleteUser(obj.force, user, true);
logger.info('User removed successfully.');
};

Expand Down
19 changes: 3 additions & 16 deletions src/controllers/user-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,11 @@ const userLoginEndPoint = async function (req) {

await Validator.validate(user, Validator.schemas.login);

const encryptedPassword = AppHelper.encryptText(user.password, user.email);
const credentials = {
email: user.email,
password: encryptedPassword
password: user.password
};

logger.info("Parameters:" + JSON.stringify(credentials));

return await UserService.login(credentials, false);
};

Expand Down Expand Up @@ -88,25 +85,15 @@ const updateUserProfileEndPoint = async function (req, user) {
};

const deleteUserProfileEndPoint = async function (req, user) {
return await UserService.deleteUser(user, false);
return await UserService.deleteUser(req.body.force, user, false);
};

const updateUserPasswordEndPoint = async function (req, user) {
const passwordUpdates = req.body;

await Validator.validate(passwordUpdates, Validator.schemas.updatePassword);

const encryptedOldPassword = AppHelper.encryptText(passwordUpdates.oldPassword, user.email);
const encryptedNewPassword = AppHelper.encryptText(passwordUpdates.newPassword, user.email);

const encryptedPasswordUpdates = {
oldPassword: encryptedOldPassword,
newPassword: encryptedNewPassword
};

logger.info("Parameters:" + JSON.stringify(encryptedPasswordUpdates));

return await UserService.updateUserPassword(encryptedPasswordUpdates, user, false);
return await UserService.updateUserPassword(passwordUpdates, user, false);
};

const resetUserPasswordEndPoint = async function (req) {
Expand Down
Loading