2017-02-25 1 views
0

Sur mon abonnement MSDN Azure, connecté après l'exécution de Login-AzureRMAccount, je peux exécuter Get-AzureRmRoleAssignment sans problème.Exécuter Get-AzureRmRoleAssignment en tant que service principal dans Azure Powershell

J'ai créé une application et un service principal avec un rôle dans Azure avec Powershell (Nouveau-AzureRmADApplication, Nouvelle-AzureRmADServicePrincipal & Nouveau-AzureRmRoleAssignment) et après la connexion avec ces informations d'identification avec ce powershell:

$psCredential = New-Object System.Management.Automation.PSCredential("98349834-8494-4813-9282-4343434", (ConvertTo-SecureString "myPassword" -AsPlainText -Force)) 
Add-AzureRMAccount -ServicePrincipal -Tenant "123456-d5bb-44f8-a283-34534434" -Credential $psCredential 

la suivant peut être exécuté avec succès:

Get-AzureRmADApplication -IdentifierUri "http://SP.5656645-408c-4980-950e-898989" 

mais lors de l'exécution

Get-AzureRmRoleAssignment -debug 

Je reçois l'exception suivante:

Microsoft.Rest.Azure.CloudException: Accès refusé à la version de l'API spécifiée

On dirait que beaucoup de gens ont cette exception et je l'ai lu beaucoup de solutions comme donnant accès au portail d'azur, (mais rien ne semble fonctionner): également après avoir donné l'application des autorisations suivantes, l'exception est soulevée encore:

  • Windows Azure Active Directory 6 autorisations d'application - 6 autorisations délégués (pas en mesure de les sélectionner, uniquement lorsque requis Administrateur = Oui)
  • Microsoft Graph - 20 autorisations d'application - (également 20 autorisations délégués ne peuvent pas les sélectionner all)

Que dois-je faire pour laisser le service principal exécuter Get-AzureRmRoleAssignment?

modifier: L'application a le rôle Propriétaire. J'ai utilisé le script ps suivant pour créer l'application, le service principal et le rôle.

param 
(
    [Parameter(Mandatory=$true, HelpMessage="Enter Azure Subscription name. You need to be Subscription Admin to execute the script")] 
    [string] $subscriptionName, 

    [Parameter(Mandatory=$true, HelpMessage="Provide a password for SPN application that you would create")] 
    [string] $password,  
Mandatory=$false, HelpMessage="Provide a SPN role assignment")] 
    [string] $spnRole = "owner" 
) 

#Initialize 
$ErrorActionPreference = "Stop" 
$VerbosePreference = "SilentlyContinue" 
$userName = $env:USERNAME 
$newguid = [guid]::NewGuid() 
$displayName = [String]::Format("SP.{0}.{1}", $resourceGroupName, $newguid) 
$homePage = "http://" + $displayName 
$identifierUri = $homePage 


#Initialize subscription 
$isAzureModulePresent = Get-Module -Name AzureRM* -ListAvailable 
if ([String]::IsNullOrEmpty($isAzureModulePresent) -eq $true) 
{ 
    Write-Output "Script requires AzureRM modules to be present. Obtain AzureRM from https://github.com/Azure/azure-powershell/releases. Please refer https://github.com/Microsoft/vsts-tasks/blob/master/Tasks/DeployAzureResourceGroup/README.md for recommended AzureRM versions." -Verbose 
    return 
} 

Import-Module -Name AzureRM.Profile 
Write-Output "Provide your credentials to access Azure subscription $subscriptionName" -Verbose 
Login-AzureRmAccount -SubscriptionName $subscriptionName 
$azureSubscription = Get-AzureRmSubscription -SubscriptionName $subscriptionName 
$connectionName = $azureSubscription.SubscriptionName 
$tenantId = $azureSubscription.TenantId 
$id = $azureSubscription.SubscriptionId 


#Create a new AD Application 
Write-Output "Creating a new Application in AAD (App URI - $identifierUri)" -Verbose 
$azureAdApplication = New-AzureRmADApplication -DisplayName $displayName -HomePage $homePage -IdentifierUris $identifierUri -Password $password -Verbose 
$appId = $azureAdApplication.ApplicationId 
Write-Output "Azure AAD Application creation completed successfully (Application Id: $appId)" -Verbose 


#Create new SPN 
Write-Output "Creating a new SPN" -Verbose 
$spn = New-AzureRmADServicePrincipal -ApplicationId $appId 
$spnName = $spn.ServicePrincipalNames 
Write-Output "SPN creation completed successfully (SPN Name: $spnName)" -Verbose 


#Assign role to SPN 
Write-Output "Waiting for SPN creation to reflect in Directory before Role assignment" 
Start-Sleep 20 
Write-Output "Assigning role ($spnRole) to SPN App ($appId)" -Verbose 
New-AzureRmRoleAssignment -RoleDefinitionName $spnRole -ServicePrincipalName $appId 
Write-Output "SPN role assignment completed successfully" -Verbose 


#Print the values 
Write-Output "`nCopy and Paste below values for Service Connection" -Verbose 
Write-Output "***************************************************************************" 
Write-Output "Connection Name: $connectionName(SPN)" 
Write-Output "Subscription Id: $id" 
Write-Output "Subscription Name: $connectionName" 
Write-Output "Service Principal Id: $appId" 
Write-Output "Service Principal key: <Password that you typed in>" 
Write-Output "Tenant Id: $tenantId" 
Write-Output "***************************************************************************" 
+0

Quelle version d'Azure PowerShell utilisez-vous? Je suis en mesure d'exécuter Get-AzureRmRoleAssignment -debug sans problème – juvchan

+0

Version 5.1.14393.693. La version AzureRM est 3.6 –

+0

Je l'ai essayé sur plusieurs PC de développement sans succès. Êtes-vous capable d'exécuter Get-AzureRmRoleAssignment -debug après vous être connecté en tant que Service Principal? (comme le powershell ci-dessus?) –

Répondre

0

Pour ce faire, vous devez attribuer l'autorisation de le faire;) L'autorisation est appelée Microsoft.Authorization/roleAssignments/read, vous pouvez obtenir une liste des autorisations avec Get-AzureRmProviderOperation Microsoft.Authorization/*, pour attribuer des autorisations dont vous avez besoin pour créer une définition de rôle personnalisé et Attribuez-le ou utilisez l'un des rôles intégrés.

https://docs.microsoft.com/en-us/azure/active-directory/role-based-access-control-custom-roles

Je peux confirmer que cela ne se produit que de Powershell (la chose la plus drôle est - Get-AzureRmRoleDefinition œuvres, alors que Get-AzureRmRoleAssignment ne fonctionne pas), le code de python peut liste des attributions de rôle lorsque vous utilisez le même service principal

from azure.common.credentials import ServicePrincipalCredentials 
from azure.mgmt.authorization import AuthorizationManagementClient 
tenant_id = 'tenant_guid' 
application_id = 'application_guid' 
application_secret = 'application_secret' 
cred = ServicePrincipalCredentials(client_id=application_id, secret=application_secret, tenant=tenant_id) 
client = AuthorizationManagementClient(cred, 'subscription_guid') 
roles = client.role_assignments.list() 
for role in roles: 
    print(role) 
+0

Le maître de service est propriétaire.Lors de l'exécution de New-AzureRmRoleAssignment après la création du service principal, j'ai passé les paramètres suivants: New-AzureRmRoleAssignment -RoleDefinitionName "Owner" -ServicePrincipalName $ appId. –

+0

bien, désolé de basculer votre bateau, mais le script que vous avez montré est clairement attribuer des autorisations de propriétaire au groupe de ressources, pas d'abonnement – 4c74356b41

+0

Je suis désolé, votre correct. Avant d'exécuter, j'ai supprimé la partie groupe de ressources. Je vais modifier mon message pour le rendre clair. –