简体   繁体   English

生成 JWT 令牌错误的 Azure 函数对存储在密钥库机密中的私钥进行消毒

[英]Azure Function to generate JWT token error desterilising private key stored in key vault secret

I am building a Azure Function to generate (and regenerate) a JWT token for me which I will then be using to call out to DocuSign API.我正在构建一个 Azure 函数来为我生成(和重新生成)一个 JWT 令牌,然后我将使用它来调用 DocuSign API。

I have this working locally and the token being generated is working when calling out to the DocuSign API.我在本地工作,并且在调用 DocuSign API 时生成的令牌正在工作。 I ran into an issue when I deploy this to an Azure Function.将其部署到 Azure 函数时遇到了问题。

Locally I am using the local Settings to store the key and then I am referencing it as an environment variable.在本地,我使用本地设置来存储密钥,然后将其作为环境变量引用。 On Azure I am using an Azure Key Vault to store this same key as a secret.在 Azure 上,我使用 Azure Key Vault 将相同的密钥存储为机密。 But I am having an issue with the key and how it being stored.但我对密钥及其存储方式有疑问。

I get this error when running the same code on the Azure Function:在 Azure 函数上运行相同的代码时出现此错误:

Failure Exception: ValueError: ('Could not deserialize key data. The data may be in an incorrect format, it may be encrypted with an unsupported algorithm, or it may be an unsupported key type (e.g. EC curves with explicit parameters).

I believe this is an issue with character set the Azure Key Vault stores the data in but I have been unable to resolve this issue even with encoding it from UTF-8 to ANSI or ANSI to UTF-8.我相信这是 Azure Key Vault 存储数据的字符集问题,但即使将其从 UTF-8 编码为 ANSI 或 ANSI 编码为 UTF-8,我也无法解决此问题。

import logging
import os
import azure.functions as func
import jwt

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    private_key = os.environ["servicePrivateKey"]

    payload = {"sub": "1234567890","name": "John Doe","iat": 1516239022}

    encoded = jwt.encode(payload, private_key, algorithm="RS256")

    return func.HttpResponse(
        encoded, status_code=200
    )

Followed the usage example here pyjwt example按照这里的用法示例pyjwt 示例

My key locally is stored like this:我的密钥在本地存储如下:

"servicePrivateKey": "-----BEGIN RSA PRIVATE KEY-----\nMIIEpQIBAAK................m1T6yWn5MparGHIY=\n-----END RSA PRIVATE KEY-----"

On the Azure Key vault I have tried to save it a few different ways in the secret but here is one example of it (This is copied out from the secret value on Azure):在 Azure Key Vault 上,我尝试以几种不同的方式将其保存在机密中,但这里是其中的一个示例(这是从 Azure 上的机密值中复制出来的):

-----BEGIN RSA PRIVATE KEY----- MIIEpQIBAAK................m1T6yWn5MparGHIY= -----END RSA PRIVATE KEY-----

I have a hit a wall now as I can't figure out what I can do without storing the private key in the azure function as clear text or uploading it with the app files somehow.我现在碰壁了,因为如果不将私钥以明文形式存储在 azure 函数中或以某种方式与应用程序文件一起上传,我无法弄清楚我能做什么。

I found the answer to this in the end.我最终找到了这个问题的答案。 The way I resolved the error was uploading the key in a txt file as an ascii encoded secret.我解决错误的方法是将 txt 文件中的密钥作为 ascii 编码的秘密上传。

Azure Docs: https://docs.microsoft.com/en-us/cli/azure/keyvault/secret?view=azure-cli-latest#az-keyvault-secret-set Azure 文档: https ://docs.microsoft.com/en-us/cli/azure/keyvault/secret?view=azure-cli-latest#az-keyvault-secret-set

az keyvault secret set --name
                       --vault-name
                       [--description]
                       [--disabled {false, true}]
                       [--encoding {ascii, base64, hex, utf-16be, utf-16le, utf-8}]
                       [--expires]
                       [--file]
                       [--not-before]
                       [--tags]
                       [--value]

Then when I read the secret it added in the \n (new line) character to the secret.然后,当我阅读秘密时,它在秘密中添加了 \n (换行符)字符。 But this was actually being read as a string and not a new line.但这实际上被读取为字符串而不是新行。 I just replaced the string version with a newline character and this worked.我只是用换行符替换了字符串版本,这很有效。

private_key = private_key.replace('\\n' , '\n')

So the final solution looks like this:所以最终的解决方案是这样的:

from dataclasses import replace
import logging
import os
import azure.functions as func
import jwt

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    private_key = os.environ["servicePrivateKey"]

    private_key = private_key.replace('\\n' , '\n')

    payload = {"sub": "1234567890","name": "John Doe","iat": 1516239022}

    encoded = jwt.encode(payload, private_key, algorithm="RS256")

    return func.HttpResponse(
        encoded, status_code=200
    )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用 python 访问 azure key vault secret - Cannot access azure key vault secret with python 从 Jupyter 交互式登录到 Azure 以从密钥库中获取 Secret - Interactive Login to Azure from Jupyter to fetch Secret from key vault 从 azure 密钥库证书中分离私钥和证书 - Separating private key and certificate from azure key vault certificate 如何从 Azure 密钥库中的证书获取私钥? - How to Get Private Key from Certificate in an Azure Key Vault? 在 python 中生成使用 RSA 密钥签名的 JWT 令牌 - Generate JWT token signed with RSA key in python Python Azure Function - 使用密钥库的 MSI 身份验证 - Python Azure Function - MSI Authentication with Key Vault Creating DataBricks Azure Key Vault Secret Scope Backend using Rest Api and DataBricks CLI - Creating DataBricks Azure Key Vault Secret Scope Backend using Rest Api and DataBricks CLI 无法使用 Key Vault Secret 从 Python 连接 Azure SQL 数据库 - Unable to connect Azure SQL database from Python using Key Vault Secret Azure 函数 EventGrid 触发器不支持 Key Vault 引用? - Azure Function EventGrid Trigger does not support Key Vault references? Azure 密钥保管库在 Python 中创建 - Azure key vault create in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM