从env var获取OAuth设置,并在键中输入句号

问题描述 投票:1回答:1

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

由于变量名中的句号,因此似乎无法执行此操作。

例如,在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

然后将它们删除。我做了一些研究,这是一个普遍的问题,如果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"
docker servicestack
1个回答
1
投票

您可以提供自己的自定义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)
© www.soinside.com 2019 - 2024. All rights reserved.