Skip to content

Commit

Permalink
Predesigned URL - Tests for non nsfs bucket
Browse files Browse the repository at this point in the history
Adding unit tests for non nsfs bucket

Signed-off-by: Vinayakswami Hariharmath <[email protected]>
  • Loading branch information
vh05 committed Nov 6, 2024
1 parent 098c340 commit 55654f5
Showing 1 changed file with 136 additions and 2 deletions.
138 changes: 136 additions & 2 deletions src/test/unit_tests/test_bucketspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ const { TMP_PATH, IS_GPFS, is_nc_coretest, get_coretest_path, invalid_nsfs_root_
generate_s3_client, exec_manage_cli, generate_anon_s3_client, generate_nsfs_account } = require('../system_tests/test_utils');
const nc_mkm = require('../../manage_nsfs/nc_master_key_manager').get_instance();


const { S3 } = require('@aws-sdk/client-s3');
const { S3, S3Client, CreateBucketCommand, PutObjectCommand, DeleteBucketCommand, DeleteObjectCommand } = require("@aws-sdk/client-s3");
const { NodeHttpHandler } = require("@smithy/node-http-handler");
const native_fs_utils = require('../../util/native_fs_utils');
const mongo_utils = require('../../util/mongo_utils');
Expand Down Expand Up @@ -2244,6 +2243,141 @@ mocha.describe('Presigned URL tests', function() {
});
});

mocha.describe('Presigned URL tests - non-nsfs bucket', function() {
this.timeout(50000); // eslint-disable-line no-invalid-this
const presigned_url_bucket = 'presigned-url-bucket';
const presigned_url_object = 'presigned-url-object.txt';
const presigned_body = 'presigned_body';
let s3_client;
let access_key;
let secret_key;
CORETEST_ENDPOINT = coretest.get_http_address();
let valid_default_presigned_url;
let presigned_url_params;

let s3_client_params;

mocha.before(async function() {
const account_info = await rpc_client.account.read_account({ email: EMAIL });
access_key = account_info.access_keys[0].access_key.unwrap();
secret_key = account_info.access_keys[0].secret_key.unwrap();
s3_client_params = {
endpoint: coretest.get_http_address(),
credentials: {
accessKeyId: access_key,
secretAccessKey: secret_key,
},
forcePathStyle: true,
region: config.DEFAULT_REGION,
requestHandler: new NodeHttpHandler({
httpAgent: http_utils.get_unsecured_agent(coretest.get_http_address()),
}),
};
s3_client = new S3Client(s3_client_params);
coretest.log('S3 CONFIG', s3_client.config);

const create_bucket = new CreateBucketCommand({
Bucket: presigned_url_bucket,
});
await s3_client.send(create_bucket);

const put_bucket = new PutObjectCommand({
Bucket: presigned_url_bucket,
Key: presigned_url_object,
Body: presigned_body
});
await s3_client.send(put_bucket);

presigned_url_params = {
bucket: new SensitiveString(presigned_url_bucket),
key: presigned_url_object,
endpoint: CORETEST_ENDPOINT,
access_key: access_key,
secret_key: secret_key
};
valid_default_presigned_url = cloud_utils.get_signed_url(presigned_url_params);
});

mocha.after(async function() {
if (!is_nc_coretest) return;

const delete_object = new DeleteObjectCommand({
Bucket: presigned_url_bucket,
Key: presigned_url_object
});
await s3_client.send(delete_object);

const delete_bucket = new DeleteBucketCommand({
Bucket: presigned_url_bucket,
});
await s3_client.send(delete_bucket);
});

it('fetch valid presigned URL - 604800 seconds - epoch expiry - should return object data', async () => {
const data = await fetchData(valid_default_presigned_url);
assert.equal(data, presigned_body);
});

it('fetch valid presigned URL - 604800 seconds - should return object data - with valid date + expiry in seconds', async () => {
const now = new Date();
const valid_url_with_date = valid_default_presigned_url + '&X-Amz-Date=' + now.toISOString() + '&X-Amz-Expires=' + 604800;
const data = await fetchData(valid_url_with_date);
assert.equal(data, presigned_body);
});

it('fetch invalid presigned URL - 604800 seconds - epoch expiry + with future date', async () => {
const now = new Date();
// Add one hour (3600000 milliseconds)
const one_hour_in_ms = 60 * 60 * 1000;
const one_hour_from_now = new Date(now.getTime() + one_hour_in_ms);
const future_presigned_url = valid_default_presigned_url + '&X-Amz-Date=' + one_hour_from_now.toISOString();
const expected_err = new S3Error(S3Error.RequestNotValidYet);
await assert_throws_async(fetchData(future_presigned_url), expected_err.message);
});

it('fetch invalid presigned URL - 604800 expiry seconds + with future date', async () => {
const now = new Date();
// Add one hour (3600000 milliseconds)
const one_hour_in_ms = 60 * 60 * 1000;
const one_hour_from_now = new Date(now.getTime() + one_hour_in_ms);
const future_presigned_url = valid_default_presigned_url + '&X-Amz-Date=' + one_hour_from_now.toISOString() + '&X-Amz-Expires=' + 604800;
const expected_err = new S3Error(S3Error.RequestNotValidYet);
await assert_throws_async(fetchData(future_presigned_url), expected_err.message);
});

it('fetch invalid presigned URL - 604800 seconds - epoch expiry - URL expired', async () => {
const expired_presigned_url = cloud_utils.get_signed_url(presigned_url_params, 1);
// wait for 2 seconds before fetching the url
await P.delay(2000);
const expected_err = new S3Error(S3Error.RequestExpired);
await assert_throws_async(fetchData(expired_presigned_url), expected_err.message);
});

it('fetch invalid presigned URL - 604800 expiry seconds - URL expired', async () => {
const now = new Date();
const expired_presigned_url = cloud_utils.get_signed_url(presigned_url_params, 1) + '&X-Amz-Date=' + now.toISOString() + '&X-Amz-Expires=' + 1;
// wait for 2 seconds before fetching the url
await P.delay(2000);
const expected_err = new S3Error(S3Error.RequestExpired);
await assert_throws_async(fetchData(expired_presigned_url), expected_err.message);
});

it('fetch invalid presigned URL - expiry expoch - expire in bigger than limit', async () => {
const invalid_expiry = 604800 + 10;
const invalid_expiry_presigned_url = cloud_utils.get_signed_url(presigned_url_params, invalid_expiry);
const expected_err = new S3Error(S3Error.AuthorizationQueryParametersError);
await assert_throws_async(fetchData(invalid_expiry_presigned_url), expected_err.message);
});

it('fetch invalid presigned URL - expire in bigger than limit', async () => {
const now = new Date();
const invalid_expiry = 604800 + 10;
const invalid_expiry_presigned_url = cloud_utils.get_signed_url(presigned_url_params, invalid_expiry) + '&X-Amz-Date=' + now.toISOString() + '&X-Amz-Expires=' + invalid_expiry;
const expected_err = new S3Error(S3Error.AuthorizationQueryParametersError);
await assert_throws_async(fetchData(invalid_expiry_presigned_url), expected_err.message);
});
});

async function fetchData(presigned_url) {
const response = await fetch(presigned_url, { agent: new http.Agent({ keepAlive: false }) });
let data;
Expand Down

0 comments on commit 55654f5

Please sign in to comment.