ssl - ServiceFabric 本地集群 SSL

标签 ssl openssl localhost kestrel azure-service-fabric

我对在本地主机上使用 SSL 在本地集群上运行 SF 有一些误解。 Microsoft 创建了 greate article about configuring HTTPS on your endpoints 但只有 如果您使用他们的证书生成器 CertSetup.ps1 才能正常工作。如果您尝试安装您自己的 pfx,它将不起作用。

首先 我通过 OpenSSL 创建了本地主机自签名证书:

set OPENSSL_CONF=W:\OpenSSL-Win32\bin\openssl.cfg
openssl genrsa -out W:\CERTS\wepapissl.key -passout pass:1234567890 -aes256 2048
openssl req -x509 -new -key W:\CERTS\wepapissl.key -days 10000 -out W:\CERTS\wepapissl.crt -passin pass:1234567890 -subj /CN="localhost"
openssl pkcs12 -export -inkey W:\CERTS\wepapissl.key -in W:\CERTS\wepapissl.crt -out W:\CERTS\wepapissl.pfx -passout pass:0987654321 -passin pass:1234567890`

其次 我创建了默认的 ASP.NET Core Web 应用程序(Core 2.0,API 模板)。并添加了配置 Kestrel 以使用 HTTPS 的代码:

public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseKestrel(opt =>
            {
                opt.Listen(IPAddress.Any, port, listenOptions =>
                {
                    listenOptions.UseHttps(GetCertificateFromStore());
                });
            })
            .UseStartup<Startup>()
            .Build();

private static X509Certificate2 GetCertificateFromStore()
    {
        var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
        try
        {
            store.Open(OpenFlags.ReadOnly);
            var certCollection = store.Certificates;
            var currentCerts = certCollection.Find(X509FindType.FindBySubjectDistinguishedName, "CN=localhost", false);
            return currentCerts.Count == 0 ? null : currentCerts[0];
        }
        finally
        {
            store.Close();
        }
    }

我得到了预期的结果。带有关于网站安全证书警告的页面: Result from ValueController with warning

第三 我创建了 Service Fabric 应用程序(无状态 ASP.NET Core 模板)。通过编辑 Endpoint 部分更改我的 ServiceManifest.xml:

<Endpoint Protocol="https" Name="ServiceEndpoint" Type="Input" Port="8256" />

并添加了配置 Kestrel 以使用 HTTPS 的代码(class Web1 : StatelessService):

    protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
    {
        return new ServiceInstanceListener[]
        {
            new ServiceInstanceListener(serviceContext =>
                new KestrelCommunicationListener(serviceContext, "ServiceEndpoint", (url, listener) =>
                {
                    ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting Kestrel on {url}");

                    return new WebHostBuilder()
                                .UseKestrel(opt =>
                                {
                                    int port = serviceContext.CodePackageActivationContext.GetEndpoint("ServiceEndpoint").Port;
                                    opt.Listen(IPAddress.IPv6Any, port, listenOptions => 
                                    {
                                        listenOptions.UseHttps(this.GetCertificateFromStore());
                                    });
                                })
                                .ConfigureServices(
                                    services => services
                                        .AddSingleton<StatelessServiceContext>(serviceContext))
                                .UseContentRoot(Directory.GetCurrentDirectory())
                                .UseStartup<Startup>()
                                .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
                                .UseUrls(url)
                                .Build();
                }))
        };
    }

    private X509Certificate2 GetCertificateFromStore()
    {
        var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
        try
        {
            store.Open(OpenFlags.ReadOnly);
            var certCollection = store.Certificates;
            var currentCerts = certCollection.Find(X509FindType.FindBySubjectDistinguishedName, "CN=localhost", false);
            return currentCerts.Count == 0 ? null : currentCerts[0];
        }
        finally
        {
            store.Close();
        }
    }

结果:在本地 SF 集群上成功构建和部署代码。 But my resource can't be reached

附言我再重复一遍,如果您使用 Mircosoft 提供的 PowerShell 安装新证书 - CertSetup.ps1,它适用于 SF 应用程序。我试图挖掘 PS 脚本,但我不明白我错过了什么。

P.P.S 我是创建证书的新手,但这看起来很奇怪。

  1. 我已经通过 CertSetup.ps1 安装了 pfx。 一切正常(资源可访问)。
  2. 然后我用私钥所有扩展属性将证书导出到pfx
  3. 从 LocalMachine(MY 和 Root)、CurrentUser (MY) 存储中删除
  4. 将导出的 pfx 安装到 LocalMachine(我的和 Root)、CurrentUser(我的)商店
  5. 重建和重新部署代码
  6. 无法访问资源

这是魔法吗?或者我错过了什么?

最佳答案

无论如何,我对几个细节还不够清楚。回答: 如果您尝试使用自己生成的证书(openssl、makecert 等),您应该为 NETWORK SERVICE 设置权限。

要在您的开发箱上手动执行此操作,请打开 certlm.msc,展开个人 -> 证书,然后右键单击您的证书。选择All Tasks->Manage private keys然后添加NETWORK SERVICE。

更多信息:https://github.com/Azure/service-fabric-issues/issues/714#issuecomment-381281701

关于ssl - ServiceFabric 本地集群 SSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50896250/

相关文章:

flash - Adobe AIR 中的 SSL 客户端证书身份验证

android - 如何解决 "No certificates found - The app Chrome has requested a certificate"Android/Google Chrome 问题

python - 使用带有 SSL 的 ibm_db 连接到 DB2 时出错

ssl - OpenSSL 通配符证书和主机名证书

macos - 不能用 openssl 满足 Plone 的 Unified Installer 预检

linux - 构建 PostgreSQL 时链接器错误 "undefined reference to SSL_get_peer_certificate"

c++ - 为什么每个架构的 opensslconf.h 不同?

php - 从本地开发服务器进行远程数据库管理

linux - 如何临时将名称解析为本地主机端口?

javascript - FB.login 仅适用于我的开发者帐户