Lambda 函数出现“gpg 返回非零错误代码”问题

问题描述 投票:0回答:1

我在尝试使用 AWS lambda 中的 gnupg 加密文件时遇到问题。 我得到的错误:

gpg returned a non-zero error code: 2

所以我有一个 lambda 函数的 ECR 映像,其中包含 gnupg 的二进制文件,我可以使用 S3 存储桶中的事件触发 lambda,以便 lambda 加密该文件并放入另一个 S3 存储桶文件夹中。

对于公钥,我将字符串文件存储在秘密管理器中,格式如下:

    -----BEGIN PGP PUBLIC KEY BLOCK-----

mQGNBGUowL8BDADUvYGdRKKn4j4ORdZ8q6GDtAJ3rVi0dxoM9HzY4MVtSB4NeFGr
/s+1BlqCnOV3p7Eg/jVf/PfAcr49P0g9rBlqMTbhBPG8ter+m3FrtvJ0iPcfhERD
umTWmWO1WFE4d+NhTQY8MXC7Jv2170/kU7TRZ9SkxSzvv/lktoqsiGZ3G91y2Hom
8iHWzVwGiM/BMjH33SPaCZNG885URYPSw8CJliC4HesTU0L/Zs0f/kLVOhj3Q8oz
EsyPapRJdMZil7QM1FtiXVqyJM9Cq4z2x0GbyUcxUkzhBBsEYzmuVgVsm1ksyqEN
ZkaXxonhV4cSFJDBJ+ffpZQR7QlpAsh8JNRYsuSD2nCADE//zreLHPr7mRKHbnk/
5GMNyL/Xvj0feQfj/KSpyd3nqFPLzbTts88I4HKb8YzoJ3efwnZTcV41rffy+Sca
cYngMEConQ4DiWqoIASmVEdi0SWzWqKx37g9YgzB3GzETtWLEZxzXOtOUzlfPGlw
LThgYpP9Vq+6cQUAEQEAAbQeYW5keSBwaGFtIDxhbmR5cGhhbUBnbWFpbC5jb20+
iQHUBBMBCgA+FiEE1DZISCCJ3amO+qOU30T6iI8+tN8FAmUowL8CGwMFCQPCZwAF
CwkIBwIGFQoJCAsCBBYCAwECHgECF4AACgkQ30T6iI8+tN9kSwwAtpXPZIL8CUic
lODyq4e0LSo7eMOg9gadCKpJFgzDNtQdqD0Z1Dyz7jIYaYI8ZXk2b73G6onsLZPD
KA710JBE6lXPvM4Zyjv2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z745VD7LExjGailOFor
q7cy/SQcTmfhpQ1nPqX3XXfCJN+Kge10pX0qcVwt6+x06buiRT0jw1qGy1dyGlTL
uxd4feyzRdEJnRSzccAaef6BhUg/kksAEQEAAYkBvAQYAQoAJhYhBIlO4a5O3s7f
UvfIJiVPnpMObRrIBQJlKWPpAhsMBQkDwmcAAAoJECVPnpMObRrIln0L/RoQOFXW
qLWRNLy6KpBDv0HhmXN385ibtk4nIJv8rGpVjj2RlWrINMI2V3w2UUVdHCmFwA2+
VRW+XVRhjq+Rch9G2i0gNAWo+UVubD+NcFMMkVICS/88XFY4KyHzyVjfAnYbkwAj
WAlPLYeSM3+33wbd0/EBdRXO+qGrpwItj3Jo9RWK/hBZlZy1jhTfLGqkkCTtx5io
ecd4vIZsWSuh2AUtLb+TLiMlM9fX9Aag/xFWk0cnB5y0YThbzef5ZWtw6L8+9OSL
BoXwu6jxA8ynVYHRcLMt2O22/XaCC0eddjEl6PYkCdhYcLgDjfu2g1W9E3dDvY8G
OeZ8LYoXeFc5erqyXJ9O5LGxXrZY2AUvpXMGwIREg3IWrw3BZzbsg9SrnT94b+aa
n+YJXmiegCgGUb4Ds0BL/ShNsdLvKXlpp/SeGym3oIwSrN/w90ZPfzptWZtiWHER
WU0pS09T+dFxBv5JIG4yLorGXABuRvwYjMq2eB4OOwiPYEkQ+ACjQ8g1zQ==
=8h+y
-----END PGP PUBLIC KEY BLOCK-----

我在想,当我将上面的字符串复制并粘贴到秘密管理器值部分时,它会更改格式,从而使 gpnup 无法正确处理它。

我也尝试使用

\n
作为换行符,但仍然不起作用。

导入密钥代码是:

gpg = gnupg.GPG(gnupghome='/tmp', gpgbinary='/usr/bin/gpg')
public_key_secret = fetch_secret(PUBLIC_KEY_SECRET_NAME)
public_key_str = private_key_secret["public_key_file"]
gpg.import_keys(public_key_str)

加密代码为:

with open(temp_path, "rb") as f:
        encrypted_path = f'/tmp/{encrypted_name}'
        encrypted_data = gpg.encrypt_file(f, '[email protected]', always_trust = True, output=encrypted_path)

如果有人有相同的解决方案经验,请帮助建议我。 非常感谢

python aws-lambda gnupg
1个回答
0
投票
  1. 尝试在 pgp 启动中启用详细信息以获取更多日志信息以帮助故障排除。它会告诉你 pgp 是否遇到任何错误
  2. 我的工作代码看起来像这样,在加密之前尝试看看你的 pgp pub key 是否导入成功。

这是我的工作代码:

        gpg = gnupg.GPG(gnupghome='/tmp', verbose=True)
        print('gpg initiation:', gpg)
        temp_file_path = f'/tmp/{filename}'
        pgp_file_path = '/tmp/<pgp_key_name>>.pub.asc'
        pgp_encrypt_file_path = f'/tmp/{filename}.gpg'
        pgp_bucket_name = '<your bucket where pgp pub key stored>'
        pgp_file_key = '<bucket key of pgp pub key includig path>.pub.asc'
        recipient='[email protected]'
        s3_pgp_encryption_path = f'<s3_path_to_upload_encrypted_file>/{filename}.gpg'

        s3_client = boto3.client('s3')
        # Get S3 file details from event
        bucket_name = bucket
        file_key = key
        s3_download_resp = s3_client.download_file(bucket_name, file_key, temp_file_path)
        # Load PGP public key (ensure key is properly formatted)
        s3_download_pgppubkey_resp = s3_client.download_file(pgp_bucket_name, pgp_file_key, pgp_file_path)
        print('below s3 files downloaded:')
        files = os.listdir("/tmp")
        # Print the list of files
        for file in files:
            print(file)
        with open(pgp_file_path, 'r') as key_file:
            key_data = key_file.read()
        print('key data looks like:',key_data)
        try:
            import_result = gpg.import_keys_file(pgp_file_path)
            
        except Exception as e:
            logging.error('Found errors while encrypting the file.')
            print('Error is: ', e)
            logging.error(e)

        # import_result = gpg.import_keys(key_data)
        print('import_result is: ', import_result)
        if import_result.count == 0:
            print("Failed to import public key.")
        else:
            print("Sucessfully imported public key.")

        public_key_fingerprint = import_result.fingerprints[0]
        
        # Read the content of the file
        with open(temp_file_path, 'rb') as f:
            file_data = f.read()
        # Encrypt the file
        encrypted_data = gpg.encrypt(file_data, public_key_fingerprint)

        if encrypted_data.ok:
            with open(pgp_encrypt_file_path,'wb') as f:
                f.write(encrypted_data.data)
            print('File encrypted successfully.')
        else:
            print('Failed to encrypt file:',encrypted_data.stderr)
    # Upload encrypted file to S3
    s3_upload_resp = s3_client.upload_file(pgp_encrypt_file_path, bucket_name, s3_pgp_encryption_path)
    print('Successfully uploaded encrypted file to S3')

© www.soinside.com 2019 - 2024. All rights reserved.