Skip to content

Commit

Permalink
align with cli
Browse files Browse the repository at this point in the history
  • Loading branch information
BethanyZhou committed Jun 4, 2024
1 parent 2549366 commit 1c40c80
Showing 1 changed file with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
using Microsoft.WindowsAzure.Commands.Utilities.Common;

using Newtonsoft.Json.Linq;

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Management.Automation;
using System.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text.RegularExpressions;

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

Expand Down Expand Up @@ -202,7 +205,7 @@ public override void ExecuteCmdlet()
switch (ParameterSetName)
{
case ImportCertificateFromFileParameterSet:
byte[] base64Bytes = File.ReadAllBytes(FilePath);
byte[] bytes = File.ReadAllBytes(FilePath);
bool doImport = false;

if (IsPemFile(FilePath))
Expand All @@ -227,15 +230,15 @@ public override void ExecuteCmdlet()
this.Track2DataClient.ImportCertificate(
VaultName,
Name,
base64Bytes,
bytes,
Password,
Tag?.ConvertToDictionary(),
IsPemFile(FilePath) ? Constants.PemContentType : Constants.Pkcs12ContentType,
PolicyObject) :
this.Track2DataClient.MergeCertificate(
VaultName,
Name,
new List<byte[]> { base64Bytes },
GetEnumerableBytes(FilePath),
Tag == null ? null : Tag.ConvertToDictionary());

break;
Expand All @@ -253,6 +256,20 @@ public override void ExecuteCmdlet()
this.WriteObject(certBundle);
}
}
private IEnumerable<byte[]> GetEnumerableBytes(string filePath)
{
var bytesList = new List<byte[]>();
string texts = File.ReadAllText(filePath);
var pattern = @"-----BEGIN CERTIFICATE-----([^-]+)-----END CERTIFICATE-----";
Match m = Regex.Match(texts, pattern, RegexOptions.IgnoreCase);
while (m.Success)
{
bytesList.Add(Convert.FromBase64String(m.Groups[1].Value.Replace(Environment.NewLine,"")));
m = m.NextMatch();
}

return bytesList;
}

private bool IsPemFile(string filePath)
{
Expand Down

0 comments on commit 1c40c80

Please sign in to comment.