首页 > 解决方案 > 使用带有 SharedAccessKey 的 Rest API 列出 Azure 服务总线的队列/主题

问题描述

我正在尝试使用 REST API 列出 Azure 服务总线中的队列/主题。

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

我在门户中使用 RootManageSharedAccessKey(仅限开发人员,稍后我可以创建一个更受限制的密钥),因此它应该具有我需要的所有访问权限,但我似乎无法让它返回任何内容。文档似乎表明这将起作用,但是没有实际的工作示例,只是理论上的响应。

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

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

我也试过这样做:

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

然后将 Authorization 标头设置为

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>

如果我将网址稍微更改为:

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 的末尾添加一个实际队列名称,它会返回到原始响应。

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

编辑/更新:如果我不包括我的 auth 声明,结果是完全相同的,这表明它没有看到我的 auth 声明或者它是无效的。但是,如果我包含它,并且只是将它作为令牌,在开始时没有 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>

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

标签: azureazureservicebusservicebusazure-servicebus-queues

解决方案


如果要列出队列或主题,我们可以使用Azure 服务总线 service rest apiAzure Resource Manager Rest API。更多详情,请参考以下步骤

  • Azure 服务总线服务休息 api
  1. 生成 SAS 令牌。更多详细信息,请参阅文档

    比如我用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>

在此处输入图像描述


推荐阅读