【发布时间】:2022-06-11 05:00:56
【问题描述】:
所以我创建了这个 Flask 应用程序,它将作为我的自定义应用程序的后端。
为了处理 JWT 令牌的创建,我决定使用加密的 JWS(JWE 中的 JWS)。为此,我选择使用python-jose 包。我以前尝试过 JWCrypto,但在 JWK(本地)方面对我来说在本地失败了,我无法理解它。
在本地设置和测试我的应用程序后,我正在使用的功能运行良好。但是,在将它们上传到云运行(成功构建)后,加密失败。声明此消息:
文件“/usr/local/lib/python3.9/site-packages/jose/jwk.py”,第 78 行,在构造 raise JWKError("Unable to find an algorithm for key: %s" % key_data) jose.exceptions.JWKError:无法找到密钥算法:b'z$C&F)J@NcRfUjXn'
这是处理加密的函数:
def encode_token(payload):
jws_secret = "RgUkXn2r5u8x/A?D(G+KbPeShVmYq3s6"
jwe_key = b"z$C&F)J@NcRfUjXn"
signed_token = jws.sign(payload, jws_secret, algorithm='HS256')
encrypted_signed_token = jwe.encrypt(signed_token, jwe_key, algorithm='dir', encryption='A128GCM')
return encrypted_signed_token
我知道我在上面共享密钥和秘密,但我的应用程序尚未连接到数据库或做任何有用的事情 + 我将在修复后更改它们。所以没关系:)
这是我的 dockerfile:
# Use the official lightweight Python image.
# https://hub.docker.com/_/python
FROM python:3.9-slim
# Allow statements and log messages to immediately appear in the Knative logs
ENV PYTHONUNBUFFERED True
# Copy local code to the container image.
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . ./
# Install production dependencies.
RUN pip install --no-cache-dir -r requirements.txt
# Run the web service on container startup. Here we use the gunicorn
# webserver, with one worker process and 8 threads.
# For environments with multiple CPU cores, increase the number of workers
# to be equal to the cores available.
# Timeout is set to 0 to disable the timeouts of the workers to allow Cloud Run to handle instance scaling.
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 main:app
最后,这是我的 requirements.txt(通过 pipenv 自动生成):
#
# These requirements were autogenerated by pipenv
# To regenerate from the project's Pipfile, run:
#
# pipenv lock --requirements
#
-i https://pypi.org/simple
click==8.1.3; python_version >= '3.7'
ecdsa==0.17.0; python_version >= '2.6' and python_version not in '3.0, 3.1, 3.2, 3.3'
flask==2.1.2
gunicorn==20.1.0
importlib-metadata==4.11.4; python_version < '3.10'
itsdangerous==2.1.2; python_version >= '3.7'
jinja2==3.1.2; python_version >= '3.7'
markupsafe==2.1.1; python_version >= '3.7'
pyasn1==0.4.8
python-jose==3.3.0
rsa==4.8; python_version >= '3.6' and python_version < '4'
setuptools==62.3.3; python_version >= '3.7'
six==1.16.0; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'
werkzeug==2.1.2; python_version >= '3.7'
zipp==3.8.0; python_version >= '3.7'
我只是不明白这如何在我的机器上运行,但在 GCP 上却不行。 在我的本地机器(Pipenv shell)上,我完美地返回了 JWE。 我的本地 python 版本是 3.8.3,但我也尝试在 Dockerfile 中使用 3.8 和 3.10,但在部署时出现了同样不幸的结果。
任何人都知道为什么会发生这种情况以及可能的修复方法吗? 如果您能指出我的替代解决方案(如果以上无法解决),我将永远感激不尽:)
【问题讨论】:
标签: docker google-cloud-run pipenv jose python-jose