azure - 授权用户访问 azure blob

标签 azure azure-active-directory azure-storage azure-blob-storage azure-resource-manager

我正在通过 adal-node 库使用 microsoft 登录对我的网页的用户进行身份验证。

adal-node 有一个AuthenticationContext,我们可以从中获取JWT token 使用acquireTokenWithAuthorizationCode

因此,我的 Active Directory 应用程序的用户现在可以使用他们的 Microsoft 帐户成功登录。

现在的问题是如何使用上面收到的 JWT token 获取特定存储帐户/容器/blob的 RBAC 角色?这可能吗?

或者我应该使用像 azure-arm-authorization 这样的库来实现此目的?我已经为每个存储帐户/容器/blob 设置了 RBAC 角色,但我没有找到任何有关如何为我的应用程序的每个登录用户获取这些角色的在线文档。

任何帮助都是无价的。

TL;DR 如何授权 azure blob?

最佳答案

根据我的研究,如果我们想使用Azure AD身份验证来访问Azure Blob存储,我们需要为Azure存储帐户或容器分配Azure RABC角色。更多详情请引用here 。此外,我们可以使用Azure CLI来分配角色并获取角色分配。

例如

# assign role
az role assignment create \
    --role "Storage Blob Data Contributor" \
    --assignee <email> \
    --scope "/subscriptions/<subscription>/resourceGroups/<resource-group>/providers/Microsoft.Storage/storageAccounts/<storage-account>/blobServices/default/containers/<container>"


# list role assignment of the resource
az role assignment list --scope "/subscriptions/<subscription>/resourceGroups/<resource-group>/providers/Microsoft.Storage/storageAccounts/<storage-account>/blobServices/default/containers/<container>"

欲了解更多信息,请阅读article

<小时/>

更新1

如果想使用nodejs sdk获取角色分配,请引用以下代码

const authorizationManagement = require('azure-arm-authorization');
const msrestAzure = require('ms-rest-azure');

const scope = '/subscriptions/<subscription>/resourceGroups/<resource-group>/providers/Microsoft.Storage/storageAccounts/<storage-account>/blobServices/default/containers/<container>';
const subscriptionId = 'e5b0fcfa-e859-43f3-8d84-5e5fe29f4c68';

msrestAzure.interactiveLogin().then(credentials => {
 const client = new authorizationManagement(credentials, subscriptionId);
 client.roleAssignments.listForScope(scope).then(result => {
    result.forEach(element => { 
       
        client.roleDefinitions.getById(element.roleDefinitionId).then(result => {
             console.log("principal ID: "+ element.principalId+"\nrole name: "+result.roleName)
        });
      }); 
 });
})

更多详情请引用Get access control list (IAM) of a resource group in Node.js

<小时/>

更新2

根据我的测试,如果您想将azure-arm-authorizationadal-node一起使用。请引用以下代码

const authorizationManagement = require('azure-arm-authorization');
const TokenCredentials = require('ms-rest').TokenCredentials
const adal = require('adal-node').AuthenticationContext;
const scope = '/subscriptions/<subscription>/resourceGroups/<resource-group>/providers/Microsoft.Storage/storageAccounts/<storage-account>/blobServices/default/containers/<container>';
const subscriptionId = 'e5b0fcfa-e859-43f3-8d84-5e5fe29f4c68';

// use service principal to get access token with adal-node
/*
  If you do not have a  service principal, you can use the following Azure CLI command(https://learn.microsoft.com/en-us/cli/azure/ad/sp?view=azure-cli-latest#az-ad-sp-create-for-rbac) to create it 
    az ad sp create-for-rbac -n "MyApp" --role contributor 
    

*/ 
const tenant = 'your-tenant-id';
const authorityUrl = "https://login.microsoftonline.com/" + tenant;
const clientId = 'your-client-id';
const clientSecret = 'your-client-secret';
const resource = 'https://management.azure.com/';
const context = new adal(authorityUrl);
context.acquireTokenWithClientCredentials(
     resource,
     clientId,
     clientSecret,
     (err, tokenResponse) => {
          if (err) {
               console.log(`Token generation failed due to ${err}`);
          } else {

               const credentials = new TokenCredentials(tokenResponse.accessToken);

               const client = new authorizationManagement(credentials, subscriptionId);
               client.roleAssignments.listForScope(scope).then(result => {
                    result.forEach(element => {

                         client.roleDefinitions.getById(element.roleDefinitionId).then(result => {
                              console.log("principal ID: " + element.principalId + "\nrole name: " + result.roleName)
                         });
                    });
               });
          }
     }
);

enter image description here

此外,如果您想知道如何使用passport-azure-ad来获取角色分配,您可以使用passport-azure-ad来获取AD访问 token ,然后调用Azure rest API 。关于如何实现,可以引用sample .

关于azure - 授权用户访问 azure blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59646020/

相关文章:

c# - Azure 应用服务中的 IWebHostEnvironment ContentRootPath 与本地不同

azure - 获得 ADAL B2C 的团体 claim ?

.net - 在 Azure 中运行控制台应用程序 - 无需 WebJobs

Azure AD - 禁用客户端的应用程序

azure-active-directory - Microsoft Azure AD 代表 B2C Flow

node.js - open id connect认证后如何将用户注册到mongodb?

Azure CloudBlockBlob.DeleteIfExists() - false 是否始终意味着 blob 不存在?

用于从 blob 中删除/筛选具有额外分隔符的行的 Azure Powershell 脚本

azure - 查看来自多个客户端的队列/服务总线消息

如果 Blob 名称存在,Azure Blob 上传重命名