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

New-AzureRmRoleAssignment problem with Azure Service Principal

Open yassirkachri opened this issue 8 years ago • 15 comments

Hi I'm using Powershell to Assign Roles to users. When I use Login-AzureRmAccount all goes well but when I try with Connect-AzureRmAccount -servicePrincipal I got the error

The servic Prinicipal is Owner of the subscription and have all permissions on Graph API

Thanks for help

 
New-AzureRmRoleAssignment : The provided information does not map to an AD object id.
At C:\Users\yassir\Documents\CloudToolBox\scripts\set-RolesFromJson.ps1:44 char:5
+     New-AzureRmRoleAssignment  -SignInName "xxxxxxx. ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [New-AzureRmRoleAssignment], KeyNotFoundException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Resources.NewAzureRoleAssignmentCommand

----------

Powershell

$AzureRMAccount = Connect-AzureRmAccount -Credential $credentials -ServicePrincipal -TenantId $tenant_id

if ($AzureRMAccount){     
    $subscription = $params.subscriptionId
    Set-AzureRmContext  -SubscriptionId $subscription  -TenantId $tenant_id
    
    New-AzureRmRoleAssignment  -SignInName "AUSER"  -RoleDefinitionName "Contributor"  -scope "/subscriptions/xxxxx/resourceGroups/xxxxx"
}

yassirkachri avatar Apr 10 '18 15:04 yassirkachri

@yassirkachri Can you turn on $DebugPreference="Continue" before runnign this script 0- this will give us the requests and responses with the server and help to diagnose what the issue might be. The error message indicates that the SignInName was not found, but the server response messages will tell us for sure.

markcowl avatar Apr 10 '18 19:04 markcowl

After $DebugPreference="Continue" I have more information and it seems that the problem is here 👍 `

DEBUG: Microsoft.IdentityModel.Clients.ActiveDirectory Information: 2 :
DEBUG: 04/12/2018 07:22:30:  - TokenCache: Serializing token cache with 2 items.
DEBUG: ============================ HTTP REQUEST ============================

HTTP Method:
GET

Absolute Uri:
https://graph.windows.net/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/users?$filter=userPrincipalName eq 'AUSER'&api-version=1.6

Headers:
x-ms-client-request-id        : XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
accept-language               : en-US

Body:


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

Status Code:
Forbidden

Headers:
Pragma                        : no-cache
ocp-aad-diagnostics-server-name: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
request-id                    : XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
client-request-id             :  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
x-ms-dirapi-data-contract-version: 1.6
ocp-aad-session-key           : XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
X-Content-Type-Options        : nosniff
DataServiceVersion            : 3.0;
Strict-Transport-Security     : max-age=31536000; includeSubDomains
Access-Control-Allow-Origin   : *
Duration                      : 584292
Cache-Control                 : no-cache
Date                          : Thu, 12 Apr 2018 07:22:30 GMT
Server                        : Microsoft-IIS/10.0
X-AspNet-Version              : 4.0.30319
X-Powered-By                  : ASP.NET

Body:
{
  "odata.error": {
    "code": "Authorization_RequestDenied",
    "message": {
      "lang": "en",
      "value": "Insufficient privileges to complete the operation."
    }
  }
}

New-AzureRmRoleAssignment : The provided information does not map to an AD object id.
At C:\Users\yassir\Documents\CloudToolBox\scripts\set-RolesFromJson.ps1:44 char:5
+     New-AzureRmRoleAssignment  -SignInName "AUSER. ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [New-AzureRmRoleAssignment], KeyNotFoundException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Resources.NewAzureRoleAssignmentCommand

DEBUG: AzureQoSEvent: CommandName - New-AzureRmRoleAssignment; IsSuccess - False; Duration - 00:00:00.3611205; Exception - System.Collections.Generic.KeyNotFoundException: The provided information does not map to an AD object
id.
   at Microsoft.Azure.Graph.RBAC.Version1_6.ActiveDirectory.ActiveDirectoryClient.GetObjectId(ADObjectFilterOptions options)
   at Microsoft.Azure.Commands.Resources.Models.Authorization.AuthorizationClient.CreateRoleAssignment(FilterRoleAssignmentsOptions parameters)
   at Microsoft.Azure.Commands.Resources.NewAzureRoleAssignmentCommand.ExecuteCmdlet()
   at Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.ProcessRecord();
DEBUG: Finish sending metric.
DEBUG: 9:22:31 AM - NewAzureRoleAssignmentCommand end processing.
DEBUG: 9:22:31 AM - NewAzureRoleAssignmentCommand end processing.
----------

yassirkachri avatar Apr 12 '18 08:04 yassirkachri

@yassirkachri does the service principal you are signing in with have the appropriate permissions to assign roles in the given scope? Based on the returned message (Insufficient privileges to complete the operation), it would seem that the service principal itself doesn't have the correct permissions to assign out roles.

CC: @darshanhs90

cormacpayne avatar Apr 13 '18 00:04 cormacpayne

the Service Principal is Owner of the subscription and have all permissions image

yassirkachri avatar Apr 13 '18 08:04 yassirkachri

@yassirkachri What are the permissions the service prinpal has under graph for the associated tenant? The 'application permissions' will need to include the ability to list directory contents and view user data.

markcowl avatar Apr 16 '18 17:04 markcowl

@markcowl can you give us the exact permissions needed please ?

YoannGUILLO avatar Apr 29 '18 22:04 YoannGUILLO

@YoannGUILLO You would need to have Read and rite directory data permissions within the tenant

markcowl avatar May 08 '18 23:05 markcowl

@markcowl where can I see the service principal permissions under graph ?

yassirkachri avatar May 09 '18 06:05 yassirkachri

These are 'AzureAD Graph' permissions, not Microsoft.Graph permissions. They should be listed as 'Application Permissions' under 'Windows Azure Active Directory '

markcowl avatar May 09 '18 19:05 markcowl

Still the same error that @yassirkachri, I have a service principal used as a run as account to execute my runbooks on Automation, my runbook execute the command New-AzureRmRoleAssignment to apply a specific user as reader on a resource group. My service principal is owner of my subscription and permissions below has been applied : image When the runbook is executed this error appear : The provided information does not map to an AD object id. at Microsoft.Azure.Commands.Resources.Models.ActiveDirectory.ActiveDirectoryClient.GetObjectId(ADObjectFilterOptions options) at Microsoft.Azure.Commands.Resources.Models.Authorization.AuthorizationClient.CreateRoleAssignment(FilterRoleAssignmentsOptions parameters) at Microsoft.Azure.Commands.Resources.NewAzureRoleAssignmentCommand.ExecuteCmdlet() at Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.ProcessRecord()

YoannGUILLO avatar May 15 '18 14:05 YoannGUILLO

@cormacpayne Can you take a look at this issue?

maddieclayton avatar May 22 '18 23:05 maddieclayton

I am also running into this same issue. When running locally the command works fine, but when I connect to Azure using the following:

$ServicePrincipalConnection = Get-AutomationConnection -Name "AzureRunAsConnection" Add-AzureRmAccount
-ServicePrincipal -TenantId $ServicePrincipalConnection.TenantId -ApplicationId $ServicePrincipalConnection.ApplicationId -CertificateThumbprint $ServicePrincipalConnection.CertificateThumbprint | Write-Verbose

I receive the New-AzureRmRoleAssignment : The provided information does not map to an AD object id. error.

jeffgebhardt avatar May 24 '18 19:05 jeffgebhardt

Error resolved, I updated the modules under my Azure Automation Account.

For information it works with just "Read directory data"

YoannGUILLO avatar May 25 '18 07:05 YoannGUILLO

@YoannGUILLO - Can you post an image of your modules and their versions?

jeffgebhardt avatar May 25 '18 17:05 jeffgebhardt

Closing as resolved.

vladimir-shcherbakov avatar Jun 19 '18 22:06 vladimir-shcherbakov