如何使用Python解码`X-ARR-ClientCert`标头?

问题描述

如何解码Azure App Service传递给我的Azure函数代码的X-ARR-ClientCert标头?

示例:

  • HTTP触发的Python Azure函数
  • Azure App Service配置为接受客户端证书
  • 请求者发送带有GET请求的客户端证书(每个Postman instructions here
  • Azure App Service通过X-ARR-ClientCert标头将客户端证书传递给功能代码

问题:

  • 我找不到有关此标头如何编码的文档
  • 我找不到如何使用Python解码此标头的示例

我最接近的是以下代码:

import logging
import base64
import azure.functions as func

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

    # Retrieve client cert from headers
    req_cert_str = req.headers.get("X-ARR-ClientCert")
    
    req_cert_bytes = base64.b64decode(req_cert_str)
    
    decoded_string = req_cert_bytes.decode('cp1252')

    return func.HttpResponse(
        decoded_string
    )
  • 哪个产生Status 500 Internal server error和:
Exception while executing function: Functions.certiFunc <--- Result: Failure Exception: UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 403: character maps to <undefined> Stack: File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/dispatcher.py",line 343,in _handle__invocation_request call_result = await self._loop.run_in_executor( File "/usr/local/lib/python3.8/concurrent/futures/thread.py",line 57,in run result = self.fn(*self.args,**self.kwargs) File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/dispatcher.py",line 480,in __run_sync_func return func(**params) File "/home/site/wwwroot/certiFunc/__init__.py",line 14,in main decoded_string = req_cert_bytes.decode('cp1252') File "/usr/local/lib/python3.8/encodings/cp1252.py",line 15,in decode return codecs.charmap_decode(input,errors,decoding_table) 
  • 替换decoded_string = req_cert_bytes.decode('utf-8')时,我得到:
Exception while executing function: Functions.certiFunc <--- Result: Failure Exception: UnicodeDecodeError: 'utf-8' codec can't decode byte 0x82 in position 1: invalid start byte Stack: File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/dispatcher.py",in main decoded_string = req_cert_bytes.decode('utf-8') 
  • 运行以下命令(直接解码标头)时...
req_cert_str = req.headers.get("X-ARR-ClientCert")
decoded_string = base64.b64decode(req_cert_str) 

...我得到一个Status 200 Success,但响应是二进制(?)字符和纯文本的混搭:

enter image description here

使用Python解码此标头的正确方法是什么?

有关Github问题的进一步阅读提出了here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)