nexus配置https
場景:
公司要求關閉nexus的匿名登錄功能,這樣一來所有使用此nexus倉庫的客戶端命令行工具就要配置登錄認證才可以使用nexus倉庫的依賴包或鏡像。
在大多數語言的命令行構建工具,比如:mvn、npm、pip等配置好登錄認證信息后即可使用nexus,即使nexus沒有配置https。
但是在實踐中,go語言的命令行構建工具配置完登錄認證是不可以的,報:“refusing to pass credentials to insecure URL”的錯誤,由此判斷go可能要求nexus的協議為https的。
通過實驗證實:go 在nexus以https協議提供服務后,即使關閉匿名登陸,只要配置好倉庫地址和登錄信息就可以正常使用了。
安裝 Nexus 3
安裝比較簡單,下載並解壓即可
下載地址:https://download.sonatype.com/nexus/3/nexus-3.24.0-02-unix.tar.gz (Nexus Repository Manager 3.24.0-02)
解壓后目錄結構
tar xf nexus-3.24.0-02-unix.tar.gz -C /usr/local/
tree /usr/local/nexus-3.24.0-02/ -L 1
/usr/local/nexus-3.24.0-02/
├── bin # nexus可執行文件,通過./nexus start啟動nexus
├── deploy
├── etc
├── lib
├── NOTICE.txt
├── OSS-LICENSE.txt
├── PRO-LICENSE.txt
├── public
└── system
配置自簽名證書文件
運行該腳本,會在當前目錄生成一個 keystore.jks
將 keystore.jks 放到/usr/local/nexus-3.24.0-02/etc/ssl 目錄,方便管理
#!/bin/bash
NEXUS_DOMAIN=192.168.199.12 ##更改為你自己的nexus的IP
NEXUS_IP_ADDRESS=192.168.199.12 ##更改為你自己的nexus的IP
PASSWD=Nexus123 ##這個密碼我寫的是nexus的登錄密碼,好像隨便寫也可以
keytool -genkeypair -keystore keystore.jks -storepass ${PASSWD} -keypass ${PASSWD} -alias nexus -keyalg RSA -keysize 2048 -validity 5000 -dname "CN=${NEXUS_DOMAIN}, OU=Nexus, O=Nexus, L=Beijing, ST=Beijing, C=CN" -ext "SAN=IP:${NEXUS_IP_ADDRESS}" -ext "BC=ca:true"
生成客戶端證書文件
keytool -export -alias nexus -keystore keystore.jks -file keystore.cer -storepass Nexus123
這里的Nexus123要對應腳本里面的passwd,運行上面的命令后會生成 keystore.cer文件。
自簽名證書配置完畢
修改nexus配置文件
/usr/local/nexus-3.24.0-02/etc/nexus-default.propertie為nexus配置文件,可以修改端口等基本配置,修改前先備份。
cp nexus-default.propertie nexus-default.propertie.bak
vim nexus-default.propertie
## DO NOT EDIT - CUSTOMIZATIONS BELONG IN $data-dir/etc/nexus.properties
##
# Jetty section
application-port-ssl=8443 ##ssl端口
application-port=8081 ##http端口
application-host=0.0.0.0
nexus-args=${jetty.etc}/jetty.xml,${jetty.etc}/jetty-http.xml,${jetty.etc}/jetty-requestlog.xml,${jetty.etc}/jetty-https.xml
nexus-context-path=/
# Nexus section
nexus-edition=nexus-pro-edition
nexus-features=\
nexus-pro-feature
nexus.hazelcast.discovery.isEnabled=true
## 注:application-port-ssl和application-port都定義的有端口,則兩個端口同時監聽,要想訪問http則訪問8081,要想訪問https則訪問8443
再修改/usr/local/nexus-3.24.0-02/etc/jetty/jetty-https.xml文件。
<Set name="KeyStorePath">/opt/nexus-3.10.0-04/etc/ssl/keystore.jks</Set>
<Set name="KeyStorePassword">Nexus123</Set>
<Set name="KeyManagerPassword">Nexus123</Set>
<Set name="TrustStorePath">/opt/nexus-3.10.0-04/etc/ssl/keystore.jks</Set>
<Set name="TrustStorePassword">Nexus123</Set>
或
<Set name="KeyStorePath"><Property name="ssl.etc"/>/keystore.jks</Set>
<Set name="KeyStorePassword">Nexus123</Set>
<Set name="KeyManagerPassword">Nexus123</Set>
<Set name="TrustStorePath"><Property name="ssl.etc"/>/keystore.jks</Set>
<Set name="TrustStorePassword">Nexus123</Set>
兩個Path區別,如果生成的證書文件在/usr/local/nexus-3.24.0-02/etc/ssl/目錄下,則使用默認的,及下面的,否則不在ssl目錄下,需要指明證書所在目錄的絕對路徑。
啟動nexus
cd /usr/local/nexus-3.24.0-02/bin/
./nexus start
ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 *:8081 *:*
LISTEN 0 1 127.0.0.1:41778 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 50 *:8443 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
## 若啟動失敗,則查看日志報錯
## 日志所在目錄:/usr/local/sonatype-work/nexus3/log/nexus.log
瀏覽器訪問
http訪問

https訪問

命令行編譯工具的配置
mvn
<!-- maven的settings.xml文件-->
<server>
<id>nexus</id>
<username>admin</username>
<password>Nexus123</password>
</server>
........
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://192.168.199.12:8081/repository/maven-public/</url>
</mirror>
........
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>nexus</id>
<url>http://192.168.199.12:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
</profile>
...........
<activeProfile>nexus</activeProfile>
npm
## 刪除~/.npmrc文件
npm config set metrics-registry "http://192.168.199.12:8081/repository/npm-group/"
npm config set registry "http://192.168.199.12:8081/repository/npm-group/"
npm login http://192.168.199.12:8081/repository/npm-group/
username:
passwd:
email:
## ~/.npmrc文件
metrics-registry=http://192.168.199.12:8081/repository/npm-group/
registry=http://192.168.199.12:8081/repository/npm-group/
//192.168.199.12:8081/repository/npm-group/:_authToken=NpmToken.f237c4f7-fdee-3650-9d6f-84fec4de2124
最后一行是執行完npm login操作以后自動生成的。
pip
## 修改 ~/.pip/pip.conf
## 沒有pip.conf則創建即可
[global]
index-url = http://admin:Nexus123@192.168.199.12:8081/repository/pip-proxy-ali/simple
[install]
trusted-host = 192.168.199.12
go
go env -w GO111MODULE="on"
go env -w GOSUMDB=off
go env -w GOPROXY=https://admin:Nexus123@192.168.199.12:8443/repository/go-group/
export GOPROXY=https://admin:Nexus123@192.168.199.12:8443/repository/go-group/
## GO111MODULE變量說明:GO111MODULE 為開啟或關閉模塊的支持,它有三個可選值:off、on、auto,默認值是 auto。
GO111MODULE=off 無模塊支持,go 會從 GOPATH 和 vendor 文件夾尋找包。
GO111MODULE=on 模塊支持,go 會忽略 GOPATH 和 vendor 文件夾,只根據 go.mod 下載依賴。
GO111MODULE=auto 在 $GOPATH/src 外面且根目錄有 go.mod 文件時,開啟模塊支持。
特別注意:
對於使用http或https的登錄認證請求,一般可以直接將登錄信息寫在http或https://username:password@ip或域名。但是如果密碼中存在特殊符號,如“#”、“!”等,則填寫時需要將特殊符號百分比保留字符編碼,特殊符號替換為表格第二行的內容。如:密碼為:Smbands!,則要寫為:https://admin:Smbands%21@IP或域名。

具體可以參考:https://en.wikipedia.org/wiki/Percent-encoding#Percent-encoding_reserved_characters
