|
1 | 1 | const assert = require('assert'); |
| 2 | +const AWS = require('aws-sdk'); |
| 3 | +const { errorInstances } = require('arsenal'); |
2 | 4 |
|
3 | 5 | const withV4 = require('../support/withV4'); |
4 | 6 | const BucketUtility = require('../../lib/utility/bucket-util'); |
5 | 7 | const checkError = require('../../lib/utility/checkError'); |
6 | 8 | const changeObjectLock = require('../../../../utilities/objectLock-util'); |
| 9 | +const { VALIDATE_CREDENTIALS, SIGN } = AWS.EventListeners.Core; |
7 | 10 |
|
8 | 11 | const bucket = 'mock-bucket-lock'; |
9 | 12 | const unlockedBucket = 'mock-bucket-no-lock'; |
@@ -190,3 +193,142 @@ describeSkipIfCeph('PUT object legal hold', () => { |
190 | 193 | }); |
191 | 194 | }); |
192 | 195 | }); |
| 196 | + |
| 197 | +// Use bucket policy to test iam action for putObjectLegalHold with version id |
| 198 | +// It used to need non standard s3:PutObjectVersionLegalHold action but was fixed |
| 199 | +// by ARSN-297 ARTESCA-7107 |
| 200 | +describeSkipIfCeph('PUT object legal hold iam action and version id', () => { |
| 201 | + withV4(sigCfg => { |
| 202 | + const bucketUtil = new BucketUtility('default', sigCfg); |
| 203 | + const s3 = bucketUtil.s3; |
| 204 | + const testBucket = 'bucket-policy-legalhold-test'; |
| 205 | + let versionId; |
| 206 | + |
| 207 | + const legalHoldConfig = { Status: 'ON' }; |
| 208 | + |
| 209 | + function awsRequest(auth, operation, params, callback) { |
| 210 | + if (auth) { |
| 211 | + bucketUtil.s3[operation](params, callback); |
| 212 | + } else { |
| 213 | + const unauthBucketUtil = new BucketUtility('default', sigCfg); |
| 214 | + const request = unauthBucketUtil.s3[operation](params); |
| 215 | + request.removeListener('validate', VALIDATE_CREDENTIALS); |
| 216 | + request.removeListener('sign', SIGN); |
| 217 | + request.send(callback); |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + function cbNoError(done) { |
| 222 | + return err => { |
| 223 | + assert.ifError(err); |
| 224 | + done(); |
| 225 | + }; |
| 226 | + } |
| 227 | + |
| 228 | + function cbWithError(done) { |
| 229 | + return err => { |
| 230 | + assert.strictEqual(err.statusCode, errorInstances.AccessDenied.code); |
| 231 | + done(); |
| 232 | + }; |
| 233 | + } |
| 234 | + |
| 235 | + beforeEach(() => { |
| 236 | + process.stdout.write('Setting up bucket policy legal hold tests\n'); |
| 237 | + return s3.createBucket({ |
| 238 | + Bucket: testBucket, |
| 239 | + ObjectLockEnabledForBucket: true, |
| 240 | + }).promise() |
| 241 | + .then(() => s3.putObject({ Bucket: testBucket, Key: key }).promise()) |
| 242 | + .then(res => { |
| 243 | + versionId = res.VersionId; |
| 244 | + }) |
| 245 | + .catch(err => { |
| 246 | + process.stdout.write('Error in beforeEach\n'); |
| 247 | + throw err; |
| 248 | + }); |
| 249 | + }); |
| 250 | + |
| 251 | + afterEach(() => { |
| 252 | + process.stdout.write('Cleaning up bucket policy legal hold tests\n'); |
| 253 | + return bucketUtil.empty(testBucket) |
| 254 | + .then(() => bucketUtil.deleteMany([testBucket])) |
| 255 | + .catch(err => { |
| 256 | + process.stdout.write('Error in afterEach\n'); |
| 257 | + throw err; |
| 258 | + }); |
| 259 | + }); |
| 260 | + |
| 261 | + const policyTestCases = [ |
| 262 | + { |
| 263 | + action: 's3:PutObjectLegalHold', |
| 264 | + expectedResult: 'allow', |
| 265 | + callback: cbNoError, |
| 266 | + }, |
| 267 | + { |
| 268 | + action: 's3:PutObjectVersionLegalHold', |
| 269 | + expectedResult: 'deny', |
| 270 | + callback: cbWithError, |
| 271 | + }, |
| 272 | + ]; |
| 273 | + |
| 274 | + policyTestCases.forEach(testCase => { |
| 275 | + describe(`with ${testCase.action} policy`, () => { |
| 276 | + beforeEach(done => { |
| 277 | + const statement = { |
| 278 | + Sid: 'AllowLegalHold', |
| 279 | + Effect: 'Allow', |
| 280 | + Principal: '*', |
| 281 | + Action: [testCase.action], |
| 282 | + Resource: [`arn:aws:s3:::${testBucket}/*`], |
| 283 | + }; |
| 284 | + const bucketPolicy = { |
| 285 | + Version: '2012-10-17', |
| 286 | + Statement: [statement], |
| 287 | + }; |
| 288 | + s3.putBucketPolicy({ |
| 289 | + Bucket: testBucket, |
| 290 | + Policy: JSON.stringify(bucketPolicy), |
| 291 | + }, err => { |
| 292 | + assert.ifError(err); |
| 293 | + done(); |
| 294 | + }); |
| 295 | + }); |
| 296 | + |
| 297 | + if (testCase.expectedResult === 'allow') { |
| 298 | + afterEach(() => |
| 299 | + s3.putObjectLegalHold({ |
| 300 | + Bucket: testBucket, |
| 301 | + Key: key, |
| 302 | + LegalHold: { Status: 'OFF' }, |
| 303 | + }).promise() |
| 304 | + .then(() => s3.putObjectLegalHold({ |
| 305 | + Bucket: testBucket, |
| 306 | + Key: key, |
| 307 | + VersionId: versionId, |
| 308 | + LegalHold: { Status: 'OFF' }, |
| 309 | + }).promise()) |
| 310 | + ); |
| 311 | + } |
| 312 | + |
| 313 | + it(`should ${testCase.expectedResult} unauthenticated putObjectLegalHold without VersionId`, done => { |
| 314 | + const params = { |
| 315 | + Bucket: testBucket, |
| 316 | + Key: key, |
| 317 | + LegalHold: legalHoldConfig, |
| 318 | + }; |
| 319 | + awsRequest(false, 'putObjectLegalHold', params, testCase.callback(done)); |
| 320 | + }); |
| 321 | + |
| 322 | + it(`should ${testCase.expectedResult} unauthenticated putObjectLegalHold with VersionId`, done => { |
| 323 | + const params = { |
| 324 | + Bucket: testBucket, |
| 325 | + Key: key, |
| 326 | + LegalHold: legalHoldConfig, |
| 327 | + VersionId: versionId, |
| 328 | + }; |
| 329 | + awsRequest(false, 'putObjectLegalHold', params, testCase.callback(done)); |
| 330 | + }); |
| 331 | + }); |
| 332 | + }); |
| 333 | + }); |
| 334 | +}); |
0 commit comments