-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Keyvault oct hsm #29579
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MaddyMicrosoft
wants to merge
25
commits into
Azure:main
Choose a base branch
from
MaddyMicrosoft:keyvault-oct-hsm
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Keyvault oct hsm #29579
Changes from 5 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
329861b
[KeyVault] Add support for creating oct (AES) HSM-backed keys on AKV …
MaddyMicrosoft cbd8797
Unit tests added
MaddyMicrosoft c15d036
Refactor and unit tests added
MaddyMicrosoft 2aefef9
Update changelog as per Copilot comment
MaddyMicrosoft 632fde5
honor -Destination for oct keys on vault path
MaddyMicrosoft 7c5b21b
Keys for auto complete added
MaddyMicrosoft 377cf01
Changelog order corrected
MaddyMicrosoft c1a3db6
Revert "Keys for auto complete added"
MaddyMicrosoft b8dd931
Changes as per CP comments
MaddyMicrosoft 40e144a
[KeyVault] Add support for creating oct (AES) HSM-backed keys on AKV …
MaddyMicrosoft 485d0d6
Unit tests added
MaddyMicrosoft 72d0e1a
Refactor and unit tests added
MaddyMicrosoft 9569497
Update changelog as per Copilot comment
MaddyMicrosoft e0536d1
honor -Destination for oct keys on vault path
MaddyMicrosoft 0019339
Keys for auto complete added
MaddyMicrosoft 44b5632
Changelog order corrected
MaddyMicrosoft ccf2cc0
Revert "Keys for auto complete added"
MaddyMicrosoft 0703f80
Changes as per CP comments
MaddyMicrosoft 1653834
Merge branch 'keyvault-oct-hsm' of https://github.com/MaddyMicrosoft/…
MaddyMicrosoft 4a2ebeb
Add oct-HSM scenario tests for Premium Key Vault
MaddyMicrosoft b67bd29
Add oct-HSM scenario test json
MaddyMicrosoft cb7e498
Tests updated to live only
MaddyMicrosoft 5cd80ef
Clean up tests scenarios
MaddyMicrosoft de6bd2b
Merge branch 'main' into keyvault-oct-hsm
MaddyMicrosoft e6564f7
Merge branch 'main' into keyvault-oct-hsm
notyashhh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
247 changes: 247 additions & 0 deletions
247
src/KeyVault/KeyVault.Test/UnitTests/AddKeyVaultOctKeyTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,247 @@ | ||
| // ---------------------------------------------------------------------------------- | ||
| // | ||
| // Copyright Microsoft Corporation | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // ---------------------------------------------------------------------------------- | ||
|
|
||
| using Microsoft.Azure.Commands.KeyVault.Models; | ||
| using Microsoft.Azure.KeyVault.WebKey; | ||
| using Microsoft.WindowsAzure.Commands.ScenarioTest; | ||
| using Moq; | ||
| using Xunit; | ||
| using WebKey = Microsoft.Azure.KeyVault.WebKey; | ||
|
|
||
| namespace Microsoft.Azure.Commands.KeyVault.Test.UnitTests | ||
| { | ||
| /// <summary> | ||
| /// Verifies the cmdlet-level wiring for creating oct (AES) HSM-backed keys | ||
| /// via Add-AzKeyVaultKey, on both Premium AKV and Managed HSM. | ||
| /// | ||
| /// These are pure cmdlet plumbing tests: they mock IKeyVaultDataServiceClient | ||
| /// (the same type used by Track2DataClient) and assert that the cmdlet: | ||
| /// 1. Translates -KeyType oct -Destination HSM into PSKeyVaultKeyAttributes | ||
| /// with KeyType == "oct-HSM" before calling the service client. | ||
| /// 2. Forwards the requested -Size to the service client unchanged. | ||
| /// 3. Routes vault calls through CreateKey and HSM calls through | ||
| /// CreateManagedHsmKey. | ||
| /// | ||
| /// Server-side behavior (e.g. the Track2 SDK CreateOctKeyOptions wire format, | ||
| /// or the eventual KeyProperties.KeySize read-back) is intentionally out of | ||
| /// scope here and is covered by manual validation / Pester scripts. | ||
| /// </summary> | ||
| public class AddKeyVaultOctKeyTests : KeyVaultUnitTestBase | ||
| { | ||
| private const string HsmName = "hsmname"; | ||
|
|
||
| private readonly AddAzureKeyVaultKey cmdlet; | ||
| private readonly Mock<IKeyVaultDataServiceClient> track2DataClientMock; | ||
|
|
||
| public AddKeyVaultOctKeyTests() | ||
| { | ||
| base.SetupTest(); | ||
|
|
||
| // KeyVaultUnitTestBase sets up keyVaultClientMock for Track1; create | ||
| // a separate mock for the Track2 client, since AddAzureKeyVaultKey | ||
| // routes oct/RSA/EC creation through Track2DataClient. | ||
| track2DataClientMock = new Mock<IKeyVaultDataServiceClient>(); | ||
|
|
||
| cmdlet = new AddAzureKeyVaultKey | ||
| { | ||
| CommandRuntime = commandRuntimeMock.Object, | ||
| DataServiceClient = keyVaultClientMock.Object, | ||
| Track2DataClient = track2DataClientMock.Object, | ||
| }; | ||
|
|
||
| // ConfirmAction / ShouldProcess must return true for ExecuteCmdlet | ||
| // to reach the create call. | ||
| commandRuntimeMock | ||
| .Setup(cr => cr.ShouldProcess(It.IsAny<string>(), It.IsAny<string>())) | ||
| .Returns(true); | ||
| } | ||
|
|
||
| private static PSKeyVaultKey StubKey(string vaultName, string name, string kty) | ||
| { | ||
| return new PSKeyVaultKey | ||
| { | ||
| VaultName = vaultName, | ||
| Name = name, | ||
| Key = new WebKey.JsonWebKey { Kty = kty }, | ||
| Attributes = new PSKeyVaultKeyAttributes(true, null, null, kty, null, null), | ||
| }; | ||
| } | ||
|
|
||
| [Theory] | ||
| [Trait(Category.AcceptanceType, Category.CheckIn)] | ||
| [InlineData(128)] | ||
| [InlineData(192)] | ||
| [InlineData(256)] | ||
| public void OctKeyOnVaultIsCreatedAsOctHsm(int size) | ||
| { | ||
| // Capture the attributes the cmdlet hands to the service client so | ||
| // we can assert KeyType is rewritten from 'oct' -> 'oct-HSM'. | ||
| PSKeyVaultKeyAttributes captured = null; | ||
| int? capturedSize = null; | ||
|
|
||
| track2DataClientMock | ||
| .Setup(c => c.CreateKey(VaultName, KeyName, | ||
| It.IsAny<PSKeyVaultKeyAttributes>(), It.IsAny<int?>(), It.IsAny<string>())) | ||
| .Callback<string, string, PSKeyVaultKeyAttributes, int?, string>( | ||
| (_, __, attrs, sz, ___) => { captured = attrs; capturedSize = sz; }) | ||
| .Returns(StubKey(VaultName, KeyName, "oct-HSM")) | ||
| .Verifiable(); | ||
|
|
||
| cmdlet.VaultName = VaultName; | ||
| cmdlet.Name = KeyName; | ||
| cmdlet.KeyType = JsonWebKeyType.Octet; // "oct" | ||
| cmdlet.Destination = "HSM"; | ||
| cmdlet.Size = size; | ||
|
|
||
| cmdlet.ExecuteCmdlet(); | ||
|
|
||
| track2DataClientMock.VerifyAll(); | ||
| Assert.NotNull(captured); | ||
| Assert.Equal("oct-HSM", captured.KeyType); | ||
| Assert.Equal(size, capturedSize); | ||
|
|
||
| // Track1 client must not be used for creation on the vault path. | ||
| keyVaultClientMock.Verify(c => c.CreateKey( | ||
| It.IsAny<string>(), It.IsAny<string>(), | ||
| It.IsAny<PSKeyVaultKeyAttributes>(), It.IsAny<int?>(), It.IsAny<string>()), | ||
| Times.Never()); | ||
| } | ||
|
|
||
| [Theory] | ||
| [Trait(Category.AcceptanceType, Category.CheckIn)] | ||
| [InlineData(128)] | ||
| [InlineData(256)] | ||
| public void OctKeyOnManagedHsmIsCreatedAsOctHsm(int size) | ||
|
MaddyMicrosoft marked this conversation as resolved.
Outdated
|
||
| { | ||
| PSKeyVaultKeyAttributes captured = null; | ||
| int? capturedSize = null; | ||
|
|
||
| track2DataClientMock | ||
| .Setup(c => c.CreateManagedHsmKey(HsmName, KeyName, | ||
| It.IsAny<PSKeyVaultKeyAttributes>(), It.IsAny<int?>(), It.IsAny<string>())) | ||
| .Callback<string, string, PSKeyVaultKeyAttributes, int?, string>( | ||
| (_, __, attrs, sz, ___) => { captured = attrs; capturedSize = sz; }) | ||
| .Returns(StubKey(HsmName, KeyName, "oct-HSM")) | ||
| .Verifiable(); | ||
|
|
||
| cmdlet.HsmName = HsmName; | ||
| cmdlet.Name = KeyName; | ||
| cmdlet.KeyType = JsonWebKeyType.Octet; // "oct" | ||
| // Note: -Destination is not used on the HSM path; Managed HSM keys | ||
| // are always HSM-backed, so the cmdlet does not rewrite the kty. | ||
| // The service still emits 'oct-HSM' on the wire. | ||
| cmdlet.Size = size; | ||
|
|
||
| cmdlet.ExecuteCmdlet(); | ||
|
|
||
| track2DataClientMock.VerifyAll(); | ||
| Assert.NotNull(captured); | ||
| Assert.Equal(size, capturedSize); | ||
|
|
||
| // Vault create must not be invoked on the HSM path. | ||
| track2DataClientMock.Verify(c => c.CreateKey( | ||
| It.IsAny<string>(), It.IsAny<string>(), | ||
| It.IsAny<PSKeyVaultKeyAttributes>(), It.IsAny<int?>(), It.IsAny<string>()), | ||
| Times.Never()); | ||
| } | ||
|
|
||
| [Fact] | ||
| [Trait(Category.AcceptanceType, Category.CheckIn)] | ||
| public void RsaKeyOnVaultWithHsmDestinationStaysAsRsaHsm() | ||
| { | ||
| // Regression guard: the new oct-HSM rewrite must not disturb the | ||
| // existing RSA / EC -> RSA-HSM / EC-HSM rewrites. | ||
| PSKeyVaultKeyAttributes captured = null; | ||
|
|
||
| track2DataClientMock | ||
| .Setup(c => c.CreateKey(VaultName, KeyName, | ||
| It.IsAny<PSKeyVaultKeyAttributes>(), It.IsAny<int?>(), It.IsAny<string>())) | ||
| .Callback<string, string, PSKeyVaultKeyAttributes, int?, string>( | ||
| (_, __, attrs, ___, ____) => captured = attrs) | ||
| .Returns(StubKey(VaultName, KeyName, JsonWebKeyType.RsaHsm)); | ||
|
|
||
| cmdlet.VaultName = VaultName; | ||
| cmdlet.Name = KeyName; | ||
| cmdlet.KeyType = JsonWebKeyType.Rsa; | ||
| cmdlet.Destination = "HSM"; | ||
| cmdlet.Size = 2048; | ||
|
|
||
| cmdlet.ExecuteCmdlet(); | ||
|
|
||
| Assert.NotNull(captured); | ||
| Assert.Equal(JsonWebKeyType.RsaHsm, captured.KeyType); | ||
| } | ||
|
|
||
| [Fact] | ||
| [Trait(Category.AcceptanceType, Category.CheckIn)] | ||
| public void RsaKeyOnVaultWithSoftwareDestinationStaysAsRsa() | ||
| { | ||
| // Software-backed RSA on a Standard or Premium AKV is the default | ||
| // path. The cmdlet must NOT rewrite the kty (stays plain "RSA") | ||
| // and must still route through Track2DataClient.CreateKey. | ||
| PSKeyVaultKeyAttributes captured = null; | ||
|
|
||
| track2DataClientMock | ||
| .Setup(c => c.CreateKey(VaultName, KeyName, | ||
| It.IsAny<PSKeyVaultKeyAttributes>(), It.IsAny<int?>(), It.IsAny<string>())) | ||
| .Callback<string, string, PSKeyVaultKeyAttributes, int?, string>( | ||
| (_, __, attrs, ___, ____) => captured = attrs) | ||
| .Returns(StubKey(VaultName, KeyName, JsonWebKeyType.Rsa)) | ||
| .Verifiable(); | ||
|
|
||
| cmdlet.VaultName = VaultName; | ||
| cmdlet.Name = KeyName; | ||
| cmdlet.KeyType = JsonWebKeyType.Rsa; | ||
| cmdlet.Destination = "Software"; | ||
| cmdlet.Size = 2048; | ||
|
|
||
| cmdlet.ExecuteCmdlet(); | ||
|
|
||
| track2DataClientMock.VerifyAll(); | ||
| Assert.NotNull(captured); | ||
| Assert.Equal(JsonWebKeyType.Rsa, captured.KeyType); | ||
| } | ||
|
|
||
| [Fact] | ||
| [Trait(Category.AcceptanceType, Category.CheckIn)] | ||
| public void OctKeyOnVaultWithoutHsmDestinationIsNotRewritten() | ||
| { | ||
| // Documents that the cmdlet only rewrites oct -> oct-HSM when | ||
| // -Destination HSM is set on a vault. Without it, kty stays "oct" | ||
| // and the request would be rejected server-side (no software-backed | ||
| // oct keys exist on AKV). We can't exercise the service rejection | ||
| // here, but we can pin the cmdlet-side contract. | ||
|
MaddyMicrosoft marked this conversation as resolved.
|
||
| PSKeyVaultKeyAttributes captured = null; | ||
|
|
||
| track2DataClientMock | ||
| .Setup(c => c.CreateKey(VaultName, KeyName, | ||
| It.IsAny<PSKeyVaultKeyAttributes>(), It.IsAny<int?>(), It.IsAny<string>())) | ||
| .Callback<string, string, PSKeyVaultKeyAttributes, int?, string>( | ||
| (_, __, attrs, ___, ____) => captured = attrs) | ||
| .Returns(StubKey(VaultName, KeyName, JsonWebKeyType.Octet)); | ||
|
|
||
| cmdlet.VaultName = VaultName; | ||
| cmdlet.Name = KeyName; | ||
| cmdlet.KeyType = JsonWebKeyType.Octet; // "oct" | ||
| // No Destination set -> kty must NOT be rewritten to "oct-HSM". | ||
| cmdlet.Size = 256; | ||
|
|
||
| cmdlet.ExecuteCmdlet(); | ||
|
|
||
| Assert.NotNull(captured); | ||
| Assert.Equal(JsonWebKeyType.Octet, captured.KeyType); | ||
| Assert.NotEqual("oct-HSM", captured.KeyType); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.