简体   繁体   English

GO - Docker 询问 K8S 容器上的证书

[英]GO - Docker ask certificate on K8S container

I use the following code with this lib我在这个库中使用以下代码

provider, err := oidc.NewProvider(ctx, providerURI)
if err != nil {
    log.Println(err)
}

While running it locally with same providerURI it works , I was able to get the provider successfully!使用相同的 providerURI在本地运行它时,它可以工作,我能够成功地获得提供者!

I deployed it to K8S with the exact same provider url (as env variable ) and debug it using port-forwarding , However, in k8S I got error and dont get the provider .我使用完全相同的提供程序 url env作为环境变量)将它部署到 K8S 并使用port-forwarding对其进行调试,但是,在 k8S 中我得到了错误并且没有得到provider

The error is:错误是:

2020/08/14 16:42:22 Get "https://ace.svar.com/.well-known/openid-configuration": x509: certificate signed by unknown authority

I've added the certificate to the image and verify it , I exec into k8s container after deploy and I see the server.crt file under /usr/local/share/ca-certificates/ path.我已将证书添加到映像并验证它,部署后我执行到exec容器中,我在/usr/local/share/ca-certificates/路径下看到server.crt文件。

And still got the same error, any idea if I miss here something else... Not sure if it really related to the OIDC lib or something more general..并且仍然得到同样的错误,如果我在这里错过了其他的任何想法......不确定它是否真的与 OIDC lib 或更一般的东西有关..

FROM golang:1.14.7 AS builder
RUN go get github.com/go-delve/delve/cmd/dlv
ADD server.crt /usr/local/share/ca-certificates/server.crt
RUN chmod 644 /usr/local/share/ca-certificates/server.crt && update-ca-certificates
RUN mkdir /app

ADD . /app
WORKDIR /app
RUN CGO_ENABLED=0 GOOS=linux go build -gcflags="all=-N -l" -o main ./...

FROM debian:buster AS production
COPY --from=builder /app .
COPY --from=builder /go/bin/dlv /
COPY --from=builder /usr/local/share/ca-certificates/ /usr/local/share/ca-certificates/
EXPOSE 8000 40000
ENV SSL_CERT_DIR=/usr/local/share/ca-certificates/
ENV PORT=8000
CMD ["/dlv", "--listen=:40000", "--headless=true", "--api-version=2", "--accept-multiclient", "exec", "./main"]

I got replay from the author of the GO-OIDC repository to try to use我从GO-OIDC存储库的作者那里得到了重播以尝试使用

https://godoc.org/github.com/coreos/go-oidc#ClientContext https://godoc.org/github.com/coreos/go-oidc#ClientContext

Not sure how, any idea?不知道如何,有什么想法吗?

From oidc.ClientContext docs it shows how to pass in a custom http.Client :oidc.ClientContext文档中,它显示了如何传入自定义http.Client

myClient := &http.Client{}
ctx := oidc.ClientContext(parentContext, myClient)

// This will use the custom client
provider, err := oidc.NewProvider(ctx, "https://accounts.example.com")

providing a custom http.Client allows for custom TLS handling.提供自定义http.Client允许自定义 TLS 处理。


TLS Config TLS 配置

To create a http.Client with a specific CA-trust file, I employ these helper functions:为了创建一个带有特定 CA 信任文件的http.Client ,我使用了这些帮助函数:

func tlsConf(trustfile string) (t *tls.Config, err error) {
    if trustfile == "" {
        // DON'T USE IN PRODUCTION (but handy for testing)
        t = &tls.Config{InsecureSkipVerify: true}
        return
    }

    pembody, err := ioutil.ReadFile(trustfile)
    if err != nil {
        err = fmt.Errorf("failed to load trust file %q: %w", trustfile, err)
        return
    }

    rootCAs := x509.NewCertPool()
    if ok := rootCAs.AppendCertsFromPEM(pembody); !ok {
        err = fmt.Errorf("invalid PEM file %q", trustfile)
        return

    }

    t = &tls.Config{RootCAs: rootCAs}
    return
}

and:和:

func httpCli(trustfile string) (hc *http.Client, err error) {
    tc, err := tlsConf(trustfile)
    if err != nil {
        return
    }
    hc = &http.Client{Transport: &http.Transport{TLSClientConfig: tc}}
    return
}

So to use the above with the OIDC package for a quick test:因此,要将上述内容与 OIDC package 一起使用进行快速测试:

hc, err := httpCli("") // DON'T USE IN PRODUCTION - will trust any TLS cert

ctx := oidc.ClientContext(parentContext, hc)
provider, err := oidc.NewProvider(ctx, "https://accounts.example.com")

If this works, then add the correct trust file to your app:如果可行,则将正确的信任文件添加到您的应用程序中:

hc, err := httpCli("/usr/local/share/ca-certificates/server.crt"))

CA Trust加州信托

If your server.crt trust file is not working, you may have the wrong subject/issuer listed.如果您的server.crt信任文件不起作用,您可能列出了错误的主题/颁发者。

To know for sure, you can grab the trust cert (and optional signing chain) from any remote server (port 443 is the default https port):要确定,您可以从任何远程服务器获取信任证书(和可选的签名链)(端口 443 是默认的 https 端口):

echo | openssl s_client -connect ace.svar.com:443 -showcerts 2> /dev/null > ace.txt

Since I don't know what your infrastructure looks like, I'll use the example output from google.com:443 :由于我不知道您的基础架构是什么样的,因此我将使用来自google.com:443的示例 output :

---
Certificate chain
 0 s:/C=US/ST=California/L=Mountain View/O=Google LLC/CN=*.google.com
   i:/C=US/O=Google Trust Services/CN=GTS CA 1O1
-----BEGIN CERTIFICATE-----
MIIKIzCCCQugAwIBAgIQF9rkH7fB/M4IAAAAAE2d0TANBgkqhkiG9w0BAQsFADBC
MQswCQYDVQQGEwJVUzEeMBwGA1UEChMVR29vZ2xlIFRydXN0IFNlcnZpY2VzMRMw
...
-----END CERTIFICATE-----

The s: indicates the subject of a cert - and you can see the server name is identified by the wildcard CN=*.google.com . s:表示证书的主题 - 您可以看到服务器名称由通配符CN=*.google.com If you see something similar within ace.txt - your server.crt should include these lines (starting at BEGIN CERTIFICATE and ending with END CERTIFICATE ).如果您在ace.txt中看到类似的内容 - 您的server.crt应该包含这些行(从BEGIN CERTIFICATE开始,以END CERTIFICATE结束)。

You may also note the i: line indicates the issuer cert name.您可能还注意到i:行表示颁发者证书名称。 If this is the same name as s: - then it is self-signed cert and you are done.如果这与s: - 同名,则它是自签名证书,您就完成了。

In the google.com:443 example the subject ( s: ) differs from the issuer ( i: ).google.com:443示例中,主题 ( s: :) 与颁发者 ( i: :) 不同。 So instead of trusting the subject cert - one can trust the issuer cert instead - allowing potentially multiple servers to be trust.因此,与其信任主题证书——人们可以信任颁发者证书——而是允许可能信任多个服务器。 Since the subject cert is signed by that issuer - the chain-of-trust is complete.由于主题证书是由该颁发者签署的 - 信任链是完整的。

Golang standard ssl library is looking for certificates in the following directories: https://github.com/golang/go/blob/master/src/crypto/x509/root_unix.go#L18-L37 && https://github.com/golang/go/blob/master/src/crypto/x509/root_linux.go#L8-L15 , if you want to look it up in the new location, you can use environment variables: SSL_CERT_FILE or SSL_CERT_DIR and set location of your certificate. Golang standard ssl library is looking for certificates in the following directories: https://github.com/golang/go/blob/master/src/crypto/x509/root_unix.go#L18-L37 && https://github.com /golang/go/blob/master/src/crypto/x509/root_linux.go#L8-L15 ,如果要在新位置查找,可以使用环境变量: SSL_CERT_FILESSL_CERT_DIR并设置证书的位置. So in your case it would be:因此,在您的情况下,它将是:

export SSL_CERT_DIR=/usr/local/share/ca-certificates/

and then run your application.然后运行您的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM