从 env var 获取 OAuth 设置
Getting OAuth settings from env var with full stop in key
我正在使用 docker linux 容器到 运行 我的 servicestack 应用程序,我需要能够从我的 docker 中定义的环境变量中读取 OAuth 密钥-compose.yml.
由于变量名中的句号,似乎无法执行此操作。
例如在 OAuthProvider.cs (https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack/Auth/OAuthProvider.cs) 第 26 行:
this.ConsumerKey = appSettings.GetString($"oauth.{oAuthProvider}.{consumerKeyName}");
它正在读取例如键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
然后他们被删除了。我做了一些研究,这是一个常见问题,如果环境变量有一个句号,那么它就会被删除。我找不到任何解决方法。
您知道我如何使用 docker 环境变量传递这些硬编码设置吗?
这是我的整个 docker 文件的表示以供参考:
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-撰写:
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"),
});
然后在您的 Auth Provider 中使用它,例如:
new MyAuthProvider(envSettings)
我正在使用 docker linux 容器到 运行 我的 servicestack 应用程序,我需要能够从我的 docker 中定义的环境变量中读取 OAuth 密钥-compose.yml.
由于变量名中的句号,似乎无法执行此操作。
例如在 OAuthProvider.cs (https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack/Auth/OAuthProvider.cs) 第 26 行:
this.ConsumerKey = appSettings.GetString($"oauth.{oAuthProvider}.{consumerKeyName}");
它正在读取例如键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
然后他们被删除了。我做了一些研究,这是一个常见问题,如果环境变量有一个句号,那么它就会被删除。我找不到任何解决方法。
您知道我如何使用 docker 环境变量传递这些硬编码设置吗?
这是我的整个 docker 文件的表示以供参考:
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-撰写:
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"),
});
然后在您的 Auth Provider 中使用它,例如:
new MyAuthProvider(envSettings)