11const assert = require ( 'assert' ) ;
22const process = require ( 'node:process' ) ;
33const cp = require ( 'child_process' ) ;
4- const { S3 } = require ( 'aws-sdk' ) ;
4+ const {
5+ S3Client,
6+ CreateBucketCommand,
7+ PutObjectCommand,
8+ GetObjectCommand,
9+ DeleteObjectCommand,
10+ DeleteBucketCommand,
11+ } = require ( '@aws-sdk/client-s3' ) ;
12+ const { getSignedUrl } = require ( '@aws-sdk/s3-request-presigner' ) ;
513const getConfig = require ( '../support/config' ) ;
614const provideRawOutput = require ( '../../lib/utility/provideRawOutput' ) ;
715
@@ -34,81 +42,100 @@ describe('aws-node-sdk v2auth query tests', function testSuite() {
3442
3543 before ( ( ) => {
3644 const config = getConfig ( 'default' , { signatureVersion : 'v2' } ) ;
37-
38- s3 = new S3 ( config ) ;
45+ s3 = new S3Client ( config ) ;
3946 } ) ;
4047
4148 // AWS allows an expiry further in the future
4249 // 604810 seconds is higher that the Expires time limit: 604800 seconds
4350 // ( seven days)
4451 itSkipAWS ( 'should return an error code if expires header is too far ' +
45- 'in the future' , done => {
46- const params = { Bucket : bucket , Expires : 604810 } ;
47- const url = s3 . getSignedUrl ( 'createBucket' , params ) ;
48- provideRawOutput ( [ '-verbose' , '-X' , 'PUT' , url ] , httpCode => {
49- assert . strictEqual ( httpCode , '403 FORBIDDEN' ) ;
50- done ( ) ;
52+ 'in the future' , async ( ) => {
53+
54+ // First, get a valid signed URL with maximum allowed expiry
55+ const command = new CreateBucketCommand ( { Bucket : bucket } ) ;
56+ const validUrl = await getSignedUrl ( s3 , command , { expiresIn : 604800 } ) ; // Exactly 7 days
57+
58+ // Manually modify the URL to have a longer expiry
59+ const urlObj = new URL ( validUrl ) ;
60+ const futureExpiry = Math . floor ( Date . now ( ) / 1000 ) + 604810 ; // 10 seconds more than limit
61+ urlObj . searchParams . set ( 'Expires' , futureExpiry . toString ( ) ) ;
62+ const invalidUrl = urlObj . toString ( ) ;
63+ await new Promise ( resolve => {
64+ provideRawOutput ( [ '-verbose' , '-X' , 'PUT' , invalidUrl ] , httpCode => {
65+ assert . strictEqual ( httpCode , '403 FORBIDDEN' ) ;
66+ resolve ( ) ;
67+ } ) ;
5168 } ) ;
5269 } ) ;
5370
54- it ( 'should return an error code if request occurs after expiry' ,
55- done => {
56- const params = { Bucket : bucket , Expires : 1 } ;
57- const url = s3 . getSignedUrl ( 'createBucket' , params ) ;
71+ it ( 'should return an error code if request occurs after expiry' , async ( ) => {
72+ const command = new CreateBucketCommand ( { Bucket : bucket } ) ;
73+ const url = await getSignedUrl ( s3 , command , { expiresIn : 1 } ) ;
74+ await new Promise ( resolve => {
5875 setTimeout ( ( ) => {
5976 provideRawOutput ( [ '-verbose' , '-X' , 'PUT' , url ] , httpCode => {
6077 assert . strictEqual ( httpCode , '403 FORBIDDEN' ) ;
61- done ( ) ;
78+ resolve ( ) ;
6279 } ) ;
6380 } , 1500 ) ;
6481 } ) ;
82+ } ) ;
6583
66- it ( 'should create a bucket' , done => {
67- const params = { Bucket : bucket , Expires : almostOutsideTime } ;
68- const url = s3 . getSignedUrl ( 'createBucket' , params ) ;
69- provideRawOutput ( [ '-verbose' , '-X' , 'PUT' , url ] , httpCode => {
70- assert . strictEqual ( httpCode , '200 OK' ) ;
71- done ( ) ;
84+ it ( 'should create a bucket' , async ( ) => {
85+ const command = new CreateBucketCommand ( { Bucket : bucket } ) ;
86+ const url = await getSignedUrl ( s3 , command , { expiresIn : almostOutsideTime } ) ;
87+ await new Promise ( resolve => {
88+ provideRawOutput ( [ '-verbose' , '-X' , 'PUT' , url ] , httpCode => {
89+ assert . strictEqual ( httpCode , '200 OK' ) ;
90+ resolve ( ) ;
91+ } ) ;
7292 } ) ;
7393 } ) ;
7494
7595
76- it ( 'should put an object' , done => {
77- const params = { Bucket : bucket , Key : 'key' , Expires :
78- almostOutsideTime } ;
79- const url = s3 . getSignedUrl ( 'putObject' , params ) ;
80- provideRawOutput ( [ '-verbose' , '-X' , 'PUT' , url ,
81- '--upload-file' , 'uploadFile' ] , httpCode => {
82- assert . strictEqual ( httpCode , '200 OK' ) ;
83- done ( ) ;
96+ it ( 'should put an object' , async ( ) => {
97+ const command = new PutObjectCommand ( { Bucket : bucket , Key : 'key' } ) ;
98+ const url = await getSignedUrl ( s3 , command , { expiresIn : almostOutsideTime } ) ;
99+ await new Promise ( resolve => {
100+ provideRawOutput ( [ '-verbose' , '-X' , 'PUT' , url ,
101+ '--upload-file' , 'uploadFile' ] , httpCode => {
102+ assert . strictEqual ( httpCode , '200 OK' ) ;
103+ resolve ( ) ;
104+ } ) ;
105+ } ) ;
106+ } ) ;
107+
108+ it ( 'should put an object with an acl setting and a storage class setting' , async ( ) => {
109+ // This will test that upper case query parameters and lowercase
110+ // query parameters (i.e., 'x-amz-acl') are being sorted properly.
111+ // This will also test that query params that contain "x-amz-"
112+ // are being added to the canonical headers list in our string
113+ // to sign.
114+ const command = new PutObjectCommand ( {
115+ Bucket : bucket ,
116+ Key : 'key' ,
117+ ACL : 'public-read' ,
118+ StorageClass : 'STANDARD'
119+ } ) ;
120+ const url = await getSignedUrl ( s3 , command ) ;
121+ await new Promise ( resolve => {
122+ provideRawOutput ( [ '-verbose' , '-X' , 'PUT' , url ,
123+ '--upload-file' , 'uploadFile' ] , httpCode => {
124+ assert . strictEqual ( httpCode , '200 OK' ) ;
125+ resolve ( ) ;
126+ } ) ;
84127 } ) ;
85128 } ) ;
86129
87- it ( 'should put an object with an acl setting and a storage class setting' ,
88- done => {
89- // This will test that upper case query parameters and lowercase
90- // query parameters (i.e., 'x-amz-acl') are being sorted properly.
91- // This will also test that query params that contain "x-amz-"
92- // are being added to the canonical headers list in our string
93- // to sign.
94- const params = { Bucket : bucket , Key : 'key' ,
95- ACL : 'public-read' , StorageClass : 'STANDARD' } ;
96- const url = s3 . getSignedUrl ( 'putObject' , params ) ;
97- provideRawOutput ( [ '-verbose' , '-X' , 'PUT' , url ,
98- '--upload-file' , 'uploadFile' ] , httpCode => {
99- assert . strictEqual ( httpCode , '200 OK' ) ;
100- done ( ) ;
101- } ) ;
102- } ) ;
103-
104-
105- it ( 'should get an object' , done => {
106- const params = { Bucket : bucket , Key : 'key' , Expires :
107- almostOutsideTime } ;
108- const url = s3 . getSignedUrl ( 'getObject' , params ) ;
109- provideRawOutput ( [ '-verbose' , '-o' , 'download' , url ] , httpCode => {
110- assert . strictEqual ( httpCode , '200 OK' ) ;
111- done ( ) ;
130+
131+ it ( 'should get an object' , async ( ) => {
132+ const command = new GetObjectCommand ( { Bucket : bucket , Key : 'key' } ) ;
133+ const url = await getSignedUrl ( s3 , command , { expiresIn : almostOutsideTime } ) ;
134+ await new Promise ( resolve => {
135+ provideRawOutput ( [ '-verbose' , '-o' , 'download' , url ] , httpCode => {
136+ assert . strictEqual ( httpCode , '200 OK' ) ;
137+ resolve ( ) ;
138+ } ) ;
112139 } ) ;
113140 } ) ;
114141
@@ -118,25 +145,26 @@ describe('aws-node-sdk v2auth query tests', function testSuite() {
118145 } ) ;
119146 } ) ;
120147
121- it ( 'should delete an object' , done => {
122- const params = { Bucket : bucket , Key : 'key' , Expires :
123- almostOutsideTime } ;
124- const url = s3 . getSignedUrl ( 'deleteObject' , params ) ;
125- provideRawOutput ( [ '-verbose' , '-X' , 'DELETE' , url ] ,
126- httpCode => {
148+ it ( 'should delete an object' , async ( ) => {
149+ const command = new DeleteObjectCommand ( { Bucket : bucket , Key : 'key' } ) ;
150+ const url = await getSignedUrl ( s3 , command , { expiresIn : almostOutsideTime } ) ;
151+ await new Promise ( resolve => {
152+ provideRawOutput ( [ '-verbose' , '-X' , 'DELETE' , url ] , httpCode => {
127153 assert . strictEqual ( httpCode , '204 NO CONTENT' ) ;
128- done ( ) ;
154+ resolve ( ) ;
129155 } ) ;
156+ } ) ;
130157 } ) ;
131158
132159
133- it ( 'should delete a bucket' , done => {
134- const params = { Bucket : bucket , Expires : almostOutsideTime } ;
135- const url = s3 . getSignedUrl ( 'deleteBucket' , params ) ;
136- provideRawOutput ( [ '-verbose' , '-X' , 'DELETE' , url ] ,
137- httpCode => {
160+ it ( 'should delete a bucket' , async ( ) => {
161+ const command = new DeleteBucketCommand ( { Bucket : bucket } ) ;
162+ const url = await getSignedUrl ( s3 , command , { expiresIn : almostOutsideTime } ) ;
163+ await new Promise ( resolve => {
164+ provideRawOutput ( [ '-verbose' , '-X' , 'DELETE' , url ] , httpCode => {
138165 assert . strictEqual ( httpCode , '204 NO CONTENT' ) ;
139- done ( ) ;
166+ resolve ( ) ;
140167 } ) ;
168+ } ) ;
141169 } ) ;
142170} ) ;
0 commit comments