lua-resty-openssl: 基於FFI的Lua OpenSSL庫
支持OpenSSL 1.1.x, 1.0.x和1.0.2系列
lua-resty-acme: ACMEv2客戶端和Let’s Encrypt證書的自動化管理
安裝
使用opm:
1 |
opm install fffonion/lua-resty-acme |
opm中沒有luaossl庫,所以這種安裝使用的是基於FFI的Openssl後端;需要OpenResty鏈接了大於等於1.1版本的OpenSSL。
也可以使用luarocks安裝:
1 |
luarocks install lua-resty-acme |
使用
以/etc/openresty目錄為例,如果目錄不存在,請自行修改。
生成一個賬戶密鑰
1 |
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:4096 -out /etc/openresty/account.key |
生成一個默認證書
1 |
openssl req -newkey rsa:2048 -nodes -keyout /etc/openresty/default.key -x509 -days 365 -out /etc/openresty/default.pem |
在Nginx配置的http
節插入以下內容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
resolver 8.8.8.8; lua_shared_dict acme 16m; init_by_lua_block { require("resty.acme.autossl").init({ -- setting the following to true -- implies that you read and accepted https://letsencrypt.org/repository/ tos_accepted = true, -- uncomment following for first time setup -- staging = true, -- uncomment folloing to enable RSA + ECC double cert -- domain_key_types = { 'rsa', 'ecc' }, account_key_path = "/etc/openresty/account.key", account_email = "此處填寫郵箱", domain_whitelist = { "你的域名1", "你的域名2" }, }) } init_worker_by_lua_block { require("resty.acme.autossl").init_worker() } |
首次配置時,建議將init_by_lua_block中的staing = true取消注釋,以防錯誤過多觸發限流;測試通過後再加回注釋使用生產API。
在需要使用證書的server
節插入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
server { server_name example.com; # required to verify Let's Encrypt API lua_ssl_trusted_certificate /etc/ssl/certs/ca-certificates.crt; lua_ssl_verify_depth 2; # fallback certs, make sure to create them before hand ssl_certificate /etc/openresty/default.pem; ssl_certificate_key /etc/openresty/default.key; ssl_certificate_by_lua_block { require("resty.acme.autossl").ssl_certificate() } location /.well-known { content_by_lua_block { require("resty.acme.autossl").serve_http_challenge() } } } |
CentOS/Fedora等系統的根證書在/etc/ssl/certs/ca-bundle.crt
,請根據實際情況修改lua_ssl_trusted_certificate。
保存後,reload nginx。
在一般情況下,domain_whitelist
必須配置,以防止惡意請求通過偽造SNI頭進行拒絕服務攻擊。
如果要匹配一系列域名,可以使用__index
來實現。比如下面的例子僅匹配example.com的子域名:
1 2 3 |
domain_whitelist = setmetatable({}, { __index = function(_, k) return ngx.re.match(k, [[\.example\.com$]], "jo") end}), |
RSA+ECC雙證書
將init_by_lua_block中的domain_key_types = { 'rsa', 'ecc' }
取消注釋後,即可同時申請兩套證書。
為了讓申請到證書前的握手不出錯斷開,給Nginx配置默認的ECC證書
1 2 3 |
openssl ecparam -name prime256v1 -genkey -out /etc/openresty/default-ecc.key openssl req -new -sha256 -key /etc/openresty/default-ecc.key -subj "/" -out temp.csr openssl x509 -req -sha256 -days 365 -in temp.csr -signkey /etc/openresty/default-ecc.key -out /etc/openresty/default-ecc.pem |
然後在server節中原有的ssl_certificate下增加兩行
1 2 |
ssl_certificate /etc/openresty/default-ecc.pem; ssl_certificate_key /etc/openresty/default-ecc.key; |
關於加密套件等的選擇可藉助搜索引擎。
TLS-ALPN-01
0.5.0開始支持tls-alpn-01,可以支持在只開放443端口的環境里完成驗證。方法是通過蜜汁FFI偏移找到當前請求的SSL結構,然後設置了新的ALPN。需要多個stream server多次proxy,拓撲結構如下:
1 2 3 4 5 |
[stream unix:/tmp/nginx-tls-alpn.sock ssl] Y / [stream 443] --- ALPN是acme-tls ? N \ [http unix:/tmp/nginx-default.sock ssl] |
第一個stream server打開了443端口,根據請求的ALPN分發到不同的後段;如果是acme-tls則轉發到我們的庫,否則轉發到正常的https。
示例配置見Github。
A systemd dependency mind blow story
交叉編譯openresty,修復支持大文件 (LFS)
老文存檔,有時間補全
http://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html
http://cgit.openembedded.org/meta-openembedded/tree/meta-webserver/recipes-httpd/nginx/files/nginx-cross.patch
https://stackoverflow.com/questions/4357570/use-file-offset64-vs-file-offset-bits-64
https://digital-domain.net/largefiles.html
https://stackoverflow.com/questions/35575749/how-to-define-file-offset-bits-large-files-macro-for-solaris-and-hp-aix
http://users.suse.com/~aj/linux_lfs.html
Jenkins 中構建有私有模塊的Go項目
更新:
可以使用 athens 來建立全局go modules緩存,管理SSH密鑰會更加方便。
go get
的底層會調用git來clone模塊,因此我們只要保證git clone repo_url
可以無交互正常運行,就可以讓go get
也正常下載模塊。
如果是在本地使用, 則可以安裝hub或者設置將https重寫成ssh地址,以自動使用私鑰下載,而無需交互輸入用戶名密碼。
如果在Jenkins中使用,就算可以馬上使用後刪除,任何時候讓一個ssh私鑰保存在磁盤上都是不安全的。所以我們使用credential.helper + 環境變量,並且用https地址的方式來給git提供用戶名密碼。
使用credential.helper 可以允許git調用配置的命令獲取用戶名和密碼,我們使用一個一行的shell腳本把環境變量$USERNAME
和 $PASSWORD
打印出來:
1 |
git config credential.helper \'!f() { sleep 1; echo "username=${USERNAME}\npassword=${PASSWORD}"; }; f\'' |
這樣,任何時候我們都不會在磁盤上保存用戶名和密碼,所有信息都在內存里。
然後,因為go get
會clone一個新的repo到本地,我們沒有辦法在這之前設置每個repo的credential.helper,所以這個配置必須是全局的設置。我們用一個docker容器來完成整個項目,然後把這個配置通過docker volume掛載到$HOME/.gitconfig下:
1 2 |
[credential] helper = "!f() { sleep 1; echo \"username=${USERNAME}\npassword=${PASSWORD}\"; }; f" |
注意Jenkins的docker插件會傳遞當前的HOME等環境變量,這個目錄往往在容器中不存在,所以我們覆蓋容器中的用戶目錄到/tmp。
完整的Jenkinsfile如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
def GOLANG_VERSION = 1.12 pipeline { agent { docker { image "golang:${GOLANG_VERSION}" args '-v ${WORKSPACE}/.gitconfig:/tmp/.gitconfig -e HOME=/tmp' } } environment { GO111MODULE = "on" GOCACHE = "/tmp/.gocache" GOPATH = "${WORKSPACE}" PATH = "${GOPATH}/bin:$PATH" } stages { stage('Checkout') { steps { withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'CREDENTIAL_ID', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) { checkout scm sh 'git config credential.helper \'!f() { sleep 1; echo "username=${USERNAME}\npassword=${PASSWORD}"; }; f\'' sh 'git fetch' } } } stage('Install dependencies') { steps { sh 'go version' script { withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'CREDENTIAL_ID', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) { sh "go get -v" } } } } stage('Run tests') { steps { script { sh "go test" } } } stage('Build') { steps { script { sh "go build -o ${item}" } } } } } |