-
Notifications
You must be signed in to change notification settings - Fork 4.1k
[Az.KeyVault] Included policy while importing Certificate to a KeyVault #21472
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
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
5ca7ada
Half way upload.
NoriZC 3a126d5
Second time commit-halp way
NoriZC f94fbf7
sync 4/2
NoriZC 6bee852
Done transfer to track2
NoriZC 0375688
sync 4/5
NoriZC 74547ca
Complete import cert with policy
NoriZC 2591b88
complete all cases for policy
NoriZC fbf60aa
Update ChangeLog
NoriZC 794efa5
Remove unnecessary lines
NoriZC 3416bcd
Resolve comments
NoriZC c7b2dbf
Update pfx file name
NoriZC cf688e8
Update according to comments
NoriZC 1b94f0d
Remove unnecessary dependecies
NoriZC c95e61e
Modify the actions number
NoriZC 734b379
Update the contentType validation
NoriZC d51b4b4
solve paramaterset ambigious
NoriZC 676e0fe
Update ChangeLog.md
NoriZC 266f0bc
Merge the exceptions, update help file
NoriZC d95d512
Merge the exceptions, update help file
NoriZC 9580fa3
Resolve the comments 4/27
NoriZC 88b0f1c
Update descriptions of policyobject and policypath
NoriZC 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
43 changes: 43 additions & 0 deletions
43
src/KeyVault/KeyVault.Test/PesterTests/Certificate.Tests.ps1
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,43 @@ | ||
| BeforeAll { | ||
| $vaultName = 'nori-kv765' | ||
| . "..\Scripts\Common.ps1" | ||
| } | ||
|
|
||
| Describe "Import Certificate with policy" { | ||
| It "ImportCertificateFromFileWithPolicyParameterSet" { | ||
| $certName = Get-CertificateName | ||
| $certFilePath = "..\Resources\importCertWithPolicy.pfx" | ||
| $policy = New-AzKeyVaultCertificatePolicy -SecretContentType "application/x-pkcs12" -SubjectName "CN=contoso.com" -IssuerName "Self" -ValidityInMonths 6 -ReuseKeyOnRenewal | ||
|
|
||
| $cert = Import-AzKeyVaultCertificate -VaultName $vaultName -Name $certName -FilePath $certFilePath -PolicyObject $policy | ||
| $cert.Policy.SecretContentType | Should -Be "application/x-pkcs12" | ||
| } | ||
| It "ImportCertificateFromFileWithPolicyFileParameterSet" { | ||
| $certName = Get-CertificateName | ||
| $certFilePath = "..\Resources\importCertWithPolicy.pfx" | ||
| $policyPath = "..\Resources\certPolicy.json" | ||
|
|
||
| $cert = Import-AzKeyVaultCertificate -VaultName $vaultName -Name $certName -FilePath $certFilePath -PolicyPath $policyPath | ||
| $cert.Policy.SecretContentType | Should -Be "application/x-pkcs12" | ||
| } | ||
| It "ImportWithPrivateKeyFromStringWithPolicyFileParameterSet" { | ||
| $certName = Get-CertificateName | ||
| $certFilePath = "..\Resources\importCertWithPolicy.pfx" | ||
| $policyPath = "..\Resources\certPolicy.json" | ||
| $Base64StringCertificate = [System.Convert]::ToBase64String([System.IO.File]::ReadAllBytes($certFilePath)) | ||
|
|
||
| $cert = Import-AzKeyVaultCertificate -VaultName $vaultName -Name $certName -CertificateString $Base64StringCertificate -PolicyPath $policyPath | ||
| $cert.Policy.SecretContentType | Should -Be "application/x-pkcs12" | ||
| } | ||
| It "ImportWithPrivateKeyFromCollectionWithPolicyFileParameterSet" { | ||
| $certName = Get-CertificateName | ||
| $certFilePath = "..\Resources\importCertWithPolicy.pfx" | ||
| $policyPath = "..\Resources\certPolicy.json" | ||
| $certCollection = [System.Security.Cryptography.X509Certificates.X509Certificate2Collection]::new() | ||
| $certCollection.Import($certFilePath, $null, [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable) | ||
|
|
||
| $cert = Import-AzKeyVaultCertificate -VaultName $vaultName -Name $certName -CertificateCollection $certCollection -PolicyPath $policyPath | ||
| $cert.Policy.SecretContentType | Should -Be "application/x-pkcs12" | ||
| } | ||
| } | ||
|
|
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,40 @@ | ||
| { | ||
| "id": "https://myvault.vault.azure.net/certificates/updateCert01/policy", | ||
| "key_props": { | ||
| "exportable": true, | ||
| "kty": "RSA", | ||
| "key_size": 2048, | ||
| "reuse_key": false | ||
| }, | ||
|
|
||
| "secret_props": { | ||
| "contentType": "application/x-pkcs12" | ||
| }, | ||
|
|
||
| "x509_props": { | ||
| "subject": "CN=KeyVaultTest", | ||
| "ekus": [], | ||
| "key_usage": [], | ||
| "validity_months": 297 | ||
| }, | ||
|
|
||
| "lifetime_actions": [ | ||
| { | ||
| "trigger": { | ||
| "lifetime_percentage": 80 | ||
| }, | ||
| "action": { | ||
| "action_type": "EmailContacts" | ||
| } | ||
| } | ||
| ], | ||
|
|
||
| "issuer": { | ||
| "name": "Unknown" | ||
| }, | ||
| "attributes": { | ||
| "enabled": true, | ||
| "created": 1482188947, | ||
| "updated": 1482188947 | ||
| } | ||
| } |
Binary file not shown.
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
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 | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -108,6 +108,21 @@ public class ImportAzureKeyVaultCertificate : KeyVaultCmdletBase | |||||||||
| HelpMessage = "Specifies the password for the certificate and private key base64 encoded string to import.")] | ||||||||||
| public SecureString Password { get; set; } | ||||||||||
|
|
||||||||||
| /// <summary> | ||||||||||
| /// File Path | ||||||||||
| /// </summary> | ||||||||||
| [Parameter(Mandatory = false, | ||||||||||
| HelpMessage = "A file path to specify management policy for the certificate that contains JSON encoded policy definition. Mutual-exclusive to PolicyObject.")] | ||||||||||
| public string PolicyPath { get; set; } | ||||||||||
|
|
||||||||||
| /// <summary> | ||||||||||
| /// File Path | ||||||||||
| /// </summary> | ||||||||||
| [Parameter(Mandatory = false, | ||||||||||
| ValueFromPipeline = true, | ||||||||||
| HelpMessage = "An in-memory object to specify management policy for the certificate. Mutual-exclusive to PolicyPath.")] | ||||||||||
| public PSKeyVaultCertificatePolicy PolicyObject { get; set; } | ||||||||||
|
|
||||||||||
| /// <summary> | ||||||||||
| /// Certificate Collection | ||||||||||
| /// </summary> | ||||||||||
|
|
@@ -131,6 +146,7 @@ public class ImportAzureKeyVaultCertificate : KeyVaultCmdletBase | |||||||||
| protected override void BeginProcessing() | ||||||||||
| { | ||||||||||
| FilePath = this.TryResolvePath(FilePath); | ||||||||||
| PolicyPath = this.TryResolvePath(PolicyPath); | ||||||||||
| base.BeginProcessing(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
@@ -143,6 +159,34 @@ private void ValidateParameters() | |||||||||
| { | ||||||||||
| throw new AzPSArgumentException(string.Format(Resources.FileNotFound, this.FilePath), nameof(FilePath)); | ||||||||||
| } | ||||||||||
| if (IsPemFile(FilePath)) | ||||||||||
| { | ||||||||||
| ContentType = Constants.PemContentType; | ||||||||||
| } | ||||||||||
| else | ||||||||||
| { | ||||||||||
| ContentType = Constants.Pkcs12ContentType; | ||||||||||
| } | ||||||||||
| } | ||||||||||
| if (this.IsParameterBound(c => c.CertificateCollection)) | ||||||||||
| { | ||||||||||
| ContentType = Constants.Pkcs12ContentType; | ||||||||||
| } | ||||||||||
| if (this.IsParameterBound(c => c.PolicyPath) && this.IsParameterBound(c => c.PolicyObject)) | ||||||||||
| { | ||||||||||
| throw new AzPSArgumentException($"Parameter {nameof(PolicyPath)} conflicts with Parameter {nameof(PolicyObject)}. Only one of these 2 parameters could be imported at once.", nameof(PolicyPath)); | ||||||||||
| } | ||||||||||
| if (this.IsParameterBound(c => c.PolicyPath)) | ||||||||||
| { | ||||||||||
| if (!File.Exists(PolicyPath)) | ||||||||||
|
Comment on lines
+179
to
+181
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
NIT |
||||||||||
| { | ||||||||||
| throw new AzPSArgumentException(string.Format(Resources.FileNotFound, this.PolicyPath), nameof(PolicyPath)); | ||||||||||
| } | ||||||||||
| PolicyObject = PSKeyVaultCertificatePolicy.FromJsonFile(PolicyPath); | ||||||||||
| } | ||||||||||
| if (PolicyObject != null && PolicyObject.SecretContentType != ContentType) | ||||||||||
| { | ||||||||||
| throw new AzPSArgumentException($"User input {ContentType} conflicts with the ContentType stated as {PolicyObject.SecretContentType} in Certificate Policy.", ContentType); | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
@@ -162,7 +206,7 @@ public override void ExecuteCmdlet() | |||||||||
| if (IsPemFile(FilePath)) | ||||||||||
| { | ||||||||||
| byte[] pemBytes = File.ReadAllBytes(FilePath); | ||||||||||
| certBundle = this.Track2DataClient.ImportCertificate(VaultName, Name, pemBytes, Password, Tag?.ConvertToDictionary(), Constants.PemContentType); | ||||||||||
| certBundle = this.Track2DataClient.ImportCertificate(VaultName, Name, pemBytes, Password, Tag?.ConvertToDictionary(), Constants.PemContentType, certPolicy: PolicyObject); | ||||||||||
| } | ||||||||||
| else | ||||||||||
| { | ||||||||||
|
|
@@ -179,8 +223,9 @@ public override void ExecuteCmdlet() | |||||||||
|
|
||||||||||
| if (doImport) | ||||||||||
| { | ||||||||||
|
|
||||||||||
| byte[] base64Bytes = userProvidedCertColl.Export(X509ContentType.Pfx, Password?.ConvertToString()); | ||||||||||
| certBundle = this.Track2DataClient.ImportCertificate(VaultName, Name, base64Bytes, Password, Tag?.ConvertToDictionary()); | ||||||||||
| certBundle = this.Track2DataClient.ImportCertificate(VaultName, Name, base64Bytes, Password, Tag?.ConvertToDictionary(), certPolicy: PolicyObject); | ||||||||||
| } | ||||||||||
| else | ||||||||||
| { | ||||||||||
|
|
@@ -194,12 +239,11 @@ public override void ExecuteCmdlet() | |||||||||
| break; | ||||||||||
|
|
||||||||||
| case ImportWithPrivateKeyFromCollectionParameterSet: | ||||||||||
| certBundle = this.DataServiceClient.ImportCertificate(VaultName, Name, CertificateCollection, Tag?.ConvertToDictionary()); | ||||||||||
|
|
||||||||||
| certBundle = this.Track2DataClient.ImportCertificate(VaultName, Name, CertificateCollection, null, Tag?.ConvertToDictionary(), certPolicy: PolicyObject); | ||||||||||
| break; | ||||||||||
|
|
||||||||||
| case ImportWithPrivateKeyFromStringParameterSet: | ||||||||||
| certBundle = this.Track2DataClient.ImportCertificate(VaultName, Name, CertificateString, Password, Tag?.ConvertToDictionary(), ContentType); | ||||||||||
| certBundle = this.Track2DataClient.ImportCertificate(VaultName, Name, CertificateString, Password, Tag?.ConvertToDictionary(), ContentType, certPolicy: PolicyObject); | ||||||||||
|
|
||||||||||
| break; | ||||||||||
| } | ||||||||||
|
|
||||||||||
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
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
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
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.