Jump to content

The Usage of Managed Identity in the Azure Batch Account and Azure Batch Pool


Recommended Posts

Guest v-mosh21
Posted

Overview:

 

The managed identities can be used both in the Azure Batch Account and Azure Batch Pool, which may cause confusion in some situations. This blog is aimed to help you understand the differences between the usage of the managed identity in the Azure Batch Account and in the Azure Batch Pool. The managed identity configured on the Batch account only grants permission for the Azure Batch Account to authenticate to other services but not for Azure Batch nodes (or Pool VMs). If the purpose is to grant the Azure Batch nodes access to other Azure resources, then you are looking for the managed identity configured in the Azure Batch Pool.

 

 

 

Outline:

 

  • Overall Pre-requirement
  • Managed Identity in the Azure Batch Account
  • Managed Identity in the Azure Batch Pool
    • Where to associate managed identity to the node for accessing auto-storage account
    • Different actions that a node could do with a managed identity
    • Different behaviors when a node is trying to get application package or resource file from auto-storage using managed identity

    [*]Self-troubleshooting

 

 

 

Overall Pre-requirement:

 

1. Have an existing Azure Batch account.

 

2. Have an existing Azure Storage account.

 

3. Associate the Azure Storage account with your Azure Batch account. Note that this linked Storage account is considered to be the auto-storage account which is used to store the application packages and task resource files.

 

largevv2px999.png.1be635da6873c7edfd5bbca0113bff82.png

 

4. Turn the authentication mode of auto-storage account to "Batch Account Managed Identity".

 

largevv2px999.png.76181aeb5d03dda3161db01882614d6e.png

 

 

 

Managed Identity in the Azure Batch Account:

 

 

Users can either enable the system-assigned managed identity, or create a separate user-assigned managed identity for the Azure Batch Account in 'Identity' section.

 

mediumvv2px400.png.1a18a36501de1152e9d821aa71b00c1e.png

 

 

After you configure managed identity for the Azure Batch Account, you can click the 'JSON View' button at the top right of the Overview of Azure Batch Account to confirm the setting as shown below:

 

largevv2px999.png.c5b754ded08748b8e7c5c4714e4e3acf.png

 

 

System-assigned managed identity

 

largevv2px999.png.30956fecf2f47cca9dda1ab5b18fce35.png

 

 

User-assigned managed identity

 

largevv2px999.png.07198c1b1b60b150302f5ba6f73b37a1.png

 

 

One of the examples to use managed identity of Batch Account is to upload the application package to the Azure Batch Account.

 

mediumvv2px400.png.def2041dfa8f7e794f48528c0bf052a4.png

 

 

The application package will be stored in the auto-storage account that is associated with Azure Batch Account. If the authentication mode of the storage account information is chosen as 'Batch Account Managed Identity', then the system will retrieve the credential from system-assigned/user-assigned managed identity of Azure Batch Account for accessing to the auto-storage account.

 

largevv2px999.png.f28c4d4af8d8cf8e8fa98b31567288ad.png

 

 

You may need to check if the system-assigned/user-assigned managed identity has enough permission on the auto-storage account via following steps: Storage Account -> Access Control (IAM) -> Check access -> Select the Managed Identity.

 

largevv2px999.png.ba640a62842cad724708cc50b307254c.png

 

 

If the permission is not granted or the permission is not enough, you will find below error popped up from top right of the Notification when you are uploading the application package to the Azure Batch Account.

 

largevv2px999.png.1c06f969ea7d75ec81851e97a490f73f.png

 

 

Capturing the HAR file of the request, there will be a POST request whose name starts with 'batch' and result is 200. From the payload, you will see the actual PUT request sending to Azure Batch Account. From the response of this actual request, this request resulted as 409 with error message displayed as "The managed identity does not have permission to access auto storage account. Please use Azure RBAC to assign the managed identity access to auto storage". This means that your system-assigned/user-assigned managed identity of Azure Batch Account does not have enough permission to perform actions on the auto-storage account.

 

largevv2px999.png.7cbb62fc33a314670bb8c4a73592d425.png

 

 

The other example for accessing the Azure Key Vault is documented here:
.

 

 

**Note that the interaction we are talking about here is between Azure Batch Account and Azure Storage/Key Vault, and if the interaction is between Azure Batch Nodes and other Azure Resources, you will need to configure the managed identity in the Azure Batch Pool.

 

 

 

Managed Identity in the Azure Batch Pool:

 

 

When the authentication mode of the linked Azure Storage account (or called auto-storage account) is set as "Batch Account Managed Identity", the Azure Batch nodes get the managed identity tokens and use them to authenticate through Azure AD authentication via the Azure Instance Metadata Service.

 

 

The Azure Batch Pool only supports user-assigned managed identity so users need to create a user-assigned managed identity in the same tenant as your Azure Batch account:
. Also, users need to grant the Storage data plane permission (e.g. Storage Blob Data Owner) to the user-assigned managed identity.

 

 

Where to associate managed identity to the node for accessing auto-storage account:

 

 

There is an explanation in document
: the reference to the user-assigned identity which compute nodes will use to access auto-storage and the identity referenced here
must be assigned to pools
which have compute nodes that need access to auto-storage. Thus, there are two places that need to be configured as shown below:

 

 

1.The Node Identity Reference

 

largevv2px999.png.436c2e7baecc4c7bc01971184f5a7273.png

 

 

2.The Pool Identity

 

largevv2px999.png.c03b7cfa37e424eee6dc0244e954696d.png

 

 

**Important: you can define multiple user-assigned managed identities in the Pool Identity, but
the one defined in the Node Identity Reference must be defined in the Pool Identity
.

 

 

 

Different actions that a node could do with a managed identity:

 

 

 

  1. Application Package

 

When creating a pool, users can specify application packages at the pool level. The application packages will be downloaded from the auto-storage account and installed in all nodes in this pool. Reference:
. The application packages should be uploaded to the Azure Batch Account before referring it during the pool creation.

 

727x695vv2.png.9c441853f63bad5c06b773d7b1b56ccf.png

 

 

2.Task Resource File

 

While creating a task, users can specify the resource files. These files are automatically downloaded to the node from the Storage before the task command is executed. Reference document:
.

 

916x788vv2.png.041f04df6fe205ba78ac4437b9b40690.png

 

Note that you can specify the resource files in three ways: auto-storage container, container URL, and http URL.

 

  • For auto-storage container, the identity reference displays as none here and cannot be modified. It means that the node will access to the auto-storage to retrieve resource files.
  • For container URL and http URL, users can define other Storage account's URL as long as there are enough permissions configured in the defined Storage account for the identity reference and the identity is added in the Batch pool.
  • The identity must be defined in both Node Identity Reference and Pool Identity when you need access to the auto-storage account.

 

The parameter "Blob Prefix" and "File Path" are optional (blob prefix is used to filter specific blob, and the file path is used to create a sub-folder in the node for storing the blob files. If it is not defined, the files should be stored under each task's path: root/wd).

 

largevv2px999.png.92b1b51f89d8a5125a67b9fdb2cf87e5.png

 

 

 

Different behaviors when a node is trying to get application package or resource file from auto-storage using managed identity:

 

 

There are some scenarios showing different behaviors when a node is trying to get application package or resource file from auto-storage by using managed identity:

 

 

For Pool Application Package:

 

 

Scenario 1:

 

If the managed identity
is given in the Node Identity Reference
, and this managed identity
has enough permission in the auto-storage account
, when
creating a pool with the same managed identity
, the
node will start successfully and the package will be downloaded to the node
.

 

largevv2px999.png.b7e2c134c03a3927932b983ad3f27d5e.png

 

 

Scenario 2:

 

If the managed identity
is given in the Node Identity Reference
, and this managed identity
does not
have enough permission in the auto-storage account
, when
creating a pool with the same managed identity
, the
node will start successfully (in idle state) but the
package will not be downloaded
to the node
.

 

largevv2px999.png.197903ac768221455662ff0717204213.png

 

 

Scenario 3:

 

If the managed identity
is
not
given in the Node Identity Reference
, no matter whether the managed identity has enough permission, and no matter whether
creating a pool
with or without
the same managed identity
, you will see the
node gets stuck in the starting state forever
. When the Azure Batch System is trying to start the node, the node identity reference becomes null in this scenario, which cause the node stuck in starting.

 

largevv2px999.png.1f884670ae44cc7db5d6f1ea9c49acfa.png

 

 

Users can double confirm it by checking the Batch agent logs. Here are the steps:

 

1. Click the Upload batch logs button inside a Batch node to upload the logs to a valid Storage container.

 

largevv2px999.png.413302f91c82562a3116a016a99ee8f3.png

 

 

2. Access to the Storage container and download the agent-debug.log file.

 

largevv2px999.png.842f986c0d0a2189a1a160601adb8ff6.png

 

 

3. From the log, the health status keeps reporting pool not fully joined yet and the node gets stuck in the JoinPool progress.

 

largevv2px999.png.933cfa911ede2dfbfb146a260c61ed8c.png

 

 

Scenario 4:

 

If the managed identity
is given in the Node Identity Reference
, no matter whether the managed identity has enough permission, when
creating a pool
without
the same managed identity
, you will see the
node becomes unusable
. Note that you can define multiple managed identities when creating the pool. However, if the identity that is defined in the Node Identity Reference is not added in the pool identity, Azure Batch system cannot find a correct identity that matches with the one defined in the node reference and finally throw the exception.

 

largevv2px999.png.f706c4b6c72957d1b81eb920e8b410ba.png

 

 

Further click the node from the pool and you will see the node error with code "ApplicaitonPackageError".

 

largevv2px999.png.263f458bb17e317b99cf8a2664d6eb45.png

 

 

For Task Resource File:

 

 

Scenario 1:

 

If the managed identity
is given in the Node Identity Reference
, and this managed identity
has enough permission in the auto-storage account
, after
creating a pool with the same managed identity
, and then create a task with resource file from auto-storage's container, users will see the blob file is downloaded under task's path: root/wd/1 (here I named file path as 1).

 

largevv2px999.png.6aa8b99008d5d051f310dd06e2981a0e.png

 

 

Scenario 2:

 

If the managed identity
is given in the Node Identity Reference
, and this managed identity
does not have
enough permission in the auto-storage account
, after
creating a pool with the same managed identity
, and then create a task with resource file to retrieve blobs from auto-storage's container, user will see the task reports error "
ResourceContainerAccessDenied
".

 

largevv2px999.png.32dc28f2e432121b7e34bb64fade5cea.png

 

 

Click the task, and select the "Json view" from the task properties, you will see the message reports as "Access for one of the specified Azure Blob container(s) is denied".

 

largevv2px999.thumb.png.756ef974e28b127538d83c8131ab8235.png

 

 

Scenario 3:

 

If the managed identity
is
not
given in the Node Identity Reference
, no matter whether the managed identity has enough permission, no matter whether
creating a pool
with or without
the same managed identity
, and then create a task with resource file to retrieve blobs from auto-storage's container, users will see the same behavior as above showing "
ResourceContainerAccessDenied
" error.

 

largevv2px999.png.71e458baeb329ae0f48c22c3661b9770.png

 

 

Scenario 4:

 

If the managed identity
is given in the Node Identity Reference
, no matter whether the managed identity has enough permission, after
creating a pool
without
the same managed identity
, and then create a task with resource file to retrieve blobs from auto-storage's container, users will see the task reports error "
ResourceContainerListMiscError
".

 

largevv2px999.png.a8c721f6bd2fc0b9bea90aae00e97f94.png

 

 

Click the task, and select the "Json view" from the task properties, users will see the message "Miscellaneous error encountered while listing one of the specified Azure Blob container(s)". The error message indicates that this is a 400 bad request when authenticating the managed identity since the identity defined in the reference does not match any of identity defined in the pool identity configuration.

 

largevv2px999.png.6ee789dda0a2cdc79322e564fb7ab584.png

 

 

 

In summary, the below table shows different behaviors when a node is trying to get application package or resource file from auto-storage using managed identity (MI):

 

 

 


Pool Application Package

Node Identify Ref with enough Permission

Node Identify Ref without enough Permission

No Node Identity Ref

MI In the Pool Identity

Work

Node Up without App Package

Node Starting Forever

MI Not In the Pool Identity

Node Unusable

Node Unusable

Node Starting Forever

 

 

 


Task Resource File

Node Identify Ref with enough Permission

Node Identify Ref without enough Permission

No Node Identity Ref

MI In the Pool Identity

Work

Failed with 403

Failed with access deny

MI Not In the Pool Identity

Failed with 400

Failed with 400

Failed with access deny

 

 

 

Self-troubleshooting:

 

 

Additionally, to confirm if the managed identity assigned to the Azure Batch node (in Windows) has enough permission to access to the Azure resource like storage account, users can:

 

 

** Note that these steps stimulate the last step to get the token from Azure Instance Metadata Service (IMDS) with valid identity ID and access to the storage account to check if the permission is enough or not. However, if the identity is not defined in the Node Identity Reference, the node is not able to get the identity ID and the entire process has already been blocked before executing the last step.
Thus, please double check if the identity is defined in the Node Identity Reference before performing the self-troubleshooting steps.

 

 

1. RDP to the node

 

 

2. Install Postman and send a GET request with following URL with Header ("Metadata = true"). The IP address 169.254.169.254 is also known as IMDS, which provides information about the virtual machine instance so you can use it to request tokens for managed identity. Reference document:
.

 

 

 

 

The highlighted part should be your resource id of user-assigned managed identity defined in the Pool Identity and you can view them from pool properties including resource ID, client ID, and principal ID. If there is only one identity defined in the Pool, you can use following URL
.

 

largevv2px999.png.5f894113162fe5209b41b7424c04c54f.png

 

 

3. The response body should contain client ID and bearer token (access token). The client ID should be the same as you see from pool properties.

 

largevv2px999.png.4fba99e07c8be29853c088eefaf0bc62.png

 

 

If you defined multiple user-assigned identities in the pool identity and did not specify an identity in the URL, you would get a 400 bad request.

 

largevv2px999.png.60a681ef910976c7c7a6922d9f398c23.png

 

 

4. You can then copy the full bearer token and further test it by retrieving the blob from the auto-storage. In below example, the managed identity does not have permission to access the storage so the 403 error is returned from the auto-storage account.

 

Note that the x-ms-version header is required for getting blob, referencing
:
.

 

largevv2px999.png.f724a7b0b22c5a7e053e7b224e96760b.png

 

 

 

Reference:

 

Create a User-assigned Managed Identity

 

Update Batch Account REST API

 

Azure Batch Upload and Manage Applications

 

Concept of Azure Batch Tasks

 

Azure Instance Metadata Service (IMDS)

 

Azure Storage Get Blob REST API

 

Continue reading...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...