Skip to content

Commit

Permalink
ARSN-436 Add error handling for KMS key deletion
Browse files Browse the repository at this point in the history
(cherry picked from commit 2c2c749)
  • Loading branch information
nicolas2bert committed Nov 12, 2024
1 parent 26d711a commit 95647f6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/network/kmsAWS/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ export default class Client {
};
this.client.scheduleKeyDeletion(params, (err: AWSError, data) => {
if (err) {
if (err.code === 'NotFoundException' || err.code === 'KMSInvalidStateException') {
// master key does not exist or is already pending deletion
logger.info('AWS KMS: key does not exist or is already pending deletion', { masterKeyId, error: err });
cb(null);
return;
}

const error = arsenalErrorAWSKMS(err);
logger.error("AWS KMS: failed to delete master encryption key", { err });
cb(error);
Expand Down
27 changes: 27 additions & 0 deletions tests/functional/kmsAWS/highlevel.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const Client = require('../../../lib/network/kmsAWS/Client').default;

describe('KmsAWSClient', () => {
const logger = {
info: () => {},
debug: () => {},
error: () => {},
};
Expand Down Expand Up @@ -165,6 +166,32 @@ describe('KmsAWSClient', () => {
});
});

it('should handle NotFoundException when deleting master key', done => {
const mockError = new Error('NotFoundException');
mockError.code = 'NotFoundException';

scheduleKeyDeletionStub.yields(mockError, null);

client.deleteMasterKey('mock-key-id', logger, err => {
assert.ifError(err);
assert(scheduleKeyDeletionStub.calledOnce);
done();
});
});

it('should handle KMSInvalidStateException when deleting master key', done => {
const mockError = new Error('KMSInvalidStateException');
mockError.code = 'KMSInvalidStateException';

scheduleKeyDeletionStub.yields(mockError, null);

client.deleteMasterKey('mock-key-id', logger, err => {
assert.ifError(err);
assert(scheduleKeyDeletionStub.calledOnce);
done();
});
});

it('should generate a data key for ciphering', done => {
const mockResponse = {
Plaintext: Buffer.from('plaintext'),
Expand Down

0 comments on commit 95647f6

Please sign in to comment.