pulumi-azure-native: Race Condition Issue with User Assigned Managed Identity's PrincipalId and SqlResourceSqlRoleAssignment
What happened?
When using a User Assigned Managed Identity and attempting to use SqlResourceSqlRoleAssignment for Cosmos DB to give RBAC permissions, occasionally it will error out the following error.
error: resource partially created but read failed autorest/azure: Service returned an error. Status=404 Code="NotFound" Message="Unable to find a SQL Role Assignment with ID [XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX].\r\nActivityId: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX, Microsoft.Azure.Documents.Common/2.14.0": Code="BadRequest" Message="The provided principal ID [XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX] was not found in the AAD tenant(s) [XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX] which are associated with the customer's subscription.\r\nActivityId: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX, Microsoft.Azure.Documents.Common/2.14.0, Microsoft.Azure.Documents.Common/2.14.0, Microsoft.Azure.Documents.Common/2.14.0, Microsoft.Azure.Documents.Common/2.14.0, Microsoft.Azure.Documents.Common/2.14.0, Microsoft.Azure.Documents.Common/2.14.0"
Subsequent runs will succeed in creating a SqlResourceSqlRoleAssignment.
Example
var userAssignedIdentity = new ManagedIdentity.UserAssignedIdentity("user-assigned-identity", new ManagedIdentity.UserAssignedIdentityArgs
{
ResourceGroupName = resourceGroupName
}).PrincipalId;
var sqlResourceSqlRoleAssignment = new SqlResourceSqlRoleAssignment($"sql-resource-sql-role-assignment", new SqlResourceSqlRoleAssignmentArgs
{
AccountName = cosmosAccount.Apply(account => account.Name),
PrincipalId = userAssignedIdentity,
ResourceGroupName = resourceGroupName,
RoleAssignmentId = new Pulumi.Random.RandomUuid("testRandomUuid").Result,
RoleDefinitionId = cosmosDBDataContributorRoleDefinition,
Scope = cosmosAccount.Apply(account => account.Id),
});
Example Repo with Referenced Code Above
Output of pulumi about
CLI
Version 3.79.0
Go Version go1.21.0
Go Compiler gc
Plugins NAME VERSION azure-native 2.10.0 azuread 5.42.0 dotnet unknown random 4.14.0
Host
OS darwin
Version 13.6
Arch arm64
Dotnet 7
Additional context
This could be an issue on the Microsoft side. When I create a normal role assignment, it performs a PUT against RoleAssignment and responds with a 201 Created immediately. When I attempt to create a SqlResourceSqlRoleAssignment, it responds 202 Accepted and has a status of “Enqueued” and eventually fails after several GET requests to check the status of the resource.
Contributing
Vote on this issue by adding a 👍 reaction. To contribute a fix for this issue, leave a comment (and link to your pull request, if you’ve opened one already).
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 6
- Comments: 24 (10 by maintainers)
I have heard of this issue and encountered it this last Spring testing Bicep deployments with user-assigned MSI and Cosmos RBAC. I also opened a support ticket on this and had both Cosmos and AAD teams look at it but could not get to the bottom of it. I also could not reproduce it reliably.
Same as with the behavior observed by others here is that rerunning the deployment always fixed the issue. My guess on why this happens is the principal id for the newly created user-assigned MSI has not fully replicated in AAD store leading to the initial 404 and eventual success of the operation.
@mikhailshilkov can you ping me directly via email? I’d like to see if we can set up a repro of this and get resolved.
They requested some fresh resources and logs, and I haven’t been able to provide them yet. I will see if I can get a repro soon again and send it over to them again and request another investigation into it. Unfortunately, the first time around it took standing up resources and tearing them down on a loop over the course of a week to capture it.
@mikocot I have sent a copy of the logs and behavior to them, met with them, and discussed the behavior. They acknowledged, but have requested a new set of logs, so I am working to send them a new set of logs.
I’m posting a full HTTP log so that it’s easier to discuss what is going on.
This is a service bug - the tenant exists but it’s reported as if it’s not (probably, some eventual consistency issue).
Maybe also related to https://github.com/Azure/go-autorest/issues/634
As an update from me,
I opened a ticket with MSFT to see if we can get some traction on that side. I provided most of the logs and described the behavior we have seen. They said they are contacting the product team to do some investigation on it. 🤞
@mikhailshilkov it’s not retrying of HTTP requests that we need - it’s retrying of the whole resource creation.
What we’re seeing is that after we’ve created the role, we can retrieve it correctly - even from other regions. However, after we’ve started the process of creating the
SqlResourceSqlRoleAssignmentthe whole creation operation fails with the error mentioned. At this point we’d need to retry the creation of a brand newSqlResourceSqlRoleAssignmentwhich is not just an HTTP retry.It could be possible to build a new kind of retry mechanism for this, though I’d also consider if this should be a feature of the core Pulumi engine rather than this specific provider: https://github.com/pulumi/pulumi/issues/7932
I was able to catch verbose logs for this and pass them along to engineering. Adding a note in here for transparency.