使用 Rest API 和 SharedAccessKey 的 Azure 服务总线列表 Queues/Topics

List Queues/Topics of Azure Service Bus using Rest API with SharedAccessKey

我正在尝试使用 REST API 在 Azure 服务总线中列出 Queues/Topics。

当我尝试连接时,我只收到一个空白提要,上面写着“这是当前可用的 publicly-listed 服务的列表”。

我在门户中使用 RootManageSharedAccessKey(仅供开发人员使用,我可以稍后创建一个更受限制的密钥)所以它应该具有我需要的所有访问权限,我似乎无法将其获取到 return 随便什么。 This 文档似乎表明这会起作用,但没有实际的工作示例,只有理论响应。

我试过使用 URL 中的签名进行 GET 请求,如下所示:

https://myservicebusnamespace.servicebus.windows.net/$Resources/Queues;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=MYSHAREDACCESSKEY

我也试过这样做:

https://myservicebusnamespace.servicebus.windows.net/$Resources

然后将授权 header 设置为

WRAP access_token="MYSHAREDACCESSKEY="

两次我都得到了这个

<feed xmlns="http://www.w3.org/2005/Atom">
    <title type="text">Publicly Listed Services</title>
    <subtitle type="text">This is the list of publicly-listed services currently available.</subtitle>
    <id>uuid:6a5d438d-1793-451b-be41-XXXXXXXXXXXX;id=XXXXXX</id>
    <updated>2020-06-28T13:03:04Z</updated>
    <generator>Service Bus 1.1</generator>
</feed>

如果我将 url 稍微更改为:

https://myservicebusnamespace.servicebus.windows.net/$Resources/Queues/

我得到的回复略有不同:

<Error>
    <Code>401</Code>
    <Detail>claim is empty. TrackingId:c40a2bd2-490d-4b5b-adde-33bc89aa84ff_G36, SystemTracker:myservicebusnamespace.servicebus.windows.net:$Resources/Queues, Timestamp:2020-06-28T13:27:40</Detail>
</Error>

这似乎表明我没有被授权,或者我遗漏了什么。如果我在那个 url 的末尾添加一个实际的 queue 名称,它会返回到原始响应。

我相信还有另一种方法可以通过使用订阅 ID 和 pem 密钥来获取此信息...使用管理 urls (https://management.core.windows.net/{subscription ID}/services/ServiceBus/Namespaces/{Namespace}/Topics/) 但这应该都可以使用上面的格式,我只是想不出所需的确切格式。

EDIT/UPDATE: 如果我不包括我的auth claim,结果是完全一样的,提示它没有看到我的auth claim 或者它是无效的。但是,如果我包含它,并将其作为令牌,但开头没有 WRAP 位,我会得到一个异常

<Error>
    <Code>401</Code>
    <Detail>MalformedToken: Invalid authorization header: The request is missing WRAP authorization credentials. TrackingId:7be2d7f0-c165-4658-8bf1-ea104c43defc_G28, SystemTracker:NoSystemTracker, Timestamp:2020-06-28T13:33:09</Detail>
</Error>

所以它就像是在读它然后忽略它?

如果您想列出队列或主题,我们可以使用 Azure service bus service rest api or Azure Resource Manager Rest API。更多详情请参考以下步骤

  • Azure 服务总线服务休息 api
  1. 生成 SAS 令牌。详情请参考document

    例如我使用python创建sas token

import hmac
import time
import hashlib
import base64
import urllib
sb_name='bowmantest'
// your entity path such as $Resources/topics (list topics) $Resources/queues(list queues)
topic='$Resources/topics' 
url=urllib.parse.quote_plus("https://{}.servicebus.windows.net/{}".format(sb_name,topic))
sas_value='' // your share access key 
sas_name='RootManageSharedAccessKey' // your share access rule name 
expiry = str(int(time.time() + 10000))
to_sign =(url + '\n' + expiry).encode('utf-8') 
sas = sas_value.encode('utf-8')
signed_hmac_sha256 = hmac.HMAC(sas, to_sign, hashlib.sha256)
signature = urllib.parse.quote(base64.b64encode(signed_hmac_sha256.digest()))
auth_format = 'SharedAccessSignature sig={0}&se={1}&skn={2}&sr={3}'
auth=auth_format.format(signature,expiry,sas_name,url)
print(auth)

  1. 调用其余的API

1).列出队列

GET https://<namespace name>.servicebus.windows.net/$Resources/queues

Authorization <sas token>

2).列出主题

GET https://<namespace name>.servicebus.windows.net/$Resources/topics

Authorization <sas token>

  • Azure 资源管理器休息 API
  1. 创建服务主体并将 Azure RABC 角色分配给 sp(我使用 Azure CLI)
az login
#it will create a service principal and assign contributor role to the sp
az ad sp create-for-rbac -n "jonsp2"

  1. 获取 Azure AD 令牌
POST /{tenant}/oauth2/v2.0/token HTTP/1.1           //Line breaks for clarity
Host: login.microsoftonline.com
Content-Type: application/x-www-form-urlencoded

client_id=<app id>
&scope=https://management.azure.com/.default
&client_secret=<app password>
&grant_type=client_credentials
  1. 调用其余的API

列出队列

GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ServiceBus/namespaces/{namespaceName}/queues?api-version=2017-04-01

Authorization Bearer <AD token>