From 6d2cfe911d28e3706cfbb161c51cb9733baeee27 Mon Sep 17 00:00:00 2001 From: Bart Jan van Assen Date: Tue, 9 Jun 2020 20:14:51 +0200 Subject: [PATCH] Feature/support multiple environment keys (#55) * add ability to provide azure keys per environment * add documentation for the azure environment keys * singleton variable will cause environment issues * adjust documentation according to new process env vars --- README.md | 31 +++++++++++++++++++------------ src/io-methods/azure.js | 21 ++++++++------------- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 1484c4a..de5e15a 100644 --- a/README.md +++ b/README.md @@ -128,7 +128,7 @@ Example config.js ```js // config.js -exports = { +module.exports = { // The username that must be provided via HTTP auth when calling the import-map-deployer username: "admin", // The password that must be provided via HTTP auth when calling the import-map-deployer @@ -224,18 +224,25 @@ config.json: Note, that you must have environment variables `AZURE_STORAGE_ACCOUNT` and `AZURE_STORAGE_ACCESS_KEY`, or `AZURE_STORAGE_CONNECTION_STRING` defined for authentication. -config.json: +If you wish to provide custom authentication keys for specific environments you can provide it also via the keys `azureConnectionString`, `azureAccount` or `azureAccessKey`. -```json -{ - "manifestFormat": "importmap", - "locations": { - "prod": { - "azureContainer": "static", - "azureBlob": "importmap.json" - } - } -} +**Its not recommended to put authentication keys in code. Always provide them via environment variables.** + +config.js: + +```js +module.exports = { + manifestFormat: "importmap", + locations: { + prod: { + azureContainer: "static", + azureBlob: "importmap.json", + azureConnectionString: process.env.AZURE_STORAGE_ACCOUNT_PROD, // optional + azureAccount: process.env.AZURE_STORAGE_ACCOUNT_PROD, // optional + azureAccessKey: process.env.AZURE_STORAGE_ACCOUNT_PROD, // optional + }, + }, +}; ``` ### Google Cloud Storage diff --git a/src/io-methods/azure.js b/src/io-methods/azure.js index 228b388..4ac30b8 100644 --- a/src/io-methods/azure.js +++ b/src/io-methods/azure.js @@ -3,12 +3,12 @@ const { StorageSharedKeyCredential, } = require("@azure/storage-blob"); -let blobService; - -async function createBlobService() { - const connectionString = process.env.AZURE_STORAGE_CONNECTION_STRING; - const account = process.env.AZURE_STORAGE_ACCOUNT; - const accessKey = process.env.AZURE_STORAGE_ACCESS_KEY; +async function createBlobService(target) { + const connectionString = + target.azureConnectionString || process.env.AZURE_STORAGE_CONNECTION_STRING; + const account = target.azureAccount || process.env.AZURE_STORAGE_ACCOUNT; + const accessKey = + target.azureAccessKey || process.env.AZURE_STORAGE_ACCESS_KEY; if (connectionString) { return await BlobServiceClient.fromConnectionString(connectionString); } else if (account && accessKey) { @@ -27,11 +27,6 @@ async function createBlobService() { } } -async function getBlobService() { - blobService = blobService || (await createBlobService()); - return blobService; -} - async function streamToString(readableStream) { return new Promise((resolve, reject) => { const chunks = []; @@ -47,7 +42,7 @@ async function streamToString(readableStream) { // Reference: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/storage/storage-blob#download-a-blob-and-convert-it-to-a-string-nodejs exports.readManifest = async function (target) { - const blobService = await getBlobService(); + const blobService = await createBlobService(target); const containerClient = blobService.getContainerClient(target.azureContainer); const blobClient = containerClient.getBlobClient(target.azureBlob); @@ -59,7 +54,7 @@ exports.readManifest = async function (target) { }; exports.writeManifest = async function (target, content) { - const blobService = await getBlobService(); + const blobService = await createBlobService(target); const containerClient = blobService.getContainerClient(target.azureContainer); const blockBlobClient = containerClient.getBlockBlobClient(target.azureBlob); return await blockBlobClient.upload(content, content.length, {