“errorMessage”: “string argument without an encoding”, [duplicate]

强颜欢笑 提交于 2020-01-06 05:26:08

问题


I'm trying to save password string encrypted in DynamoDb, I get this error.

Response:

{
  "errorMessage": "string argument without an encoding",
  "errorType": "TypeError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 25, in lambda_handler\n    encrypted_password = encrypt(session, plain_text_password, key_alias)\n",
    "  File \"/var/task/lambda_function.py\", line 11, in encrypt\n    Plaintext=bytes(secret)\n"
  ]
}

This is the code I'm trying to work with.

import boto3
import base64
from botocore.exceptions import ClientError

def encrypt(session, secret, alias):
    client = session.client('kms')
    ciphertext = client.encrypt(
        KeyId=alias,
        Plaintext=bytes(secret)
    )
    return base64.b64encode(ciphertext["CiphertextBlob"])

def lambda_handler(event, context):

    plain_text_password = event['password']
    username = event['username']
    key_alias = 'alias/ProjectKey'
    table_name = 'Authentication'

    session = boto3.session.Session()
    table = boto3.resource('dynamodb').Table(table_name)

    encrypted_password = encrypt(session, plain_text_password, key_alias)
    print('ENCRYPTED STRING: ' + encrypted_password)

    item = {
        'username':username,
        'password':encrypted_password
    }

    #check if item with the username already exists; if so, update password; else create new item
    entry = table.get_item(TableName=table_name, Key={'username':username})

    # if an entry with that username already exists, then update its corresponding password
    if 'Item' in entry:
        print('Item found. Updating password.')
        print("entry['Item']" + str(entry['Item']))
        response = table.update_item(
            Key={
                'username': username
            },
            UpdateExpression="set password = :p",
            ExpressionAttributeValues={
                ':p': encrypted_password
            },
            ReturnValues="UPDATED_NEW"
        )
    else:
        #if an entry with that username doesn't already exist, then create it
        print('Adding new item to table.')
        table.put_item(Item=item)
        new_entry = table.get_item(TableName=table_name, Key={'username':username})
        if 'Item' in new_entry:
            print('A new item was inserted in the table.')
        else:
            print('Failed to insert new item in table')

    return 'Function succeeded!'

I tried to run in python 2.7 and python 3 but no go. I have added Lambda full access and dynamodb full access roles for Lambda and DB respectively and for KMS I have given the same accessess to administrate and key usage.


回答1:


could you provide more informations (type, ...) on ciphertext["CiphertextBlob"] ?

maybe you just need to convert to bytes, e.g

base64.b64encode(bytes("yourstring", 'utf-8'))

or another way

base64.b64encode(ciphertext["CiphertextBlob"].encode('utf-8'))


来源:https://stackoverflow.com/questions/59584558/errormessage-string-argument-without-an-encoding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!