Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

$debugModulePath = "$PSScriptRoot\..\..\..\..\artifacts\Debug\Az.KeyVault\Az.KeyVault.psd1"
Import-Module $debugModulePath -Force

Comment on lines +2 to +4
Copy link

Copilot AI Dec 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hardcoded debug module path should not be included in committed tests. Other test files in the PesterTests directory do not import modules explicitly at the file level. This import should either be removed or handled through the test infrastructure. Additionally, the path uses a relative reference that assumes a specific build artifact location which may not be portable across different test environments.

Suggested change
$debugModulePath = "$PSScriptRoot\..\..\..\..\artifacts\Debug\Az.KeyVault\Az.KeyVault.psd1"
Import-Module $debugModulePath -Force

Copilot uses AI. Check for mistakes.
$vaultName = 'danielKV7103'
. "$PSScriptRoot\..\Scripts\Common.ps1"
Comment on lines +5 to +6
Copy link

Copilot AI Dec 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hardcoded vault name 'danielKV7103' should not be committed to the repository. This appears to be a personal test vault. Following the pattern in other test files like Certificate.Tests.ps1 and Key.Tests.ps1, you should either use a placeholder comment or use a vault name that's clearly for testing purposes. Consider using a BeforeAll block to set this value as well, consistent with Certificate.Tests.ps1.

Suggested change
$vaultName = 'danielKV7103'
. "$PSScriptRoot\..\Scripts\Common.ps1"
. "$PSScriptRoot\..\Scripts\Common.ps1"
$vaultName = Get-KeyVaultName

Copilot uses AI. Check for mistakes.

Describe "Get-AzKeyVaultKey filters certificate-backed keys" {
It "Should not return certificate-backed managed keys" {
$certName = Get-CertificateName
$keyName = Get-KeyName

# Create a self-signed certificate (creates a managed key)
$policy = New-AzKeyVaultCertificatePolicy -SecretContentType "application/x-pkcs12" -SubjectName "CN=test.contoso.com" -IssuerName Self -ValidityInMonths 6
$certOp = Add-AzKeyVaultCertificate -VaultName $vaultName -Name $certName -CertificatePolicy $policy

Start-Sleep -Seconds 30
Copy link

Copilot AI Dec 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 30-second sleep duration may not be sufficient for certificate creation to complete in all environments. Azure Key Vault certificate creation is asynchronous and can take longer depending on load. Consider either increasing the sleep duration to 60 seconds, or implementing a polling mechanism that waits for the certificate operation to complete (checking $certOp.Status until it's "completed"). The same issue exists on line 43.

Copilot uses AI. Check for mistakes.
$cert = Get-AzKeyVaultCertificate -VaultName $vaultName -Name $certName
$cert | Should Not BeNullOrEmpty
Copy link

Copilot AI Dec 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test uses outdated Pester syntax without the dash separator. Following the established convention in this codebase (see ManagedHsmDataPlaneTests.Tests.ps1 and MhsmKey.Tests.ps1), this should use modern Pester syntax with 'Should -Not -BeNullOrEmpty'. The same issue applies to lines 22, 27, 30, 47, 52, and 55.

Copilot uses AI. Check for mistakes.

$key = Add-AzKeyVaultKey -VaultName $vaultName -Name $keyName -Destination Software
$key | Should Not BeNullOrEmpty

$keys = Get-AzKeyVaultKey -VaultName $vaultName

$standaloneKey = $keys | Where-Object { $_.Name -eq $keyName }
$standaloneKey | Should Not BeNullOrEmpty

$certBackedKey = $keys | Where-Object { $_.Name -eq $certName }
$certBackedKey | Should BeNullOrEmpty
}
}

Describe "Get-AzKeyVaultSecret filters certificate-backed secrets" {
It "Should not return certificate-backed managed secrets" {
$certName = Get-CertificateName
$secretName = Get-SecretName

# Create a certificate (creates both managed key AND managed secret)
$policy = New-AzKeyVaultCertificatePolicy -SecretContentType "application/x-pkcs12" -SubjectName "CN=test2.contoso.com" -IssuerName Self -ValidityInMonths 6
$certOp = Add-AzKeyVaultCertificate -VaultName $vaultName -Name $certName -CertificatePolicy $policy

Start-Sleep -Seconds 30

$secretValue = ConvertTo-SecureString "MySecretValue123!" -AsPlainText -Force
$secret = Set-AzKeyVaultSecret -VaultName $vaultName -Name $secretName -SecretValue $secretValue
$secret | Should Not BeNullOrEmpty

$secrets = Get-AzKeyVaultSecret -VaultName $vaultName

$standaloneSecret = $secrets | Where-Object { $_.Name -eq $secretName }
$standaloneSecret | Should Not BeNullOrEmpty

$certBackedSecret = $secrets | Where-Object { $_.Name -eq $certName }
$certBackedSecret | Should BeNullOrEmpty
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,8 @@ public IEnumerable<PSKeyVaultSecretIdentityItem> GetSecrets(KeyVaultObjectFilter

options.NextLink = result.NextPageLink;
return (result == null) ? new List<PSKeyVaultSecretIdentityItem>() :
result.Select((secretItem) => new PSKeyVaultSecretIdentityItem(secretItem, this.vaultUriHelper));
result.Where((secretItem) => secretItem.Managed != true)
.Select((secretItem) => new PSKeyVaultSecretIdentityItem(secretItem, this.vaultUriHelper));
Comment on lines +722 to +723
Copy link

Copilot AI Dec 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider filtering deleted secrets as well. The GetDeletedSecrets() method should also filter out certificate-backed secrets (where Managed == true) for consistency. When a certificate is deleted, its corresponding secret is also deleted, and it should remain hidden from the deleted secrets list. Apply the same filtering pattern used here for GetSecrets() and GetSecretVersions().

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: need to discuss if we should actually implement this on deleted secrets, azure-cli does not filter on this property for deleted keys/secrets.

}
catch (Exception ex)
{
Expand Down Expand Up @@ -748,7 +749,8 @@ public IEnumerable<PSKeyVaultSecretIdentityItem> GetSecretVersions(KeyVaultObjec
result = this.keyVaultClient.GetSecretVersionsNextAsync(options.NextLink).GetAwaiter().GetResult();

options.NextLink = result.NextPageLink;
return result.Select((secretItem) => new PSKeyVaultSecretIdentityItem(secretItem, this.vaultUriHelper));
return result.Where((secretItem) => secretItem.Managed != true)
.Select((secretItem) => new PSKeyVaultSecretIdentityItem(secretItem, this.vaultUriHelper));
}
catch (Exception ex)
{
Expand Down
10 changes: 8 additions & 2 deletions src/KeyVault/KeyVault/Track2Models/Track2VaultClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ private IEnumerable<PSKeyVaultKeyIdentityItem> GetKeys(KeyClient client)
var allKeys = client.GetPropertiesOfKeys();
foreach (var keyProperties in allKeys)
{
results.Add(new PSKeyVaultKeyIdentityItem(keyProperties, _vaultUriHelper, false));
if (keyProperties.Managed != true)
{
results.Add(new PSKeyVaultKeyIdentityItem(keyProperties, _vaultUriHelper, false));
}
}
return results;
}
Expand All @@ -159,7 +162,10 @@ private IEnumerable<PSKeyVaultKeyIdentityItem> GetKeyVersions(KeyClient client,
var allKeys = client.GetPropertiesOfKeyVersions(keyName);
foreach (var keyProperties in allKeys)
{
results.Add(new PSKeyVaultKeyIdentityItem(keyProperties, _vaultUriHelper, false));
if (keyProperties.Managed != true)
{
results.Add(new PSKeyVaultKeyIdentityItem(keyProperties, _vaultUriHelper, false));
}
}
return results;
}
Expand Down
Loading