如何为Azure Cosmos DB REST API构造哈希令牌签名以列出用户?

问题描述

根据Cosmos DB REST API的文档,对于每个API调用,必须设置Authorization标头。其值的构造如下:https://docs.microsoft.com/en-us/rest/api/cosmos-db/access-control-on-cosmosdb-resources

我正在Python中实现此目标,如下所示:

def get_authorisation_token(verb,resource_type,resource_id,date,master_key):
     key = base64.b64decode(master_key)

     text = f"""{verb.lower()}\n{resource_type.lower()}\n{resource_id.lower()}\n{date.lower()}\n\n"""

     text_encoded = text.encode('utf-8')

     signature_hash = hmac.new(key,text_encoded,digestmod=hashlib.sha256).digest()
     signature = base64.b64encode(signature_hash).decode()

     key_type = 'master'
     version = '1.0'

     uri = f'type={key_type}&ver={version}&sig={signature}'
     uri_encoded = urllib.parse.quote(uri)

     return uri_encoded

由于每次调用都会发送此消息,因此需要重新创建auth令牌以匹配请求URL。因此,例如,要获取数据库列表,必须提供资源类型为dbs,资源链接/ ID为空字符串,其URL为:https://{databaseaccount}.documents.azure.com/dbs/

我不知道的部分是资源类型和资源ID /链接的正确组合,可以从特定数据库获取所有用户。文档可以在这里找到:https://docs.microsoft.com/en-us/rest/api/cosmos-db/list-users

我尝试了一些组合,但没有任何回报用户,我只得到了401:

{
    "code": "Unauthorized","message": "The input authorization token can't serve the request. Please check that the expected payload is built as per the protocol,and check the key being used. Server used the following payload to sign: 'get\nusers\ndbs/<db_name>\nmon,09 nov 2020 23:37:24 gmt\n\n'\r\nActivityId: 697a4159-f160-4aab-ae90-6cb5eaadb710,Microsoft.Azure.Documents.Common/2.11.0"
}

解决方法

关于此问题,请参考以下代码

int global = 0;
void func(int arg1,int arg2) {
    page_table_base_register = nullptr; // defined globally elsewhere
    int local = arg1;
}

enter image description here