|
| 1 | +const assert = require('assert'); |
| 2 | +const { |
| 3 | + PutObjectCommand, |
| 4 | + GetObjectCommand, |
| 5 | + CopyObjectCommand, |
| 6 | + CreateMultipartUploadCommand, |
| 7 | + UploadPartCopyCommand, |
| 8 | + AbortMultipartUploadCommand, |
| 9 | + PutBucketPolicyCommand, |
| 10 | + DeleteBucketPolicyCommand, |
| 11 | + PutBucketVersioningCommand, |
| 12 | +} = require('@aws-sdk/client-s3'); |
| 13 | +const { v4: uuid } = require('uuid'); |
| 14 | + |
| 15 | +const BucketUtility = require('../../lib/utility/bucket-util'); |
| 16 | +const { makeIamClient, createUserWithPolicy } = require('../../lib/utility/iam-util'); |
| 17 | +const { config } = require('../../../../../lib/Config'); |
| 18 | + |
| 19 | +// this test needs a real vault to create IAM users |
| 20 | +const isVaultScality = config.backends.auth !== 'mem'; |
| 21 | + |
| 22 | +const describeWithVault = isVaultScality ? describe : describe.skip; |
| 23 | + |
| 24 | +describeWithVault('Copy of a specific source version', () => { |
| 25 | + const secretKey = 'secret'; |
| 26 | + |
| 27 | + async function assertAccessDenied(promise) { |
| 28 | + await assert.rejects(promise, { name: 'AccessDenied' }); |
| 29 | + } |
| 30 | + |
| 31 | + describe('in the same account, without IAM permission to read the source', () => { |
| 32 | + const ownBucketName = `copy-source-own-${uuid()}`; |
| 33 | + const bucketUtil = new BucketUtility('default'); |
| 34 | + const iamClient = makeIamClient('default'); |
| 35 | + const mpuKey = 'mine/mpu-copy'; |
| 36 | + let user; |
| 37 | + let uploadId; |
| 38 | + |
| 39 | + before(async () => { |
| 40 | + await bucketUtil.createOne(ownBucketName); |
| 41 | + await bucketUtil.s3.send( |
| 42 | + new PutObjectCommand({ |
| 43 | + Bucket: ownBucketName, |
| 44 | + Key: secretKey, |
| 45 | + Body: 'secret content', |
| 46 | + }), |
| 47 | + ); |
| 48 | + user = await createUserWithPolicy(iamClient, [ |
| 49 | + { |
| 50 | + Effect: 'Allow', |
| 51 | + Action: ['s3:GetObject', 's3:PutObject'], |
| 52 | + Resource: `arn:aws:s3:::${ownBucketName}/mine/*`, |
| 53 | + }, |
| 54 | + ]); |
| 55 | + const mpuResponse = await user.s3Client.send( |
| 56 | + new CreateMultipartUploadCommand({ |
| 57 | + Bucket: ownBucketName, |
| 58 | + Key: mpuKey, |
| 59 | + }), |
| 60 | + ); |
| 61 | + uploadId = mpuResponse.UploadId; |
| 62 | + }); |
| 63 | + |
| 64 | + after(async () => { |
| 65 | + if (uploadId) { |
| 66 | + await bucketUtil.s3.send( |
| 67 | + new AbortMultipartUploadCommand({ |
| 68 | + Bucket: ownBucketName, |
| 69 | + Key: mpuKey, |
| 70 | + UploadId: uploadId, |
| 71 | + }), |
| 72 | + ); |
| 73 | + } |
| 74 | + await bucketUtil.empty(ownBucketName); |
| 75 | + await bucketUtil.deleteOne(ownBucketName); |
| 76 | + if (user) { |
| 77 | + await user.deleteUser(); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + it('should deny CopyObject from the latest version', async () => { |
| 82 | + await assertAccessDenied( |
| 83 | + user.s3Client.send( |
| 84 | + new CopyObjectCommand({ |
| 85 | + Bucket: ownBucketName, |
| 86 | + Key: 'mine/copy', |
| 87 | + CopySource: `${ownBucketName}/${secretKey}`, |
| 88 | + }), |
| 89 | + ), |
| 90 | + ); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should deny CopyObject from the null version', async () => { |
| 94 | + await assertAccessDenied( |
| 95 | + user.s3Client.send( |
| 96 | + new CopyObjectCommand({ |
| 97 | + Bucket: ownBucketName, |
| 98 | + Key: 'mine/copy', |
| 99 | + CopySource: `${ownBucketName}/${secretKey}?versionId=null`, |
| 100 | + }), |
| 101 | + ), |
| 102 | + ); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should deny UploadPartCopy from the null version', async () => { |
| 106 | + await assertAccessDenied( |
| 107 | + user.s3Client.send( |
| 108 | + new UploadPartCopyCommand({ |
| 109 | + Bucket: ownBucketName, |
| 110 | + Key: mpuKey, |
| 111 | + UploadId: uploadId, |
| 112 | + PartNumber: 1, |
| 113 | + CopySource: `${ownBucketName}/${secretKey}?versionId=null`, |
| 114 | + }), |
| 115 | + ), |
| 116 | + ); |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + describe('with a bucket policy denying s3:GetObjectVersion', () => { |
| 121 | + const policyBucketName = `copy-source-policy-${uuid()}`; |
| 122 | + const bucketUtil = new BucketUtility('default'); |
| 123 | + const iamClient = makeIamClient('default'); |
| 124 | + const latestContent = 'latest content'; |
| 125 | + let user; |
| 126 | + let oldVersionId; |
| 127 | + |
| 128 | + before(async () => { |
| 129 | + await bucketUtil.createOne(policyBucketName); |
| 130 | + await bucketUtil.s3.send( |
| 131 | + new PutBucketVersioningCommand({ |
| 132 | + Bucket: policyBucketName, |
| 133 | + VersioningConfiguration: { Status: 'Enabled' }, |
| 134 | + }), |
| 135 | + ); |
| 136 | + const oldPut = await bucketUtil.s3.send( |
| 137 | + new PutObjectCommand({ |
| 138 | + Bucket: policyBucketName, |
| 139 | + Key: secretKey, |
| 140 | + Body: 'old content', |
| 141 | + }), |
| 142 | + ); |
| 143 | + oldVersionId = oldPut.VersionId; |
| 144 | + await bucketUtil.s3.send( |
| 145 | + new PutObjectCommand({ |
| 146 | + Bucket: policyBucketName, |
| 147 | + Key: secretKey, |
| 148 | + Body: latestContent, |
| 149 | + }), |
| 150 | + ); |
| 151 | + await bucketUtil.s3.send( |
| 152 | + new PutBucketPolicyCommand({ |
| 153 | + Bucket: policyBucketName, |
| 154 | + Policy: JSON.stringify({ |
| 155 | + Version: '2012-10-17', |
| 156 | + Statement: [ |
| 157 | + { |
| 158 | + Effect: 'Deny', |
| 159 | + Principal: '*', |
| 160 | + Action: 's3:GetObjectVersion', |
| 161 | + Resource: `arn:aws:s3:::${policyBucketName}/*`, |
| 162 | + }, |
| 163 | + ], |
| 164 | + }), |
| 165 | + }), |
| 166 | + ); |
| 167 | + user = await createUserWithPolicy(iamClient, [ |
| 168 | + { |
| 169 | + Effect: 'Allow', |
| 170 | + Action: ['s3:GetObject', 's3:GetObjectVersion', 's3:PutObject'], |
| 171 | + Resource: `arn:aws:s3:::${policyBucketName}/*`, |
| 172 | + }, |
| 173 | + ]); |
| 174 | + }); |
| 175 | + |
| 176 | + after(async () => { |
| 177 | + await bucketUtil.s3.send(new DeleteBucketPolicyCommand({ Bucket: policyBucketName })); |
| 178 | + await bucketUtil.empty(policyBucketName); |
| 179 | + await bucketUtil.deleteOne(policyBucketName); |
| 180 | + if (user) { |
| 181 | + await user.deleteUser(); |
| 182 | + } |
| 183 | + }); |
| 184 | + |
| 185 | + it('should deny GetObject of an older version', async () => { |
| 186 | + await assertAccessDenied( |
| 187 | + user.s3Client.send( |
| 188 | + new GetObjectCommand({ |
| 189 | + Bucket: policyBucketName, |
| 190 | + Key: secretKey, |
| 191 | + VersionId: oldVersionId, |
| 192 | + }), |
| 193 | + ), |
| 194 | + ); |
| 195 | + }); |
| 196 | + |
| 197 | + it('should deny CopyObject from an older version', async () => { |
| 198 | + await assertAccessDenied( |
| 199 | + user.s3Client.send( |
| 200 | + new CopyObjectCommand({ |
| 201 | + Bucket: policyBucketName, |
| 202 | + Key: 'leak', |
| 203 | + CopySource: `${policyBucketName}/${secretKey}?versionId=${oldVersionId}`, |
| 204 | + }), |
| 205 | + ), |
| 206 | + ); |
| 207 | + }); |
| 208 | + |
| 209 | + it('should allow CopyObject from the latest version', async () => { |
| 210 | + await user.s3Client.send( |
| 211 | + new CopyObjectCommand({ |
| 212 | + Bucket: policyBucketName, |
| 213 | + Key: 'latest-copy', |
| 214 | + CopySource: `${policyBucketName}/${secretKey}`, |
| 215 | + }), |
| 216 | + ); |
| 217 | + const getResponse = await user.s3Client.send( |
| 218 | + new GetObjectCommand({ |
| 219 | + Bucket: policyBucketName, |
| 220 | + Key: 'latest-copy', |
| 221 | + }), |
| 222 | + ); |
| 223 | + assert.strictEqual(await getResponse.Body.transformToString(), latestContent); |
| 224 | + }); |
| 225 | + }); |
| 226 | +}); |
0 commit comments