无法通过X509Store访问自签名CA

时间:2018-07-25 14:39:42

标签: docker ubuntu openssl .net-core asp.net-core-2.0

我正在Win10机器中使用LINUX容器运行dotnet core 2.1,并且 我使用openssl创建了一个自签名CA,并安装在docker计算机中。 Docker输出显示已经添加了CA。

enter image description here

当我运行以下命令时,它还会向我显示已安装的证书

awk -v cmd='openssl x509 -noout -subject' '/BEGIN/{close(cmd)};{print | cmd}' < /etc/ssl/certs/ca-certificates.crt

enter image description here

但是,无法通过X509Store访问已安装的证书

下面的代码显示计数:0

 using (var store = new X509Store(StoreName.CertificateAuthority, StoreLocation.LocalMachine))
            {
                store.Open(OpenFlags.ReadOnly);
                Console.WriteLine($"LocalMachine-> CertificateAuthority-> Count: {store.Certificates.Count}");
                foreach (var cert in store.Certificates)
                {
                    Console.WriteLine($"cert: {cert}");
                }
            }

下面的代码显示计数:151

  using (var store = new X509Store(StoreName.Root, StoreLocation.LocalMachine))
            {
                store.Open(OpenFlags.ReadOnly);
                Console.WriteLine($"LocalMachine-> Root-> Count: {store.Certificates.Count}");

                foreach (var cert in store.Certificates)
                {
                    Console.WriteLine($"cert: {cert.IssuerName.Name}");
                }
            }

但是我认为应该是152。

这是我的docker文件

FROM microsoft/dotnet:2.1-runtime AS base
WORKDIR /app

FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY TestWebApp1/TestWebApp1.csproj TestWebApp1/
RUN dotnet restore TestWebApp1/TestWebApp1.csproj
COPY . .
WORKDIR /src/TestWebApp1
RUN dotnet build TestWebApp1.csproj -c Release -o /app

FROM build AS publish
RUN dotnet publish TestWebApp1.csproj -c Release -o /app 
RUN ls -l
RUN ls certificate/ 


COPY TestWebApp1/certificate/ca.crt /usr/share/ca-certificates/ca.crt
RUN echo ca.crt >> /etc/ca-certificates.conf 

RUN ls /usr/local/share/ca-certificates/
RUN dpkg-reconfigure -p critical ca-certificates
RUN update-ca-certificates 
RUN awk -v cmd='openssl x509 -noout -subject' '/BEGIN/{close(cmd)};{print | cmd}' < /etc/ssl/certs/ca-certificates.crt

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "TestWebApp1.dll"]

任何帮助将不胜感激。

谢谢。

1 个答案:

答案 0 :(得分:2)

这似乎归因于多阶段Dockerfile。

您已将证书安装在publish映像中,但未安装在final映像中。另外,base不包括新安装的证书。

我建议

  1. 手动将证书从publish图像复制到final图像
  2. 或在dpkg-reconfigure ... update-ca-certificates阶段执行final
  3. 或将证书安装在base映像中

我的偏好是选项1。