@@ -658,21 +658,25 @@ function Test-NewADServicePrincipalWithCustomScope
658
658
659
659
<#
660
660
. SYNOPSIS
661
- Tests Creating and deleting application using Password Credentials.
661
+ Tests Creating and deleting application using App Credentials.
662
662
#>
663
- function Test-CreateDeleteAppPasswordCredentials
663
+ function Test-CreateDeleteAppCredentials
664
664
{
665
665
# Setup
666
- $displayName = getAssetName
666
+ $getAssetName = ConvertTo-SecureString " test" - AsPlainText - Force
667
+ $displayName = " test"
667
668
$identifierUri = " http://" + $displayName
668
- $password = getAssetName
669
+ $password = $getAssetName
670
+ $keyId1 = " 316af45c-83ff-42a5-a1d1-8fe9b2de3ac1"
671
+ $keyId2 = " 9b7fda23-cb39-4504-8aa6-3570c4239620"
672
+ $keyId3 = " 4141b479-4ca0-4919-8451-7e155de6aa0f"
669
673
670
674
# Test - Add application with a password cred
671
675
$application = New-AzADApplication - DisplayName $displayName - IdentifierUris $identifierUri - Password $password
672
676
673
677
# Assert
674
678
Assert-NotNull $application
675
-
679
+ Try {
676
680
# Get Application by ObjectId
677
681
$app1 = Get-AzADApplication - ObjectId $application.ObjectId
678
682
Assert-NotNull $app1
@@ -685,7 +689,7 @@ function Test-CreateDeleteAppPasswordCredentials
685
689
# Add 1 more password credential to the same app
686
690
$start = (Get-Date ).ToUniversalTime()
687
691
$end = $start.AddYears (1 )
688
- $cred = New-AzADAppCredential - ObjectId $application.ObjectId - Password $password - StartDate $start - EndDate $end
692
+ $cred = New-AzADAppCredentialWithId - ObjectId $application.ObjectId - Password $password - StartDate $start - EndDate $end - KeyId $keyId1
689
693
Assert-NotNull $cred
690
694
691
695
# Get credential should fetch 2 credentials
@@ -694,39 +698,83 @@ function Test-CreateDeleteAppPasswordCredentials
694
698
Assert-AreEqual $cred2.Count 2
695
699
$credCount = $cred2 | where {$_.KeyId -in $cred1.KeyId , $cred.KeyId }
696
700
Assert-AreEqual $credCount.Count 2
701
+ $cred2 = $cred
702
+
703
+ # Add 1 key credential to the same app
704
+ $certPath = Join-Path $ResourcesPath " certificate.pfx"
705
+ $cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($certPath )
706
+
707
+ $binCert = $cert.GetRawCertData ()
708
+ $credValue = [System.Convert ]::ToBase64String($binCert )
709
+ $start = (Get-Date ).ToUniversalTime()
710
+ $end = $start.AddDays (1 )
711
+ $cred = New-AzADAppCredentialWithId - ObjectId $application.ObjectId - CertValue $credValue - StartDate $start - EndDate $end - KeyId $keyId2
712
+ Assert-NotNull $cred
713
+
714
+ # Get credential should fetch 3 credentials
715
+ $cred3 = Get-AzADAppCredential - ObjectId $application.ObjectId
716
+ Assert-NotNull $cred3
717
+ Assert-AreEqual $cred3.Count 3
718
+ $credCount = $cred3 | where {$_.KeyId -in $cred1.KeyId , $cred2.KeyId , $cred.KeyId }
719
+ Assert-AreEqual $credCount.Count 3
720
+ $cred3 = $cred
721
+
722
+ # Add 1 more key credential to the same app
723
+ $binCert = $cert.GetRawCertData ()
724
+ $credValue = [System.Convert ]::ToBase64String($binCert )
725
+ $start = (Get-Date ).ToUniversalTime()
726
+ $end = $start.AddDays (1 )
727
+ $cred = New-AzADAppCredentialWithId - ObjectId $application.ObjectId - CertValue $credValue - StartDate $start - EndDate $end - KeyId $keyId3
728
+ Assert-NotNull $cred
729
+
730
+ # Get credential should fetch 4 credentials
731
+ $cred4 = Get-AzADAppCredential - ObjectId $application.ObjectId
732
+ Assert-NotNull $cred4
733
+ Assert-AreEqual $cred4.Count 4
734
+ $credCount = $cred4 | where {$_.KeyId -in $cred1.KeyId , $cred2.KeyId , $cred3.KeyId , $cred.KeyId }
735
+ Assert-AreEqual $credCount.Count 4
697
736
698
737
# Remove cred by KeyId
699
738
Remove-AzADAppCredential - ApplicationId $application.ApplicationId - KeyId $cred.KeyId - Force
700
- $cred3 = Get-AzADAppCredential - ApplicationId $application.ApplicationId
701
- Assert-NotNull $cred3
702
- Assert-AreEqual $cred3 .Count 1
703
- Assert-AreEqual $cred3 [ 0 ].KeyId $cred1.KeyId
739
+ $cred5 = Get-AzADAppCredential - ApplicationId $application.ApplicationId
740
+ Assert-NotNull $cred5
741
+ Assert-AreEqual $cred5 .Count 3
742
+ Assert-AreEqual $cred5 [ 2 ].KeyId $cred1.KeyId
704
743
705
744
# Remove All creds
706
- Remove-AzADAppCredential - ObjectId $application.ObjectId - All - Force
707
- $cred3 = Get-AzADAppCredential - ObjectId $application.ObjectId
708
- Assert-Null $cred3
709
-
745
+ Remove-AzADAppCredential - ObjectId $application.ObjectId - Force
746
+ $cred5 = Get-AzADAppCredential - ObjectId $application.ObjectId
747
+ Assert-Null $cred5
748
+
710
749
$newApplication = Get-AzADApplication - DisplayNameStartWith " PowershellTestingApp"
711
750
Assert-Throws { New-AzADAppCredential - ApplicationId $newApplication.ApplicationId - Password " Somedummypwd" }
712
-
713
- # Remove App
714
- Remove-AzADApplication - ObjectId $application.ObjectId - Force
751
+ }
752
+ Finally {
753
+ # Remove App
754
+ Remove-AzADApplication - ObjectId $application.ObjectId - Force
755
+ }
715
756
}
716
757
717
758
718
759
<#
719
760
. SYNOPSIS
720
761
Tests Creating and deleting application using Service Principal Credentials.
721
762
#>
722
- function Test-CreateDeleteSpPasswordCredentials
763
+ function Test-CreateDeleteSpCredentials
723
764
{
765
+ param ([string ]$applicationId )
766
+
724
767
# Setup
725
- $displayName = getAssetName
726
- $password = getAssetName
768
+ $getAssetName = ConvertTo-SecureString " test" - AsPlainText - Force
769
+ $displayName = " test"
770
+ $identifierUri = " http://" + $displayName
771
+ $password = $getAssetName
772
+ $keyId1 = " 316af45c-83ff-42a5-a1d1-8fe9b2de3ac1"
773
+ $keyId2 = " 9b7fda23-cb39-4504-8aa6-3570c4239620"
774
+ $keyId3 = " 4141b479-4ca0-4919-8451-7e155de6aa0f"
727
775
728
- # Test - Add SP with a password cred
729
- $servicePrincipal = New-AzADServicePrincipal - DisplayName $displayName - Password $password
776
+ # Test - Add SP
777
+ $servicePrincipal = New-AzADServicePrincipal - DisplayName $displayName - ApplicationId $applicationId
730
778
731
779
# Assert
732
780
Assert-NotNull $servicePrincipal
@@ -742,10 +790,10 @@ function Test-CreateDeleteSpPasswordCredentials
742
790
Assert-NotNull $cred1
743
791
Assert-AreEqual $cred1.Count 1
744
792
745
- # Add 1 more passowrd credential to the same app
793
+ # Add 1 more password credential to the same app
746
794
$start = (Get-Date ).ToUniversalTime()
747
795
$end = $start.AddYears (1 )
748
- $cred = New-AzADSpCredential - ObjectId $servicePrincipal.Id - Password $password - StartDate $start - EndDate $end
796
+ $cred = New-AzADSpCredentialWithId - ObjectId $servicePrincipal.Id - StartDate $start - EndDate $end - KeyId $keyId1
749
797
Assert-NotNull $cred
750
798
751
799
# Get credential should fetch 2 credentials
@@ -754,23 +802,58 @@ function Test-CreateDeleteSpPasswordCredentials
754
802
Assert-AreEqual $cred2.Count 2
755
803
$credCount = $cred2 | where {$_.KeyId -in $cred1.KeyId , $cred.KeyId }
756
804
Assert-AreEqual $credCount.Count 2
805
+ $cred2 = $cred
806
+
807
+ # Add 1 key credential to the same app
808
+ $certPath = Join-Path $ResourcesPath " certificate.pfx"
809
+ $cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($certPath )
810
+
811
+ $binCert = $cert.GetRawCertData ()
812
+ $credValue = [System.Convert ]::ToBase64String($binCert )
813
+ $start = (Get-Date ).ToUniversalTime()
814
+ $end = $start.AddDays (1 )
815
+ $cred = New-AzADSpCredentialWithId - ObjectId $servicePrincipal.Id - CertValue $credValue - StartDate $start - EndDate $end - KeyId $keyId2
816
+ Assert-NotNull $cred
817
+
818
+ # Get credential should fetch 3 credentials
819
+ $cred3 = Get-AzADSpCredential - ObjectId $servicePrincipal.Id
820
+ Assert-NotNull $cred3
821
+ Assert-AreEqual $cred3.Count 3
822
+ $credCount = $cred3 | where {$_.KeyId -in $cred1.KeyId , $cred2.KeyId , $cred.KeyId }
823
+ Assert-AreEqual $credCount.Count 3
824
+ $cred3 = $cred
825
+
826
+ # Add 1 more key credential to the same app
827
+ $binCert = $cert.GetRawCertData ()
828
+ $credValue = [System.Convert ]::ToBase64String($binCert )
829
+ $start = (Get-Date ).ToUniversalTime()
830
+ $end = $start.AddDays (1 )
831
+ $cred = New-AzADSpCredentialWithId - ObjectId $servicePrincipal.Id - CertValue $credValue - StartDate $start - EndDate $end - KeyId $keyId3
832
+ Assert-NotNull $cred
833
+
834
+ # Get credential should fetch 4 credentials
835
+ $cred4 = Get-AzADSpCredential - ObjectId $servicePrincipal.Id
836
+ Assert-NotNull $cred4
837
+ Assert-AreEqual $cred4.Count 4
838
+ $credCount = $cred4 | where {$_.KeyId -in $cred1.KeyId , $cred2.KeyId , $cred3.KeyId , $cred.KeyId }
839
+ Assert-AreEqual $credCount.Count 4
840
+
757
841
758
842
# Remove cred by KeyId
759
843
Remove-AzADSpCredential - ServicePrincipalName $servicePrincipal.ServicePrincipalNames [0 ] - KeyId $cred.KeyId - Force
760
- $cred3 = Get-AzADSpCredential - ServicePrincipalName $servicePrincipal.ServicePrincipalNames [0 ]
761
- Assert-NotNull $cred3
762
- Assert-AreEqual $cred3 .Count 1
763
- Assert-AreEqual $cred3 [ 0 ].KeyId $cred1.KeyId
844
+ $cred5 = Get-AzADSpCredential - ServicePrincipalName $servicePrincipal.ServicePrincipalNames [0 ]
845
+ Assert-NotNull $cred5
846
+ Assert-AreEqual $cred5 .Count 3
847
+ Assert-AreEqual $cred5 [ 2 ].KeyId $cred1.KeyId
764
848
765
849
# Remove All creds
766
- Remove-AzADSpCredential - ObjectId $servicePrincipal.Id - All - Force
767
- $cred3 = Get-AzADSpCredential - ObjectId $servicePrincipal.Id
768
- Assert-Null $cred3
850
+ Remove-AzADSpCredential - ObjectId $servicePrincipal.Id - Force
851
+ $cred5 = Get-AzADSpCredential - ObjectId $servicePrincipal.Id
852
+ Assert-Null $cred5
769
853
}
770
854
Finally
771
855
{
772
- # Remove App
773
- $app = Get-AzADApplication - ApplicationId $servicePrincipal.ApplicationId
774
- Remove-AzADApplication - ObjectId $app.ObjectId - Force
856
+ # Remove Service Principal
857
+ Remove-AzADServicePrincipal - ObjectId $servicePrincipal.Id - Force
775
858
}
776
859
}
0 commit comments