mobiletto 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -11
- package/index.js +20 -9
- package/package.json +1 -1
- package/test/index.spec.js +1 -1
- package/util/crypt.js +9 -6
package/README.md
CHANGED
|
@@ -5,20 +5,20 @@ Mobiletto is a JavaScript storage abstraction layer.
|
|
|
5
5
|
|
|
6
6
|
It supports apps that are agnostic to where files are stored.
|
|
7
7
|
|
|
8
|
-
#
|
|
8
|
+
# Basic usage
|
|
9
9
|
const { mobiletto } = require('mobiletto')
|
|
10
10
|
|
|
11
|
-
//
|
|
11
|
+
// General usage
|
|
12
12
|
const api = await mobiletto(driverName, key, secret, opts)
|
|
13
13
|
|
|
14
|
-
//
|
|
14
|
+
// To use 'local' driver:
|
|
15
15
|
// * key: base directory
|
|
16
16
|
// * secret: ignored, can be null
|
|
17
17
|
// * opts object:
|
|
18
18
|
// * mode: optional, filesystem permissions to set when creating new files and directories, default is 0700
|
|
19
19
|
const local = await mobiletto('local', '/home/ubuntu/tmp', null, {mode: '0700'})
|
|
20
20
|
|
|
21
|
-
//
|
|
21
|
+
// To use 's3' driver:
|
|
22
22
|
// * key: AWS access key
|
|
23
23
|
// * secret: AWS secret key
|
|
24
24
|
// * opts object:
|
|
@@ -28,26 +28,26 @@ It supports apps that are agnostic to where files are stored.
|
|
|
28
28
|
// * delimiter: optional, directory delimiter, default is '/'
|
|
29
29
|
const s3 = await mobiletto('s3', aws_key, aws_secret, {bucket: 'bk', region: 'us-east-1'})
|
|
30
30
|
|
|
31
|
-
//
|
|
31
|
+
// List files
|
|
32
32
|
local.list() // --> returns an array of file objects
|
|
33
33
|
s3.list() // --> returns an array of file objects
|
|
34
34
|
|
|
35
|
-
//
|
|
35
|
+
// List files in a directory
|
|
36
36
|
const path = 'some/path'
|
|
37
37
|
local.list(path) // --> returns an array of file objects
|
|
38
38
|
s3.list(path) // --> returns an array of file objects
|
|
39
39
|
|
|
40
|
-
//
|
|
40
|
+
// Read metadata for a file
|
|
41
41
|
local.metadata(path)
|
|
42
42
|
s3.metadata(path)
|
|
43
43
|
|
|
44
|
-
//
|
|
44
|
+
// Read a file
|
|
45
45
|
// Provide a callback that writes the data someplace
|
|
46
46
|
const callback = (chunk) => { ... write chunk somewhere ... }
|
|
47
47
|
local.read(path, callback)
|
|
48
48
|
s3.read(path, callback)
|
|
49
49
|
|
|
50
|
-
//
|
|
50
|
+
// Write a file
|
|
51
51
|
// Provide a generator function that yields chunks of data
|
|
52
52
|
const generator = function* () {
|
|
53
53
|
while ( ... more-data-to-return ... ) {
|
|
@@ -58,17 +58,32 @@ It supports apps that are agnostic to where files are stored.
|
|
|
58
58
|
local.write(path, generator)
|
|
59
59
|
s3.write(path, generator)
|
|
60
60
|
|
|
61
|
-
//
|
|
61
|
+
// Delete a file
|
|
62
62
|
// Quiet param is optional (default false), when set errors will not be thrown if the path does not exist
|
|
63
63
|
const quiet = true
|
|
64
64
|
local.remove(path, {quiet})
|
|
65
65
|
s3.remove(path, {quiet})
|
|
66
66
|
|
|
67
|
-
//
|
|
67
|
+
// Recursively delete a directory
|
|
68
68
|
const recursive = true
|
|
69
69
|
local.remove(path, {recursive, quiet})
|
|
70
70
|
s3.remove(path, {recursive, quiet})
|
|
71
71
|
|
|
72
|
+
# Transparent Encryption
|
|
73
|
+
Enable transparent client-side encryption:
|
|
74
|
+
|
|
75
|
+
// Pass encryption parameters
|
|
76
|
+
const encryption = {
|
|
77
|
+
key: randomstring.generate(128), // required, must be >= 16 chars
|
|
78
|
+
iv: randomstring.generate(128), // optional
|
|
79
|
+
algo: 'aes-256-cbc' // optional, aes-256-cbc is the default
|
|
80
|
+
}
|
|
81
|
+
const api = await mobiletto(driverName, key, secret, opts, encryption)
|
|
82
|
+
|
|
83
|
+
// Subsequent write operations will encrypt data (client side) when writing
|
|
84
|
+
// Subsequent read operations will decrypt data (client side) when reading
|
|
85
|
+
// Path names will also be encrypted
|
|
86
|
+
|
|
72
87
|
# Driver Interface
|
|
73
88
|
A driver is any JS file that exports a 'storageClient' function with this signature:
|
|
74
89
|
|
package/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const crypt = require('./util/crypt')
|
|
2
|
-
const {normalizeKey, normalizeIV} = require("./util/crypt");
|
|
2
|
+
const {DEFAULT_CRYPT_ALGO, normalizeKey, normalizeIV} = require("./util/crypt");
|
|
3
3
|
|
|
4
4
|
// adapted from https://stackoverflow.com/a/27724419
|
|
5
5
|
function MobilettoError (message, err) {
|
|
@@ -24,22 +24,33 @@ function MobilettoNotFoundError (message) {
|
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
async function mobiletto (driverPath, key, secret, opts,
|
|
27
|
+
async function mobiletto (driverPath, key, secret, opts, encryption = null) {
|
|
28
28
|
const driver = require(driverPath.includes('/') ? driverPath : `./drivers/${driverPath}/index.js`)
|
|
29
29
|
const client = driver.storageClient(key, secret, opts)
|
|
30
30
|
if (!(await client.testConfig())) {
|
|
31
|
-
throw new MobilettoError(`
|
|
31
|
+
throw new MobilettoError(`mobiletto(${driverPath}) error: test API call failed`)
|
|
32
32
|
}
|
|
33
|
-
if (
|
|
33
|
+
if (encryption === null) {
|
|
34
34
|
return client
|
|
35
35
|
}
|
|
36
|
-
const encKey = normalizeKey(
|
|
37
|
-
|
|
36
|
+
const encKey = normalizeKey(encryption.key)
|
|
37
|
+
if (!encKey) {
|
|
38
|
+
throw new MobilettoError(`mobiletto(${driverPath}): invalid encryption key`)
|
|
39
|
+
}
|
|
40
|
+
const iv = normalizeIV(encryption.iv, encKey)
|
|
41
|
+
if (!iv) {
|
|
42
|
+
throw new MobilettoError(`mobiletto(${driverPath}): invalid encryption IV`)
|
|
43
|
+
}
|
|
44
|
+
const enc = {
|
|
45
|
+
key: encKey,
|
|
46
|
+
iv,
|
|
47
|
+
algo: encryption.algo || DEFAULT_CRYPT_ALGO
|
|
48
|
+
}
|
|
38
49
|
return {
|
|
39
50
|
list: async (path) => client.list(path),
|
|
40
51
|
metadata: async (path) => client.metadata(path),
|
|
41
52
|
read: async (path, callback) => {
|
|
42
|
-
const cipher = crypt.startDecryptStream(
|
|
53
|
+
const cipher = crypt.startDecryptStream(enc)
|
|
43
54
|
return client.read(path,
|
|
44
55
|
(chunk) => {
|
|
45
56
|
return callback(crypt.updateCryptStream(cipher, chunk))
|
|
@@ -56,7 +67,7 @@ async function mobiletto (driverPath, key, secret, opts, encryptionKey = null, e
|
|
|
56
67
|
}
|
|
57
68
|
yield cipher.final()
|
|
58
69
|
}
|
|
59
|
-
const cipher = crypt.startEncryptStream(
|
|
70
|
+
const cipher = crypt.startEncryptStream(enc)
|
|
60
71
|
return client.write(path, cryptGenerator(readFunc))
|
|
61
72
|
},
|
|
62
73
|
remove: async (path, options) => {
|
|
@@ -131,7 +142,7 @@ async function streamReader (stream, callback, endCallback) {
|
|
|
131
142
|
})
|
|
132
143
|
}
|
|
133
144
|
await streamHandler(stream).then(() => {
|
|
134
|
-
console.log('
|
|
145
|
+
// console.log('streamHandler ended')
|
|
135
146
|
})
|
|
136
147
|
return count
|
|
137
148
|
}
|
package/package.json
CHANGED
package/test/index.spec.js
CHANGED
|
@@ -155,7 +155,7 @@ for (const driverName of Object.keys(DRIVER_CONFIG)) {
|
|
|
155
155
|
let fixture
|
|
156
156
|
beforeEach((done) => {
|
|
157
157
|
const name = `test_file_${fileSuffix}`
|
|
158
|
-
mobiletto(driverName, config.key, config.secret, config.opts, encryptionKey)
|
|
158
|
+
mobiletto(driverName, config.key, config.secret, config.opts, {key: encryptionKey})
|
|
159
159
|
.then(api => { fixture = {api, name, randomData} })
|
|
160
160
|
.finally(done)
|
|
161
161
|
})
|
package/util/crypt.js
CHANGED
|
@@ -4,6 +4,7 @@ const shasum = require('shasum')
|
|
|
4
4
|
|
|
5
5
|
const WARN_PLAINTEXT = !process.env.IGNORE_DISABLED_ENCRYPTION
|
|
6
6
|
const MIN_KEY_LEN = 16
|
|
7
|
+
const DEFAULT_CRYPT_ALGO = 'aes-256-cbc';
|
|
7
8
|
|
|
8
9
|
function normalizeKey (k) {
|
|
9
10
|
return (typeof k === 'string' && k.trim().length > MIN_KEY_LEN)
|
|
@@ -31,8 +32,8 @@ function setDefaultIV (iv) {
|
|
|
31
32
|
CRYPTO_IV = normalizeIV(iv, KEY)
|
|
32
33
|
}
|
|
33
34
|
|
|
34
|
-
function getCipher(
|
|
35
|
-
return crypto.createCipheriv(
|
|
35
|
+
function getCipher(enc) {
|
|
36
|
+
return crypto.createCipheriv(enc.algo, enc.key, enc.iv);
|
|
36
37
|
}
|
|
37
38
|
|
|
38
39
|
function encrypt (plainText, key = KEY, iv = CRYPTO_IV, outputEncoding = 'base64') {
|
|
@@ -46,8 +47,8 @@ function encrypt (plainText, key = KEY, iv = CRYPTO_IV, outputEncoding = 'base64
|
|
|
46
47
|
return Buffer.concat([cipher.update(plainText), cipher.final()]).toString(outputEncoding)
|
|
47
48
|
}
|
|
48
49
|
|
|
49
|
-
function getDecipher(
|
|
50
|
-
return crypto.createDecipheriv(
|
|
50
|
+
function getDecipher(enc) {
|
|
51
|
+
return crypto.createDecipheriv(enc.algo, enc.key, enc.iv);
|
|
51
52
|
}
|
|
52
53
|
|
|
53
54
|
function decrypt (cipherText, key = KEY, iv = CRYPTO_IV, outputEncoding = 'utf8') {
|
|
@@ -59,12 +60,14 @@ function decrypt (cipherText, key = KEY, iv = CRYPTO_IV, outputEncoding = 'utf8'
|
|
|
59
60
|
return Buffer.concat([cipher.update(data), cipher.final()]).toString(outputEncoding)
|
|
60
61
|
}
|
|
61
62
|
|
|
62
|
-
const startEncryptStream = (
|
|
63
|
-
const startDecryptStream = (
|
|
63
|
+
const startEncryptStream = (enc) => getCipher(enc)
|
|
64
|
+
const startDecryptStream = (enc) => getDecipher(enc)
|
|
64
65
|
const updateCryptStream = (cipher, data) => cipher.update(data)
|
|
65
66
|
const closeCryptStream = (cipher) => cipher.final()
|
|
66
67
|
|
|
67
68
|
module.exports = {
|
|
69
|
+
DEFAULT_CRYPT_ALGO,
|
|
70
|
+
setDefaultKey, setDefaultIV,
|
|
68
71
|
encrypt, decrypt,
|
|
69
72
|
normalizeKey, normalizeIV,
|
|
70
73
|
startEncryptStream, startDecryptStream, updateCryptStream, closeCryptStream
|