最近我们根据本指南在 Azure AD 中为 RabbitMQ 实例设置 OAuth 授权https://www.rabbitmq.com/oauth2-examples-azure.html.
交互式授权对于 AD 用户(分配了应用程序注册角色)来说效果很好,但现在我们需要能够使用 oauth 通过 powershell 向rabbitmq 授权,这就是我们陷入困境的地方。
如果我们通过交互式授权并从 Google Chrome Dev Tools 获取令牌,我们可以使用此令牌进行授权,但我们无法使用 powershell 获取此令牌。
我们的rabbitmq.conf看起来像这样:
listeners.ssl.default = 5671
ssl_options.cacertfile = <Cert Path>
ssl_options.certfile = <Cert Path>
ssl_options.keyfile = <Cert Path>
ssl_options.password = <password>
management.ssl.port = 15671
management.ssl.cacertfile = <Cert Path>
management.ssl.certfile = <Cert Path>
management.ssl.keyfile = <Cert Path>
management.ssl.password = <Password>
log.file.level = debug
auth_backends.1 = rabbit_auth_backend_oauth2
auth_backends.2 = internal
auth_oauth2.https.peer_verification = verify_none
auth_oauth2.resource_server_id = <AppRegistrationID>
auth_oauth2.jwks_url = https://login.microsoftonline.com/<TenantID>/discovery/v2.0/keys
auth_oauth2.additional_scopes_key = roles
auth_oauth2.https.cacertfile = <Cert Path>
management.oauth_enabled = true
management.oauth_client_id = <AppRegistrationID>
management.oauth_provider_url = https://login.microsoftonline.com/<TenantID>
我们尝试使用像这样的PowerShell脚本来获取令牌:
$tokenParams = @{
"grant_type"="client_credentials"
"scope"="api://<AppRegistrationID>/access-api"
"client_id"=$clientID
"client_secret"=$clientSecret
"redirect_uri"="https://<rabbitserver>:15671/js/oidc-oauth/login-callback.html"
}
$tokenResponse = Invoke-RestMethod -Uri $tokenEndpoint -Method POST -ContentType "application/x-www-form-urlencoded" -Body $tokenParams
$accessToken = $tokenResponse.access_token
然后使用这个token进行授权:
$headers = @{
"Authorization" = "Bearer $accessToken"
}
Invoke-RestMethod -Uri "https://<rabbitserver>:15671/api/whoami" -Method GET -Headers $headers
但它返回“未授权”,这是有道理的,因为我们通过 powershell 获得的令牌不包含角色,并且我们无法使其接受 AD 用户凭据,它仅适用于客户端密钥。如果我们在交互式登录后使用浏览器开发工具中的令牌,它也可以工作。
我们怀疑具有角色声明的用户授权流程可能不适用于非交互式登录,但我们找不到任何有关rabbitmq/azure ad oauth 替代配置的文档。 有没有什么方法可以使用 powershell 以非交互方式获取令牌,然后用于对 RabbitMQ 进行身份验证?
想通了。我们尝试使用第二个应用程序注册(客户端)和第一个应用程序(服务器)的 api 权限,假设请求客户端令牌将为我们提供服务器应用程序注册的角色声明。这是真的。问题是,当我们尝试这种方法时,我们在令牌请求和rabbitmq.conf中使用了客户端的客户端id,而实际上我们应该在rabbitmq.conf中使用服务器的应用程序注册id,并在powershell令牌请求中使用客户端的id。现在我们可以交互登录并使用可以访问 api 的 powershell 请求令牌。