Skip to content

Commit 41d2bdf

Browse files
author
BradleyBartlett
committed
fixes to custom RBAC role workaround; token collection cleaned up
1 parent 66e7874 commit 41d2bdf

File tree

1 file changed

+35
-66
lines changed

1 file changed

+35
-66
lines changed

Registration/RegisterWithAzure.psm1

Lines changed: 35 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -736,24 +736,28 @@ function New-RBACAssignment{
736736
if (-not $customRoleDefined)
737737
{
738738
$customRoleName = "Registration Reader-$($RegistrationResource.SubscriptionId)"
739-
# Create new RBAC role definition
740-
$role = Get-AzureRmRoleDefinition -Name 'Reader'
741-
$role.Name = $customRoleName
742-
$role.id = [guid]::newguid()
743-
$role.IsCustom = $true
744-
$role.Actions.Add('Microsoft.AzureStack/registrations/products/listDetails/action')
745-
$role.Actions.Add('Microsoft.AzureStack/registrations/products/read')
746-
$role.AssignableScopes.Clear()
747-
$role.AssignableScopes.Add("/subscriptions/$($RegistrationResource.SubscriptionId)")
748-
$role.Description = "Custom RBAC role for registration actions such as downloading products from Azure marketplace"
749-
try
750-
{
751-
New-AzureRmRoleDefinition -Role $role
752-
}
753-
catch
739+
$customRoleDefined = Get-AzureRmRoleDefinition -Name $customRoleName
740+
if (-not $customRoleDefined)
754741
{
755-
Log-Throw -Message "Defining custom RBAC role $customRoleName failed: `r`n$($_.Exception)" -CallingFunction $PSCmdlet.MyInvocation.InvocationName
756-
}
742+
# Create new RBAC role definition
743+
$role = Get-AzureRmRoleDefinition -Name 'Reader'
744+
$role.Name = $customRoleName
745+
$role.id = [guid]::newguid()
746+
$role.IsCustom = $true
747+
$role.Actions.Add('Microsoft.AzureStack/registrations/products/listDetails/action')
748+
$role.Actions.Add('Microsoft.AzureStack/registrations/products/read')
749+
$role.AssignableScopes.Clear()
750+
$role.AssignableScopes.Add("/subscriptions/$($RegistrationResource.SubscriptionId)")
751+
$role.Description = "Custom RBAC role for registration actions such as downloading products from Azure marketplace"
752+
try
753+
{
754+
New-AzureRmRoleDefinition -Role $role
755+
}
756+
catch
757+
{
758+
Log-Throw -Message "Defining custom RBAC role $customRoleName failed: `r`n$($_.Exception)" -CallingFunction $PSCmdlet.MyInvocation.InvocationName
759+
}
760+
}
757761
}
758762

759763
# Determine if custom RBAC role has been assigned
@@ -827,7 +831,8 @@ function Connect-AzureAccount{
827831
Add-AzureRmAccount -SubscriptionId $SubscriptionId
828832
Set-AzureRmContext -SubscriptionId $SubscriptionId -TenantId $AzureDirectoryTenantId
829833
$environment = Get-AzureRmEnvironment -Name $AzureEnvironmentName
830-
$subscription = Get-AzureRmSubscription -SubscriptionId $SubscriptionId
834+
$subscription = Get-AzureRmSubscription -SubscriptionId $SubscriptionId
835+
$context = Get-AzureRmContext
831836
}
832837
catch
833838
{
@@ -840,58 +845,22 @@ function Connect-AzureAccount{
840845
}
841846

842847

843-
[Version]$azurePSVersion = (Get-Module AzureRm.Profile).Version
844-
Log-Output "Using AzureRm.Profile version: $azurePSVersion"
845-
846-
if ($azurePSVersion -ge [Version]"3.3.2")
847-
{
848-
$tokens = [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.TokenCache.ReadItems()
849-
if (-not $tokens -or ($tokens.Count -le 0))
850-
{
851-
$tokens = $context.TokenCache.ReadItems()
848+
$tokens = @()
849+
try{$tokens += [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.TokenCache.ReadItems()}catch{}
850+
try{$tokens += [Microsoft.IdentityModel.Clients.ActiveDirectory.TokenCache]::DefaultShared.ReadItems()}catch{}
851+
try{$tokens += $context.TokenCache.ReadItems()}catch{}
852852

853-
if (-not $tokens -or ($tokens.Count -le 0))
854-
{
855-
Log-Throw -Message "Token cache is empty `r`n$($_.Exception)" -CallingFunction $PSCmdlet.MyInvocation.InvocationName
856-
}
857-
else
858-
{
859-
$token = $tokens |
860-
Where Resource -EQ $environment.ActiveDirectoryServiceEndpointResourceId |
861-
Where { $_.TenantId -eq $subscription.TenantId } |
862-
Where { $_.ExpiresOn -gt [datetime]::UtcNow } |
863-
Select -First 1
864-
}
865-
}
866-
else
867-
{
868-
$token = $tokens |
869-
Where Resource -EQ $environment.ActiveDirectoryServiceEndpointResourceId |
870-
Where { $_.TenantId -eq $subscription.TenantId } |
871-
Where { $_.ExpiresOn -gt [datetime]::UtcNow } |
872-
Select -First 1
873-
}
874-
}
875-
else
853+
if (-not $tokens -or ($tokens.Count -le 0))
876854
{
877-
$tokens = [Microsoft.IdentityModel.Clients.ActiveDirectory.TokenCache]::DefaultShared.ReadItems()
878-
if (-not $tokens -or ($tokens.Count -le 0))
879-
{
880-
if (-not $tokens -or ($tokens.Count -le 0))
881-
{
882-
Log-Throw -Message "Token cache is empty `r`n$($_.Exception)" -CallingFunction $PSCmdlet.MyInvocation.InvocationName
883-
}
884-
}
885-
else
886-
{
887-
$token = $tokens |
888-
Where Resource -EQ $environment.ActiveDirectoryServiceEndpointResourceId |
889-
Where { $_.TenantId -eq $subscription.TenantId } |
890-
Where { $_.ExpiresOn -gt [datetime]::UtcNow } |
891-
Select -First 1
892-
}
855+
Log-Throw -Message "Token cache is empty `r`n$($_.Exception)" -CallingFunction $PSCmdlet.MyInvocation.InvocationName
893856
}
894857

858+
$token = $tokens |
859+
Where Resource -EQ $environment.ActiveDirectoryServiceEndpointResourceId |
860+
Where { $_.TenantId -eq $subscription.TenantId } |
861+
Sort ExpiresOn |
862+
Select -Last 1
863+
895864

896865
if (-not $token)
897866
{

0 commit comments

Comments
 (0)