Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BACK-43] jellyfish updates based on reuse of legacy _id for migration #203

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
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
42 changes: 37 additions & 5 deletions lib/misc.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/*
* == BSD2 LICENSE ==
* Copyright (c) 2014, Tidepool Project
*
*
darinkrauss marked this conversation as resolved.
Show resolved Hide resolved
* This program is free software; you can redistribute it and/or modify it under
* the terms of the associated License, which is identical to the BSD 2-Clause
* License as published by the Open Source Initiative at opensource.org.
*
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the License for more details.
*
*
* You should have received a copy of the License along with this program; if
* not, you can obtain one from Tidepool Project at tidepool.org.
* == BSD2 LICENSE ==
Expand All @@ -18,7 +18,7 @@
'use strict';

var crypto = require('crypto');

var fs = require('fs');
var amoeba = require('amoeba');
var base32hex = amoeba.base32hex;
var except = amoeba.except;
Expand All @@ -45,7 +45,7 @@ var except = amoeba.except;
* @param fields an array of values to be concatenated together into a unique string
* @returns {string} the base32 encoded hash of the delimited-concatenation of the provided fields (also known as a "unique" id)
*/
exports.generateId = function(fields) {
exports.generateId = function (fields) {
var hasher = crypto.createHash('sha1');

for (var i = 0; i < fields.length; ++i) {
Expand All @@ -65,3 +65,35 @@ exports.generateId = function(fields) {
return base32hex.encodeBuffer(hasher.digest(), { paddingChar: '-' });
};

exports.encryptArrayToFile = function (dataArray, filePath, env, serverSecret) {
darinkrauss marked this conversation as resolved.
Show resolved Hide resolved
var iv = `${env}-environment`.substring(0, 16);
var key = serverSecret.substring(0, 32);
var algorithm = 'aes-256-cbc';
darinkrauss marked this conversation as resolved.
Show resolved Hide resolved

var encryptedArray = dataArray.map((item) => {
darinkrauss marked this conversation as resolved.
Show resolved Hide resolved
var cipher = crypto.createCipheriv(algorithm, key, iv);
let encrypted = cipher.update(item, 'utf8', 'hex');
encrypted += cipher.final('hex');
return encrypted;
});
fs.writeFileSync(filePath, JSON.stringify(encryptedArray));
};

exports.decryptArrayFromFile = function (filePath, env, serverSecret) {
if (!fs.existsSync(filePath)) {
throw new Error(`Missing required file ${filePath}`);
}
var iv = `${env}-environment`.substring(0, 16);
var key = serverSecret.substring(0, 32);
var algorithm = 'aes-256-cbc';
var encryptedArray = JSON.parse(fs.readFileSync(filePath, 'utf8'));

var decryptedArray = encryptedArray.map((item) => {
var decipher = crypto.createDecipheriv(algorithm, key, iv);
let decrypted = decipher.update(item, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
});

return decryptedArray;
};
1 change: 1 addition & 0 deletions lib/schema/qa3_user_ids.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might as well go ahead and check in similar files for all the environments. That way if this is deployed anywhere it will still function, but effectively act the same and use Jellyfish.

24 changes: 14 additions & 10 deletions lib/schema/schemaEnv.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,19 @@
'use strict';

var config = require('amoeba').config;
var schema = require('./schema.js');
var misc = require('../misc.js');

function loadEnvironmentUserIds() {
const environment = config.fromEnvironment('POD_NAMESPACE', 'local');
const serverSecret = config.fromEnvironment('TIDEPOOL_SERVER_SECRET');
const environmentUserIdsFile = `${__dirname}/../${environment}_user_ids.json`;

return misc.decryptArrayFromFile(
environmentUserIdsFile,
environment,
serverSecret
);
}

module.exports = (function () {
var schemaEnv = {};
Expand All @@ -32,14 +44,6 @@ module.exports = (function () {

schemaEnv.authRealm = config.fromEnvironment('KEYCLOAK_AUTH_REALM', null);
schemaEnv.authUrl = config.fromEnvironment('KEYCLOAK_AUTH_URL', null);
var usersStr = config.fromEnvironment('UPLOADER_PLATFORM_USER_IDS', null);
var platformUsers = [];
if (usersStr) {
platformUsers = usersStr
.split(',')
.map((item) => item.toLowerCase().trim());
}
schemaEnv.platformUserIds = platformUsers;

schemaEnv.platformUserIds = loadEnvironmentUserIds();
return schemaEnv;
})();
Loading