【问题标题】:Setting up AWS Secrets Manager .Net Core设置 AWS Secrets Manager .Net Core
【发布时间】:2021-09-03 22:12:40
【问题描述】:

我有一个在 EC2 实例上运行的 .Net Core 应用程序。 我想使用 Secrets Manager 来包含我的 Web 应用程序的秘密,例如“连接字符串”等。 AWS Secrets Manager 文档不是很有用,我似乎找不到一个教程来展示/解释如何在 EC2 上使用 Secrets Manager。

我已经能够使用邮递员并使用以下代码成功提取“秘密”: 但是 Access Key 和 Secrets Key 都是硬编码的。

我不希望出现这种情况。 我已安装 SDK 并将访问密钥和密钥加载到此配置文件中。

基本上我的问题是如何从 SDK 中提取访问密钥和秘密密钥来签署请求?

if (secretsDetail == null)
        {
            return "Please provide SecretsDetails.";
        }
        string secretName = "";
        string secret = "";

        MemoryStream memoryStream = new MemoryStream();
        AmazonSecretsManagerConfig amazonSecretsManagerConfig = new AmazonSecretsManagerConfig();
        amazonSecretsManagerConfig.ServiceURL = secretsDetail.ServiceURL;

        IAmazonSecretsManager client = new AmazonSecretsManagerClient(RegionEndpoint.GetBySystemName("eu-west-2"));

        GetSecretValueRequest request = new GetSecretValueRequest();
        request.SecretId = secretName;
        request.VersionStage = secretsDetail.VersionStage == null ? "AWSCURRENT" : secretsDetail.VersionStage; // VersionStage defaults to AWSCURRENT if unspecified.

        GetSecretValueResponse response = null;


        try
        {
            response = client.GetSecretValueAsync(request).Result;
        }
        catch (DecryptionFailureException)
        {
            // Secrets Manager can't decrypt the protected secret text using the provided KMS key.
            // Deal with the exception here, and/or rethrow at your discretion
            throw;
        }
        catch (InternalServiceErrorException)
        {
            // An error occurred on the server side.
            // Deal with the exception here, and/or rethrow at your discretion
            throw;
        }
        catch (InvalidParameterException)
        {
            // You provided an invalid value for a parameter.
            // Deal with the exception here, and/or rethrow at your discretion
            throw;
        }
        catch (InvalidRequestException)
        {
            // You provided a parameter value that is not valid for the current state of the resource.
            // Deal with the exception here, and/or rethrow at your discretion.
            throw;
        }
        catch (ResourceNotFoundException)
        {
            // We can't find the resource that you asked for.
            // Deal with the exception here, and/or rethrow at your discretion.
            throw;
        }
        catch (System.AggregateException)
        {
            // More than one of the above exceptions were triggered.
            // Deal with the exception here, and/or rethrow at your discretion.
            throw;
        }
        // Decrypts secret using the associated KMS CMK.
        // Depending on whether the secret is a string or binary, one of these fields will be populated.
        if (response.SecretString != null)
        {
            return secret = response.SecretString;
        }
        else
        {
            memoryStream = response.SecretBinary;
            StreamReader reader = new StreamReader(memoryStream);
            string decodedBinarySecret = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(reader.ReadToEnd()));
            return decodedBinarySecret;
        }

【问题讨论】:

    标签: amazon-web-services amazon-ec2 aws-secrets-manager


    【解决方案1】:

    在 EC2/Lambda 中使用 SDK 时,您不必提供访问/密钥。要访问 EC2 或 Lambda 内的 Secrets Manager 等服务,您可以将角色附加到资源(EC2、Lambda),并将策略附加到它。

    1 - 首先创建一个角色

    如果您已经拥有 EC2/Lambda 的角色,则可以跳过此步骤。

    选择类型(EC2):

    2 - 附加 SecretsManagerReadWrite 策略:


    3 - 将角色附加到 EC2 实例

    对于新实例,您可以选择您创建的 IAM 角色,如下所示。

    对于现有实例,选择实例并更改 IAM 角色如下:


    通过执行这些步骤,您的实例将能够使用 SDK,而无需明确提供访问/密钥。您可以稍后在需要使用新资源(例如 SQS)时将新策略附加到角色。

    【讨论】:

      【解决方案2】:

      您的问题的简短回答是您没有。 SDK 将自动为您获取凭据。 Using Credentials in an Application。您可以使用此处描述的任何方法,但 #4 是首选且最安全的选项。

      对于在 Amazon EC2 实例上运行的应用程序,存储在实例配置文件中的凭证。

      【讨论】:

      • 因此,对于在 EC2 实例上运行的应用程序,我创建了一个实例配置文件来存储凭证,我使用什么代码来检索秘密?抱歉,我无法链接所有 AWS 教程。我不知道我应该使用什么,不应该使用什么,
      • 实例配置文件并不真正存储凭证,它有一个角色,该角色用于生成临时凭证,当您使用 SDK 时,该角色将自动添加到请求中。要获取秘密,您只需使用AmazonSecretsManagerClient。在大多数情况下,您可以调用空构造函数。仅供参考,将异步操作与非异步代码结合使用时要小心。
      【解决方案3】:

      此问题的其他答案是准确的,但可能无法涵盖您拥有的所有用例。对于与 AWS 服务的连接,您可以依赖其他答案中所述的 IAM 设置。根本无需担心凭据或连接字符串!

      但假设您需要连接 AWS 之外的第 3 方服务?

      如果您想在 AWS Secrets Manager 中存储这些第 3 方服务的连接字符串或 API 密钥,您有以下几种选择:

      1. 在部署期间将这些机密作为环境变量注入到您的托管进程中。
      2. 修改您的 .NET Core 代码以访问机密。

      对于方法 #2,我发现的最好的文章是这篇文章: http://blog.travisgosselin.com/net-core-and-aws-secrets/

      它深入讨论了如何扩展 .NET Core 的配置管理器以合并 AWS Secrets Manager 机密。

      它指的是这个 NuGet 包,它是几个解决方案的基础。 https://www.nuget.org/packages/Kralizek.Extensions.Configuration.AWSSecretsManager

      亚马逊有一个使用 C# 获取机密的第一方包,在这里宣布: https://aws.amazon.com/blogs/security/how-to-use-aws-secrets-manager-client-side-caching-in-dotnet/

      您可能需要尝试几种方法才能找到最适合您的方法 - 截至 2021 年 9 月,还没有一个完整的“开箱即用”解决方案。

      【讨论】:

        猜你喜欢
        • 2020-02-28
        • 2021-10-08
        • 1970-01-01
        • 2022-07-22
        • 2020-08-23
        • 1970-01-01
        • 1970-01-01
        • 2021-03-17
        • 2022-09-23
        相关资源
        最近更新 更多