azure-powershell icon indicating copy to clipboard operation
azure-powershell copied to clipboard

Get-AzureRmRoleAssignment command failing

Open dmittal02 opened this issue 9 years ago • 51 comments

Cmdlet(s)

PowerShell Version

Instructions: to get PowerShell version, type $PSVersionTable and look for the value associated with PSVersion latest

Module Version

Azure PS version: 1.0.0

OS Version

Instructions: to get OS version, type $PSversionTable and look for value associated with BuildVersion Windows 2012 R2

Description

Error when trying to run the PS command: Get-AzureRmRoleAssignment : ##[error]Exception of type 'Microsoft.Rest.Azure.CloudException' was thrown. 2017-01-18T08:40:40.1990848Z Microsoft.Azure.Commands.Resources.Models.Authorization.PSRoleDefinition

Debug Output

Instructions: to get Debug Output, set $DebugPreference="Continue" and then execute the cmdlet or script causing the issue

Script/Steps for Reproduction

2017-01-18T17:43:54.3907947Z ##[debug]Leaving Initialize-Azure. 2017-01-18T17:43:54.4499767Z ##[command]& 'C:\Users\buildguest\AppData\Local\Temp\bfdd2b32-1828-48c5-a451-d7143f0a9e30.ps1' 2017-01-18T17:43:56.0065186Z ##[debug]Error record: 2017-01-18T17:43:56.1317540Z ##[debug]Get-AzureRmRoleAssignment : Exception of type 'Microsoft.Rest.Azure.CloudException' was thrown. 2017-01-18T17:43:56.1337554Z ##[debug]At C:\Users\buildguest\AppData\Local\Temp\bfdd2b32-1828-48c5-a451-d7143f0a9e30.ps1:3 char:1 2017-01-18T17:43:56.1357540Z ##[debug]+ Get-AzureRmRoleAssignment -verbose 2017-01-18T17:43:56.1367563Z ##[debug]+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 2017-01-18T17:43:56.1387550Z ##[debug] + CategoryInfo : CloseError: (:) [Get-AzureRmRoleAssignment], CloudException 2017-01-18T17:43:56.1407543Z ##[debug] + FullyQualifiedErrorId : Microsoft.Azure.Commands.Resources.GetAzureRoleAssignmentCommand 2017-01-18T17:43:56.1417557Z ##[debug] 2017-01-18T17:43:56.1447543Z ##[debug]Script stack trace: 2017-01-18T17:43:56.1497562Z ##[debug]at , C:\Users\buildguest\AppData\Local\Temp\bfdd2b32-1828-48c5-a451-d7143f0a9e30.ps1: line 3 2017-01-18T17:43:56.1517543Z ##[debug]at , : line 1 2017-01-18T17:43:56.1527562Z ##[debug]at , C:\a_tasks\AzurePowerShell_72a1931b-effb-4d2e-8fd8-f8472a07cb62\1.1.5\AzurePowerShell.ps1: line 64 2017-01-18T17:43:56.1547543Z ##[debug]at , C:\a_tasks\AzurePowerShell_72a1931b-effb-4d2e-8fd8-f8472a07cb62\1.1.5\AzurePowerShell.ps1: line 60 2017-01-18T17:43:56.1557558Z ##[debug]at , : line 1 2017-01-18T17:43:56.1577601Z ##[debug]at , : line 22 2017-01-18T17:43:56.1597546Z ##[debug]at , : line 18 2017-01-18T17:43:56.1613921Z ##[debug]at , : line 1 2017-01-18T17:43:56.1644002Z ##[debug]Exception: 2017-01-18T17:43:56.2355650Z ##[debug]Microsoft.Rest.Azure.CloudException: Exception of type 'Microsoft.Rest.Azure.CloudException' was thrown. 2017-01-18T17:43:56.2375685Z ##[debug] at Microsoft.Azure.Graph.RBAC.ObjectsOperations.d__6.MoveNext() 2017-01-18T17:43:56.2395630Z ##[debug]--- End of stack trace from previous location where exception was thrown --- 2017-01-18T17:43:56.2415644Z ##[debug] at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 2017-01-18T17:43:56.2425679Z ##[debug] at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 2017-01-18T17:43:56.2445651Z ##[debug] at Microsoft.Azure.Graph.RBAC.ObjectsOperationsExtensions.d__3.MoveNext() 2017-01-18T17:43:56.2465644Z ##[debug]--- End of stack trace from previous location where exception was thrown --- 2017-01-18T17:43:56.2485657Z ##[debug] at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 2017-01-18T17:43:56.2495653Z ##[debug] at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 2017-01-18T17:43:56.2516192Z ##[debug] at Microsoft.Azure.Graph.RBAC.ObjectsOperationsExtensions.GetObjectsByObjectIds(IObjectsOperations operations, GetObjectsParameters parameters) 2017-01-18T17:43:56.2536390Z ##[debug] at Microsoft.Azure.Commands.Resources.Models.ActiveDirectory.ActiveDirectoryClient.GetObjectsByObjectId(List1 objectIds) 2017-01-18T17:43:56.2549680Z ##[debug] at Microsoft.Azure.Commands.Resources.Models.Authorization.AuthorizationClientExtensions.ToPSRoleAssignments(IEnumerable1 assignments, List`1 roleDefinitions, AuthorizationClient policyClient, ActiveDirectoryClient activeDirectoryClient, Boolean excludeAssignmentsForDeletedPrincipals) 2017-01-18T17:43:56.2569844Z ##[debug] at Microsoft.Azure.Commands.Resources.Models.Authorization.AuthorizationClient.FilterRoleAssignments(FilterRoleAssignmentsOptions options, String currentSubscription) 2017-01-18T17:43:56.2579843Z ##[debug] at Microsoft.Azure.Commands.Resources.GetAzureRoleAssignmentCommand.ExecuteCmdlet() 2017-01-18T17:43:56.2599842Z ##[debug] at Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.ProcessRecord() 2017-01-18T17:43:56.2959008Z ##[error]Exception of type 'Microsoft.Rest.Azure.CloudException' was thrown. 2017-01-18T17:43:56.2959008Z ##[debug]Processed: ##vso[task.logissue type=error]Exception of type 'Microsoft.Rest.Azure.CloudException' was thrown. 2017-01-18T17:43:56.2989006Z ##[debug]Processed: ##vso[task.complete result=Failed] 2017-01-18T17:43:56.3229004Z ##[section]Finishing: Azure PowerShell script: InlineScript

dmittal02 avatar Jan 19 '17 08:01 dmittal02

@dmittal02 Please include the text of the cmdlet invocation (what command line did you use) and a description of the expected result. Also, can you try your command with the latest version?

markcowl avatar Jan 19 '17 20:01 markcowl

To give you more context, we are using this command line in VSTS build Azure PowerShell build task. this is the command line, we use: Get-AzureRmRoleAssignment and this command line will be executed on hosted build agent. In fact for customer when he run it on the private build agent to which he can remote, he get the required data, but I am getting authorization error there as well. Please let me know if that helps. You can also reach out to me at: [email protected] to discuss in more detail.

dmittal02 avatar Jan 23 '17 18:01 dmittal02

@markcowl We get the same error when we try to call Get-AzureRmRoleAssignment in our VSTS release pipeline from an Azure PowerShell release task. This is running on a hosted agent with an Azure Resource Manager connection. The Azure PowerShell version on the hosted agent is 3.1.0.

efridman avatar Jan 25 '17 16:01 efridman

@markcowl Here is the script as with which we are able to reproduce it locally:

$psCredential = New-Object System.Management.Automation.PSCredential( "serviceprincipalID", (ConvertTo-SecureString "Password" -AsPlainText -Force)) Add-AzureRMAccount -ServicePrincipal -Tenant "" -Credential $psCredential
$additional = @{} $additional['TenantId'] = "" Select-AzureRMSubscription -SubscriptionId "" @additional

Get-AzureRmRoleAssignment -debug

You will get the issue. Service principal ID choosen is owner on the subscription.

dmittal02 avatar Jan 27 '17 11:01 dmittal02

What are the permissions in ActiveDirectory of your service principal? To use the role assignment cmdlets, you need access to the graph api, which is managed by permissions granted to your service principal in AD. To view role assignments, you will need at least read access to the tenant in AD.

markcowl avatar Jan 27 '17 18:01 markcowl

@markcowl It looks like missing AAD permissions were the cause of our issue. Thanks.

efridman avatar Feb 01 '17 11:02 efridman

@efridman @dmittal02 Can you guys verify that this was resolved?

markcowl avatar Feb 08 '17 17:02 markcowl

@markcowl Yes, our problem did turn out to be that the SPN did not have permissions on AAD. Thanks.

efridman avatar Feb 08 '17 18:02 efridman

@efridman We are running into the same problem. Can you elaborate on what permissions you gave the SPN? We have been trying things, but still no success

keeshuizer avatar Feb 14 '17 10:02 keeshuizer

@keeshuizer We actually did not check this all the way through, since we don't want to give the SPN permissions on AAD for other reasons. But permissions were missing and that's why we thought that was the issue. I would assume read permissions to read roles and write permissions to assign them, but I'm guessing you tried that?

efridman avatar Feb 15 '17 08:02 efridman

I have a repro of the issue with current cmdlets, it is definitely related to the whitelist for graph api-version used in the call to graph to get friendly names for the users and groups, and not the permissions on the service principal.

Here is a request/response showing the failure

DEBUG: ============================ HTTP REQUEST ============================

HTTP Method:
POST

Absolute Uri:
https://graph.windows.net/54826b22-38d6-4fb2-bad9-b7b93a3e9c5a/getObjectsByObjectIds?api-version=1.6-internal

Headers:
x-ms-client-request-id        : 83854599-8e85-4858-95c9-db7eae89ad68
accept-language               : en-US

Body:
{
  "objectIds": [
    -- redacted --
  ],
  "includeDirectoryObjectReferences": true
}

DEBUG: ============================ HTTP RESPONSE ============================

Status Code:
Forbidden

Headers:
ocp-aad-diagnostics-server-name: XYNBldSbijB5bKKmRhxq1vFYS8rdTdBj0OJLIY4iTms=
request-id                    : 6bd997bd-a7f4-4692-ae08-f19f5136157a
client-request-id             : 097a0dcb-d072-4a10-a230-cd4e198547e2
x-ms-dirapi-data-contract-version: 1.6-internal
ocp-aad-session-key           :
ynVvvPIP2mAgozVdI3czQ2GZgS8yxN9FKz-wyhh3yVii1HCy93ZYZagfW5owHDfYGypn_eXgD9i1O6Wq6utrZj-1dkEXjoPQqXlFrB3Q36sZA2DqTI56p_-
cPXRiM6pN.FJsGF_JL39ZHV7R7XgD0DkGidkT-KaokgM4QPdcOdE0
Strict-Transport-Security     : max-age=31536000; includeSubDomains
Access-Control-Allow-Origin   : *
Duration                      : 632811
Cache-Control                 : private
Server                        : Microsoft-IIS/8.5
X-AspNet-Version              : 4.0.30319
X-Powered-By                  : ASP.NET,ASP.NET
Date                          : Thu, 23 Feb 2017 01:32:14 GMT

Body:
{
  "odata.error": {
    "code": "Authentication_Unauthorized",
    "message": {
      "lang": "en",
      "value": "Access denied to the specified API version."
    }
  }
}

markcowl avatar Feb 23 '17 01:02 markcowl

cmdlet nor working for me, too. A simple Get-AzureRMRoleAssignment ist not working any more and I don't have any idea what changed... Here is the output, pls. help:

PS C:\PublicProjects\PowerShell> Login-AzureRmAccount


Environment           : AzureCloud
Account               : [email protected]
TenantId              : 8dff03-...-312cf7c
SubscriptionId        : abcde-730316a2e9e0
SubscriptionName      : Visual Studio Ultimate mit MSDN
CurrentStorageAccount :



PS C:\PublicProjects\PowerShell> Get-AzureRmRoleAssignment
Get-AzureRmRoleAssignment : Run Login-AzureRmAccount to login.
In Zeile:1 Zeichen:1
+ Get-AzureRmRoleAssignment
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-AzureRmRoleAssignment], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.Azure.Commands.Resources.GetAzureRoleAssignmentCommand

gitralf avatar Mar 01 '17 10:03 gitralf

@gitralf This looks unrelated to the issue at hand - the issue you are seeing is likely that you have incompatible versions of the cmdlets installed. Normally you can resolve this by:

(1) Installing the latest MSI -or- (2) Updating to the latest AzureRM and Azure modules from the PSGallery.

If you'd like to confirm this is the issue, run

Get-Module -ListAvailable

And send the output - most likely there will be some mismatched moduel versions.

markcowl avatar Mar 20 '17 07:03 markcowl

This issue is due to be resolved when the newest version of AAD Graph is released. This is due to heppen in the May release, so moving this to May

markcowl avatar Mar 20 '17 07:03 markcowl

@shuagarw We need the fix for azurestack in the stack-dev branch

bganapa avatar Apr 03 '17 23:04 bganapa

@shuagarw, any update on this?

twitchax avatar Apr 21 '17 23:04 twitchax

@rbacask for FYI @darshanhs90 as he is working on this fix in master branch. @afshins to port the fix in AzureStack once it is completed in master branch.

shuagarw avatar Apr 24 '17 18:04 shuagarw

This should be fixed. Please let us know if you see a recurrence

markcowl avatar May 10 '17 17:05 markcowl

appears to be working for me, thanks!

4c74356b41 avatar Jun 10 '17 12:06 4c74356b41

Same error when I use the command with my Automation Account (Run As Account created by default with my Automation account) Get-AzureRmRoleAssignment : Exception of type 'Microsoft.Rest.Azure.CloudException' was thrown. My modules have been updated, AzureRM.Resources : version 4.1.0

YoannGUILLO avatar Jun 13 '17 07:06 YoannGUILLO