diff --git a/src/scripts/backuptests3.sh b/src/scripts/backuptests3.sh new file mode 100755 index 000000000..28917561b --- /dev/null +++ b/src/scripts/backuptests3.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +set -eu -o pipefail + +readonly s3_url="$1" +export AWS_ACCESS_KEY_ID="$2" +export AWS_SECRET_ACCESS_KEY="$3" +export AWS_DEFAULT_REGION="$4" + +echo "Test Content" | aws s3 cp - "${s3_url}" + +aws s3 rm "${s3_url}" diff --git a/src/storage/s3.js b/src/storage/s3.js index efbfd0abe..a646c75a5 100644 --- a/src/storage/s3.js +++ b/src/storage/s3.js @@ -18,6 +18,7 @@ var assert = require('assert'), AWS = require('aws-sdk'), safe = require('safetydance'), SettingsError = require('../settings.js').SettingsError, + shell = require('../shell.js'), superagent = require('superagent'); function getBackupCredentials(apiConfig, callback) { @@ -164,6 +165,8 @@ function testConfig(apiConfig, callback) { if (typeof apiConfig.prefix !== 'string') return callback(new SettingsError(SettingsError.BAD_FIELD, 'prefix must be a string')); // attempt to upload and delete a file with new credentials + // First use the javascript api, to get better feedback, then use aws cli tool + // The javascript api always autodetects the correct settings, regardless of the region provided, the cli tool does not getBackupCredentials(apiConfig, function (error, credentials) { if (error) return callback(error); @@ -185,7 +188,17 @@ function testConfig(apiConfig, callback) { s3.deleteObject(params, function (error) { if (error) return callback(new SettingsError(SettingsError.EXTERNAL_ERROR, error.message)); - callback(); + // now perform the same as what we do in the backup shell scripts + var BACKUP_TEST_CMD = require('path').join(__dirname, '../scripts/backuptests3.sh'); + var tmpUrl = 's3://' + apiConfig.bucket + '/' + apiConfig.prefix + '/testfile'; + var args = [ tmpUrl, apiConfig.accessKeyId, apiConfig.secretAccessKey, apiConfig.region ]; + + // if this fails the region is wrong, otherwise we would have failed earlier. + shell.exec('backupTestS3', BACKUP_TEST_CMD, args, function (error) { + if (error) return callback(new SettingsError(SettingsError.EXTERNAL_ERROR, 'Wrong region')); + + callback(); + }); }); }); });