gpt4 book ai didi

docker - 从 env var 获取 OAuth 设置,并在键中使用句号

转载 作者:行者123 更新时间:2023-12-02 19:20:00 25 4
gpt4 key购买 nike

我正在使用 docker linux 容器来运行我的 servicestack 应用程序,并且我需要能够从 docker-compose.yml 中定义的环境变量中读取 OAuth key 。

由于变量名中的句号,这似乎是不可能的。

例如在 OAuthProvider.cs ( https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack/Auth/OAuthProvider.cs ) 第 26 行:

this.ConsumerKey = appSettings.GetString($"oauth.{oAuthProvider}.{consumerKeyName}");

例如,它正在读取 key oauth.google.ConsumerKey .

Linux 不支持环境变量中的句号。使用调试器,我可以看到,如果我将变量放入如下:
environment:
- oauth.RedirectUrl=http://example.com
- oauth.CallbackUrl=http://example.com/auth/{0}
- oauth.basecamp.ConsumerKey=fgshghfdhfghgfdhf
- oauth.basecamp.ConsumerSecret=fdghfdghdfghgdfhdfghfgd

然后它们被移除。我做了一些研究,这是一个常见问题,如果 env var 有一个句号,那么它就会被删除。我找不到任何解决方法。

您知道如何使用 docker 环境变量传递这些硬编码设置吗?

这是我的整个 dockerfile 的表示以供引用:
ARG BUILD_MODE=Release
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
RUN apt-get update && apt-get install openssh-server unzip -y
RUN curl -sSL https://aka.ms/getvsdbgsh | /bin/sh /dev/stdin -v latest -l ~/vsdbg
COPY sshd_config /etc/ssh/
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["Project/Project.csproj", "Project/"]
COPY ["Project.ServiceModel/Project.ServiceModel.csproj", "Project.ServiceModel/"]
COPY ["Project.ServiceInterface/Project.ServiceInterface.csproj", "Project.ServiceInterface/"]
COPY ["NuGet.config", "NuGet.config"]
RUN dotnet restore "Project/Project.csproj"
COPY . .
WORKDIR "/src/Project"
RUN dotnet build "Project.csproj" -c "$BUILD_MODE" -o /app/build

FROM build AS publish
RUN dotnet publish "Project.csproj" -c "$BUILD_MODE" -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT service ssh start && echo "root:$SSH_PASSWORD" | chpasswd && dotnet Project.dll

这是 docker-compose:
version: "3.7"
services:
project:
build:
context: E:\project\
dockerfile: E:\project\project\Dockerfile
image: project:local
ports:
- "57008:80"
- "57009:443"
- "57001:2222"
restart: always
depends_on:
- "db"
environment:
- oauth.RedirectUrl=http://example.com
- oauth.CallbackUrl=http://example.com/auth/{0}
- oauth.basecamp.ConsumerKey=fgshghfdhfghgfdhf
- oauth.basecamp.ConsumerSecret=fdghfdghdfghgdfhdfghfgd
db:
image: postgres:10.9
restart: always
environment:
POSTGRES_PASSWORD: fdgdfgdfgdf
POSTGRES_USER: project
ports:
- "5445:5432"

最佳答案

您可以提供自己的定制AppSettings provider或使用 DictionarySettings并使用适合在 Docker 中使用的映射环境变量填充它,例如:

在 Docker 中使用下划线分隔符:

environment:
- oauth_RedirectUrl=http://example.com
- oauth_CallbackUrl=http://example.com/auth/{0}
- oauth_basecamp_ConsumerKey=fgshghfdhfghgfdhf
- oauth_basecamp_ConsumerSecret=fdghfdghdfghgdfhdfghfgd

然后使用 ServiceStack 期望的键创建一个新的 Dictionary AppSettings,例如:
string env(string key) => 
Environment.GetEnvironmentVariable(key.Replace(".","_"));

var envSettings = new DictionarySettings(new Dictionary<string,string> {
["oauth.RedirectUrl"] = env("oauth.RedirectUrl"),
["oauth.CallbackUrl"] = env("oauth.CallbackUrl"),
["oauth.basecamp.ConsumerKey"] = env("oauth.basecamp.ConsumerKey"),
["oauth.basecamp.ConsumerSecret"] = env("oauth.basecamp.ConsumerSecret"),
});

然后在您的身份验证提供程序中使用它,例如:
new MyAuthProvider(envSettings)

关于docker - 从 env var 获取 OAuth 设置,并在键中使用句号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60728825/

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