gpt4 book ai didi

azure - 从asp.net core 2.2 Web应用程序读取Azure容器实例 secret 卷

转载 作者:行者123 更新时间:2023-12-01 13:17:06 25 4
gpt4 key购买 nike

我已阅读 Microsoft 的此文档,其中描述了如何将 secret 卷添加到容器实例:

https://learn.microsoft.com/bs-latn-ba/azure///container-instances/container-instances-volume-secret

我现在想从我的 ASP.NET Core 应用程序中读取这些安全值。我怎样才能做到这一点?我在任何地方都找不到任何相关文档。

我理想地希望在我的 Startup 类中执行此配置:

这里有一些东西:

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseSerilog()
.UseSetting(WebHostDefaults.ApplicationKey, typeof(Program).GetTypeInfo().Assembly.FullName); // beware of this
// shouldn't be removed otherwise site will start outputting 404.
// see: https://github.com/aspnet/Hosting/issues/903#issuecomment-269103645
}

最后,我希望能够在本地运行代码,以便在将容器部署到 azure 之前检查它是否正常工作。有没有一种方法可以在我的本地安装上模拟/伪造这些 secret (Visual Studio 2017,解决方案启用了 docker 支持,docker 在我的计算机上本地运行)确实让我有信心一切正常?

我编辑了这个问题,以明确这是关于 secret 卷的

最佳答案

环境变量

最初的问题是关于环境变量的,所以这部分是关于使用环境变量的。

这些文章描述了如何将这些 secret 写入环境变量。要在您的应用程序中使用它们,您必须读取环境变量。 ASP.NET Core 的配置完全支持这一点:

假设您有以下配置(来自 https://learn.microsoft.com/en-us/azure/container-instances/container-instances-environment-variables#secure-values ):

apiVersion: 2018-10-01
location: eastus
name: securetest
properties:
containers:
- name: mycontainer
properties:
environmentVariables:
- name: 'NOTSECRET'
value: 'my-exposed-value'
- name: 'SECRET'
secureValue: 'my-secret-value'

以及文档中所述的默认设置:https://learn.microsoft.com/en-us/azure/container-instances/container-instances-environment-variables#secure-values

The 2.x sample app takes advantage of the static convenience method CreateDefaultBuilder to build the host, which includes a call to AddEnvironmentVariables.

你可以像这样阅读你的 secret

var secret = config.GetValue<string>("SECRET", '');

如下所述:https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.2#getvalue

secret 卷

secret 卷将包含每个 secret 值一个文件。

https://learn.microsoft.com/bs-latn-ba/azure///container-instances/container-instances-volume-secret#mount-secret-volume---yaml 中的示例为例

  volumes:
- name: secretvolume1
secret:
mysecret1: TXkgZmlyc3Qgc2VjcmV0IEZPTwo=
mysecret2: TXkgc2Vjb25kIHNlY3JldCBCQVIK

您将拥有一个包含文件“mysecret1”和“mysecret2”的目录。

您可以使用每个文件 key 配置提供程序添加这些值

config.AddKeyPerFile(directoryPath: path, optional: true);

如下所述:https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.2#key-per-file-configuration-provider

添加“配置源”后,您可以像这样访问值

 var secret = config.GetValue<string>("mysecret1", '');

关于azure - 从asp.net core 2.2 Web应用程序读取Azure容器实例 secret 卷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53871858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com