簡體   English   中英

授權用戶訪問 azure blob

[英]Authorize a user for azure blob access

我正在通過adal-node庫使用microsoft 登錄對用戶進行身份驗證。

adal-node有一個AuthenticationContext ,我們可以從中獲取JWT 令牌使用acquireTokenWithAuthorizationCode

因此,我的 Active Directory 應用程序的用戶現在可以使用他們的 Microsoft 帳戶成功登錄。

現在,問題是如何使用上述收到的JWT 令牌獲取特定存儲帳戶/容器/blob的 RBAC 角色? 這甚至可能嗎?

或者我應該為此目的使用像azure-arm-authorization這樣的庫嗎? 我已經為每個storageaccount/container/blob設置了 RBAC 角色,但我沒有找到任何關於如何為我的應用程序的每個登錄用戶獲取這些角色的在線文檔。

任何幫助都是無價的。

TL;DR我如何授權天藍色斑點?

根據我的研究,如果我們想使用 Azure AD 身份驗證來訪問 Azure blob 存儲,我們需要為 Azure 存儲帳戶或容器分配 Azure RABC 角色。 有關詳細信息,請參閱此處 此外,我們可以使用 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>"

欲了解更多信息,請閱讀文章


更新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)
        });
      }); 
 });
})

更多詳情請參考Node.js中獲取資源組的訪問控制列表(IAM)


更新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://docs.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)
                         });
                    });
               });
          }
     }
);

在此處輸入圖片說明

另外如果你想知道如何使用passport-azure-ad來獲取角色分配,你可以使用passport-azure-ad來獲取AD訪問令牌,然后調用Azure rest API 具體實現方法可以參考示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM