Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Get parameters
let account = req.params.account;
let container = req.params.container;
let level = req.params.level;
// Check that the client is authorized to access given account and container
await req.authorize({level, account, container, levelIsReadOnly: level === 'read-only'});
// Check that the account exists
if (!this.azureAccounts[account]) {
return res.reportError('ResourceNotFound',
`Account '${level}' not found, can't delegate access.`);
}
// Construct client
let blob = new azure.Blob({
accountId: account,
accessKey: this.azureAccounts[account],
});
// Create container ignore error, if it already exists
if (level === 'read-write') {
const key = `${account}/${container}`;
if (!containerLastCreated[key] || new Date() - containerLastCreated[key] > 6 * 3600 * 1000) {
try {
await blob.createContainer(container);
} catch (err) {
if (err.code !== 'ContainerAlreadyExists') {
throw err;
}
}
containerLastCreated[key] = new Date();
const backupContainer = async ({azureCreds, s3, bucket, containerName, utils}) => {
const stream = new zlib.createGzip();
const container = new azure.Blob(azureCreds);
// Versioning is enabled in the backups bucket so we just overwrite the
// previous backup every time. The bucket is configured to delete previous
// versions after N days, but the current version will never be deleted.
let upload = s3.upload({
Bucket: bucket,
Key: `${azureCreds.accountId}/container/${containerName}`,
Body: stream,
StorageClass: 'STANDARD_IA',
}).promise();
let count = 0;
let nextUpdateCount = 1000;
let marker;
do {
let results = await container.listBlobs(containerName, {marker});
let restoreContainer = async ({azureCreds, s3, bucket, containerName, destContainerName, versionId, utils}) => {
let blobsvc = new azure.Blob(azureCreds);
try {
await blobsvc.createContainer(destContainerName);
} catch (err) {
if (err.code !== 'ContainerAlreadyExists') {
throw err;
}
throw new Error(`Refusing to overwrite container ${destContainerName} as it already exists!`);
}
try {
await s3.headObject({
Bucket: bucket,
Key: `${azureCreds.accountId}/container/${containerName}`,
VersionId: versionId,
}).promise();
}, async function(req, res) {
let account = req.params.account;
let continuationToken = req.query.continuationToken || null;
let blob = new azure.Blob({
accountId: account,
accessKey: this.azureAccounts[account],
});
let result = await blob.listContainers({marker: continuationToken});
let data = {containers: result.containers.map(c => c.name)};
if (result.nextMarker) {
data.continuationToken = result.nextMarker;
}
return res.reply(data);
});