-
Notifications
You must be signed in to change notification settings - Fork 14
/
AzureBlobFiles.js
641 lines (576 loc) · 21.8 KB
/
AzureBlobFiles.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/*
Copyright 2019 Adobe. All rights reserved.
This file is licensed to you 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 REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
/* global AzureCredentials */ // for linter
const azure = require('@azure/storage-blob')
const joi = require('joi')
const stream = require('stream')
const mime = require('mime-types')
const fetch = require('node-fetch')
const crypto = require('crypto')
const { v4: uuidv4 } = require('uuid')
const xmlJS = require('xml-js')
const logger = require('@adobe/aio-lib-core-logging')('@adobe/aio-lib-files', { provider: 'debug' })
const utils = require('../utils')
const { Files, FilePermissions, UrlType } = require('../Files')
const { codes, logAndThrow } = require('../FilesError')
require('../types.jsdoc') // for VS Code autocomplete
const STREAM_BUFFER_SIZE = 4 * 1024 * 1024 // 4 MB
const STREAM_MAX_CONCURRENCY = 10
const AZURE_STORAGE_DOMAIN = 'blob.core.windows.net'
const DEFAULT_CDN_STORAGE_HOST = 'firefly.azureedge.net'
const STAGE_CDN_STORAGE_HOST = 'firefly-stage.azureedge.net'
const PUBLIC_CONTAINER_SUFFIX = '-public'
const {
getCliEnv, /* function */
STAGE_ENV /* string */
} = require('@adobe/aio-lib-env')
// todo move somewhere else
// eslint-disable-next-line jsdoc/require-jsdoc
function lookupMimeType (filePath) {
return mime.lookup(filePath) || 'application/octet-stream'
}
/**
* @class AzureBlobFiles
* @classdesc Cloud Files Implementation on top of Azure Blob
* @augments Files
* @hideconstructor
* @private
*/
class AzureBlobFiles extends Files {
/**
* [INTERNAL] Creates an instance of AzureBlobFiles. Use static init instead.
*
* @param {AzureCredentials} credentials {@link AzureCredentials}
* @param {object} tvm TVM Client instance
* @memberof AzureBlobFiles
* @private
*/
constructor (credentials = {}, tvm = null) {
super()
/** @private */
this.tvm = tvm
/** @private */
this.hasOwnCredentials = (tvm === null)
const cloned = utils.withHiddenFields(credentials, ['storageAccessKey', 'sasURLPrivate', 'sasURLPublic'])
logger.debug(`init AzureBlobFiles with config ${JSON.stringify(cloned, null, 2)}`)
const res = joi.object().label('credentials').keys({
// either
sasURLPrivate: joi.string().uri(),
sasURLPublic: joi.string().uri(),
// or
storageAccessKey: joi.string(),
storageAccount: joi.string(),
containerName: joi.string()
}).required().unknown()
.and('storageAccount', 'storageAccessKey', 'containerName')
.and('sasURLPrivate', 'sasURLPublic')
.xor('sasURLPrivate', 'storageAccount')
.validate(credentials)
if (res.error) {
logAndThrow(new codes.ERROR_BAD_ARGUMENT({ messageValues: [res.error.message], sdkDetails: cloned }))
}
/** @private */
this._azure = {}
/** @private */
this.credentials = utils.clone(credentials)
const env = getCliEnv()
/** @private */
this.defaultHostName = env === STAGE_ENV ? STAGE_CDN_STORAGE_HOST : DEFAULT_CDN_STORAGE_HOST
/**
* @private
* @type {azure.ContainerClient}
**/
this.containerClientPrivate = null
/**
* @private
* @type {azure.ContainerClient}
**/
this.containerClientPublic = null
/** @private */
this.usesSASCreds = null
if (credentials.sasURLPrivate) {
this._initWithNewCreds(credentials)
this.usesSASCreds = true
} else {
const azureCreds = new azure.StorageSharedKeyCredential(credentials.storageAccount, credentials.storageAccessKey)
const blobServiceClient = new azure.BlobServiceClient(`https://${credentials.storageAccount}.blob.core.windows.net/`, azureCreds)
this.containerClientPrivate = blobServiceClient.getContainerClient(credentials.containerName)
this.containerClientPublic = blobServiceClient.getContainerClient(credentials.containerName + PUBLIC_CONTAINER_SUFFIX)
this.usesSASCreds = false
}
}
/**
* Creates and return an instance of AzureBlobFiles. Also creates needed azure
* containers if credentials are not SAS
*
* @static
* @param {AzureCredentials} credentials {@link AzureCredentials}
* @param {object} tvm TVM Client instance
* @returns {Promise<AzureBlobFiles>} new instance
* @memberof AzureBlobFiles
*/
static async init (credentials, tvm) {
const azureFiles = new AzureBlobFiles(credentials, tvm)
// container sas creds are not allowed to create containers and so those
// credentials must point to already existing containers
if (!azureFiles.usesSASCreds) {
logger.debug('using azure storage account credentials')
// for the non sasCreds case we can make sure those containers exists
await azureFiles._wrapProviderRequest(
azureFiles.containerClientPrivate.createIfNotExists(),
{ containerName: credentials.containerName, storageAccount: credentials.storageAccount }
)
await azureFiles._wrapProviderRequest(
azureFiles.containerClientPublic.createIfNotExists({ access: 'blob' }),
{ containerName: credentials.containerName + PUBLIC_CONTAINER_SUFFIX, storageAccount: credentials.storageAccount }
)
// TODO !!!! => no need for the sign logic anymore
await azureFiles._addAccessPolicyIfNotExists()
return azureFiles
}
logger.debug('using azure SAS credentials')
return azureFiles
}
/* **************************** PRIVATE HELPERS ***************************** */
/**
* @typedef AzureProps
* @type {object}
* @property {azure.ContainerClient} containerClient private or public Azure blob container
* @property {azure.BlockBlobClient} blockBlobClient the azure blockBlobClient
* @private
*/
/**
* [Internal] Parses the input path to determine if it is public or private
* then returns an internal property object that can be used to access the file
*
* @param {RemotePathString} filePath {RemotePathString}
* @returns {AzureProps} object containing azure resources for the specified path
*
* @private
* @memberof AzureBlobFiles
*/
_propsForPath (filePath) {
// this fails with empty string, so set to '/'
const usePath = filePath || '/'
const azureProps = {}
if (Files._isRemotePublic(usePath)) {
azureProps.containerClient = this.containerClientPublic
} else {
azureProps.containerClient = this.containerClientPrivate
}
azureProps.blockBlobClient = azureProps.containerClient.getBlockBlobClient(usePath)
return azureProps
}
/**
* @memberof AzureBlobFiles
* @override
* @private
*/
async _initWithNewCreds (creds) {
let credentials = creds
// get credentails if not passed
if (!credentials && !this.hasOwnCredentials) {
// use TVM Client to get credentials, this will refresh token if it was expired
credentials = await utils.wrapTVMRequest(this.tvm.getAzureBlobCredentials())
}
if (credentials || !this.hasOwnCredentials) {
const azureCreds = new azure.AnonymousCredential()
const pipeline = azure.newPipeline(azureCreds)
this.containerClientPrivate = new azure.ContainerClient(credentials.sasURLPrivate, pipeline)
this.containerClientPublic = new azure.ContainerClient(credentials.sasURLPublic, pipeline)
}
}
/** [Internal] Set empty access policy
* @memberof AzureBlobFiles
* @private
*/
async _setAccessPolicy () {
const id = uuidv4()
// set access policy with new id and without any permissions
await this.containerClientPrivate.setAccessPolicy(undefined, [{ id, accessPolicy: { permission: '' } }])
}
/** [Internal] get Access policy ID
* @memberof AzureBlobFiles
* @private
*/
async _getAccessPolicy () {
// use API call as this.containerClientPrivate.getAccessPolicy calls fails for policy with empty permissions
const index = this.containerClientPrivate.url.lastIndexOf('/')
const containerName = this.containerClientPrivate.url.substring(index + 1, this.containerClientPrivate.url.length)
const resource = '/' + this.credentials.storageAccount + '/' + containerName + '\ncomp:acl\nrestype:container'
const date = new Date().toUTCString()
const sign = this._signRequest('GET', resource, date)
const reqHeaders = {
'x-ms-date': date,
'x-ms-version': '2019-02-02',
authorization: 'SharedKey ' + this.credentials.storageAccount + ':' + sign
}
const url = this.containerClientPrivate.url + '?restype=container&comp=acl'
const res = await fetch(url, { method: 'GET', headers: reqHeaders })
const acl = await res.text()
const aclObj = xmlJS.xml2js(acl)
let id
if (aclObj.elements) {
const signedIdentifiers = aclObj.elements[0]
if (signedIdentifiers.elements) {
if (signedIdentifiers.elements.length > 1) {
const msg = 'Container has one or more custom policies defined. Either remove all custom policies or use another container.'
logAndThrow(new codes.ERROR_INIT_FAILURE({ messageValues: [msg], sdkDetails: {} }))
}
const signedIdentifier = signedIdentifiers.elements[0].elements
signedIdentifier.forEach(function (val, index, arr) {
if (val.name === 'Id') {
id = val.elements[0].text
return id
}
})
}
}
return id
}
/** [Internal] Sign the given request
* @memberof AzureBlobFiles
* @private
*/
_signRequest (method, resource, date) {
const canonicalHeaders = 'x-ms-date:' + date + '\n' + 'x-ms-version:2019-02-02'
const stringToSign = method + '\n\n\n\n\n\n\n\n\n\n\n\n' + canonicalHeaders + '\n' + resource
return crypto.createHmac('sha256', Buffer.from(this.credentials.storageAccessKey, 'base64')).update(stringToSign, 'utf8').digest('base64')
}
/**
* [Internal] Adds a default access policy if none exists
*
* @private
*/
async _addAccessPolicyIfNotExists () {
const identifier = await this._getAccessPolicy()
if (identifier) {
logger.debug('found access policy with identifier ' + identifier)
if (this.hasOwnCredentials) {
// check if identifier is custom or not
if (this._isCustomPolicy(identifier)) {
const msg = 'Container has one or more custom policies defined. Either remove all custom policies or use another container.'
logAndThrow(new codes.ERROR_INIT_FAILURE({ messageValues: [msg], sdkDetails: {} }))
}
}
} else {
logger.debug('adding default access policy')
await this._setAccessPolicy()
}
}
/**
* [Internal] Check if identifier is custom or not
*
* @private
*/
_isCustomPolicy (identifier) {
// if its uuidv4 type of identifier then consider it as non custom, this is to support any already created policies by aio-lib-flies
const testUUIDv4 = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/gi
return !(testUUIDv4.test(identifier))
}
/* **************************** PRIVATE METHODS TO IMPLEMENT ***************************** */
/**
* @memberof AzureBlobFiles
* @override
* @private
*/
async getFileInfo (filePath) {
Files._throwIfRemoteDirectory(filePath)
const azureProps = this._propsForPath(filePath)
// Note: this throws on 404
const blobProps = await this._wrapProviderRequest(
azureProps.blockBlobClient.getProperties(),
{ filePath },
filePath
)
const blobDesc = {
name: filePath,
creationTime: blobProps.createdOn,
lastModified: blobProps.lastModified,
etag: blobProps.etag,
contentLength: blobProps.contentLength,
contentType: blobProps.contentType,
isDirectory: Files._isRemoteDirectory(filePath),
isPublic: Files._isRemotePublic(filePath),
url: this._getUrl(filePath),
internalUrl: this._getUrl(filePath, UrlType.internal)
}
return blobDesc
}
/**
* @memberof AzureBlobFiles
* @override
* @private
*/
async _listFolder (filePath) {
const __listFolder = async (_filePath, /** @type {AzureProps} */azureProps) => {
const elements = []
const iterator = azureProps.containerClient.listBlobsFlat({ prefix: _filePath }).byPage({ maxPageSize: 1000 })
const response = (await iterator.next()).value
for (const item of response.segment.blobItems) {
elements.push({
name: item.name,
creationTime: item.properties.createdOn,
lastModified: item.properties.lastModified,
etag: item.properties.etag,
contentLength: item.properties.contentLength,
contentType: item.properties.contentType,
isDirectory: Files._isRemoteDirectory(item.name),
isPublic: Files._isRemotePublic(item.name),
url: this._getUrl(item.name),
internalUrl: this._getUrl(item.name, UrlType.internal)
})
}
return elements
}
const azureProps = this._propsForPath(filePath)
// case 1 we list the root => need to list both public and private containers
if (Files._isRemoteRoot(filePath)) {
const res = await Promise.all([
__listFolder(filePath, azureProps),
__listFolder(Files.publicPrefix,
this._propsForPath(Files.publicPrefix))])
return res[0].concat(res[1])
}
// case 2 we list a non root folder => only list one container
return __listFolder(filePath, azureProps)
}
/**
* @memberof AzureBlobFiles
* @override
* @private
*/
async _fileExists (filePath) {
const azureProps = this._propsForPath(filePath)
// wrapProviderRequest returns null on 404, see _wrapProviderRequest
const blobProps = await this._wrapProviderRequest(azureProps.blockBlobClient.getProperties(), { filePath })
return !!blobProps
}
/**
* @memberof AzureBlobFiles
* @override
* @private
*/
async _deleteFile (file) {
const filePath = file.name || file
const azureProps = this._propsForPath(filePath)
await this._wrapProviderRequest(azureProps.blockBlobClient.delete(), { filePath }, filePath)
}
/**
* @memberof AzureBlobFiles
* @override
* @private
*/
async _createReadStream (filePath, options) {
const azureProps = this._propsForPath(filePath)
return (await this._wrapProviderRequest(azureProps.blockBlobClient.download(options.position, options.length), { filePath, options }, filePath)).readableStreamBody
}
/**
* @memberof AzureBlobFiles
* @override
* @private
*/
async _createWriteStream (filePath) {
// NOTE: this is an experimental method
const emitErrorIfPromiseRejects = (azureBlobWriteStream) => {
azureBlobWriteStream._promise.catch(e => { azureBlobWriteStream.emit('error', e) })
}
// Azure does not seem to support creating write streams OOTB
// this is a hack using PassThrough streams
const AzureBlobWriteStream = class AzureBlobWriteStream extends stream.PassThrough {
constructor (azureProps, wrapProviderRequest) {
super()
this.bytesWritten = 0
this.on('data', chunk => { this.bytesWritten += chunk.length })
this._promise = wrapProviderRequest(
azureProps.blockBlobClient.uploadStream(
// here is the trick, uploadStream will start reading from this while the user
// writes to this stream, this works because this class extends PassThrough
this,
STREAM_BUFFER_SIZE,
STREAM_MAX_CONCURRENCY,
{ blobHTTPHeaders: { blobContentType: lookupMimeType(filePath) } }
),
{ filePath },
filePath
).then(_ => this.bytesWritten)
}
// emit an error only if user tries to writes something
_write (chunk, enc, cb) {
emitErrorIfPromiseRejects(this)
super._write(chunk, enc, cb)
}
/* istanbul ignore next */
_writev (chunks, cb) {
emitErrorIfPromiseRejects(this)
super._writev(chunks, cb)
}
emit (event, ...args) {
// stale the finish event until the data got written
if (event === 'finish') {
this._promise.then(res => super.emit(event, res, ...args))
} else {
super.emit(event, ...args)
}
}
}
return new AzureBlobWriteStream(this._propsForPath(filePath), this._wrapProviderRequest.bind(this))
}
/**
* @memberof AzureBlobFiles
* @override
* @private
*/
async _writeStream (filePath, content) {
const azureProps = this._propsForPath(filePath)
const uploadOptions = { blobHTTPHeaders: { blobContentType: lookupMimeType(filePath) } }
let length = 0
// we know that dependency will wait until end event, so we can simply plug
// to data event.
content.on('data', chunk => { length += chunk.length })
await this._wrapProviderRequest(
// there is potential for perf improvements here, more in the azure docs
azureProps.blockBlobClient.uploadStream(
content,
STREAM_BUFFER_SIZE,
STREAM_MAX_CONCURRENCY,
uploadOptions
),
// error details with content type instead of content in case of error
{ filePath, contentType: 'Readable' }, filePath)
return length
}
/**
* @memberof AzureBlobFiles
* @override
* @private
*/
async _writeBuffer (filePath, content) {
const azureProps = this._propsForPath(filePath)
const uploadOptions = { blobHTTPHeaders: { blobContentType: lookupMimeType(filePath) } }
// think about it: supports on upload progress callback
await this._wrapProviderRequest(azureProps.blockBlobClient.upload(content, content.length, uploadOptions), { filePath, contentType: 'Buffer' }, filePath)
return content.length
}
/**
* @memberof AzureBlobFiles
* @override
* @private
*/
async _copyRemoteToRemoteFile (srcPath, destPath) {
const destProps = this._propsForPath(destPath)
await this._wrapProviderRequest(destProps.blockBlobClient.startCopyFromURL(this._propsForPath(srcPath).blockBlobClient.url), { srcPath, destPath }, srcPath)
}
/**
* @memberof AzureBlobFiles
* @override
* @private
*/
_getUrl (filePath, urlType = UrlType.external) {
const azureURL = this._propsForPath(filePath).blockBlobClient.url.split('?')[0]
if (urlType === UrlType.internal) {
return azureURL
}
let hostNameToUse = this.defaultHostName
if (this.hasOwnCredentials) {
if (this.credentials.hostName) {
hostNameToUse = this.credentials.hostName
} else {
return azureURL
}
}
const index = azureURL.indexOf(AZURE_STORAGE_DOMAIN)
return 'https://' + hostNameToUse + azureURL.substring(index + AZURE_STORAGE_DOMAIN.length)
}
/**
* @memberof AzureBlobFiles
* @override
* @private
*/
async _getPresignUrl (filePath, options) {
if (!options || !options.expiryInSeconds) {
logAndThrow(new codes.ERROR_MISSING_OPTION({ messageValues: ['expiryInSeconds'], sdkDetails: [filePath, options] }))
}
if (!options.permissions) {
options.permissions = FilePermissions.READ
}
const urlType = options.urlType
delete options.urlType // remove extra key for presign options
const presignOptions = Object.assign({ blobName: filePath }, options)
let cred
if (this.hasOwnCredentials) {
// generate signature based on options and using own credentials
cred = await this._getAzureBlobPresignCredentials(presignOptions)
} else {
cred = await utils.wrapTVMRequest(this.tvm.getAzureBlobPresignCredentials(presignOptions))
}
return this._getUrl(filePath, urlType) + '?' + cred.signature
}
/**
* @memberof AzureBlobFiles
* @private
*/
async _getAzureBlobPresignCredentials (params) {
if (this.usesSASCreds) {
const msg = 'generatePresignURL is not supported with Azure Container SAS credentials, please initialize the SDK with Azure storage account credentials instead'
logAndThrow(new codes.ERROR_UNSUPPORTED_OPERATION({ messageValues: [msg], sdkDetails: params }))
}
const sharedKeyCredential = new azure.StorageSharedKeyCredential(this.credentials.storageAccount, this.credentials.storageAccessKey)
const containerName = this.credentials.containerName
// generate SAS token
const expiryTime = new Date(Date.now() + (1000 * params.expiryInSeconds))
const identifier = await this._getAccessPolicy()
const permissions = azure.BlobSASPermissions.parse(params.permissions)
const commonSasParams = {
permissions: permissions.toString(),
expiresOn: expiryTime,
blobName: params.blobName,
identifier
}
const sasQueryParamsPrivate = azure.generateBlobSASQueryParameters({ ...commonSasParams, containerName }, sharedKeyCredential)
return {
signature: sasQueryParamsPrivate.toString()
}
}
/**
* @memberof AzureBlobFiles
* @private
*/
async _revokePresignURLs () {
if (this.usesSASCreds) {
const msg = 'revokeAllPresignURLs is not supported with Azure Container SAS credentials, please initialize the SDK with Azure storage account credentials instead'
logAndThrow(new codes.ERROR_UNSUPPORTED_OPERATION({ messageValues: [msg], sdkDetails: {} }))
}
await this._setAccessPolicy()
}
/**
* @memberof AzureBlobFiles
* @override
* @private
*/
async _revokeAllPresignURLs () {
if (this.hasOwnCredentials) {
// revoke signature using own credentials
await this._revokePresignURLs()
} else {
await utils.wrapTVMRequest(this.tvm.revokePresignURLs())
}
}
/**
* @memberof AzureBlobFiles
* @override
* @private
*/
_statusFromProviderError (e) {
return e.response && e.response.status
}
}
module.exports = { AzureBlobFiles }