1. 程式人生 > >[轉] 使用HTTPS在Nexus Repository Manager 3.0上搭建私有Docker倉庫

[轉] 使用HTTPS在Nexus Repository Manager 3.0上搭建私有Docker倉庫

dha cau target article data- 重新啟動 crt main specified

FROM: https://www.hifreud.com/2018/06/06/03-nexus-docker-repository-with-ssl/

搭建方式

搭建SSL的Nexus官方提供兩種方式

  • 第一種是反向代理服務器,Nexus Repository Manager使用HTTP對外提供服務,然後使用Nginx之類的反向代理服務器對外提供HTTPS服務,但是反向代理服務器與Nexus Repository Manager之間依舊使用HTTP交互。
  • 第二種就是比較正常的,在Nexus Repository Manager上做一些配置,使得Nexus Repository Manager直接對外提供HTTPS服務。

本文主要描述的就是第二種方式

配置HTTPS

生成keystore文件

在項目的$install-dir/etc/ssl/目錄下,執行命令

#{NEXUS_DOMAIN} = nexus為服務器域名
#{NEXUS_IP} = 192.168.59.1 為服務器IP
$ cd $install-dir/etc/ssl/
$ keytool -genkeypair -keystore keystore.jks -storepass nexus3 -keypass nexus3 -alias jetty -keyalg RSA -keysize 2048 -validity 5000 -dname "CN=*.{NEXUS_DOMAIN}, OU=Example, O=Sonatype, L=Unspecified, ST=Unspecified, C=US" -ext "SAN=DNS:{NEXUS_DOMAIN},IP:{NEXUS_IP}" -ext "BC=ca:true"

添加SSL端口

修改$data-dir/etc/nexus.properties文件,在第一行添加application-port-ssl=8443

添加HTTPS支持配置文件

修改$data-dir/etc/nexus.properties文件,修改Key為nexus-args所在行的值,在後面添加,${jetty.etc}/jetty-https.xml,${jetty.etc}/jetty-http-redirect-to-https.xml

nexus-args=${jetty.etc}/jetty.xml,${jetty.etc}/jetty-http.xml,${jetty.etc}/jetty-requestlog.xml,${jetty.etc}/jetty-https.xml,${jetty.etc}/jetty-http-redirect-to-https.xml

修改HTTPS配置文件

修改${jetty.etc}/jetty-https.xml文件中keystore和truststore的配置部分

<Set name="KeyStorePath"><Property name="ssl.etc"/>/keystore.jks</Set>
<Set name="KeyStorePassword">nexus3</Set>
<Set name="KeyManagerPassword">nexus3</Set>
<Set name="TrustStorePath"><Property name="ssl.etc"/>/keystore.jks</Set>
<Set name="TrustStorePassword">nexus3</Set>

驗證

重新啟動服務

$nexus.exe /run

Web訪問

可以訪問http://localhost:8081/或者https://localhost:8443/來查看,如果能夠正常打開網頁則配置成功。此處由於配置了jetty-http-redirect-to-https.xml,所以在訪問http的時候會自動redirect到https地址。

docker配置

登錄報錯

[root@localhost docker]# docker login -u admin -p admin123 192.168.59.1:8551
Error response from daemon: Get https://192.168.59.1:8551/v1/users/: x509: certificate signed by unknown authority

此處有個小插曲就是之前是報錯如下Error response from daemon: Get https://192.168.59.1:8551/v1/users/: x509: cannot validate certificate for 192.168.59.1 because it doesn‘t contain any IP SANs這是因為在生成keystore的時候沒有指定IP

此處有兩種方式解決上述問題,第一種是添加insecure-registries,不對SSL進行認證校驗,第二種是安裝簽名證書,進行校驗。

1-修改daemon.json文件

[root@localhost docker]# vi /etc/docker/daemon.json
{
  "insecure-registries": [
    "192.168.59.1:8551"
  ],
  "disable-legacy-registry": true
}
[root@localhost docker]# service docker restart
[root@localhost docker]# docker login -u admin -p admin123 192.168.59.1:8551
Login Succeeded

2-配置ca-trust(centos)

[root@localhost docker]# docker login -u admin -p admin123 192.168.59.1:8551
Error response from daemon: Get https://192.168.59.1:8551/v2/: x509: certificate has expired or is not yet valid

出現上述問題,搜索之後大多數人說是服務器時間不同步問題,解決如下:

# 先解決時區問題
[root@localhost ~]# ls -l /etc/localtime 
lrwxrwxrwx. 1 root root 38 Apr 25 07:09 /etc/localtime -> ../usr/share/zoneinfo/America/New_York
[root@localhost ~]# rm -f /etc/localtime
[root@localhost ~]# ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# 在解決時間問題
[root@localhost docker]# yum install ntp.x86_64
# 可用的ntp服務器列表 http://www.ntp.org.cn/pool.php
[root@localhost docker]# ntpdate cn.ntp.org.cn
6 Jun 17:50:20 ntpdate[18252]: no server suitable for synchronization found
# 由於公司代理服務器問題,連接不上NTP服務器,所以手動設置
[root@localhost ~]# date -s 20180606
Wed Jun  6 00:00:00 CST 2018
[root@localhost ~]# date -s 17:53:35
Wed Jun  6 17:53:35 CST 2018

基於centos7.0版本,生成和導入cert文件

#生成cert文件
[root@localhost ~]# keytool -printcert -sslserver 192.168.59.1:8443 -rfc >nexus.crt
[root@localhost ~]# yum install ca-certificates
[root@localhost ~]# update-ca-trust force-enable
# 還可以放在/etc/docker/certs.d/192.168.59.1:8443目錄下
[root@localhost ~]# mv nexus.crt /etc/pki/ca-trust/source/anchors/nexus.crt
[root@localhost ~]# update-ca-trust
[root@localhost ~]# service docker restart
[root@localhost ~]# docker login -u admin -p admin123 192.168.59.1:8551
Error response from daemon: Get https://192.168.59.1:8551/v2/: x509: certificate signed by unknown authority

對於Ubuntu系統來說certificate的存放路徑是/usr/local/share/ca-certificates

#生成cert文件
[root@localhost ~]# keytool -printcert -sslserver 192.168.59.1:8443 -rfc >nexus.crt
# 還可以放在/etc/docker/certs.d/192.168.59.1:8443目錄下
[root@localhost ~]# mv nexus.crt /usr/local/share/ca-certificates/nexus.crt
[root@localhost ~]# update-ca-certificates
[root@localhost ~]# service docker restart
[root@localhost ~]# docker login -u admin -p admin123 192.168.59.1:8551

依然報錯如上,說是Unkonw authority,搜索之後發現 一般情況下,證書只支持域名訪問,要使其支持IP地址訪問,需要修改配置文件openssl.cnf。

在Redhat7系統中,文件所在位置是/etc/pki/tls/openssl.cnf。在其中的[ v3_ca]部分,添加subjectAltName選項:

[ v3_ca ]  
subjectAltName = IP:192.168.59.1

再次執行docker login

[root@localhost ~]# service docker restart
[root@localhost ~]# docker login -u admin -p admin123 192.168.59.1:8551
Login Succeeded

至此,大功告成!

參考資料

SSL and Repository Connector Configuration : https://help.sonatype.com/repomanager3/private-registry-for-docker/ssl-and-repository-connector-configuration

Inbound SSL - Configuring to Serve Content via HTTPS : https://help.sonatype.com/repomanager3/security/configuring-ssl#ConfiguringSSL-InboundSSL-ConfiguringtoServeContentviaHTTPS

Using Self-Signed Certificates with Nexus Repository Manager and Docker Daemon https://support.sonatype.com/hc/en-us/articles/217542177-Using-Self-Signed-Certificates-with-Nexus-Repository-Manager-and-Docker-Daemon

ca證書校驗用戶證書 : https://www.cnblogs.com/cmsd/p/6078705.html

03搭建docker私有倉庫 : https://blog.csdn.net/gqtcgq/article/details/51163558

docker 報錯:x509 : certificate has expired or is not yet valid: https://blog.csdn.net/bjbs_270/article/details/48784807

linux設置系統時間 : https://www.cnblogs.com/boshen-hzb/p/6269378.html

[轉] 使用HTTPS在Nexus Repository Manager 3.0上搭建私有Docker倉庫