|
4 | 4 | // setup coretest first to prepare the env |
5 | 5 | const _ = require('lodash'); |
6 | 6 | const coretest = require('../../utils/coretest/coretest'); |
7 | | -const { rpc_client, EMAIL } = coretest; |
| 7 | +const { rpc_client, EMAIL, POOL_LIST } = coretest; |
8 | 8 | coretest.setup({ pools_to_create: [coretest.POOL_LIST[1]] }); |
9 | 9 | const { S3 } = require('@aws-sdk/client-s3'); |
10 | 10 | const { NodeHttpHandler } = require("@smithy/node-http-handler"); |
11 | 11 | const http = require('http'); |
| 12 | +const SensitiveString = require('../../../util/sensitive_string'); |
12 | 13 | const system_store = require('../../../server/system_services/system_store').get_instance(); |
13 | 14 | const 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'); |
14 | 16 | const upgrade_bucket_cors = require('../../../upgrade/upgrade_scripts/5.19.0/upgrade_bucket_cors'); |
15 | 17 | const remove_mongo_pool = require('../../../upgrade/upgrade_scripts/5.20.0/remove_mongo_pool'); |
16 | 18 | const dbg = require('../../../util/debug_module')(__filename); |
@@ -199,6 +201,80 @@ mocha.describe('test upgrade scripts', async function() { |
199 | 201 | assert.strictEqual(updated_bucket.tiering.tiers[0].tier.mirrors[0].spread_pools[0].name, default_pool_name); |
200 | 202 | }); |
201 | 203 |
|
| 204 | + mocha.it('test upgrade bucket policy to ARN version 5.21.0', async function() { |
| 205 | + const iam_username = 'iam_username'; |
| 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], `aws:arn:${account._id.toString()}:root`); |
| 275 | + assert.strictEqual(new_policy.Statement[1].Principal.AWS[0], `aws:arn:${iam_account._id.toString()}:user/${iam_account.email.unwrap()}`); |
| 276 | + }); |
| 277 | + |
202 | 278 | mocha.after(async function() { |
203 | 279 | await s3.deleteBucket({ Bucket: BKT }); |
204 | 280 | await s3.deleteBucket({ Bucket: BKT1 }); |
|
0 commit comments