无法在带有OpenSSL 1.0.2g和Python 2.7的Docker Alpine Linux 3.3中安装cryptography。

50

已解决哇,这些家伙真快...基本上就是这个https://github.com/pyca/cryptography/issues/2750。结果openssl的一个安全更新(DROWN攻击)发布了,而该更新包含了一个意外的函数签名更改,导致了不兼容性,所以这对我来说只是倒霉。


我需要在运行Alpine Linux的Docker容器中使用pip install cryptography。实际上,是另一个模块,service_identity,但问题存在于依赖项cryptography模块中。

我有以下的Dockerfile

FROM alpine:3.3

RUN apk --update add build-base libffi-dev openssl-dev python-dev py-pip
RUN pip install cryptography

无法正常工作,出现以下错误

generating cffi module 'build/temp.linux-x86_64-2.7/_openssl.c'
building '_openssl' extension
creating build/temp.linux-x86_64-2.7/build
creating build/temp.linux-x86_64-2.7/build/temp.linux-x86_64-2.7
gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -I/usr/include/python2.7 -c build/temp.linux-x86_64-2.7/_openssl.c -o build/temp.linux-x86_64-2.7/build/temp.linux-x86_64-2.7/_openssl.o
build/temp.linux-x86_64-2.7/_openssl.c:726:6: error: conflicting types for 'BIO_new_mem_buf'
 BIO *BIO_new_mem_buf(void *, int);
      ^
In file included from /usr/include/openssl/asn1.h:65:0,
                 from build/temp.linux-x86_64-2.7/_openssl.c:434:
/usr/include/openssl/bio.h:692:6: note: previous declaration of 'BIO_new_mem_buf' was here
 BIO *BIO_new_mem_buf(const void *buf, int len);
      ^
error: command 'gcc' failed with exit status 1

openssl 1.0.2g于2016年3月1日发布(昨天),而alpine软件包已经升级到该版本。这可能与此有关吗?

我该如何解决这个问题?也许可以设置一些环境变量?

更新:我一直在检查openssl的GitHub Repo,实际上在1.0.2f到1.0.2g的过渡期间,openssl/bio.hBIO *BIO_new_mem_buf(void *buf, int len)已更改为BIO *BIO_new_mem_buf(const void *buf, int len)(在https://github.com/openssl/openssl/compare/OpenSSL_1_0_2f...OpenSSL_1_0_2g中搜索“BIO_new_mem_buf”)。我不知道这个导入过时版本openssl/bio.hopenssl/asn1.h是从哪里来的,因为它看起来不像openssl repo中的那个。有什么建议吗?

好的,我看到有些人已经在解决这个问题了:https://github.com/pyca/cryptography/issues/2750


3
今天我遇到了安装cryptography 1.2.2的同样问题,升级到1.2.3版本后问题得到解决。 - gogasca
5个回答

79

对于那些仍然在像这样的Alpine 3.7中安装cryptography==2.1.4时遇到问题的人:

writing manifest file 'src/cryptography.egg-info/SOURCES.txt'
running build_ext
generating cffi module 'build/temp.linux-x86_64-2.7/_padding.c'
creating build/temp.linux-x86_64-2.7
generating cffi module 'build/temp.linux-x86_64-2.7/_constant_time.c'
generating cffi module 'build/temp.linux-x86_64-2.7/_openssl.c'
building '_openssl' extension
creating build/temp.linux-x86_64-2.7/build
creating build/temp.linux-x86_64-2.7/build/temp.linux-x86_64-2.7
gcc -fno-strict-aliasing -Os -fomit-frame-pointer -g -DNDEBUG -Os -fomit-frame-pointer -g -DTHREAD_STACK_SIZE=0x100000 -fPIC -I/usr/include/python2.7 -c build/temp.linux-x86_64-2.7/_openssl.c -o build/temp.linux-x86_64-2.7/build/temp.linux-x86_64-2.7/_openssl.o -Wconversion -Wno-error=sign-conversion
build/temp.linux-x86_64-2.7/_openssl.c:493:30: fatal error: openssl/opensslv.h: No such file or directory
 #include <openssl/opensslv.h>
                              ^
compilation terminated.
error: command 'gcc' failed with exit status 1

解决方案

在Alpine容器中安装这些依赖项:

$ apk add --no-cache libressl-dev musl-dev libffi-dev

使用Dockerfile安装这些依赖项:

RUN apk add --no-cache \
        libressl-dev \
        musl-dev \
        libffi-dev && \
    pip install --no-cache-dir cryptography==2.1.4 && \
    apk del \
        libressl-dev \
        musl-dev \
        libffi-dev

参考

有关在Alpine上安装cryptography的说明可以在此处找到:

以下是相关部分:

在Linux上构建密码学

[跳过非Alpine Linux部分]

$ pip install cryptography

如果您在Alpine上或只想自己编译,则cryptography需要一个编译器,Python的头文件(如果您没有使用pypy),以及OpenSSL和libffi库的头文件在您的系统上可用。

阿尔卑斯山

如果您正在使用Python 2,请将python3-dev替换为python-dev

$ sudo apk add gcc musl-dev python3-dev libffi-dev openssl-dev
如果您在使用openssl-dev时遇到错误,可能需要使用libressl-dev

在重新排版您的代码以符合规范时,我注意到您实际上没有安装 cryptography 模块。 - Daniel F
1
@DanielF 顺便说一下,我在示例中包含了tini和其他库,而实际解决方案中并没有。如果这太偏离主题了,我会将其删除... - Manoj Kasyap
对我来说,添加 libressl-dev libffi-dev 解决了 python:3.7-alpine 的问题。 - A Campos
需要 Alpine 3.10 并安装 cryptography==3.3.2 - onkar
libressl-dev 似乎不能适用于所有设置。使用过时的 postgreSQL alpine 镜像时,我遇到了 ERROR: Service 'xyz' failed to build: The command '/bin/sh -c apk add libressl-dev' returned a non-zero code: 2 错误。 - questionto42
显示剩余5条评论

14

如果由于Rust版本的问题而失败,则建议按照加密文档中的以下步骤操作:

The Rust available by default in Alpine < 3.12 is older than the 
minimum supported version. See the Rust installation instructions
 for information about installing a newer Rust.
$ sudo apk add gcc musl-dev python3-dev libffi-dev openssl-dev cargo

在我的情况下,使用Python3.8-alpine,添加cargo解决了问题。


"cargo" 安装 "Rust"(已测试)。奇怪的是,安装 Poetry 并在 Poetry 尝试安装 "cryptography" 时遇到类似错误后,Poetry 的错误仍然保持不变,即它仍然需要 "Rust"。我不得不单独安装 "cryptography" 才能完成安装,就像接受的答案所做的那样。由于我使用的是较旧的 Python 版本,我还必须添加一个选择的版本,请参见 Failed to install cryptography package with Poetry on Python 3.9。之后,我才能安装 Poetry。 - questionto42
我在Ubuntu 22.04上使用Python 3.6时遇到了这个问题,但没有使用Docker。我只需要使用curl https://sh.rustup.rs -sSf | sh命令,并将$HOME/.cargo/bin添加到我的路径中即可解决问题。 - Caleb Syring

0

在安装之前加入以下内容:

RUN apk -U upgrade

RUN apk add --no-cache libffi-dev openssl-dev


0

-1

检查您是否正在为正确的架构构建!

x86-64或amd64架构运行类似的软件,另一类是aarch64或arm架构芯片,例如Apple Silicon M1或您的手机CPU。


这并没有回答问题。一旦您拥有足够的声望,您将能够评论任何帖子;相反,提供不需要询问者澄清的答案。- 来自审核 - f.khantsis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接