« SecretsManager » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
(Page créée avec « Category:AWS = Load the secrets into a dictionary = <kode lang='cs'> var secretsManagerConfig = new AmazonSecretsManagerConfig() { Profile = new Profile("MyProfile"), RegionEndpoint = RegionEndpoint.EUCentral1 }; var secretsManager = new AmazonSecretsManagerClient(secretsManagerConfig); var request = new GetSecretValueRequest { SecretId = "MySecretName" }; var response = await secretsManager.GetSecretValueAsync(request); var secret = JsonConver... »)
 
Aucun résumé des modifications
Ligne 1 : Ligne 1 :
[[Category:AWS]]
[[Category:AWS]]
= Links =
* [https://docs.aws.amazon.com/secretsmanager/latest/userguide/retrieving-secrets_cache-net.html Secrets Manager Cache]
* [https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/csharp_secrets-manager_code_examples.html Secrets Manager]
= Load the secrets into a dictionary =
= Load the secrets into a dictionary =
<kode lang='cs'>
<kode lang='cs'>
Ligne 20 : Ligne 24 :
return secret;
return secret;
</kode>
</kode>
= [https://aws.amazon.com/blogs/modernizing-with-aws/how-to-load-net-configuration-from-aws-secrets-manager/ Load .NET configuration from Secrets Manager] =
<filebox fn='AmazonSecretsManagerConfigurationProvider.cs' collapsed>
public class AmazonSecretsManagerConfigurationProvider : ConfigurationProvider
{
    private readonly string secretName;
    public AmazonSecretsManagerConfigurationProvider(string secretName)
    {
        this.secretName = secretName;
    }
    public override void Load()
    {
        var secret = GetSecret();
        Data = JsonSerializer.Deserialize<Dictionary<string, string>>(secret)!;
    }
    private string GetSecret()
    {
        var request = new GetSecretValueRequest
        {
            SecretId = this.secretName
        };
        using (var client = new AmazonSecretsManagerClient())
        {
            var response = client.GetSecretValueAsync(request).Result;
            return response.SecretString;
        }
    }
}
</filebox>
<filebox fn='AmazonSecretsManagerConfigurationSource.cs' collapsed>
public class AmazonSecretsManagerConfigurationSource : IConfigurationSource
{
    private readonly string secretName;
    public AmazonSecretsManagerConfigurationSource(string secretName)
    {
        this.secretName = secretName;
    }
    public IConfigurationProvider Build(IConfigurationBuilder builder)
    {
        return new AmazonSecretsManagerConfigurationProvider(this.secretName);
    }
}
</filebox>
<filebox fn='ConfigurationBuilderExtensions.cs' collapsed>
public static class ConfigurationBuilderExtensions
{
    public static void AddAmazonSecretsManager(
        this IConfigurationBuilder configurationBuilder, string secretName)
    {
        var configurationSource = new AmazonSecretsManagerConfigurationSource(secretName);
        configurationBuilder.Add(configurationSource);
    }
}
</filebox>
<filebox fn='Program.cs'>
builder.Configuration.AddAmazonSecretsManager("Secret name");
var secretValue = builder.Configuration["Secret key"];
</filebox>

Version du 10 juin 2024 à 16:00

Links

Load the secrets into a dictionary

Cs.svg
var secretsManagerConfig = new AmazonSecretsManagerConfig()
{
    Profile = new Profile("MyProfile"),
    RegionEndpoint = RegionEndpoint.EUCentral1
};
var secretsManager = new AmazonSecretsManagerClient(secretsManagerConfig);

var request = new GetSecretValueRequest
{
    SecretId = "MySecretName"
};

var response = await secretsManager.GetSecretValueAsync(request);

var secret = JsonConvert.DeserializeObject<Dictionary<string, string>>(response.SecretString);

return secret;

Load .NET configuration from Secrets Manager

AmazonSecretsManagerConfigurationProvider.cs
public class AmazonSecretsManagerConfigurationProvider : ConfigurationProvider
{
    private readonly string secretName;

    public AmazonSecretsManagerConfigurationProvider(string secretName)
    {
        this.secretName = secretName;
    }

    public override void Load()
    {
        var secret = GetSecret();
        Data = JsonSerializer.Deserialize<Dictionary<string, string>>(secret)!;
    }

    private string GetSecret()
    {
        var request = new GetSecretValueRequest
        {
            SecretId = this.secretName
        };

        using (var client = new AmazonSecretsManagerClient())
        {
            var response = client.GetSecretValueAsync(request).Result;
            return response.SecretString;
        }
    }
}
AmazonSecretsManagerConfigurationSource.cs
public class AmazonSecretsManagerConfigurationSource : IConfigurationSource
{
    private readonly string secretName;

    public AmazonSecretsManagerConfigurationSource(string secretName)
    {
        this.secretName = secretName;
    }

    public IConfigurationProvider Build(IConfigurationBuilder builder)
    {
        return new AmazonSecretsManagerConfigurationProvider(this.secretName);
    }
}
ConfigurationBuilderExtensions.cs
public static class ConfigurationBuilderExtensions
{
    public static void AddAmazonSecretsManager(
        this IConfigurationBuilder configurationBuilder, string secretName)
    {
        var configurationSource = new AmazonSecretsManagerConfigurationSource(secretName);
        configurationBuilder.Add(configurationSource);
    }
}
Program.cs
builder.Configuration.AddAmazonSecretsManager("Secret name");

var secretValue = builder.Configuration["Secret key"];