Comparing version 1.0.3 to 1.0.4
29
index.js
const crypt = require('./util/crypt') | ||
const {normalizeKey, normalizeIV} = require("./util/crypt"); | ||
const {DEFAULT_CRYPT_ALGO, normalizeKey, normalizeIV} = require("./util/crypt"); | ||
@@ -27,13 +27,24 @@ // adapted from https://stackoverflow.com/a/27724419 | ||
async function mobiletto (driverPath, key, secret, opts, encryptionKey = null, encryptionIV = null) { | ||
async function mobiletto (driverPath, key, secret, opts, encryption = null) { | ||
const driver = require(driverPath.includes('/') ? driverPath : `./drivers/${driverPath}/index.js`) | ||
const client = driver.storageClient(key, secret, opts) | ||
if (!(await client.testConfig())) { | ||
throw new MobilettoError(`mobiletti(${driverPath}) error: test API call failed`) | ||
throw new MobilettoError(`mobiletto(${driverPath}) error: test API call failed`) | ||
} | ||
if (encryptionKey === null) { | ||
if (encryption === null) { | ||
return client | ||
} | ||
const encKey = normalizeKey(encryptionKey) | ||
const iv = normalizeIV(encryptionIV, key) | ||
const encKey = normalizeKey(encryption.key) | ||
if (!encKey) { | ||
throw new MobilettoError(`mobiletto(${driverPath}): invalid encryption key`) | ||
} | ||
const iv = normalizeIV(encryption.iv, encKey) | ||
if (!iv) { | ||
throw new MobilettoError(`mobiletto(${driverPath}): invalid encryption IV`) | ||
} | ||
const enc = { | ||
key: encKey, | ||
iv, | ||
algo: encryption.algo || DEFAULT_CRYPT_ALGO | ||
} | ||
return { | ||
@@ -43,3 +54,3 @@ list: async (path) => client.list(path), | ||
read: async (path, callback) => { | ||
const cipher = crypt.startDecryptStream(encKey, iv) | ||
const cipher = crypt.startDecryptStream(enc) | ||
return client.read(path, | ||
@@ -61,3 +72,3 @@ (chunk) => { | ||
} | ||
const cipher = crypt.startEncryptStream(encKey, iv) | ||
const cipher = crypt.startEncryptStream(enc) | ||
return client.write(path, cryptGenerator(readFunc)) | ||
@@ -137,3 +148,3 @@ }, | ||
await streamHandler(stream).then(() => { | ||
console.log('streamhandler ended') | ||
// console.log('streamHandler ended') | ||
}) | ||
@@ -140,0 +151,0 @@ return count |
{ | ||
"name": "mobiletto", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "A storage abstraction layer", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -8,9 +8,9 @@ Mobiletto | ||
# Using storage services | ||
# Basic usage | ||
const { mobiletto } = require('mobiletto') | ||
// general usage | ||
// General usage | ||
const api = await mobiletto(driverName, key, secret, opts) | ||
// to use 'local' driver: | ||
// To use 'local' driver: | ||
// * key: base directory | ||
@@ -22,3 +22,3 @@ // * secret: ignored, can be null | ||
// to use 's3' driver: | ||
// To use 's3' driver: | ||
// * key: AWS access key | ||
@@ -33,7 +33,7 @@ // * secret: AWS secret key | ||
// list files | ||
// List files | ||
local.list() // --> returns an array of file objects | ||
s3.list() // --> returns an array of file objects | ||
// list files in a directory | ||
// List files in a directory | ||
const path = 'some/path' | ||
@@ -43,7 +43,7 @@ local.list(path) // --> returns an array of file objects | ||
// read metadata for a file | ||
// Read metadata for a file | ||
local.metadata(path) | ||
s3.metadata(path) | ||
// read a file | ||
// Read a file | ||
// Provide a callback that writes the data someplace | ||
@@ -54,3 +54,3 @@ const callback = (chunk) => { ... write chunk somewhere ... } | ||
// write a file | ||
// Write a file | ||
// Provide a generator function that yields chunks of data | ||
@@ -66,3 +66,3 @@ const generator = function* () { | ||
// delete a file | ||
// Delete a file | ||
// Quiet param is optional (default false), when set errors will not be thrown if the path does not exist | ||
@@ -73,3 +73,3 @@ const quiet = true | ||
// recursively delete a directory | ||
// Recursively delete a directory | ||
const recursive = true | ||
@@ -79,2 +79,17 @@ local.remove(path, {recursive, quiet}) | ||
# Transparent Encryption | ||
Enable transparent client-side encryption: | ||
// Pass encryption parameters | ||
const encryption = { | ||
key: randomstring.generate(128), // required, must be >= 16 chars | ||
iv: randomstring.generate(128), // optional | ||
algo: 'aes-256-cbc' // optional, aes-256-cbc is the default | ||
} | ||
const api = await mobiletto(driverName, key, secret, opts, encryption) | ||
// Subsequent write operations will encrypt data (client side) when writing | ||
// Subsequent read operations will decrypt data (client side) when reading | ||
// Path names will also be encrypted | ||
# Driver Interface | ||
@@ -81,0 +96,0 @@ A driver is any JS file that exports a 'storageClient' function with this signature: |
@@ -158,3 +158,3 @@ // To run the tests, you need a .env file one level above this directory | ||
const name = `test_file_${fileSuffix}` | ||
mobiletto(driverName, config.key, config.secret, config.opts, encryptionKey) | ||
mobiletto(driverName, config.key, config.secret, config.opts, {key: encryptionKey}) | ||
.then(api => { fixture = {api, name, randomData} }) | ||
@@ -161,0 +161,0 @@ .finally(done) |
@@ -7,2 +7,3 @@ // adapted from https://stackoverflow.com/a/64136185 | ||
const MIN_KEY_LEN = 16 | ||
const DEFAULT_CRYPT_ALGO = 'aes-256-cbc'; | ||
@@ -35,4 +36,4 @@ function normalizeKey (k) { | ||
function getCipher(key, iv) { | ||
return crypto.createCipheriv('aes-256-cbc', key, iv); | ||
function getCipher(enc) { | ||
return crypto.createCipheriv(enc.algo, enc.key, enc.iv); | ||
} | ||
@@ -51,4 +52,4 @@ | ||
function getDecipher(key, iv) { | ||
return crypto.createDecipheriv('aes-256-cbc', key, iv); | ||
function getDecipher(enc) { | ||
return crypto.createDecipheriv(enc.algo, enc.key, enc.iv); | ||
} | ||
@@ -65,4 +66,4 @@ | ||
const startEncryptStream = (key = KEY, iv = CRYPTO_IV) => getCipher(key, iv) | ||
const startDecryptStream = (key = KEY, iv = CRYPTO_IV) => getDecipher(key, iv) | ||
const startEncryptStream = (enc) => getCipher(enc) | ||
const startDecryptStream = (enc) => getDecipher(enc) | ||
const updateCryptStream = (cipher, data) => cipher.update(data) | ||
@@ -72,2 +73,4 @@ const closeCryptStream = (cipher) => cipher.final() | ||
module.exports = { | ||
DEFAULT_CRYPT_ALGO, | ||
setDefaultKey, setDefaultIV, | ||
encrypt, decrypt, | ||
@@ -74,0 +77,0 @@ normalizeKey, normalizeIV, |
53914
898
120