diff --git a/app/s3/s3Client.js b/app/s3/s3Client.js index 31bbcd1bb..39d50379f 100644 --- a/app/s3/s3Client.js +++ b/app/s3/s3Client.js @@ -206,23 +206,3 @@ module.exports = class S3Client { } }; -// ;((async()=>{ -// var s3Client = new module.exports(require('../conf.js').conf); -// var bucketName = 'razee--k4tty77xnpmgjppfw'; -// var path = 'blah'; -// var content = 'this is teh content'; -// var encryptionKey = 'orgApiKey-21fd8bfa-cc1d-43dd-988f-ddec98d72db7'; -// var ivText = 'oRAApY8YmWQx5a98rUVkhg=='; -// var iv = Buffer.from(ivText, 'base64'); -// -// console.log(11111, bucketName, path, content, encryptionKey, ivText, iv); -// -// var out = await s3Client.encryptAndUploadFile(bucketName, path, content, encryptionKey, iv); -// -// console.log('uploaded', out); -// -// var out = await s3Client.getAndDecryptFile(bucketName, path, encryptionKey, iv); -// -// console.log('downloaded', out); -// })()); - diff --git a/app/s3/s3Client.tests.js b/app/s3/s3Client.tests.js new file mode 100644 index 000000000..77ff0eba9 --- /dev/null +++ b/app/s3/s3Client.tests.js @@ -0,0 +1,68 @@ +/* eslint-env node, mocha */ +/** + * Copyright 2019 IBM Corp. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +const assert = require('assert'); +const rewire = require('rewire'); +const AwsS3Mock = require('mock-aws-s3'); +var stream = require('stream'); + +const S3Client = rewire('./s3Client'); + +AwsS3Mock.config.basePath = '/tmp/buckets'; +S3Client.__set__({ + AWS: AwsS3Mock, + bucketExists: ()=>{return true;}, //mock library doesnt have this function +}); + +describe('s3', () => { + describe('s3Client', () => { + it('encrypt, upload, download, and decrypt', async () => { + //assert.equal(responseCodeMapper(500), 'error'); + var s3Client = new S3Client({ + s3: { + endpoint: 'http://someS3/', + accessKeyId: 'accessKey', + secretAccessKey: 'secretKey', + locationConstraint: 'us-standard', + bucketPrefix: 'razee', + s3ForcePathStyle: true, + signatureVersion: 'v4', + sslEnabled: false, + }, + }); + + var bucketName = 'razee--k4tty77xnpmgjppfw'; + var path = 'blah'; + var inContent = 'this is teh content'; + var fileStream = stream.Readable.from([ inContent ]); + var encryptionKey = 'orgApiKey-21fd8bfa-cc1d-43dd-988f-ddec98d72db7'; + var ivText = 'oRAApY8YmWQx5a98rUVkhg=='; + var iv = Buffer.from(ivText, 'base64'); + + await s3Client.createBucket(bucketName); + s3Client.bucketExists = ()=>{return true;}; + + // encrypts and uploads + await s3Client.encryptAndUploadFile(bucketName, path, fileStream, encryptionKey, iv); + + // downloads and decrypts + var outContent = await s3Client.getAndDecryptFile(bucketName, path, encryptionKey, iv); + + // makes sure we got the right content back + assert.equal(inContent, outContent); + }); + }); +}); diff --git a/package-lock.json b/package-lock.json index 6babf6e68..e0d02d69e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3028,6 +3028,16 @@ "integrity": "sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==", "dev": true }, + "fs-extra": { + "version": "7.0.1", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-7.0.1.tgz", + "integrity": "sha512-YJDaCJZEnBmcbw13fvdAM9AwNOJwOzrE4pqMqBq5nFiEqXUqHwlK4B+3pUw6JNvfSPtX05xFHtYy/1ni01eGCw==", + "requires": { + "graceful-fs": "^4.1.2", + "jsonfile": "^4.0.0", + "universalify": "^0.1.0" + } + }, "fs-minipass": { "version": "1.2.7", "resolved": "https://registry.npmjs.org/fs-minipass/-/fs-minipass-1.2.7.tgz", @@ -4194,6 +4204,14 @@ "integrity": "sha512-o6/yDBYccGvTz1+QFevz6l6OBZ2+fMVu2JZ9CIhzsYRX4mjaK5IyX9eldUdCmga16zlgQxyrj5pt9kzuj2C02w==", "dev": true }, + "jsonfile": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz", + "integrity": "sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss=", + "requires": { + "graceful-fs": "^4.1.6" + } + }, "jsonwebtoken": { "version": "8.5.1", "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-8.5.1.tgz", @@ -4755,6 +4773,16 @@ } } }, + "mock-aws-s3": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/mock-aws-s3/-/mock-aws-s3-4.0.1.tgz", + "integrity": "sha512-KJp1zgNjogMXdFBghg2CJF+25zCmnsPXnFJE9ejkPxDLYTy2F5oY3iD1a3r4llgxpkh+EpxUe+HdTemPExqN9Q==", + "requires": { + "bluebird": "^3.5.1", + "fs-extra": "^7.0.1", + "underscore": "1.8.3" + } + }, "modifyjs": { "version": "0.3.1", "resolved": "https://registry.npmjs.org/modifyjs/-/modifyjs-0.3.1.tgz", @@ -7216,6 +7244,11 @@ "debug": "^2.2.0" } }, + "underscore": { + "version": "1.8.3", + "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.8.3.tgz", + "integrity": "sha1-Tz+1OxBuYJf8+ctBCfKl6b36UCI=" + }, "unique-string": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/unique-string/-/unique-string-1.0.0.tgz", @@ -7225,6 +7258,11 @@ "crypto-random-string": "^1.0.0" } }, + "universalify": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz", + "integrity": "sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==" + }, "unpipe": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", diff --git a/package.json b/package.json index 00cbb67b0..c0ad7be6e 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,7 @@ "express-mongo-sanitize": "^2.0.0", "express-query-boolean": "^2.0.0", "express-request-id": "^1.4.1", + "form-data": "^3.0.0", "fs-readfile-promise": "^3.0.1", "glob": "^7.1.6", "glob-promise": "^3.4.0", @@ -60,6 +61,7 @@ "json-key-validate": "^1.0.2", "jsonwebtoken": "^8.5.1", "lodash": "^4.17.14", + "mock-aws-s3": "^4.0.1", "moment": "^2.24.0", "mongodb": "^3.5.5", "mongoose": "^5.9.7",