35

X509: certificate relies on legacy Common Name field, use SANs or temporarily en...

 2 years ago
source link: http://i.lckiss.com/?p=7479
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

X509: certificate relies on legacy Common Name field, use SANs or temporarily enable Common Name matching with GODEBUG=x509ignoreCN=0)

2021-08-29

日常在周末更新相关容器,更新 potainer 2.6.3 后发现所有远程 docker 节点都无法连接了,看了下日志报错是这样的:

background schedule error (endpoint snapshot). Unable to create snapshot (endpoint=TOS, URL=tcp://host:port) (err=error during connect: Get "https://host:port/_ping": x509: certificate relies on legacy Common Name field, use SANs or temporarily enable Common Name matching with GODEBUG=x509ignoreCN=0)

从网上检索的信息大概是这个意思:Go 1.15 版本开始废弃 CommonName,因此推荐使用 SAN 证书。

想必就是potainer 更新了 Go 版本吧。虽然也有说可以暂时忽略,但既然更新就有其更新的原因,经过一上午的研究,发现真香。在我之前的文章 Portainer 使用 TLS 加密连接远端节点上的 Docker 中写得是又臭又长,我自己都看不下去了,仔细看了下,决定取其精华去其糟粕,重新生成一套证书,走你。

根证书以及私钥创建

openssl genrsa -aes256 -out ca-key.pem 4096
openssl req -new -x509 -days 365 -key ca-key.pem -sha256 -subj "/CN=$HOST" -out ca.pem
openssl genrsa -out server-key.pem 4096

自行更改根证书$HOST,同时在生成 ca-key.pem 时会要求输入密码,后面的密码确认都需要。

服务器证书签发

openssl req -new -sha256 \
    -key server-key.pem \
    -subj "/C=CN/OU=$ORG/O=$COMP/CN=$NAME" \
    -reqexts SAN \
    -config <(cat /etc/ssl/openssl.cnf \
        <(printf "\n[SAN]\nsubjectAltName=DNS:$HOST1,DNS:$HOST2")) \
    -out server.csr
openssl x509 -req -days 365 \
    -in server.csr -out server-cert.pem \
    -CA ca.pem -CAkey ca-key.pem -CAcreateserial \
    -extensions SAN \
    -extfile <(cat /etc/ssl/openssl.cnf <(printf "[SAN]\nsubjectAltName=DNS:$HOST1,DNS:$HOST2"))

需要自行修改$ORG $COMP $NAMECN 也可改,没记错的话分别对应组织,机构,机构名(哎呀,自己签发的随便写)

接着就是DNS:$HOST1,DNS:$HOST2 这就是 SAN 证书的好处,网上都会拿 google 的 tls 证书举例,其实就是支持通配符,比如我有子域名 server1.lckiss.com,server2.lckiss.com,那么我统一授权为 *.lckiss.com 即可,后面还可以继续加域名。

PS:上述命令中的 /etc/ssl/openssl.cnf,即 openssl 的配置文件路径并不一定适合所有系统,可以使用 find 命令找出自己系统中 openssl.cnf 的位置:

find / -name openssl.cnf

查看服务器证书信息

上述命令输入后,该有的文件就有了,可以看看最终的证书情况:

openssl x509 -noout -text -in server-cert.pem
...
        X509v3 extensions:
            X509v3 Subject Alternative Name: 
                DNS:*.lckiss.com, DNS:lckiss-otherhost.com
...

最终需要的是三个文件:ca.pemserver-certserver-key.pem

配置 Docker 启动服务

vi /usr/lib/systemd/system/docker.service

找到ExecStart字段在-H之前加入以下配置:

--tlsverify --tlscacert=/path/to/ca.pem --tlscert=/path/to/server-cert.pem --tlskey=/path/to/server-key.pem -H tcp://0.0.0.0:port

重启 docker ,在 portainer 中配置 Endpoint 证书即可。

systemctl daemon-reload && systemctl restart docker

这部分可以参考旧文: Portainer 使用 TLS 加密连接远端节点上的 Docker

使用openssl创建包含SAN的证书


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK