storage: verifyConfig is now async

This commit is contained in:
Girish Ramakrishnan
2022-04-14 07:59:50 -05:00
parent a39e0ab934
commit 11f7be2065
6 changed files with 75 additions and 89 deletions
+31 -30
View File
@@ -25,16 +25,18 @@ exports = module.exports = {
_mockRestore: mockRestore
};
var assert = require('assert'),
const assert = require('assert'),
async = require('async'),
BoxError = require('../boxerror.js'),
constants = require('../constants.js'),
DataLayout = require('../datalayout.js'),
debug = require('debug')('box:storage/gcs'),
EventEmitter = require('events'),
GCS = require('@google-cloud/storage').Storage,
PassThrough = require('stream').PassThrough,
path = require('path');
path = require('path'),
safe = require('safetydance');
let GCS = require('@google-cloud/storage').Storage;
// test only
var originalGCS;
@@ -266,44 +268,43 @@ async function remount(apiConfig) {
assert.strictEqual(typeof apiConfig, 'object');
}
function testConfig(apiConfig, callback) {
async function testConfig(apiConfig) {
assert.strictEqual(typeof apiConfig, 'object');
assert.strictEqual(typeof callback, 'function');
if (typeof apiConfig.projectId !== 'string') return callback(new BoxError(BoxError.BAD_FIELD, 'projectId must be a string'));
if (!apiConfig.credentials || typeof apiConfig.credentials !== 'object') return callback(new BoxError(BoxError.BAD_FIELD, 'credentials must be an object'));
if (typeof apiConfig.credentials.client_email !== 'string') return callback(new BoxError(BoxError.BAD_FIELD, 'credentials.client_email must be a string'));
if (typeof apiConfig.credentials.private_key !== 'string') return callback(new BoxError(BoxError.BAD_FIELD, 'credentials.private_key must be a string'));
if (typeof apiConfig.projectId !== 'string') throw new BoxError(BoxError.BAD_FIELD, 'projectId must be a string');
if (!apiConfig.credentials || typeof apiConfig.credentials !== 'object') throw new BoxError(BoxError.BAD_FIELD, 'credentials must be an object');
if (typeof apiConfig.credentials.client_email !== 'string') throw new BoxError(BoxError.BAD_FIELD, 'credentials.client_email must be a string');
if (typeof apiConfig.credentials.private_key !== 'string') throw new BoxError(BoxError.BAD_FIELD, 'credentials.private_key must be a string');
if (typeof apiConfig.bucket !== 'string') return callback(new BoxError(BoxError.BAD_FIELD, 'bucket must be a string'));
if (typeof apiConfig.prefix !== 'string') return callback(new BoxError(BoxError.BAD_FIELD, 'prefix must be a string'));
if (typeof apiConfig.bucket !== 'string') throw new BoxError(BoxError.BAD_FIELD, 'bucket must be a string');
if (typeof apiConfig.prefix !== 'string') throw new BoxError(BoxError.BAD_FIELD, 'prefix must be a string');
// attempt to upload and delete a file with new credentials
var bucket = getBucket(apiConfig);
const bucket = getBucket(apiConfig);
var testFile = bucket.file(path.join(apiConfig.prefix, 'cloudron-testfile'));
const testFile = bucket.file(path.join(apiConfig.prefix, 'cloudron-testfile'));
var uploadStream = testFile.createWriteStream({ resumable: false });
uploadStream.write('testfilecontents');
uploadStream.end();
const uploadStream = testFile.createWriteStream({ resumable: false });
uploadStream.on('error', function(error) {
debug('testConfig: failed uploading cloudron-testfile', error);
if (error && error.code && (error.code == 403 || error.code == 404)) {
return callback(new BoxError(BoxError.BAD_FIELD, error.message));
}
await new Promise((resolve, reject) => {
uploadStream.on('error', function(error) {
debug('testConfig: failed uploading cloudron-testfile', error);
if (error && error.code && (error.code == 403 || error.code == 404)) {
return reject(new BoxError(BoxError.BAD_FIELD, error.message));
}
return callback(new BoxError(BoxError.EXTERNAL_ERROR, error.message));
});
uploadStream.on('finish', function() {
debug('testConfig: uploaded cloudron-testfile ' + JSON.stringify(arguments));
bucket.file(path.join(apiConfig.prefix, 'cloudron-testfile')).delete(function(error) {
if (error) return callback(new BoxError(BoxError.EXTERNAL_ERROR, error.message));
debug('testConfig: deleted cloudron-testfile');
callback();
return reject(new BoxError(BoxError.EXTERNAL_ERROR, error.message));
});
uploadStream.write('testfilecontents');
uploadStream.end();
uploadStream.on('finish', resolve);
});
debug('testConfig: uploaded cloudron-testfile');
const [delError] = await safe(bucket.file(path.join(apiConfig.prefix, 'cloudron-testfile')).delete());
if (delError) throw new BoxError(BoxError.EXTERNAL_ERROR, delError.message);
debug('testConfig: deleted cloudron-testfile');
}
function removePrivateFields(apiConfig) {