Skip to content

Commit

Permalink
Feature/support multiple environment keys (#55)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
BartJanvanAssen committed Jun 9, 2020
1 parent f72390d commit 6d2cfe9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
31 changes: 19 additions & 12 deletions README.md
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
21 changes: 8 additions & 13 deletions src/io-methods/azure.js
Expand Up @@ -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) {
Expand All @@ -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 = [];
Expand All @@ -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);

Expand All @@ -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, {
Expand Down

0 comments on commit 6d2cfe9

Please sign in to comment.