局域网部署服务解决证书问题

time:2024/07/1

问题场景

这里补充了证书知识,绕了一大圈

对于非公网部署的环境,都会涉及到一个问题,访问时遇到证书问题,场景有:

1:web访问,需要点击:高级=》继续访问,才能继续访问

2:git访问,http连接或者https带证书连接,需要强制指定http或者跳过tls验证。

3:访问不验证证书存在安全隐患。

在访问比较多的环境下。比如公司内部,如果每个人都要面临同样的问题,同样的操作重复在每台设备上设置,这是什么感觉!!!

我们想要像访问互联网服务那样访问内网部署的服务,怎么解决这个问题。

这里踩了一些坑

开始我以为创建个证书,导入到gitlab,在导入到浏览器,就可以了,但是按照想法操作后,发现浏览器还是报安全错误,gitlaber runner添加还是报tls错误(不在这里解决)。这里还卡了2天,涉及证书类似的问题还有harber,虽然harber是可以修改为http,但是后续会统一解决证书问题

CA证书:根证书。问题就出现在这里,gitlab runner报错

tls: failed to verify certificate: x509: certificate relies on legacy Common Name field, use SANs instead

在gitlab的文档中,有这一节[Configure HTTPS manually](Configure SSL for a Linux package installation | GitLab),这里提到一个full certificate chain 完整的证书链,但是文档并没有说怎么创建具有完整证书链的证书,在继续一番搜索后,找到这个harbor文档Configure HTTPS Access to Harbor ,在这里,文档演示了创建具有完整证书链的证书,我们只要跟着顺序修改domain后,就可以创建属于自己的证书,注意domain要和后面自定义的域名一致,不然还是报错。


创建证书

1:创建CA密钥和证书

#创建CA密钥
openssl genrsa -out ca.key 4096
#创建ca证书
openssl req -x509 -new -nodes -sha512 -days 3650 \
 -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=yourdomain.com" \
 -key ca.key \
 -out ca.crt

2:创建服务密钥和证书请求文件

#我们使用自定义的domain创建密钥
openssl genrsa -out yourdomain.com.key 4096
#使用key创建一个domain证书申请文件
openssl req -sha512 -new \
    -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=yourdomain.com" \
    -key yourdomain.com.key \
    -out yourdomain.com.csr

3:创建一个x509 v3扩展文件

文件内容包含我们的域名和FQDN,文档有说到x509,我们报错也遇到过,但是这是什么东西???

cat > v3.ext <<-EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names

[alt_names]
DNS.1=yourdomain.com
DNS.2=yourdomain
DNS.3=hostname
EOF

4:根据扩展文件,使用CA证书对domain申请文件签发domain证书

openssl x509 -req -sha512 -days 3650 \
    -extfile v3.ext \
    -CA ca.crt -CAkey ca.key -CAcreateserial \
    -in yourdomain.com.csr \
    -out yourdomain.com.crt

这样我们在目录下会看到以下文件

ls
ca.crt  ca.key  ca.srl  gitlab.hjg.com.cert  gitlab.hjg.com.crt  gitlab.hjg.com.csr  gitlab.hjg.com.key  v3.ext

将证书导入服务

证书导入到gitlab服务

我们将证书gitlab.hjg.com.crt密钥 gitlab.hjg.com.key 放到虚拟机的gitlab/config/ssl 下,并修改gitlab/config/gitlab.rb配置 ,重启容器加载配置文件,这里参考gitlab文档[Configure HTTPS manually](Configure SSL for a Linux package installation | GitLab)

sudo vi gitlab/config/gitlab.rb
。。。
#修改domain
external_url "https://gitlab.example.com"
#禁止Let’s Encrypt自动签发证书
letsencrypt['enable'] = false
nginx['ssl_client_certificate'] = "/etc/gitlab/ssl/yourdomain.com.crt"
nginx['ssl_certificate'] = "/etc/gitlab/ssl/yourdomain.com.crt"
nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/yourdomain.com.key"
。。。

#重启容器
sudo docker compose restart

在客户端导入证书

我们将创建的CA证书导入到本地使用的设备,这样后续使用CA为其他域名签发的证书可以继承证书认证,不需要反复导入指定域名证书,一劳永逸。

windows证书导入

选中ca.crt》右键》安装证书》存储位置“本地计算机”》自动存储证书》完成。

导入完成后,打开无痕浏览器重新访问域名,已经不在跳安全告警。

coreDNS已经上线,可以在内网通过域名直接访问相关服务。

我们直接将ca证书添加到系统中,因为domain是跟证书签发的,添加后就可以直接访问域名,hosts文件要做指定

(这里后续要在上线 一个本地DNS,就不用该hosts文件了)

如果要上线其他服务,比如harbor,那么使用跟证书在签发一个harbor的域名证书,就可以直接访问,因为浏览器已经导入ca证书,二次签发的证书继承。

linux证书导入

证书导入到git cline,fedora 40 ,shell端进行git clone时,没有经过配置会报错

致命错误:无法访问 'https://gitlab.xxx.com/bt7/cicd-test.git/':SSL certificate problem: unable to get local issuer certificate

我尝试通过浏览器,将自签ca证书导入,在此进行clone尝试,依旧报错

在搜索后,发现可以通过update-ca-trust 找到解决方法

$ update-ca-trust --help
Usage: /usr/bin/update-ca-trust [extract] [-o DIR|--output=DIR]

Update the system trust store in /etc/pki/ca-trust/extracted.

COMMANDS
(absent/empty command): Same as the extract command described below.

extract: Instruct update-ca-trust to scan the source configuration in
/usr/share/pki/ca-trust-source and /etc/pki/ca-trust/source and produce
updated versions of the consolidated configuration files stored below
the /etc/pki/ca-trust/extracted directory hierarchy.

EXTRACT OPTIONS
-o DIR, --output=DIR: Write the extracted trust store into the given
directory instead of updating /etc/pki/ca-trust/extracted.
#查看/etc/pki/ca-trust/source中有那些文件
$ ls /etc/pki/ca-trust/source/
anchors  blacklist  blocklist  ca-bundle.legacy.crt  README
#查看README文件
$ cat /etc/pki/ca-trust/source/README
This directory /etc/pki/ca-trust/source/ contains CA certificates and 
trust settings in the PEM file format. The trust settings found here will be
interpreted with a high priority - higher than the ones found in 
/usr/share/pki/ca-trust-source/.

=============================================================================
QUICK HELP: To add a certificate in the simple PEM or DER file formats to the
            list of CAs trusted on the system:

            Copy it to the
                    /etc/pki/ca-trust/source/anchors/
            subdirectory, and run the
                    update-ca-trust
            command.

            If your certificate is in the extended BEGIN TRUSTED file format,
            then place it into the main source/ directory instead.
=============================================================================

Please refer to the update-ca-trust(8) manual page for additional information.

#将证书copy到文件夹中
$ sudo cp ca.crt /etc/pki/ca-trust/source/anchors/
#更新证书
$ sudo update-ca-trust

再次进行clone,就可以看到回显,输入账号密码就可以正常clone。

linux如果安装了桌面,浏览器打开网页也不会在爆安全页面。