Skip to content

Commit 9e168fb

Browse files
(wip) migration check file previous key type
1 parent ee963ef commit 9e168fb

File tree

1 file changed

+46
-11
lines changed

1 file changed

+46
-11
lines changed

tests/functional/sse-kms-migration/migration.js

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const getConfig = require('../aws-node-sdk/test/support/config');
33
const { S3 } = require('aws-sdk');
44
const kms = require('../../../lib/kms/wrapper');
5+
const filekms = require('../../../lib/kms/file/backend');
56
const { promisify } = require('util');
67
const BucketInfo = require('arsenal').models.BucketInfo;
78
const { DummyRequestLogger } = require('../../unit/helpers');
@@ -65,8 +66,7 @@ kms.client._supportsDefaultKeyPerAccount = false; // To generate keys without va
6566
// Fix for before migration run
6667
// if (!kms.arnPrefix) kms.arnPrefix = '';
6768

68-
const memKms = require('../../../lib/kms/in_memory/backend').backend;
69-
69+
const fileKmsPrefix = filekms.backend.arnPrefix;
7070

7171
function hydrateSSEConfig({ algo: SSEAlgorithm, masterKeyId: KMSMasterKeyID }) {
7272
// stringify and parse to strip undefined values
@@ -123,6 +123,41 @@ async function putEncryptedObject(Bucket, Key, sseConfig, kmsKeyId, Body) {
123123
}).promise();
124124
}
125125

126+
async function assertObjectSSEMigrationFILE(Bucket, Key, objConf, obj, bktConf, bkt, VersionId, Body) {
127+
const sseMD = await getObjectMDSSE(Bucket, Key);
128+
const head = await s3.headObject({ Bucket, Key, VersionId }).promise();
129+
const sseMDMigrated = await getObjectMDSSE(Bucket, Key);
130+
const expectedKey = `${sseMD.SSEKMSKeyId && sseMD.SSEKMSKeyId.startsWith('arn:scality:kms')
131+
? '' : fileKmsPrefix}${sseMD.SSEKMSKeyId}`;
132+
133+
if (sseMD.SSEKMSKeyId) {
134+
// assert.doesNotMatch(sseMD.SSEKMSKeyId, /^arn:scality:kms/);
135+
}
136+
137+
// obj precedence over bkt
138+
assert.strictEqual(head.ServerSideEncryption, (objConf.algo || bktConf.algo));
139+
140+
if (sseMDMigrated.SSEKMSKeyId) {
141+
assert.strictEqual(sseMDMigrated.SSEKMSKeyId, expectedKey);
142+
}
143+
144+
if (obj.kmsKey) {
145+
assert.strictEqual(head.SSEKMSKeyId, getKey(expectedKey));
146+
} else if (objConf.algo !== 'AES256' && bkt.kmsKey) {
147+
assert.strictEqual(head.SSEKMSKeyId, getKey(expectedKey));
148+
} else if (head.ServerSideEncryption === 'aws:kms') {
149+
// We differ from aws behavior and always return a
150+
// masterKeyId even when not explicitly configured.
151+
assert.strictEqual(head.SSEKMSKeyId, getKey(expectedKey));
152+
} else {
153+
assert.strictEqual(head.SSEKMSKeyId, undefined);
154+
}
155+
156+
// always verify GetObject as well to ensure acurate decryption
157+
const get = await s3.getObject({ Bucket, Key, ...(VersionId && { VersionId }) }).promise();
158+
assert.strictEqual(get.Body.toString(), Body);
159+
}
160+
126161
async function assertObjectSSEMigration(Bucket, Key, objConf, obj, bktConf, bkt, VersionId, Body) {
127162
const sseMD = await getObjectMDSSE(Bucket, Key);
128163
const head = await s3.headObject({ Bucket, Key, VersionId }).promise();
@@ -298,7 +333,7 @@ describe('SSE KMS migration', () => {
298333

299334
testCasesObj.forEach(objConf => it(`should have pre uploaded object with SSE ${objConf.name}`, async () => {
300335
const obj = bkt.objs[objConf.name];
301-
void await assertObjectSSEMigration(bkt.name, obj.name, objConf, obj, bktConf, bkt, null, obj.body);
336+
void await assertObjectSSEMigrationFILE(bkt.name, obj.name, objConf, obj, bktConf, bkt, null, obj.body);
302337
}));
303338

304339
testCasesObj.forEach(objConf => describe(`object enc-obj-${objConf.name}`, () => {
@@ -336,19 +371,19 @@ describe('SSE KMS migration', () => {
336371
const mpuKey = `${obj.name}-mpu`;
337372

338373
const fullBody = `${obj.body}-MPU1${obj.body}-MPU2`;
339-
void await assertObjectSSEMigration(bkt.name, mpuKey, objConf, obj, bktConf, bkt, null, fullBody);
374+
void await assertObjectSSEMigrationFILE(bkt.name, mpuKey, objConf, obj, bktConf, bkt, null, fullBody);
340375
});
341376

342377
optionalSkip('should migrate completed MPU that had copy', async () => {
343378
const mpuKey = `${obj.name}-mpucopy`;
344379
const fullBody = `BODY(copy)${obj.body}-MPU2`;
345-
void await assertObjectSSEMigration(bkt.name, mpuKey, objConf, obj, bktConf, bkt, null, fullBody);
380+
void await assertObjectSSEMigrationFILE(bkt.name, mpuKey, objConf, obj, bktConf, bkt, null, fullBody);
346381
});
347382

348383
optionalSkip('should migrate completed MPU that had byte range copy', async () => {
349384
const mpuKey = `${obj.name}-mpucopyrange`;
350385
const fullBody = 'copyBODY';
351-
void await assertObjectSSEMigration(bkt.name, mpuKey, objConf, obj, bktConf, bkt, null, fullBody);
386+
void await assertObjectSSEMigrationFILE(bkt.name, mpuKey, objConf, obj, bktConf, bkt, null, fullBody);
352387
});
353388
const mpus = {};
354389
before('retrieve MPUS', async () => {
@@ -399,7 +434,7 @@ describe('SSE KMS migration', () => {
399434
}).promise();
400435
// console.log('complete', complete);
401436
const fullBody = `${obj.body}-MPU1${obj.body}-MPU2`;
402-
void await assertObjectSSEMigration(bkt.name, mpuKey, objConf, obj, bktConf, bkt, null, fullBody);
437+
void await assertObjectSSEMigrationFILE(bkt.name, mpuKey, objConf, obj, bktConf, bkt, null, fullBody);
403438
});
404439

405440
optionalSkip('should prepare encrypte MPU and put 2 encrypted parts without completion', async () => {
@@ -444,7 +479,7 @@ describe('SSE KMS migration', () => {
444479
}).promise();
445480
// console.log('complete', complete);
446481
const fullBody = `${obj.body}-MPU1${obj.body}-MPU2`.repeat(2);
447-
void await assertObjectSSEMigration(bkt.name, mpuKey, objConf, obj, bktConf, bkt, null, fullBody);
482+
void await assertObjectSSEMigrationFILE(bkt.name, mpuKey, objConf, obj, bktConf, bkt, null, fullBody);
448483
});
449484

450485
optionalSkip('should prepare encrypted MPU and copy an encrypted parts from encrypted bucket without completion', async () => {
@@ -490,7 +525,7 @@ describe('SSE KMS migration', () => {
490525
}).promise();
491526
// console.log('complete', complete);
492527
const fullBody = `BODY(copy)${obj.body}-MPU2`.repeat(2);
493-
void await assertObjectSSEMigration(bkt.name, mpuKey, objConf, obj, bktConf, bkt, null, fullBody);
528+
void await assertObjectSSEMigrationFILE(bkt.name, mpuKey, objConf, obj, bktConf, bkt, null, fullBody);
494529
});
495530

496531
optionalSkip('should prepare encrypte MPU and copy an encrypted range parts from encrypted bucket without completion', async () => {
@@ -537,7 +572,7 @@ describe('SSE KMS migration', () => {
537572
}).promise();
538573
// console.log('complete', complete);
539574
const fullBody = 'copyBODY'.repeat(2);
540-
void await assertObjectSSEMigration(bkt.name, mpuKey, objConf, obj, bktConf, bkt, null, fullBody);
575+
void await assertObjectSSEMigrationFILE(bkt.name, mpuKey, objConf, obj, bktConf, bkt, null, fullBody);
541576
});
542577

543578
it(`should CopyObject ${obj.name} into encrypted destination bucket`, async () => {
@@ -706,6 +741,6 @@ describe('SSE KMS migration', () => {
706741
}).promise();
707742
// console.log('complete', complete);
708743
const fullBody = parts.reduce((acc, part) => `${acc}${part.body}`, '').repeat(2);
709-
void await assertObjectSSEMigration(mpuCopyBkt, mpuKey, {}, {}, { algo: 'AES256' }, {}, null, fullBody);
744+
void await assertObjectSSEMigrationFILE(mpuCopyBkt, mpuKey, {}, {}, { algo: 'AES256' }, {}, null, fullBody);
710745
});
711746
});

0 commit comments

Comments
 (0)