@@ -52,6 +52,8 @@ public abstract class ServiceFabricClusterCertificateCmdlet : ServiceFabricClust
52
52
53
53
private string keyVaultCertificateName { get ; set ; }
54
54
55
+ private const string BasicConstraintsExtensionName = "Basic Constraints" ;
56
+
55
57
/// <summary>
56
58
/// Resource group name
57
59
/// </summary>
@@ -661,15 +663,14 @@ private string GetThumbprintFromSecret(string secretUrl)
661
663
}
662
664
}
663
665
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 )
668
668
{
669
- return lastCert . Thumbprint ;
669
+ throw new PSInvalidOperationException ( string . Format ( " Thumbprint from secretUrl: {0} is null." , secretUrl ) ) ;
670
670
}
671
671
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 ;
673
674
}
674
675
675
676
private string GetCommonNameFromSecret ( string secretUrl )
@@ -692,14 +693,50 @@ private string GetCommonNameFromSecret(string secretUrl)
692
693
}
693
694
}
694
695
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
+ {
695
704
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 )
698
707
{
699
- return lastCert . GetNameInfo ( X509NameType . SimpleName , false ) ;
708
+ throw new PSInvalidOperationException ( string . Format ( "Failed to get certificate from secretUrl: {0}. Certcollection is empty" , secretUrl ) ) ;
700
709
}
701
710
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 ;
703
740
}
704
741
705
742
private X509Certificate2Collection GetCertCollectionFromSecret ( string secretUrl )
0 commit comments