예제 #1
0
        public static X509Certificate2 FindBindedCertificate(ServiceFabricApplication fabricApplication, string applicationTypeName, string serviceManifestName)
        {
            using (var client = new FabricClient())
            {
                var manifest =
                    client.ApplicationManager.GetApplicationManifestAsync(
                        applicationTypeName,
                        fabricApplication.GetApplicationVersionFunction()).ConfigureAwait(false).GetAwaiter()
                    .GetResult();

                var doc = XDocument.Parse(manifest);
                var ns  = doc.Root?.GetDefaultNamespace();

                var certRefs = from manifestImport in doc.Root?.Descendants(ns + "ServiceManifestImport")
                               where manifestImport.Element(ns + "ServiceManifestRef")?.Attribute("ServiceManifestName")?.Value ==
                               serviceManifestName
                               select manifestImport.Element(ns + "Policies")?.Element(ns + "EndpointBindingPolicy")?
                               .Attribute("CertificateRef")?.Value;

                var certificates = certRefs.ToList();

                if (!certificates.Any())
                {
                    throw new InvalidProgramException("Unable to find https CertificateRef");
                }

                var certRef = certificates[0];

                var eCertificates = from certificate in doc.Root?.Descendants(ns + "Certificates")
                                    let endpointCertificate = certificate.Element(ns + "EndpointCertificate")
                                                              where endpointCertificate?.Attribute("Name")?.Value == certRef
                                                              select endpointCertificate;

                var endpointCertificates = eCertificates.ToList();

                if (!endpointCertificates.Any())
                {
                    throw new InvalidProgramException("Unable to find https EndpointCertificate");
                }

                var thumbprint = endpointCertificates[0].Attribute("X509FindValue")?.Value;

                if (thumbprint == null)
                {
                    throw new InvalidProgramException("Https EndpointCertificate X509FindValue not found");
                }

                var regexItem = new Regex("^[a-zA-Z0-9 ]+$");

                if (!regexItem.IsMatch(thumbprint))
                {
                    thumbprint = fabricApplication.Application?.ApplicationParameters[thumbprint.Split('[', ']')[1]].Value;
                }

                return(GetCertificateFromStore(
                           thumbprint,
                           endpointCertificates[0].Attribute("X509StoreName")?.Value));
            }
        }
예제 #2
0
 public void Initialize()
 {
     FabricApplication.Name = context.CodePackageActivationContext.ApplicationName;
     fabricApplication      = new ServiceFabricApplication(serviceTypeName, Context);
 }