Skip to content

Commit a024080

Browse files
author
BradleyBartlett
committed
Use official Azure Stack Registration Owner RBAC role
1 parent 352d947 commit a024080

File tree

1 file changed

+7
-74
lines changed

1 file changed

+7
-74
lines changed

Registration/RegisterWithAzure.psm1

Lines changed: 7 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -460,44 +460,6 @@ function Set-AzsRegistrationSubscription{
460460

461461
Log-Output "Logging in to Azure."
462462
$connection = Connect-AzureAccount -SubscriptionId $CurrentAzureSubscriptionId -AzureEnvironment $AzureEnvironmentName -AzureDirectoryTenantName $AzureDirectoryTenantName -Verbose
463-
464-
$role = Get-AzureRmRoleDefinition -Name 'Registration Reader' -Scope "/subscriptions/$CurrentAzureSubscriptionId"
465-
if (-not $role)
466-
{
467-
$role = Get-AzureRmRoleDefinition -Name "Registration Reader-$($CurrentAzureSubscriptionId)" -Scope "/subscriptions/$CurrentAzureSubscriptionId"
468-
}
469-
470-
if($role)
471-
{
472-
if(-not($role.AssignableScopes -icontains "/subscriptions/$NewAzureSubscriptionId"))
473-
{
474-
try
475-
{
476-
Log-Output "Adding alternate subscription Id to scope of custom RBAC role"
477-
$role.AssignableScopes.Add("/subscriptions/$NewAzureSubscriptionId")
478-
Set-AzureRmRoleDefinition -Role $role
479-
}
480-
catch
481-
{
482-
if($_.Exception -ilike "*AuthorizationFailed:*")
483-
{
484-
Log-Warning "Unable to add the new subscription: $NewAzureSubscriptionId to the scope of existing RBAC role definition. Continuing with transfer of registration `r`n$($_.Exception)"
485-
}
486-
else
487-
{
488-
Log-Throw "Unable to swap to the provided NewAzureSubscriptionId $NewAzureSubscriptionId `r`n$($_.Exception)" -CallingFunction $PSCmdlet.MyInvocation.InvocationName
489-
}
490-
}
491-
}
492-
else
493-
{
494-
Log-Output "The provided subscription is already in the assignable scopes of RBAC role 'Registration Reader'. Continuing with transfer of registration."
495-
}
496-
}
497-
else
498-
{
499-
Log-Throw -Message "The 'Registration Reader' custom RBAC role has not been defined. Please run Add-AzsRegistration to ensure it is created." -CallingFunction $PSCmdlet.MyInvocation.InvocationName
500-
}
501463

502464
$params = @{}
503465
$PSCmdlet.MyInvocation.BoundParameters.Keys.ForEach({if(($value=Get-Variable -Name $_ -ValueOnly -ErrorAction Ignore)){$params[$_]=$value}})
@@ -733,53 +695,24 @@ function New-RBACAssignment{
733695
)
734696

735697
Log-Output "Setting Registration Reader role on '$($RegistrationResource.ResourceId)' for service principal $ServicePrincipalObjectId."
736-
$customRoleAssigned = $false
737-
$customRoleName = "Registration Reader"
738-
739-
# Determine if the custom RBAC role has been defined
740-
$customRoleDefined = Get-AzureRmRoleDefinition -Name $customRoleName
741-
if (-not $customRoleDefined)
742-
{
743-
$customRoleName = "Registration Reader-$($RegistrationResource.SubscriptionId)"
744-
$customRoleDefined = Get-AzureRmRoleDefinition -Name $customRoleName
745-
if (-not $customRoleDefined)
746-
{
747-
# Create new RBAC role definition
748-
$role = Get-AzureRmRoleDefinition -Name 'Reader'
749-
$role.Name = $customRoleName
750-
$role.id = [guid]::newguid()
751-
$role.IsCustom = $true
752-
$role.Actions.Add('Microsoft.AzureStack/registrations/products/listDetails/action')
753-
$role.Actions.Add('Microsoft.AzureStack/registrations/products/read')
754-
$role.AssignableScopes.Clear()
755-
$role.AssignableScopes.Add("/subscriptions/$($RegistrationResource.SubscriptionId)")
756-
$role.Description = "Custom RBAC role for registration actions such as downloading products from Azure marketplace"
757-
try
758-
{
759-
New-AzureRmRoleDefinition -Role $role
760-
}
761-
catch
762-
{
763-
Log-Throw -Message "Defining custom RBAC role $customRoleName failed: `r`n$($_.Exception)" -CallingFunction $PSCmdlet.MyInvocation.InvocationName
764-
}
765-
}
766-
}
698+
$roleAssigned = $false
699+
$roleName = "Azure Stack Registration Owner"
767700

768-
# Determine if custom RBAC role has been assigned
701+
# Determine if Azure Stack Registration Owner RBAC role has been assigned
769702
$roleAssignmentScope = "/subscriptions/$($RegistrationResource.SubscriptionId)/resourceGroups/$($RegistrationResource.ResourceGroupName)/providers/Microsoft.AzureStack/registrations/$($RegistrationResource.ResourceName)"
770703
$roleAssignments = Get-AzureRmRoleAssignment -Scope $roleAssignmentScope -ObjectId $ServicePrincipalObjectId -ErrorAction SilentlyContinue
771704

772705
foreach ($role in $roleAssignments)
773706
{
774-
if ($role.RoleDefinitionName -eq $customRoleName)
707+
if ($role.RoleDefinitionName -eq $roleName)
775708
{
776-
$customRoleAssigned = $true
709+
$roleAssigned = $true
777710
}
778711
}
779712

780-
if (-not $customRoleAssigned)
713+
if (-not $roleAssigned)
781714
{
782-
New-AzureRmRoleAssignment -Scope $roleAssignmentScope -RoleDefinitionName $customRoleName -ObjectId $ServicePrincipalObjectId
715+
New-AzureRmRoleAssignment -Scope $roleAssignmentScope -RoleDefinitionName $roleName -ObjectId $ServicePrincipalObjectId
783716
}
784717
}
785718

0 commit comments

Comments
 (0)