首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从Azure函数访问Azure批处理

从Azure函数访问Azure批处理
EN

Stack Overflow用户
提问于 2017-11-02 14:37:54
回答 3查看 796关注 0票数 1

我试图使用服务原则从Azure函数访问批处理池,并遇到我不理解的身份验证问题。带有服务原则的初始登录很好,但是使用凭据访问批处理池会返回401。

下面是我的代码的简缩版本,并在关键位置进行注释。

代码语言:javascript
复制
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和机密是属于批处理帐户所有者的。知道接下来该做什么吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 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的地方,您仍然可以在这里使用服务主体,但是您的代码如下:

  1. 获取服务主对密钥库以获取名称和密钥
  2. 使用名称和键创建凭据
票数 2
EN

Stack Overflow用户

发布于 2017-11-02 16:17:18

不能使用与批处理一起从端点返回的相同OAuth令牌。假设您的服务主体拥有正确的RBAC权限,那么使用Azure批处理端点:https://batch.core.windows.net/ (假设您使用的是公共Azure)。

您不需要获得批处理帐户的共享密钥凭据,如果使用AAD服务主体,则应该使用通过AAD的凭据。

票数 0
EN

Stack Overflow用户

发布于 2020-02-14 20:50:48

我碰巧遇到了同样的问题,我没有选择使用SharedKeyCredentials,所以我想分享我的解决方案,以防其他人发现它有帮助。

正如fpark所提到的,我们需要获得一个OAuth令牌来用于批处理,而不是默认的。下面是Mark发布的原始代码,为使其与批处理一起工作所需的小修改:

代码语言:javascript
复制
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();
            }
        });
    });
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47077954

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档