Kralizek / AWSSecretsManagerConfigurationExtensions

This repository contains a provider for Microsoft.Extensions.Configuration that retrieves secrets stored in AWS Secrets Manager.
MIT License
219 stars 43 forks source link

How to access secrets json and bind to a class? #56

Closed goors closed 3 years ago

goors commented 3 years ago

If i have secret called Config and in it plain:

{
  "ConectionStrings": {

    "UserDb": "...."
  }
}
.ConfigureServices((hostContext, services) =>

  services.AddOptions<DataAccessOptions>().Bind(hostContext.Configuration);
)

Where DataAccessOptions is a class with:

public class DataAccessOptions
    {
        public ConfigOptions Config { get; set; }

        public class ConfigOptions
        {
            public DataAccessConnectionString ConnectionStrings { get; set; }
        }

        public class DataAccessConnectionString
        {
            public string UserDb { get; set; }

        }
}

As far as I can tell this does not work.

This does not work as well.

var options = new DataAccessOptions(); hostContext.Configuration.GetSection("Config").Bind(options);

this does not work hostContext.Configuration.GetSection("Config");

Only thing that works is:

hostContext.Configuration.GetValue<string>("Config:ConnectionStrings:UserDb");

And that kind of is not a way. Imagine i got N of stuff in secret. Then I have to initiate class and fill in all of that stuff manually like:

var options = new DataAccessOptions {
     Config = new ConfigOptions {
        ConnectionStrings = new DataAccessConnectionString {
             UserDb = hostContext.Configuration.GetValue<string>("Config:ConnectionStrings:UserDb");
             // and N other stuff in here
        }
     }

}

Is there a way to bind secrets to a class and not to do that get one at the time.

On Azure when you define secret "Config:ConnectionStrings:UserDb" syntax is recognised like section on you can unpack in one line of code.

goors commented 3 years ago

I am sorry. So stupid class is wrong. Config is extra property that is not needed. And hostContext.Configuration.GetSection("Config"); this Osx debugger is now showing value ....