Skip to content

Commit bba45a8

Browse files
Fix Issue New-AzStorageSyncCloudEndpoint across subscriptions failing Azure#11213 (Azure#12601)
1 parent d2ef80f commit bba45a8

File tree

3 files changed

+53
-34
lines changed

3 files changed

+53
-34
lines changed

src/StorageSync/StorageSync/CloudEndpoint/NewCloudEndpointCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public override void ExecuteCmdlet()
212212
}
213213

214214
PSADServicePrincipal servicePrincipal = StorageSyncClientWrapper.EnsureServicePrincipal();
215-
RoleAssignment roleAssignment = StorageSyncClientWrapper.EnsureRoleAssignment(servicePrincipal, StorageAccountResourceId);
215+
RoleAssignment roleAssignment = StorageSyncClientWrapper.EnsureRoleAssignment(servicePrincipal, storageAccountResourceIdentifier.Subscription, StorageAccountResourceId);
216216

217217
var parentResourceIdentifier = default(ResourceIdentifier);
218218

src/StorageSync/StorageSync/Common/StorageSyncClientWrapper.cs

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -270,46 +270,64 @@ public PSADServicePrincipal EnsureServicePrincipal()
270270
/// Ensures the role assignment.
271271
/// </summary>
272272
/// <param name="serverPrincipal">The server principal.</param>
273+
/// <param name="storageAccountSubscriptionId">The storage account subscription identifier.</param>
273274
/// <param name="storageAccountResourceId">The storage account resource identifier.</param>
274275
/// <returns>RoleAssignment.</returns>
275-
/// <exception cref="PSArgumentException">roleDefinition</exception>
276-
public RoleAssignment EnsureRoleAssignment(PSADServicePrincipal serverPrincipal, string storageAccountResourceId)
276+
public RoleAssignment EnsureRoleAssignment(PSADServicePrincipal serverPrincipal, string storageAccountSubscriptionId, string storageAccountResourceId)
277277
{
278-
var resourceIdentifier = new ResourceIdentifier(storageAccountResourceId);
279-
string roleDefinitionScope = "/";
280-
RoleDefinition roleDefinition = AuthorizationManagementClient.RoleDefinitions.Get(roleDefinitionScope, BuiltInRoleDefinitionId);
281-
282-
var serverPrincipalId = serverPrincipal.Id.ToString();
283-
var roleAssignments = AuthorizationManagementClient.RoleAssignments
284-
.ListForResource(
285-
resourceIdentifier.ResourceGroupName,
286-
ResourceIdentifier.GetProviderFromResourceType(resourceIdentifier.ResourceType),
287-
resourceIdentifier.ParentResource ?? "/",
288-
ResourceIdentifier.GetTypeFromResourceType(resourceIdentifier.ResourceType),
289-
resourceIdentifier.ResourceName,
290-
odataQuery: new ODataQuery<RoleAssignmentFilter>(f => f.AssignedTo(serverPrincipalId)));
291-
var roleAssignmentScope = storageAccountResourceId;
292-
Guid roleAssignmentId = StorageSyncResourceManager.GetGuid();
293-
294-
RoleAssignment roleAssignment = roleAssignments.FirstOrDefault();
295-
if (roleAssignment == null)
278+
string currentSubscriptionId = AuthorizationManagementClient.SubscriptionId;
279+
bool hasMismatchSubscription = currentSubscriptionId != storageAccountSubscriptionId;
280+
281+
try
296282
{
297-
VerboseLogger.Invoke(StorageSyncResources.CreateRoleAssignmentMessage);
298-
var createParameters = new RoleAssignmentCreateParameters
283+
if(hasMismatchSubscription)
284+
{
285+
AuthorizationManagementClient.SubscriptionId = storageAccountSubscriptionId;
286+
}
287+
288+
var resourceIdentifier = new ResourceIdentifier(storageAccountResourceId);
289+
string roleDefinitionScope = "/";
290+
RoleDefinition roleDefinition = AuthorizationManagementClient.RoleDefinitions.Get(roleDefinitionScope, BuiltInRoleDefinitionId);
291+
292+
var serverPrincipalId = serverPrincipal.Id.ToString();
293+
var roleAssignments = AuthorizationManagementClient.RoleAssignments
294+
.ListForResource(
295+
resourceIdentifier.ResourceGroupName,
296+
ResourceIdentifier.GetProviderFromResourceType(resourceIdentifier.ResourceType),
297+
resourceIdentifier.ParentResource ?? "/",
298+
ResourceIdentifier.GetTypeFromResourceType(resourceIdentifier.ResourceType),
299+
resourceIdentifier.ResourceName,
300+
odataQuery: new ODataQuery<RoleAssignmentFilter>(f => f.AssignedTo(serverPrincipalId)));
301+
var roleAssignmentScope = storageAccountResourceId;
302+
Guid roleAssignmentId = StorageSyncResourceManager.GetGuid();
303+
304+
RoleAssignment roleAssignment = roleAssignments.FirstOrDefault();
305+
if (roleAssignment == null)
299306
{
300-
Properties = new RoleAssignmentProperties
307+
VerboseLogger.Invoke(StorageSyncResources.CreateRoleAssignmentMessage);
308+
var createParameters = new RoleAssignmentCreateParameters
301309
{
302-
PrincipalId = serverPrincipalId,
303-
RoleDefinitionId = AuthorizationHelper.ConstructFullyQualifiedRoleDefinitionIdFromSubscriptionAndIdAsGuid(resourceIdentifier.Subscription, BuiltInRoleDefinitionId)
304-
}
305-
};
310+
Properties = new RoleAssignmentProperties
311+
{
312+
PrincipalId = serverPrincipalId,
313+
RoleDefinitionId = AuthorizationHelper.ConstructFullyQualifiedRoleDefinitionIdFromSubscriptionAndIdAsGuid(resourceIdentifier.Subscription, BuiltInRoleDefinitionId)
314+
}
315+
};
306316

307-
roleAssignment = AuthorizationManagementClient.RoleAssignments.Create(roleAssignmentScope, roleAssignmentId.ToString(), createParameters);
308-
StorageSyncResourceManager.Wait();
317+
roleAssignment = AuthorizationManagementClient.RoleAssignments.Create(roleAssignmentScope, roleAssignmentId.ToString(), createParameters);
318+
StorageSyncResourceManager.Wait();
309319

310-
}
320+
}
311321

312-
return roleAssignment;
322+
return roleAssignment;
323+
}
324+
finally
325+
{
326+
if (hasMismatchSubscription)
327+
{
328+
AuthorizationManagementClient.SubscriptionId = currentSubscriptionId;
329+
}
330+
}
313331
}
314332

315333
/// <summary>

src/StorageSync/StorageSync/Interfaces/IStorageSyncClientWrapper.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,10 @@ public interface IStorageSyncClientWrapper
8181
/// Ensures the role assignment.
8282
/// </summary>
8383
/// <param name="serverPrincipal">The server principal.</param>
84-
/// <param name="resourceId">The resource identifier.</param>
84+
/// <param name="storageAccountSubscriptionId">The storage account subscription identifier.</param>
85+
/// <param name="storageAccountResourceId">The storage account resource identifier.</param>
8586
/// <returns>RoleAssignment.</returns>
86-
RoleAssignment EnsureRoleAssignment(PSADServicePrincipal serverPrincipal,string resourceId);
87+
RoleAssignment EnsureRoleAssignment(PSADServicePrincipal serverPrincipal, string storageAccountSubscriptionId, string storageAccountResourceId);
8788

8889
/// <summary>
8990
/// This function will invoke the registration and continue operation with a success function call.

0 commit comments

Comments
 (0)