44// setup coretest first to prepare the env
55const _ = require ( 'lodash' ) ;
66const coretest = require ( '../../utils/coretest/coretest' ) ;
7- const { rpc_client, EMAIL } = coretest ;
7+ const { rpc_client, EMAIL , POOL_LIST } = coretest ;
88coretest . setup ( { pools_to_create : [ coretest . POOL_LIST [ 1 ] ] } ) ;
99const { S3 } = require ( '@aws-sdk/client-s3' ) ;
1010const { NodeHttpHandler } = require ( "@smithy/node-http-handler" ) ;
1111const http = require ( 'http' ) ;
12+ const SensitiveString = require ( '../../../util/sensitive_string' ) ;
1213const system_store = require ( '../../../server/system_services/system_store' ) . get_instance ( ) ;
1314const upgrade_bucket_policy = require ( '../../../upgrade/upgrade_scripts/5.15.6/upgrade_bucket_policy' ) ;
15+ const upgrade_bucket_policy_principal = require ( '../../../upgrade/upgrade_scripts/5.21.0/upgrade_bucket_policy_principal' ) ;
1416const upgrade_bucket_cors = require ( '../../../upgrade/upgrade_scripts/5.19.0/upgrade_bucket_cors' ) ;
1517const remove_mongo_pool = require ( '../../../upgrade/upgrade_scripts/5.20.0/remove_mongo_pool' ) ;
1618const dbg = require ( '../../../util/debug_module' ) ( __filename ) ;
@@ -20,6 +22,7 @@ const config = require('../../../../config');
2022
2123const BKT = 'test-bucket' ;
2224const BKT1 = 'test-bucket1' ;
25+ const iam_username = 'iam_username' ;
2326/** @type {S3 } */
2427let s3 ;
2528
@@ -199,8 +202,85 @@ mocha.describe('test upgrade scripts', async function() {
199202 assert . strictEqual ( updated_bucket . tiering . tiers [ 0 ] . tier . mirrors [ 0 ] . spread_pools [ 0 ] . name , default_pool_name ) ;
200203 } ) ;
201204
205+ mocha . it ( 'test upgrade bucket policy to ARN version 5.21.0' , async function ( ) {
206+ const old_policy = {
207+ Version : '2012-10-17' ,
208+ Statement : [ {
209+ Sid : 'id-1' ,
210+ Effect : 'Allow' ,
211+ Principal : {
212+ "AWS" : [ new SensitiveString ( EMAIL ) ] ,
213+ } ,
214+ Action : [ 's3:GetObject' , 's3:*' ] ,
215+ Resource : [ `arn:aws:s3:::*` ]
216+ } ,
217+ {
218+ Effect : 'Deny' ,
219+ Principal : {
220+ "AWS" : [ new SensitiveString ( iam_username ) ] ,
221+ } ,
222+ Action : [ 's3:PutObject' ] ,
223+ Resource : [ `arn:aws:s3:::*` ]
224+ } ,
225+ ]
226+ } ;
227+ // clean all leftover bucket policies as upgrade script doesn't work on updated policies
228+ await _clean_all_bucket_policies ( ) ;
229+
230+ const bucket = system_store . data . buckets . find ( bucket_obj => bucket_obj . name . unwrap ( ) === BKT ) ;
231+ await system_store . make_changes ( {
232+ update : {
233+ buckets : [ {
234+ _id : bucket . _id ,
235+ s3_policy : old_policy
236+ } ]
237+ }
238+ } ) ;
239+ const account = system_store . data . accounts . find ( acc => acc . email . unwrap ( ) === EMAIL ) ;
240+ const nsr = 's3_bucket_policy_nsr' ;
241+ const iam_acc = {
242+ name : iam_username ,
243+ email : iam_username ,
244+ has_login : false ,
245+ s3_access : true ,
246+ default_resource : process . env . NC_CORETEST ? nsr : POOL_LIST [ 1 ] . name ,
247+ } ;
248+ await rpc_client . account . create_account ( iam_acc ) ;
249+
250+ const iam_account = system_store . data . accounts . find ( acc => acc . email . unwrap ( ) === iam_username ) ;
251+ await system_store . make_changes ( {
252+ update : {
253+ accounts : [ {
254+ _id : iam_account . _id ,
255+ owner : account . _id . toString ( ) ,
256+ } ]
257+ }
258+ } ) ;
259+
260+ await upgrade_bucket_policy_principal . run ( { dbg, system_store, system_server : null } ) ;
261+ const res = await s3 . getBucketPolicy ( { // should work - bucket policy should fit current schema
262+ Bucket : BKT ,
263+ } ) ;
264+ const new_policy = JSON . parse ( res . Policy ) ;
265+
266+ assert . strictEqual ( new_policy . Statement . length , old_policy . Statement . length ) ;
267+ assert . strictEqual ( new_policy . Version , old_policy . Version ) ;
268+ assert . strictEqual ( new_policy . Statement [ 0 ] . Sid , old_policy . Statement [ 0 ] . Sid ) ;
269+ assert . strictEqual ( new_policy . Statement [ 0 ] . Effect , 'Allow' ) ;
270+ assert . strictEqual ( new_policy . Statement [ 0 ] . Action [ 0 ] , 's3:GetObject' ) ;
271+ assert . strictEqual ( new_policy . Statement [ 0 ] . Action [ 1 ] , 's3:*' ) ;
272+ assert . strictEqual ( new_policy . Statement [ 0 ] . Resource [ 0 ] , old_policy . Statement [ 0 ] . Resource [ 0 ] ) ;
273+
274+ assert . strictEqual ( new_policy . Statement [ 0 ] . Principal . AWS [ 0 ] , `arn:aws:iam::${ account . _id . toString ( ) } :root` ) ;
275+ assert . strictEqual ( new_policy . Statement [ 1 ] . Principal . AWS [ 0 ] , `arn:aws:iam::${ iam_account . _id . toString ( ) } :user/${ iam_account . email . unwrap ( ) } ` ) ;
276+ } ) ;
277+
202278 mocha . after ( async function ( ) {
203279 await s3 . deleteBucket ( { Bucket : BKT } ) ;
204280 await s3 . deleteBucket ( { Bucket : BKT1 } ) ;
281+ const iam_acc = {
282+ email : iam_username ,
283+ } ;
284+ await rpc_client . account . delete_account ( iam_acc ) ;
205285 } ) ;
206286} ) ;
0 commit comments