AWS KMS got timed out while decrypting

36
November 21, 2020, at 11:00 PM

I'm developing a node app using AWS KMS to encrypt and decrypt data. It's working just fine, but sometimes an exception happened (1,2 times/day). I tried to debug and figured it out that the reason is, sometimes decrypt process took a lot of time and return exception instead. I fixed it by adding a retry after 20s but what I don't understand is why does kms take that much time and return exception sometimes. This is my code:

@AfterLoad() // I'm using typeORM and all entities extend EncryptedSerializable
async decryptPersonalData() {
    await Promise.all(this.encryptedUser.map(async userData => { // encryptedUser is an User entity
        await this.decryptValue(userData); 
    }));
}
async decryptValue(userData: string) {
    const encryptedValue = (this as any)['encrypted_' + userData];
    let retryTIme = Number(20000);
    (this as any)[userData] = null;
    if (encryptedValue){
        try {
            const decrypted = await Promise.race([decrypt(Buffer.from((this as any)['encrypted_' + userData], "base64")), this.retryAfter(retryTIme)]); // I set it so that after 20s, if the decrypt process is not complete, TimeoutException will be returned.
            if (decrypted){
                const v = decrypted.toString();
                if (v){
                    (this as any)[userData] = v;
                }
            }else{
                (this as any)[userData] = encryptedValue;
            }
        } catch (err) {
            if (err.message === 'KMS-ETIMEDOUT'){
                outputKmsTimeOutInfoLog(err.message);
                await this.decryptValue(userData);
            } else {
                outputAuroraLoadErrorLog(err);
            }
            
        }
    }
}
retryAfter(ms: number){
    return new Promise(function (resolve,reject) {
        setTimeout(function () {
            reject(new Error("KMS-ETIMEDOUT"));
        }, ms);
    }) as any
}

EncryptService.js

export async function decrypt(ciphertextBlob: Buffer) {
    if (ciphertextBlob.length === 0){
        return null;
    }
    try {
        const result = await kms.decrypt({CiphertextBlob: ciphertextBlob}).promise();
        return result.Plaintext as Buffer;
    } catch (err){
        if (err.code == 'InvalidCiphertextException'){
            console.log(err);
            return null;
        }
        outputDecryptionErrorLog(err);
        console.log(util.inspect(err));
        throw err;
    }
}
READ ALSO
Creating Amazon Alexa Skill interactive Story(audio files) [closed]

Creating Amazon Alexa Skill interactive Story(audio files) [closed]

Want to improve this question? Update the question so it's on-topic for Stack Overflow

40
UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'voice' of undefined

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'voice' of undefined

So I'm trying to make a command to play music in a vc, but here's my problem whenever I run the command it gives me this error (node:11) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'voice' of undefined whenever I try to do anything...

51
Encountered comments is undefined error in react

Encountered comments is undefined error in react

I'm trying to learn react I keep encountering this error while rendering the post dialog box in the website

33
How to find duplicates in mongoDB with two conditions

How to find duplicates in mongoDB with two conditions

I want to find duplicates in the MongoDB atlasI have seen examples for finding duplicates but they didn't take two conditions as I understand it

38