【问题标题】:Configure ClientCredential of IdentityModel using values retrieved from Cloud Foundry CredHub使用从 Cloud Foundry CredHub 检索的值配置 IdentityModel 的 ClientCredential
【发布时间】:2021-08-26 11:13:29
【问题描述】:

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

现有状况:
假设我有一个名为SystemConfig 的类用于保存配置值。我在“开发”阶段使用 json 文件和 dotnet 机密的组合来存储这些值,并且可以通过

services.Configure<SystemConfig>(Configuration.GetSection("GLOBAL"));

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

Configure(IApplicationBuilder app) 中检索
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;

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

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) 内部检索到的。
也不建议使用services.BuildServiceProvider(),因为它会创建额外的单例服务副本,可能会导致意外错误。

所以问题归结为:

  • 我们可以检索 ConfigureService(IServiceCollection services) 中的 CredHub 值吗?
  • 如果不可能,那我们可以在Configure(IApplicationBuilder app)中设置AddAccessTokenManagement吗?

【问题讨论】:

    标签: .net-5 identitymodel steeltoe


    【解决方案1】:

    我无法在这里谈论 IdentityModel 的复杂性,一眼看去,我没有看到任何让这方面变得容易的东西,但我有一个应该可以通过自定义方式使用 Steeltoe 连接器的解决方案 @ 987654321@可以定位到。

    理想情况下,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"]
        }]
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-04
      • 2019-10-19
      • 2015-03-28
      • 1970-01-01
      • 2018-02-15
      • 2017-06-13
      相关资源
      最近更新 更多