问题描述
我在尝试通过 ERROR: The request did not have a subscription or a valid tenant level resource provider.
的 service principal
下的 azure cli 创建 scope
时得到一个 Azure Iot Hub
。我正在使用 CLI (bash) 但 python 也足够了。
如最后所示,我拥有在此订阅中创建 sp 的正确凭据和权限,并且我拥有相关物联网中心的所有者权限。
如果我错过了更好的方法来实现这一点,这里是上下文:我们需要验证一项工作,该工作在新设备刷新后立即自动注册,然后再发货以插入电源。这对自定义闪存文件系统做了很多事情(例如,添加唯一的设备主机名和本地密码);最后需要向IotHub注册设备。
az iot hub device-identity create --device-id [device id] --hub-name [hub name] --edge-enabled
使用我的用户权限,我可以az login
并完成所有这些 - 但它需要在没有交互式登录的自动作业中运行。我相信 service principal
是实现这一目标的方式 (?)。
因此,尝试创建我运行的主体:
# the following pulls a valid(looking) `"/subscriptions/NAME/resourceGroups/THEGROUP/providers/Microsoft.Devices/IotHubs/THEHUB"`
IOTHUB_ID="$(az iot hub show --name TheHubName --query id)
az ad sp create-for-rbac --name http://my-iothub-serviceprincipal --scopes $IOTHUB_ID --role contributor --query password --output tsv
失败,如上所示(注意:贡献者范围太广,稍后将是自定义角色):
WARNING: Role assignment creation Failed.
ERROR: The request did not have a subscription or a valid tenant level resource provider.
作为确保我拥有正确的 az login
和其他本地状态的测试,Azure ACR
scope
的以下类似命令确实成功,带有门户中可见的新服务主体。
ACR_ID="$(az iot hub show --name TheAcrName --query id)
az ad sp create-for-rbac --name http://acr-service-principal-foobar --scopes $ACR_ID --role acrpull --query password --output tsv
解决方法
这是由 azure CLI 中的错误引起的。 az iot hub show
返回一个不正确引用的字符串; az acr show
例如没有。
az iot hub show --name your-iothub-name --query id
返回如下所示的字符串。 两个引号 "
都在原文中
'"/subscriptions/guid/.../IotHubs/your-iothub-name"'
az acr show --name your-acr-name --query id
返回相同的格式字符串,但没有额外的 '
引用。
"/subscriptions/.../registries/your-acr-name"
az iot hub device-identity create
无法处理 '"
..."'
(可以理解)但不幸的是并没有完全失败,这使得这有点难以追踪,因为引用有点混合用于脚本输出。