Skip to content

Commit 574e505

Browse files
committed
Fix sign job, add tests for service principal creation with role or scope
1 parent 0acb1cf commit 574e505

File tree

7 files changed

+5394
-24
lines changed

7 files changed

+5394
-24
lines changed

src/ResourceManager/Resources/ChangeLog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* Revert change to `New-AzureRmADServicePrincipal` that gave service principals "Contributor" permissions over the current subscription if no values were provided for the `Role` or `Scope` parameters
2222
- If no values are provided for `Role` or `Scope`, the service principal is created with no permissions
2323
- If a `Role` is provided, but no `Scope`, the service principal is created with the specified `Role` permissions over the current subscription
24-
- If a `Scope` is provided, but no `Contributor`, the service principal is created with `Contributor` permissions over the specified `Scope`
24+
- If a `Scope` is provided, but no `Scope`, the service principal is created with `Contributor` permissions over the specified `Scope`
2525
- If both `Role` and `Scope` are provided, the service principal is created with the specified `Role` permissions over the specified `Scope`
2626

2727
## Version 6.0.0

src/ResourceManager/Resources/Commands.Resources.Test/Commands.Resources.Test.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,15 @@
356356
<None Include="SessionRecords\Microsoft.Azure.Commands.Resources.Test.ScenarioTests.ActiveDirectoryTests\TestGetADUserWithMail.json">
357357
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
358358
</None>
359+
<None Include="SessionRecords\Microsoft.Azure.Commands.Resources.Test.ScenarioTests.ActiveDirectoryTests\TestNewADServicePrincipalWithCustomScope.json">
360+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
361+
</None>
359362
<None Include="SessionRecords\Microsoft.Azure.Commands.Resources.Test.ScenarioTests.ActiveDirectoryTests\TestNewADServicePrincipalWithoutApp.json">
360363
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
361364
</None>
365+
<None Include="SessionRecords\Microsoft.Azure.Commands.Resources.Test.ScenarioTests.ActiveDirectoryTests\TestNewADServicePrincipalWithReaderRole.json">
366+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
367+
</None>
362368
<None Include="SessionRecords\Microsoft.Azure.Commands.Resources.Test.ScenarioTests.DeploymentTests\TestCrossResourceGroupDeploymentFromTemplateFile.json">
363369
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
364370
</None>

src/ResourceManager/Resources/Commands.Resources.Test/ScenarioTests/ActiveDirectoryTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,20 @@ public void TestNewADServicePrincipalWithoutApp()
606606
ResourcesController.NewInstance.RunPsTest("Test-NewADServicePrincipalWithoutApp");
607607
}
608608

609+
[Fact]
610+
[Trait(Category.AcceptanceType, Category.CheckIn)]
611+
public void TestNewADServicePrincipalWithReaderRole()
612+
{
613+
ResourcesController.NewInstance.RunPsTest("Test-NewADServicePrincipalWithReaderRole");
614+
}
615+
616+
[Fact]
617+
[Trait(Category.AcceptanceType, Category.CheckIn)]
618+
public void TestNewADServicePrincipalWithCustomScope()
619+
{
620+
ResourcesController.NewInstance.RunPsTest("Test-NewADServicePrincipalWithCustomScope");
621+
}
622+
609623
[Fact(Skip = "Not working in playback.")]
610624
[Trait(Category.AcceptanceType, Category.CheckIn)]
611625
public void TestCreateDeleteAppPasswordCredentials()

src/ResourceManager/Resources/Commands.Resources.Test/ScenarioTests/ActiveDirectoryTests.ps1

Lines changed: 89 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ function Test-GetAllADGroups
3333
.SYNOPSIS
3434
Tests getting Active Directory groups.
3535
#>
36-
function Test-GetADGroupWithSearchString
36+
function Test-GetADGroupWithSearchString
3737
{
3838
param([string]$displayName)
39-
39+
4040
# Test
4141
# Select at most 10 groups. Groups are restricted to contain "test" to fasten the test
4242
$groups = Get-AzureRmADGroup -SearchString $displayName
@@ -68,7 +68,7 @@ Tests getting Active Directory groups.
6868
function Test-GetADGroupWithObjectId
6969
{
7070
param([string]$objectId)
71-
71+
7272
# Test
7373
$groups = Get-AzureRmADGroup -ObjectId $objectId
7474

@@ -85,7 +85,7 @@ Tests getting Active Directory group with security enabled .
8585
function Test-GetADGroupSecurityEnabled
8686
{
8787
param([string]$objectId, [string]$securityEnabled)
88-
88+
8989
# Test
9090
$groups = Get-AzureRmADGroup -ObjectId $objectId
9191

@@ -134,8 +134,8 @@ function Test-GetADGroupMemberWithGroupObjectId
134134

135135
# Test
136136
$members = Get-AzureRmADGroupMember -GroupObjectId $groupObjectId
137-
138-
# Assert
137+
138+
# Assert
139139
Assert-AreEqual $members.Count 1
140140
Assert-AreEqual $members[0].Id $userObjectId
141141
Assert-AreEqual $members[0].DisplayName $userName
@@ -148,7 +148,7 @@ Tests getting members from an Active Directory group.
148148
function Test-GetADGroupMemberWithBadGroupObjectId
149149
{
150150
# Test
151-
Assert-Throws { Get-AzureRmADGroupMember -GroupObjectId "baadc0de-baad-c0de-baad-c0debaadc0de" }
151+
Assert-Throws { Get-AzureRmADGroupMember -GroupObjectId "baadc0de-baad-c0de-baad-c0debaadc0de" }
152152
}
153153

154154
<#
@@ -160,7 +160,7 @@ function Test-GetADGroupMemberWithUserObjectId
160160
param([string]$objectId)
161161

162162
# Test
163-
Assert-Throws { Get-AzureRmADGroupMember -GroupObjectId $objectId }
163+
Assert-Throws { Get-AzureRmADGroupMember -GroupObjectId $objectId }
164164
}
165165

166166
<#
@@ -173,8 +173,8 @@ function Test-GetADGroupMemberFromEmptyGroup
173173

174174
# Test
175175
$members = Get-AzureRmADGroupMember -GroupObjectId $objectId
176-
177-
# Assert
176+
177+
# Assert
178178
Assert-Null($members)
179179
}
180180

@@ -462,7 +462,7 @@ function Test-NewADApplication
462462

463463
# Assert
464464
Assert-NotNull $application
465-
$apps = Get-AzureRmADApplication
465+
$apps = Get-AzureRmADApplication
466466
Assert-NotNull $apps
467467
Assert-True { $apps.Count -ge 0 }
468468

@@ -489,13 +489,13 @@ function Test-NewADApplication
489489
$newDisplayName = getAssetName
490490
$newHomePage = "http://" + $newDisplayName + ".com"
491491
$newIdentifierUri = "http://" + $newDisplayName
492-
492+
493493
# Update displayName and HomePage
494494
Set-AzureRmADApplication -ObjectId $application.ObjectId -DisplayName $newDisplayName -HomePage $newHomePage
495495

496-
# Update identifierUri
496+
# Update identifierUri
497497
Set-AzureRmADApplication -ApplicationId $application.ApplicationId -IdentifierUris $newIdentifierUri
498-
498+
499499
# Get application and verify updated properties
500500
$app1 = Get-AzureRmADApplication -ObjectId $application.ObjectId
501501
Assert-NotNull $app1
@@ -504,7 +504,7 @@ function Test-NewADApplication
504504
Assert-AreEqual $app1.HomePage $newHomePage
505505
Assert-AreEqual $app1.IdentifierUris[0] $newIdentifierUri
506506

507-
# Delete
507+
# Delete
508508
Remove-AzureRmADApplication -ObjectId $application.ObjectId -Force
509509
}
510510

@@ -543,7 +543,7 @@ function Test-NewADServicePrincipal
543543
Tests Creating and deleting service principal without an exisitng application.
544544
#>
545545
function Test-NewADServicePrincipalWithoutApp
546-
{
546+
{
547547
# Setup
548548
$displayName = getAssetName
549549

@@ -573,7 +573,7 @@ function Test-NewADServicePrincipalWithoutApp
573573

574574
# update SP displayName
575575
$newDisplayName = getAssetName
576-
576+
577577
Set-AzureRmADServicePrincipal -ObjectId $servicePrincipal.Id -DisplayName $newDisplayName
578578

579579
# Get SP and verify updated name
@@ -588,12 +588,78 @@ function Test-NewADServicePrincipalWithoutApp
588588
Assert-Throws { Remove-AzureRmADServicePrincipal -ObjectId $servicePrincipal.Id -Force}
589589
}
590590

591+
<#
592+
.SYNOPSIS
593+
Tests creating a service principal with reader permissions
594+
#>
595+
function Test-NewADServicePrincipalWithReaderRole
596+
{
597+
# Setup
598+
$displayName = getAssetName
599+
$roleDefinitionName = "Reader"
600+
601+
# Test
602+
$servicePrincipal = New-AzureRmADServicePrincipal -DisplayName $displayName -Role $roleDefinitionName
603+
Assert-NotNull $servicePrincipal
604+
Assert-AreEqual $servicePrincipal.DisplayName $displayName
605+
606+
try
607+
{
608+
$role = Get-AzureRmRoleAssignment -ObjectId $servicePrincipal.Id
609+
Assert-AreEqual $role.Count 1
610+
Assert-AreEqual $role.DisplayName $servicePrincipal.DisplayName
611+
Assert-AreEqual $role.ObjectId $servicePrincipal.Id
612+
Assert-AreEqual $role.RoleDefinitionName $roleDefinitionName
613+
Assert-AreEqual $role.ObjectType "ServicePrincipal"
614+
}
615+
finally
616+
{
617+
Remove-AzureRmADApplication -ApplicationId $servicePrincipal.ApplicationId -Force
618+
Remove-AzureRmRoleAssignment -ObjectId $servicePrincipal.Id -RoleDefinitionName $roleDefinitionName
619+
}
620+
}
621+
622+
<#
623+
.SYNOPSIS
624+
Tests creating a service principal with permissions over a custom scope
625+
#>
626+
function Test-NewADServicePrincipalWithCustomScope
627+
{
628+
# Setup
629+
$displayName = getAssetName
630+
$defaultRoleDefinitionName = "Contributor"
631+
$subscription = Get-AzureRmSubscription | Select -Last 1 -Wait
632+
$resourceGroup = Get-AzureRmResourceGroup | Select -Last 1 -Wait
633+
$scope = "/subscriptions/" + $subscription.Id + "/resourceGroups/" + $resourceGroup.ResourceGroupName
634+
635+
# Test
636+
$servicePrincipal = New-AzureRmADServicePrincipal -DisplayName $displayName -Scope $scope
637+
Assert-NotNull $servicePrincipal
638+
Assert-AreEqual $servicePrincipal.DisplayName $displayName
639+
640+
try
641+
{
642+
$role = Get-AzureRmRoleAssignment -ObjectId $servicePrincipal.Id
643+
Assert-AreEqual $role.Count 1
644+
Assert-AreEqual $role.DisplayName $servicePrincipal.DisplayName
645+
Assert-AreEqual $role.ObjectId $servicePrincipal.Id
646+
Assert-AreEqual $role.RoleDefinitionName $defaultRoleDefinitionName
647+
Assert-AreEqual $role.Scope $scope
648+
Assert-AreEqual $role.ObjectType "ServicePrincipal"
649+
}
650+
finally
651+
{
652+
Remove-AzureRmADApplication -ApplicationId $servicePrincipal.ApplicationId -Force
653+
Remove-AzureRmRoleAssignment -ObjectId $servicePrincipal.Id -Scope $scope -RoleDefinitionName $defaultRoleDefinitionName
654+
}
655+
}
656+
591657
<#
592658
.SYNOPSIS
593659
Tests Creating and deleting application using Password Credentials.
594660
#>
595661
function Test-CreateDeleteAppPasswordCredentials
596-
{
662+
{
597663
# Setup
598664
$displayName = getAssetName
599665
$identifierUri = "http://" + $displayName
@@ -629,7 +695,7 @@ function Test-CreateDeleteAppPasswordCredentials
629695

630696
# Remove cred by KeyId
631697
Remove-AzureRmADAppCredential -ApplicationId $application.ApplicationId -KeyId $cred.KeyId -Force
632-
$cred3 = Get-AzureRmADAppCredential -ApplicationId $application.ApplicationId
698+
$cred3 = Get-AzureRmADAppCredential -ApplicationId $application.ApplicationId
633699
Assert-NotNull $cred3
634700
Assert-AreEqual $cred3.Count 1
635701
Assert-AreEqual $cred3[0].KeyId $cred1.KeyId
@@ -642,7 +708,7 @@ function Test-CreateDeleteAppPasswordCredentials
642708
$newApplication = Get-AzureRmADApplication -DisplayNameStartWith "PowershellTestingApp"
643709
Assert-Throws { New-AzureRmADAppCredential -ApplicationId $newApplication.ApplicationId -Password "Somedummypwd"}
644710

645-
# Remove App
711+
# Remove App
646712
Remove-AzureRmADApplication -ObjectId $application.ObjectId -Force
647713
}
648714

@@ -652,7 +718,7 @@ function Test-CreateDeleteAppPasswordCredentials
652718
Tests Creating and deleting application using Service Principal Credentials.
653719
#>
654720
function Test-CreateDeleteSpPasswordCredentials
655-
{
721+
{
656722
# Setup
657723
$displayName = getAssetName
658724
$password = getAssetName
@@ -689,7 +755,7 @@ function Test-CreateDeleteSpPasswordCredentials
689755

690756
# Remove cred by KeyId
691757
Remove-AzureRmADSpCredential -ServicePrincipalName $servicePrincipal.ServicePrincipalNames[0] -KeyId $cred.KeyId -Force
692-
$cred3 = Get-AzureRmADSpCredential -ServicePrincipalName $servicePrincipal.ServicePrincipalNames[0]
758+
$cred3 = Get-AzureRmADSpCredential -ServicePrincipalName $servicePrincipal.ServicePrincipalNames[0]
693759
Assert-NotNull $cred3
694760
Assert-AreEqual $cred3.Count 1
695761
Assert-AreEqual $cred3[0].KeyId $cred1.KeyId
@@ -701,7 +767,7 @@ function Test-CreateDeleteSpPasswordCredentials
701767
}
702768
Finally
703769
{
704-
# Remove App
770+
# Remove App
705771
$app = Get-AzureRmADApplication -ApplicationId $servicePrincipal.ApplicationId
706772
Remove-AzureRmADApplication -ObjectId $app.ObjectId -Force
707773
}

0 commit comments

Comments
 (0)