从 asp.netcore 2.2 网络应用程序读取 Azure 容器实例机密卷
Reading Azure container instance secret volumes from asp.netcore 2.2 web app
我已阅读 Microsoft 的文档,其中描述了如何将机密卷添加到容器实例:
https://docs.microsoft.com/bs-latn-ba/azure///container-instances/container-instances-volume-secret
我现在想从我的 asp.net 核心应用程序中读取这些安全值。我怎样才能做到这一点?我在任何地方都找不到这方面的任何文档。
我希望在我的启动程序中执行此配置 class:
这里有东西:
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 之前检查它是否正常工作。有没有办法在我的本地安装上 mock/fake 这些秘密(visual studio 2017,解决方案启用了 docker 支持,docker 在我的本地 运行ning机器)让我相信一切正常吗?
我已经编辑了这个问题以明确这是关于秘密卷的
环境变量
原来的问题是关于环境变量的,所以这部分是关于使用环境变量的。
这些文章描述了如何将这些秘密写入环境变量。要在您的应用程序中使用它们,您必须阅读环境变量。 asp.net 核心配置完美支持:
假设您有以下配置(来自 https://docs.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://docs.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.
你可以这样阅读你的秘密
var secret = config.GetValue<string>("SECRET", '');
密卷
机密卷将包含每个机密值一个文件。
中的示例为例
volumes:
- name: secretvolume1
secret:
mysecret1: TXkgZmlyc3Qgc2VjcmV0IEZPTwo=
mysecret2: TXkgc2Vjb25kIHNlY3JldCBCQVIK
您将拥有一个包含文件 "mysecret1" 和 "mysecret2".
的目录
您可以使用 Key-per-file 配置提供程序添加这些值
config.AddKeyPerFile(directoryPath: path, optional: true);
添加 "configuration sources" 后,您可以像这样访问这些值
var secret = config.GetValue<string>("mysecret1", '');
我已阅读 Microsoft 的文档,其中描述了如何将机密卷添加到容器实例:
https://docs.microsoft.com/bs-latn-ba/azure///container-instances/container-instances-volume-secret
我现在想从我的 asp.net 核心应用程序中读取这些安全值。我怎样才能做到这一点?我在任何地方都找不到这方面的任何文档。
我希望在我的启动程序中执行此配置 class:
这里有东西:
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 之前检查它是否正常工作。有没有办法在我的本地安装上 mock/fake 这些秘密(visual studio 2017,解决方案启用了 docker 支持,docker 在我的本地 运行ning机器)让我相信一切正常吗?
我已经编辑了这个问题以明确这是关于秘密卷的
环境变量
原来的问题是关于环境变量的,所以这部分是关于使用环境变量的。
这些文章描述了如何将这些秘密写入环境变量。要在您的应用程序中使用它们,您必须阅读环境变量。 asp.net 核心配置完美支持:
假设您有以下配置(来自 https://docs.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://docs.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.
你可以这样阅读你的秘密
var secret = config.GetValue<string>("SECRET", '');
密卷
机密卷将包含每个机密值一个文件。
中的示例为例 volumes:
- name: secretvolume1
secret:
mysecret1: TXkgZmlyc3Qgc2VjcmV0IEZPTwo=
mysecret2: TXkgc2Vjb25kIHNlY3JldCBCQVIK
您将拥有一个包含文件 "mysecret1" 和 "mysecret2".
的目录您可以使用 Key-per-file 配置提供程序添加这些值
config.AddKeyPerFile(directoryPath: path, optional: true);
添加 "configuration sources" 后,您可以像这样访问这些值
var secret = config.GetValue<string>("mysecret1", '');