首页 > 解决方案 > 如何获取我的脚本访问 Azure 存储过程所需的令牌?

问题描述

我有一个 azure 存储过程,我需要使用我将作为 webjob 上传的 python 脚本来安排它每天运行一次。

我一直在阅读有关执行存储过程的文档、Azure Cosmos DB 休息调用的常见请求标头以及访问控制页面,但访问控制页面提到这些键仅用于读取查询(所以我假设不是用于访问存储过程,这些存储过程有权执行任何类型的查询,否则这似乎是一个巨大的漏洞)。

我需要具体了解如何在 python 中从 Azure 获取密钥以访问我的存储过程端点?

更新 1

最后,我能够构造授权字符串并将其与其他一些标头一起发送到服务器。但我仍然收到未经授权的回复。

响应:

{
    "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: 'post\nsprocs\ndbs/metrics/colls/LoungeVisits/sprocs/calculateAverage\nfri, 05 oct 2018 19:06:17 gmt\n\n'\r\nActivityId: 41cd36af-ad0e-40c3-84c8-761ebd14bf6d, Microsoft.Azure.Documents.Common/2.1.0.0"
}

请求标头:

{
    Authorization: [my-auth-string],
    x-ms-version: "2017-02-22", //My DB was created after this, the latest version, so I assume it uses this version; can I verify this somehow?
    x-ms-date: "Fri, 05 Oct 2018 19:06:17 GMT", // My js for returning the auth string also returns the date, so I copy both in
    Content-Type: application/json
}

生成身份验证字符串的代码,然后将其复制/粘贴到 Postman 中:

var crypto = require("crypto");

var inputKey = "my-key-from-azure";

var today = new Date().toUTCString();

console.log(today);

console.log(getAuthorizationTokenUsingMasterKey("POST", "dbs", "dbs/ToDoList", today, inputKey))

function getAuthorizationTokenUsingMasterKey(verb, resourceType, resourceId, date, masterKey) 
 {  
    var key = new Buffer(masterKey, "base64");  

    var text = (verb || "").toLowerCase() + "\n" +   
           (resourceType || "").toLowerCase() + "\n" +   
           (resourceId || "") + "\n" +   
           date.toLowerCase() + "\n" +   
           "" + "\n";  

    var body = new Buffer(text, "utf8");  
    var signature = crypto.createHmac("sha256", key).update(body).digest("base64");  

    var MasterToken = "master";  

    var TokenVersion = "1.0";  

    return encodeURIComponent("type=" + MasterToken + "&ver=" + TokenVersion + "&sig=" + signature);  
} 

标签: azurestored-proceduresazure-cosmosdb

解决方案


有关授权标头的页面适用于任何 Cosmos DB REST 请求:查询、存储过程等。


推荐阅读