Harbor2.11.1生成自签证和配置HTTPS访问

文章目录

HTTPS的工作流程

HTTPS的工作流程

客户端发起连接请求:客户端(通常是浏览器)向服务器发送一个安全连接请求,使用HTTPS的URL或点击HTTPS链接触发。

服务器证书发送:服务器收到请求后,将自己的数字证书发送给客户端。证书中包含了服务器的公钥、数字签名和其他相关信息。

客户端验证证书:浏览器接收到服务器证书后,会进行一系列的验证步骤,包括验证证书是否由受信任的证书颁发机构签发,以及证书中的域名是否与目标服务器的域名相匹配。如果验证通过,客户端可以确定所连接的服务器是可信的。

密钥协商:一旦服务器证书被验证通过,客户端会生成一个随机的对称密钥,用于后续的数据加密和解密。该对称密钥通过服务器证书中的公钥进行加密,并发送给服务器。

通信加密:服务器使用其私钥解密客户端发送的对称密钥,并与客户端之间建立起一个加密的安全通道。从此之后,客户端和服务器之间的数据传输都会在此加密通道中进行,保证数据的机密性。

安全数据传输:在建立了安全通道后,客户端和服务器可以安全地传输数据了。数据在发送前,会使用对称密钥对数据进行加密,然后在接收端使用相同的对称密钥解密。

完整性检查:为了确保数据在传输过程中没有被篡改,HTTPS使用消息摘要函数进行完整性检查。接收方会对接收到的数据进行校验,并比对校验结果与发送方计算的结果是否相同。

通过上述流程,HTTPS保证了在传输过程中的数据加密、身份认证和完整性保护,提供了更安全可靠的网络通信。这使得敏感信息的传输、交易和共享在更加安全的环境下进行。

也就是说:我们介绍HTTPS,更多是在介绍后面这个S,也就是对HTTP的加密方式。

部署Harbor可参考上一篇文章

linux rocky 9.4部署和管理docker harbor私有源

生成自签证书

1.修改/etc/hosts文件

复制代码
[root@harbor ~]# cat  /etc/hosts 
192.168.0.200  yunzhidong.harbor.com

这里我们使用yunzhidong.harbor.com 这个域名访问Harbor Web服务

2.生成证书

a.创建存放证书路径
复制代码
[root@harbor ~]# mkdir -pv /usr/local/ssl
[root@harbor ~]# cd /usr/local/ssl/
b.创建ca.key密钥
复制代码
[root@harbor ssl]# openssl genrsa -out ca.key 4096
[root@harbor ssl]# ls
ca.key
[root@harbor ssl]# 
c.创建ca.crt

-- 提示:可将下面信息修改成自己的信息

复制代码
[root@harbor ssl]# openssl req -x509 -new -nodes -sha512 -days 365 -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=Harbor Root CA" -key ca.key  -out ca.crt
[root@harbor ssl]# ls
ca.crt  ca.key
[root@harbor ssl]# 
d.创建给Harbor服务器使用密钥 yunzhidong.harbor.com.key
复制代码
提示:需要把yunzhidong.harbor.com 改成自己的域名,并且同hosts文件内相同
[root@harbor ssl]# openssl genrsa -out yunzhidong.harbor.com.key 4096
[root@harbor ssl]# ls
ca.crt  ca.key  yunzhidong.harbor.com.key
[root@harbor ssl]# 
e.创建给Harbor服务器使用证书签名请求文件 yunzhidong.harbor.com.csr
复制代码
[root@harbor ssl]# openssl req -sha512 -new   -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=yunzhidong.harbor.com"    -key yunzhidong.harbor.com.key    -out yunzhidong.harbor.com.csr
[root@harbor ssl]# 
[root@harbor ssl]# ls
ca.crt  ca.key  yunzhidong.harbor.com.csr  yunzhidong.harbor.com.key
[root@harbor ssl]# 
f.构建用于域名配置的 v3.ext 文件

-- 提示这里只需要修改DNS.1=域名

复制代码
cat > v3.ext <<-EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names
 
[alt_names]
DNS.1=yunzhidong.harbor.com
EOF
g.生成客户端数字证书yunzhidong.harbor.com.crt
复制代码
[root@harbor ssl]# openssl x509 -req -sha512 -days 3650   -extfile v3.ext     -CA ca.crt -CAkey ca.key -CAcreateserial     -in yunzhidong.harbor.com.csr     -out yunzhidong.harbor.com.crt
h.生成证书yunzhidong.harbor.com.cert
复制代码
[root@harbor ssl]# openssl x509 -inform PEM -in yunzhidong.harbor.com.crt -out yunzhidong.harbor.com.cert

执行好,步骤abcdefgh

会生成8个文件

复制代码
ca.crt  ca.key  ca.srl  v3.ext  yunzhidong.harbor.com.crt  yunzhidong.harbor.com.csr  yunzhidong.harbor.com.key

Harbor 启用https 需要ca.crt ,yunzhidong.harbor.com.key,yunzhidong.harbor.com.crt
Docker配置证书需要 ca.crt ,yunzhidong.com.key ,yunzhidong.harbor.com.cert
linux系统或者windows系统信任证书需要ca.crt

配置证书

编辑harbor.yml

1.把hostname: yunzhidong.harbor.com 改成自己的域名

2.指定刚才证书存放路径

复制代码
https:
  # https port for harbor, default is 443
    port: 443
  # The path of cert and key files for nginx
  certificate: /usr/local/ssl/yunzhidong.harbor.crt
  private_key: /usr/local/ssl/yunzhidong.harbor.key

3.[root@harbor harbor]# ./install.sh


4.WEB验证

使用IP登录,自动跳转https登录

5.将刚才生成的ca.crt证书拿到本地电脑安装证书

C:\Windows\System32\drivers\etc\hosts,类似/etc/hosts

添加192.168.0.200 yunzhidong.harbor.com

6.域名访问

7.curl测试访问

复制代码
[root@harbor harbor]# curl -i https://yunzhidong.harbor.com

报错:
curl: (60) SSL certificate problem: unable to get local issuer certificate
More details here: https://curl.se/docs/sslcerts.html

curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.

报错原因是:linux 没有安装ca.crt证书或者docker没有添加证书,这里有两种办法解决。

或者我们先curl -k 跳过证书访问试下

复制代码
[root@harbor harbor]# curl -i -k https://yunzhidong.harbor.com
HTTP/1.1 200 OK

8.linux 信任证书,不同linux 系统不同的文件目录

我这里是Rocky Linux release 9.4 (Blue Onyx)

复制代码
[root@harbor harbor]# cat /etc/redhat-release 
Rocky Linux release 9.4 (Blue Onyx)

复制ca.crt证书到 /etc/pki/ca-trust/source/anchors/

复制代码
[root@harbor harbor]# cp /usr/local/ssl/ca.crt /etc/pki/ca-trust/source/anchors/

然后执行信任操作

复制代码
[root@harbor harbor]# update-ca-trust 

我们再次尝试 [root@harbor harbor]# curl -i https://yunzhidong.harbor.com

不再报错,如果还是不行,请重启Harbor和docker服务。

并且在为配置docker证书的情况下,依然能使用docker登录harbor

复制代码
[root@harbor harbor]# docker login https://yunzhidong.harbor.com

9.取消linux 信任证书

复制代码
[root@harbor harbor]# rm -rf /etc/pki/ca-trust/source/anchors/ca.crt 
[root@harbor harbor]# update-ca-trust 
[root@harbor harbor]# 

curl -i 无法正常登录,证明删除信任成功

复制代码
[root@harbor harbor]# docker-compose down 
[root@harbor harbor]# systemctl restart docker
[root@harbor harbor]# docker-compose up -d


[root@harbor harbor]# docker login https://yunzhidong.harbor.com 也无法使用

配置Docker证书

10.配置Docker证书

https://goharbor.io/docs/2.11.0/install-config/configure-https/

此链接也有官方生成证书教程

很明显,/etc/docker/certs.d/ 是存放docker证书的路径

所以我们创建此路径并把证书拷贝至此。

复制代码
[root@harbor harbor]# ls /etc/docker/
daemon.json
[root@harbor harbor]# mkdir -pv /etc/docker/certs.d
mkdir: created directory '/etc/docker/certs.d'
[root@harbor harbor]# cd /etc/docker/certs.d/
[root@harbor certs.d]# ls

创建跟生成证书时和/etc/hosts 域名相同的目录

复制代码
[root@harbor certs.d]# mkdir -pv yunzhidong.harbor.com
mkdir: created directory 'yunzhidong.harbor.com'
[root@harbor certs.d]# cd yunzhidong.harbor.com/
[root@harbor yunzhidong.harbor.com]# pwd
/etc/docker/certs.d/yunzhidong.harbor.com
[root@harbor yunzhidong.harbor.com]# 


拷贝证书

复制代码
[root@harbor yunzhidong.harbor.com]# ls
[root@harbor yunzhidong.harbor.com]# cp /usr/local/ssl/ca.crt /etc/docker/certs.d/yunzhidong.harbor.com/
[root@harbor yunzhidong.harbor.com]# cp /usr/local/ssl/yunzhidong.harbor.com.cert /etc/docker/certs.d/yunzhidong.harbor.com/
[root@harbor yunzhidong.harbor.com]# cp /usr/local/ssl/yunzhidong.harbor.com.key /etc/docker/certs.d/yunzhidong.harbor.com/
[root@harbor yunzhidong.harbor.com]# ls
ca.crt  yunzhidong.harbor.com.cert  yunzhidong.harbor.com.key
[root@harbor yunzhidong.harbor.com]# 

重启docker服务

复制代码
[root@harbor yunzhidong.harbor.com]# systemctl restart docker
[root@harbor yunzhidong.harbor.com]# docker login https://yunzhidong.harbor.com
Authenticating with existing credentials...
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credential-stores

Login Succeeded
[root@harbor yunzhidong.harbor.com]# 

并且 curl -i 无法登录,证明docker证书生效

复制代码
[root@harbor yunzhidong.harbor.com]# curl -i https://yunzhidong.harbor.com
curl: (60) SSL certificate problem: unable to get local issuer certificate
More details here: https://curl.se/docs/sslcerts.html

curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.

现在全球都在推进 SSL 安全加密,越来越多的网站采用了 HTTPS 访问,没有启用 HTTPS 的网站可能即将在 浏览器 里禁止访问了,刚刚我没装ca.crt证书在本地电脑上,压根不能使用域名web访问。

相关推荐
小白学大数据1 小时前
异步读取HTTP响应体的Rust实现
网络协议·http·rust
Bruce-li__2 小时前
创建私人阿里云docker镜像仓库
阿里云·docker·云计算
傻啦嘿哟2 小时前
HTTP代理:网页加速的隐形引擎
网络·网络协议·http
傻啦嘿哟2 小时前
开发者视角:应用程序中HTTP代理的集成指南
网络·网络协议·http
爱写代码的小朋友3 小时前
IP 地址规划中的子网划分:/18 网络容纳 64 个 C 段(/24)的原理与应用解析
网络·网络协议·tcp/ip
十五年专注C++开发3 小时前
WebSocket原理详解(二)
网络·websocket·网络协议
JhonKI4 小时前
从零实现Json-Rpc框架】- 项目实现 - 服务端registry&discovery实现
网络协议·rpc·json
蜗牛沐雨4 小时前
MCP协议的Streamable HTTP:革新数据传输的未来
网络·网络协议·http
金丝猴也是猿4 小时前
如何在Flutter上实现高性能的动态模板渲染
websocket·网络协议·tcp/ip·http·网络安全·https·udp
swift开发pk OC开发4 小时前
Flutter性能优化细节
websocket·网络协议·tcp/ip·http·网络安全·https·udp