python - 在 AWS Lambda 上使用的加密包中找不到恒定时间模块

标签 python amazon-web-services paramiko aws-lambda pycrypto

[我是 Python 2.7 和 AWS Lambda 的新手,感谢任何帮助]

我关注了 AWS Lambda tutorial并创建了一个 virtualenv 以包含与使用 paramiko 相关联的 Python 库,以将文件复制到 SFTP 服务器作为 AWS Lambda 上的计划任务以运行以下脚本:

import paramiko

def worker_handler(event, context):

    host = "sftpserver.testdpom.com" 
    port = 22
    transport = paramiko.Transport((host, port))

    sftp = paramiko.SFTPClient.from_transport(transport)

    username = "xxxx"
    password = "xxxxxx"

    transport.connect(username = username, password = password)

    sftp = paramiko.SFTPClient.from_transport(transport)

    sftp.put("test.txt", "test.txt")
    sftp.close()
    transport.close()

    return
    {
        'message' : "Script execution completed. See Cloudwatch logs for complete output"
    }

python 脚本在我的本地机器上运行正常,但是当我在 AWS Lambda 上测试包时,我收到错误“ImportError:没有名为 _constant_time 的模块”和下面的堆栈跟踪。

您能想到在 AWS Lambda 环境中出现此错误的任何可能原因吗?

  File "/var/task/paramiko/kex_group1.py", line 111, in _parse_kexdh_reply
    self.transport._verify_key(host_key, sig)
  File "/var/task/paramiko/transport.py", line 1617, in _verify_key
    key = self._key_info[self.host_key_type](Message(host_key))
  File "/var/task/paramiko/rsakey.py", line 58, in __init__
    ).public_key(default_backend())
  File "/var/task/cryptography/hazmat/backends/__init__.py", line 35, in default_backend
    _default_backend = MultiBackend(_available_backends())
  File "/var/task/cryptography/hazmat/backends/__init__.py", line 22, in _available_backends
    "cryptography.backends"
  File "/var/task/pkg_resources/__init__.py", line 2235, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "/var/task/cryptography/hazmat/backends/openssl/__init__.py", line 7, in <module>
    from cryptography.hazmat.backends.openssl.backend import backend
  File "/var/task/cryptography/hazmat/backends/openssl/backend.py", line 15, in <module>
    from cryptography import utils, x509
  File "/var/task/cryptography/x509/__init__.py", line 7, in <module>
    from cryptography.x509.base import (
  File "/var/task/cryptography/x509/base.py", line 15, in <module>
    from cryptography.x509.extensions import Extension, ExtensionType
  File "/var/task/cryptography/x509/extensions.py", line 19, in <module>
    from cryptography.hazmat.primitives import constant_time, serialization
  File "/var/task/cryptography/hazmat/primitives/constant_time.py", line 9, in <module>
    from cryptography.hazmat.bindings._constant_time import lib
ImportError: No module named _constant_time

最佳答案

由于 lambda 在 amazon linux 实例上运行,您基本上需要:

  1. 启动一个 amazon linux ec2 实例
  2. 创建一个虚拟环境并pip install所有你需要的包
  3. scp 将文件下载到本地部署包所在的位置

这一切都是由于 pip install 如何根据您使用的是 linux 还是 mac(我假设也是 windows)做不同的事情而发生的。

这是一个让 ec2 实例加速 afaik 的启动脚本

#!/bin/bash

sudo yum upgrade -y
sudo yum group install -y "Development tools"

sudo yum install -y \
    python27 \
    libffi libffi-devel \
    openssl openssl-devel

virtualenv venv
source venv/bin/activate

pip install paramiko

paramiko 包将在 /path/to/venv/lib/python2.7/site-packages/paramikocryptography 内容将在 path/to/venv/lib64/python2.7/cryptography 中。

我一直在我的本地 mac 上使用 pip install 的组合,并在包不起作用时执行此操作(例如 paramikopsycopg2 ),还有一些其他有用的包,人们已经预编译并放在 github 上其他地方专门用于 lambda。

喂!

关于python - 在 AWS Lambda 上使用的加密包中找不到恒定时间模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38003860/

相关文章:

javascript - 如何在 Web Worker 上填充 DOMParser 以供 AWS S3 SDK 上传使用?

python - 如何弥补 Paramiko key 更新失败?

python - IO错误 : [Errno 22] Invalid argument when reading/writing large bytestring

python - 更新zip存档时文件重复

python - 自动生成的名称为字母而不是数字

amazon-web-services - AWS CloudWatch终端输出日志

python - 覆盖和自定义 "django.contrib.auth.views.login"

amazon-web-services - S3静态网站存储桶不可用

python - Paramiko 在 psql 上执行命令

python - 在 Web 服务器上运行时,Paramiko SSH 失败并显示 "Server ' .. .' not found in known_hosts"