使用从 Cloud Foundry CredHub 检索的值配置 IdentityModel 的 ClientCredential

Configure ClientCredential of IdentityModel using values retrieved from Cloud Foundry CredHub

简短的问题陈述:
如何从 Cloud Foundry CredHub 获取价值以供内部使用 ConfigureServices(IServiceCollection services)?

现有条件:
假设我有一个名为 SystemConfig 的 class 用于保存配置值。我在 'Development' 阶段使用 json 文件和 dotnet 秘密的组合来存储这些值,并且可以通过

检索它们
services.Configure<SystemConfig>(Configuration.GetSection("GLOBAL"));

在 'SIT' 阶段,我使用 Pivotal Cloud Foundry 来托管应用程序,并结合使用 ConfigServer - CredHub 来存储配置值。可以通过

Configure(IApplicationBuilder app) 中检索来自 CredHub 的值
var credhub = app.ApplicationServices.GetService<IOptions<CloudFoundryServicesOptions>>().Value.Services["credhub"].First(x => x.Name.Equals("credhub-instance"));
var sysConfig = app.ApplicationServices.GetService<IOptions<SystemConfig>>().Value;
sysConfig.SAMPLE_A = credhub.Credentials[nameof(sysConfig.SAMPLE_A)].Value;

当我需要从 ConfigureServices 中的 IdentityModel.AspNetCore 配置访问令牌管理时出现问题:

services.AddAccessTokenManagement(x => 
{
     x.Client.Clients.Add("key_sample", new IdentityModel.Client.ClientCredentialsTokenRequest()
     {
         Address = "sysConfig.SAMPLE_A",
         ClientId = "taken from sysConfig as well",
         ClientSecret = "taken from sysConfig as well"
     });
});

方法 AddAccessTokenManagement 只能在 ConfigureService(IServiceCollection services) 中使用,但同时我还没有来自 CredHub 的值,因为它们是在 Configure(IApplicationBuilder app).[=37 中检索的=] 也不推荐使用 services.BuildServiceProvider(),因为它会创建可能导致意外错误的额外副本服务。

所以问题归结为:

我不能在这里谈论 IdentityModel 的复杂性,乍看之下我没有看到任何使这方面变得容易的东西,但我有一个应该工作的解决方案,它通过自定义使用 Steeltoe 连接器方式SsoServiceInfo可以定位。

理想情况下,Steeltoe 会自动将这些凭据绑定到 SsoServiceInfo,但目前没有任何内置的东西可以建立这种连接。当我们在这里完成时,此代码将用于检索服务信息:

services.AddAccessTokenManagement(x =>
{
    var creds = Configuration.GetServiceInfo<SsoServiceInfo>("credhub-instance");
    x.Client.Clients.Add("key_sample", new IdentityModel.Client.ClientCredentialsTokenRequest()
    {
        Address = creds.AuthDomain,
        ClientId = creds.ClientId,
        ClientSecret = creds.ClientSecret
    });
});

为了启用上述代码,我们需要添加一个自定义 ServiceInfoFactory,它可以将您的 credhub 服务实例绑定到 SsoServiceInfo:

using Steeltoe.Connector.Services;
using Steeltoe.Extensions.Configuration;

public class AuthServiceInfoFactory : ServiceInfoFactory
{
    public AuthServiceInfoFactory()
        : base(new Tags("credhub"), "credhub")
         // second param here for url scheme won't actually be used but can't be empty
    {
    }

    public override IServiceInfo Create(Service binding)
    {
        // these methods ship with Steeltoe, but look for "client_id" and "client_secret" - customize as desired
        var clientId = GetClientIdFromCredentials(binding.Credentials);
        var clientSecret = GetClientSecretFromCredentials(binding.Credentials);
        var authDomain = GetStringFromCredentials(binding.Credentials, "auth_domain");

        return new SsoServiceInfo(binding.Name, clientId, clientSecret, authDomain);
    }
}

接下来将 ServiceInfoFactoryAssembly 属性添加到 AssemblyInfo.cs 以便 Steeltoe 可以找到您的新工厂:

using Steeltoe.Connector;

[assembly: ServiceInfoFactoryAssembly]

此外,连接器构建于 CloudFoundry 配置提供程序之上,因此请确保您已将其添加到您的主机构建器或配置构建器中,如下所示:

Host.CreateDefaultBuilder(args)
     .AddCloudFoundryConfiguration();

最后,请注意,从 v3.0 开始,除非已设置 vcap:application,否则 Steeltoe 将无法识别 vcap:services,因此您可能希望在非 CloudFoundry 环境中使用此配置格式:

  "services": {
    "credhub": [{
      "name": "credhub-instance",
      "credentials": {
        "Client_Id": "myClientId",
        "Client_Secret": "myClientSecret"
      },
      "tags": ["credhub"]
    }]
  }