使用Amazon KMS加密值,使用DynamoDB和Lambda(NodeJS)存储/检索

时间:2016-02-04 23:21:05

标签: node.js amazon-web-services encryption amazon-dynamodb aws-lambda

我有一个Lambda(NodeJS)函数,可以将数据写入DynamoDB。其中一些数据需要加密。我正在使用KMS加密和存储进行加密。当我使用不同的Lambda函数从Dynamo检索并尝试解密时,我收到错误。如果我加密然后转换解密,我能够做到这一点,但如果我从数据库中读取加密值,它将无法解密。 我的加密/存储代码如下:

console.log('Loading event');

var AWS = require('aws-sdk');

var keyId = "arn:aws:kms:us-east-1:5423542542:key/xxxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxxx";
var tableName = "person";
var dynamoDBConfiguration = {
    "region": "us-west-2"
};
AWS.config.update(dynamoDBConfiguration);
var dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
var kms = new AWS.KMS({region: 'us-east-1'});
var newId = "1234-56789-101112-13141516";
var item = {};

exports.handler = function (event, context) {
    console.log('ssn');
    //encrypt it
    var ssnParams = {
        KeyId: keyId,
        Plaintext: "123-45-6789"
    };
    kms.encrypt(ssnParams, function (err, data) {
        if (err) {
            console.log(err, err.stack);
        }
        else {
            console.log(' ssn encrypted');

            var enc_ssn = data.CiphertextBlob;
            item["SSN"] = {"Value": {"B": enc_ssn}};
            item["First_Name"] = {"Value": {"S": "Joe"}};
            item["Last_Name"] = {"Value": {"S": "Blow"}};
            dynamodb.updateItem({
                "TableName": tableName,
                "AttributeUpdates": item,
                "ReturnValues": "ALL_NEW",
                "Key": {
                    "id": {"S": newId}
                }

            }, function (err, data) {
                if (err) {
                    context.done(err);
                }
                else {
                    console.log('great success: %j', data);
                    context.succeed("Person Successfully Inserted");
                }
            });
        }
    });
};

我的检索/解密代码如下:

console.log('Loading event');
var AWS = require('aws-sdk');
var dynamoDBConfiguration = {
    "region": "us-west-2"
};
AWS.config.update(dynamoDBConfiguration);
var dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
var keyId = "arn:aws:kms:us-east-1:5423542542:key/xxxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxxx";
var tableName = "person";
var kms = new AWS.KMS({region: 'us-east-1'});

exports.handler = function (event, context) {
    console.log(JSON.stringify(event, null, '  '));
    var params = {};
    var id = event.id;
    console.log(id);
    if (id && id !== '') {
        params = {
            "TableName": tableName,
            KeyConditionExpression: "id = :id",
            ExpressionAttributeValues: {
                ':id': {'S': id}
            }
        };
        dynamodb.query(params, function (err, data) {
            if (err) {
                context.done(err);
            }
            else {
                var person = data.Items[0];
                console.log('query success');
                console.log(person);
                if (person.SSN) {
                    console.log('have ssn');
                    var b_ssn = person.SSN;
                    console.log(b_ssn);
                    person.SSNtext = "";
                    var encryptedParams = {
                        CiphertextBlob: Buffer(b_ssn, 'base64'),
                    };
                    kms.decrypt(encryptedParams, function (err, decrypteddata) {
                        if (err) {
                            console.log(err, err.stack);
                            //context.done(err);
                        }
                        else {
                            person.SSNtext = decrypteddata.Plaintext.toString();
                            console.log(decrypteddata.Plaintext.toString());
                            context.succeed(person);
                        }
                    });
                }
            }
        });
    }
    else {
        params = {
            "TableName": tableName
        };
        dynamodb.scan(params, function (err, data) {
            if (err) {
                context.done(err);
            }
            else {
                console.log('scan success');
                context.succeed(data);
            }
        });
    }
};

当我运行此代码时,出现以下错误:

START RequestId: 639590ac-cb95-11e5-91e4-d706c725f529 Version: $LATEST
2016-02-04T23:16:58.713Z    639590ac-cb95-11e5-91e4-d706c725f529    Loading event
2016-02-04T23:17:00.215Z    639590ac-cb95-11e5-91e4-d706c725f529    {
  "id": "1234-56789-101112-13141516"
}
2016-02-04T23:17:00.215Z    639590ac-cb95-11e5-91e4-d706c725f529    1234-56789-101112-13141516
2016-02-04T23:17:00.954Z    639590ac-cb95-11e5-91e4-d706c725f529    query success
2016-02-04T23:17:00.954Z    639590ac-cb95-11e5-91e4-d706c725f529    { Last_Name: { S: 'Blow' },
  id: { S: '1234-56789-101112-13141516' },
  First_Name: { S: 'Joe' },
  SSN: { B: <Buffer 0a 20 ec 00 75 21 f2 61 7d ba 2e 38 7e c6 fd 24 6d 32 b4 c2 b3 29 47 9e 9b 97 f2 a8 46 f2 d0 38 da 37 12 92 01 01 01 02 00 78 ec 00 75 21 f2 61 7d ba 2e ...> } }
2016-02-04T23:17:00.956Z    639590ac-cb95-11e5-91e4-d706c725f529    have ssn
2016-02-04T23:17:00.956Z    639590ac-cb95-11e5-91e4-d706c725f529    { B: <Buffer 0a 20 ec 00 75 21 f2 61 7d ba 2e 38 7e c6 fd 24 6d 32 b4 c2 b3 29 47 9e 9b 97 f2 a8 46 f2 d0 38 da 37 12 92 01 01 01 02 00 78 ec 00 75 21 f2 61 7d ba 2e ...> }
2016-02-04T23:17:01.573Z    639590ac-cb95-11e5-91e4-d706c725f529    { [InvalidCiphertextException: null]
  message: null,
  code: 'InvalidCiphertextException',
  time: Thu Feb 04 2016 23:17:01 GMT+0000 (UTC),

我可以对加密值进行加密和解密,但是当我存储该值,检索它并尝试解密时,它就会失败。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:13)

好的 - 我有这个工作,我想发布在这里以防其他人可能正在努力同样的事情。将数据放入DynamoDB时,可以使用以下内容:

item["First_Name"] = {"Value":{"S": "Joe"}};

当我检索它时,我没有得到一个字符串,我有一个对象。所以,当我有一行名为person的行时,我必须得到这样的值:

first_name = person.First_Name.S;
//results in first_name = "Joe";

所以我遇到的问题是我试图将对象person.First_Name传递给decrypt方法而不是person.First_Name.S的值。