Skip to content

Commit 43b22c8

Browse files
author
Alfredo Santamaria Gomez
committed
Fix add certificate ByExistingKeyVault getting wrong thumbprint in some cases
1 parent 5fe49e1 commit 43b22c8

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

src/ServiceFabric/ServiceFabric/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+
* Fix add certificate ByExistingKeyVault getting the wrong thumbprint in some cases
2122

2223
## Version 1.1.0
2324
* Fix typo in error message for `Update-AzServiceFabricReliability`

src/ServiceFabric/ServiceFabric/Commands/ServiceFabricClusterCertificateCmdlet.cs

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public abstract class ServiceFabricClusterCertificateCmdlet : ServiceFabricClust
5252

5353
private string keyVaultCertificateName { get; set; }
5454

55+
private const string BasicConstraintsExtensionName = "Basic Constraints";
56+
5557
/// <summary>
5658
/// Resource group name
5759
/// </summary>
@@ -661,15 +663,14 @@ private string GetThumbprintFromSecret(string secretUrl)
661663
}
662664
}
663665

664-
X509Certificate2Collection certCollection = GetCertCollectionFromSecret(secretUrl);
665-
666-
var lastCert = certCollection.Count > 0 ? certCollection[certCollection.Count - 1] : null;
667-
if (lastCert?.Thumbprint != null)
666+
X509Certificate2 cert = GetCertFromSecret(secretUrl);
667+
if (cert.Thumbprint == null)
668668
{
669-
return lastCert.Thumbprint;
669+
throw new PSInvalidOperationException(string.Format("Thumbprint from secretUrl: {0} is null.", secretUrl));
670670
}
671671

672-
throw new PSInvalidOperationException(string.Format("Failed to find the thumbprint from {0}", secretUrl));
672+
WriteVerboseWithTimestamp("Certificate found from secret with thumbprint: {0}", cert.Thumbprint);
673+
return cert.Thumbprint;
673674
}
674675

675676
private string GetCommonNameFromSecret(string secretUrl)
@@ -692,14 +693,50 @@ private string GetCommonNameFromSecret(string secretUrl)
692693
}
693694
}
694695

696+
var cert = GetCertFromSecret(secretUrl);
697+
string commonName = cert.GetNameInfo(X509NameType.SimpleName, false);
698+
WriteVerboseWithTimestamp("Certificate found from secret with common name: {0}", commonName);
699+
return commonName;
700+
}
701+
702+
private X509Certificate2 GetCertFromSecret(string secretUrl)
703+
{
695704
X509Certificate2Collection certCollection = GetCertCollectionFromSecret(secretUrl);
696-
var lastCert = certCollection.Count > 0 ? certCollection[certCollection.Count - 1] : null;
697-
if (lastCert != null)
705+
706+
if (certCollection.Count == 0)
698707
{
699-
return lastCert.GetNameInfo(X509NameType.SimpleName, false);
708+
throw new PSInvalidOperationException(string.Format("Failed to get certificate from secretUrl: {0}. Certcollection is empty", secretUrl));
700709
}
701710

702-
throw new PSInvalidOperationException(string.Format("Failed to find the common name from {0}", secretUrl));
711+
var firstCert = certCollection[0];
712+
var lastCert = certCollection[certCollection.Count - 1];
713+
714+
if (!IsCertCA(firstCert))
715+
{
716+
return firstCert;
717+
}
718+
else if (!IsCertCA(lastCert))
719+
{
720+
return lastCert;
721+
}
722+
else
723+
{
724+
throw new PSInvalidOperationException(string.Format("Failed to get certificate from secretUrl: {0}. All certs in the chain are Certificate Authority", secretUrl));
725+
}
726+
}
727+
728+
private bool IsCertCA(X509Certificate2 cert)
729+
{
730+
foreach (var currExt in cert.Extensions)
731+
{
732+
if (currExt.Oid.FriendlyName == BasicConstraintsExtensionName)
733+
{
734+
X509BasicConstraintsExtension ext = (X509BasicConstraintsExtension)currExt;
735+
return ext.CertificateAuthority;
736+
}
737+
}
738+
739+
return false;
703740
}
704741

705742
private X509Certificate2Collection GetCertCollectionFromSecret(string secretUrl)

0 commit comments

Comments
 (0)