Skip to content

Commit 04f3563

Browse files
authored
Fixed Key Vault Certificate Merge issue (#25333)
* align with cli * remove unused namespace * refine
1 parent 5eca227 commit 04f3563

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

src/KeyVault/KeyVault/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Fixed an issue during merging certificate process. [#24323]
2122

2223
## Version 6.0.0
2324
* [Breaking change] Removed the offline fallback policy if specify parameter `UseDefaultCVMPolicy` in `Add-AzKeyVaultKey`. Key creation will fail if unable to get regional default CVM SKR policy from MAA Service Discovery API.

src/KeyVault/KeyVault/Commands/Certificate/ImportAzureKeyVaultCertificate.cs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using System.Management.Automation;
2626
using System.Security;
2727
using System.Security.Cryptography.X509Certificates;
28+
using System.Text.RegularExpressions;
2829

2930
using KeyVaultProperties = Microsoft.Azure.Commands.KeyVault.Properties;
3031

@@ -202,7 +203,7 @@ public override void ExecuteCmdlet()
202203
switch (ParameterSetName)
203204
{
204205
case ImportCertificateFromFileParameterSet:
205-
byte[] base64Bytes = File.ReadAllBytes(FilePath);
206+
byte[] bytes = File.ReadAllBytes(FilePath);
206207
bool doImport = false;
207208

208209
if (IsPemFile(FilePath))
@@ -227,15 +228,15 @@ public override void ExecuteCmdlet()
227228
this.Track2DataClient.ImportCertificate(
228229
VaultName,
229230
Name,
230-
base64Bytes,
231+
bytes,
231232
Password,
232233
Tag?.ConvertToDictionary(),
233234
IsPemFile(FilePath) ? Constants.PemContentType : Constants.Pkcs12ContentType,
234235
PolicyObject) :
235236
this.Track2DataClient.MergeCertificate(
236237
VaultName,
237238
Name,
238-
new List<byte[]> { base64Bytes },
239+
GetEnumerableBytes(FilePath),
239240
Tag == null ? null : Tag.ConvertToDictionary());
240241

241242
break;
@@ -254,6 +255,34 @@ public override void ExecuteCmdlet()
254255
}
255256
}
256257

258+
/// <summary>
259+
/// Read cert data between cert header and footer and convert it to bytes list
260+
/// </summary>
261+
/// <param name="filePath"> The full path to cert</param>
262+
/// <returns>Bytes list for cert data</returns>
263+
/// <exception cref="AzPSException"></exception>
264+
private IEnumerable<byte[]> GetEnumerableBytes(string filePath)
265+
{
266+
var bytesList = new List<byte[]>();
267+
try
268+
{
269+
string texts = File.ReadAllText(filePath);
270+
// Match cert data between cert header and footer and convert it to bytes
271+
var pattern = @"-----BEGIN CERTIFICATE-----([^-]+)-----END CERTIFICATE-----";
272+
Match m = Regex.Match(texts, pattern, RegexOptions.IgnoreCase);
273+
while (m.Success)
274+
{
275+
bytesList.Add(Convert.FromBase64String(m.Groups[1].Value.Replace(Environment.NewLine, "")));
276+
m = m.NextMatch();
277+
}
278+
}
279+
catch (Exception ex)
280+
{
281+
throw new AzPSException(string.Format(Resources.ProcessingCertError, filePath, ex.Message), Common.ErrorKind.UserError);
282+
}
283+
return bytesList;
284+
}
285+
257286
private bool IsPemFile(string filePath)
258287
{
259288
return ".pem".Equals(Path.GetExtension(filePath), StringComparison.OrdinalIgnoreCase);

src/KeyVault/KeyVault/Properties/Resources.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/KeyVault/KeyVault/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,4 +627,7 @@ You can find the object ID using Azure Active Directory Module for Windows Power
627627
<data name="NoVaultWithGivenNameFound" xml:space="preserve">
628628
<value>Vault '{0}' does not exist in current subscription. If this vault exists in your tenant, please switch to the correct subscription.</value>
629629
</data>
630+
<data name="ProcessingCertError" xml:space="preserve">
631+
<value>Error happens when processing certificate '{0}'. See detailed error: {1}</value>
632+
</data>
630633
</root>

0 commit comments

Comments
 (0)