我试图使用服务原则从Azure函数访问批处理池,并遇到我不理解的身份验证问题。带有服务原则的初始登录很好,但是使用凭据访问批处理池会返回401。
下面是我的代码的简缩版本,并在关键位置进行注释。
module.exports.dispatch = function (context) {
MsRest.loginWithServicePrincipalSecret('AppId', 'Secret', 'TennantId', function(err, credentials){
if (err) throw err;
// This works as it prints the credentials
context.log(credentials);
var batch_client = new batch.ServiceClient(credentials, accountUrl);
batch_client.pool.get('mycluster', function(error, result){
if(error === null)
{
context.log('Accessed pool');
context.log(result);
}
else
{
//Request to batch service returns a 401
if(error.statusCode === 404)
{
context.log('Pool not found yet returned 404...');
}
else
{
context.log('Error occurred while retrieving pool data');
context.log(error);
}
//'Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly.
context.res = { body: error.body.message.value };
context.done();
}
});
});
};如何使用服务原则进行初始登录没有问题,但它返回的凭据却无法访问批处理池?
实际的错误是检查请求上的auth标头,我可以看到,授权标头甚至不存在。
我已经三次检查了批处理帐户的访问控制,App和机密是属于批处理帐户所有者的。知道接下来该做什么吗?
发布于 2017-11-02 15:34:00
Azure批处理npm客户端所需的凭据不是Azure Active凭据/令牌,而是批处理帐户的密钥。您可以使用Azure命令列出您的密钥,如下所示:
az batch account keys list -g "<resource-group-name>" -n "<batch-account-name>"
然后可以使用以下键创建凭据参数:
var credentials = new batch.SharedKeyCredentials('your-account-name', 'your-account-key');
如果您想要将批处理密钥存储在类似Key的地方,您仍然可以在这里使用服务主体,但是您的代码如下:
发布于 2017-11-02 16:17:18
不能使用与批处理一起从端点返回的相同OAuth令牌。假设您的服务主体拥有正确的RBAC权限,那么使用Azure批处理端点:https://batch.core.windows.net/ (假设您使用的是公共Azure)。
您不需要获得批处理帐户的共享密钥凭据,如果使用AAD服务主体,则应该使用通过AAD的凭据。
发布于 2020-02-14 20:50:48
我碰巧遇到了同样的问题,我没有选择使用SharedKeyCredentials,所以我想分享我的解决方案,以防其他人发现它有帮助。
正如fpark所提到的,我们需要获得一个OAuth令牌来用于批处理,而不是默认的。下面是Mark发布的原始代码,为使其与批处理一起工作所需的小修改:
module.exports.dispatch = function (context) {
let authOptions = {tokenAudience: 'batch'};
MsRest.loginWithServicePrincipalSecret('AppId', 'Secret', 'TennantId', authOptions, function(err, credentials){
if (err) throw err;
// This works as it prints the credentials
context.log(credentials);
var batch_client = new batch.ServiceClient(credentials, accountUrl);
batch_client.pool.get('mycluster', function(error, result){
if(error === null)
{
context.log('Accessed pool');
context.log(result);
}
else
{
//Request to batch service returns a 401
if(error.statusCode === 404)
{
context.log('Pool not found yet returned 404...');
}
else
{
context.log('Error occurred while retrieving pool data');
context.log(error);
}
//'Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly.
context.res = { body: error.body.message.value };
context.done();
}
});
});
};https://stackoverflow.com/questions/47077954
复制相似问题