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}" } } } } } |