代码之家  ›  专栏  ›  技术社区  ›  Chad Thweatt

部署Azure功能(Python v1)时出现MSAL身份验证问题,但在本地工作

  •  0
  • Chad Thweatt  · 技术社区  · 2 年前

    我已经部署Azure功能很长一段时间了。该函数是用Python编写的,使用的是v1模型。我正在使用服务主体从Azure Synapse中的数据库中读取数据。直到今天上午10点,CST,这工作没有任何问题。然而,在上述时间之后,我开始在日志中看到500个服务器错误。错误消息指示以下内容(就好像它正在传递一个空用户):

    Result: Failure Exception: InterfaceError: ('28000', "[28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user ''. (18456) (SQLDriverConnect)") Stack: File "/azure-functions-host/workers/python/3.10/LINUX/X64/azure_functions_worker/dispatcher.py", line 475, in _handle__invocation_request call_result = await self._run_async_func( File "/azure-functions-host/workers/python/3.10/LINUX/X64/azure_functions_worker/dispatcher.py", line 758, in _run_async_func return await ExtensionManager.get_async_invocation_wrapper( File "/azure-functions-host/workers/python/3.10/LINUX/X64/azure_functions_worker/extension.py", line 147, in get_async_invocation_wrapper result = await function(**args) File "/home/site/wwwroot/TestTrigger/__init__.py", line 58, in main sqlRoutesOutput = currentRoutes.synapseSql(); File "/home/site/wwwroot/classes/nexsql.py", line 32, in synapseSql synConn = pyodbc.connect(synConnString, attrs_before = {SQL_COPT_SS_ACCESS_TOKEN:tokenstruct})
    

    这是代码块:

     def synapseSql(self):
            creds = ConfidentialClientApplication(
                client_id=os.environ['clientid'], 
                authority=f"https://login.microsoftonline.com/{os.envion['tenantid']}",
                client_credential= os.environ['sqlcreds'])
    
            token = creds.acquire_token_for_client(scopes=['https://database.windows.net/.default'])
    
            tokenb = bytes(token["access_token"], "UTF-8")
            exptoken = b''
            for i in tokenb:
                exptoken += bytes({i})
                exptoken += bytes(1)
            tokenstruct = struct.pack("=i", len(exptoken)) + exptoken
            SQL_COPT_SS_ACCESS_TOKEN = 1256 
    
            synConnString = 'DRIVER={ODBC Driver 17 for SQL Server};' \
                        + f'SERVER={self.server};' \
                        + f'DATABASE={self.database};'\
                        + 'ENCRYPT=Yes;'
    
            synConn = pyodbc.connect(synConnString, attrs_before = {SQL_COPT_SS_ACCESS_TOKEN:tokenstruct})
            cursor = synConn.cursor()
            cursor.execute(self.query)
            return cursor
    

    正如您所看到的,我正在使用ConfidentialClientApplication来创建信任和获取令牌。然后使用我在这里为令牌结构找到的后续代码。

    如果我能提供更多的信息来帮助你,请告诉我。

    谢谢

    我真的很不知所措,因为它在当地有效,我周围的每个人都说什么都没有改变。我对服务主体权限、密钥到期等进行了三次检查。

    0 回复  |  直到 2 年前