Eri*_*fin 3 lua nginx openresty
我正在尝试找到一种方法,从 nginx/lua 模块内向服务器发出安全 GET 请求,以检查入口调用的身份验证。关于如何做到这一点的信息似乎很少。我当前的尝试主要围绕使用resty.http 和以下内容。
-- Create http client
local httpc = http.new();
httpc:set_timeout(500)
ngx.log(ngx.DEBUG, '**** Connect with TLS ****');
ok, err = httpc:connect(my_server, port);
Run Code Online (Sandbox Code Playgroud)
然而,my_server 在输入时需要证书、ca 和密钥,但同样,不知道如何做到这一点,使用 ["ca"] = myca.pem; 等等...不起作用。如果我设置
lua_ssl_trusted_certificate=myca.pem
Run Code Online (Sandbox Code Playgroud)
请求失败并显示以下内容:
2018/02/23 19:22:17 [crit] 19#0: *4 SSL_shutdown() failed (SSL: error:140E0197:SSL routines:SSL_shutdown:shutdown while in init), client: 127.0.0.1, server: my_server
Run Code Online (Sandbox Code Playgroud)
我查看了https://github.com/brunoos/luasec/archive/luasec-0.6,但坦率地说无法让它在我的 alpine linux 容器上编译干净。目前不确定要使用什么模块或如何继续,有什么想法吗?
更新 基于我收到的评论和答案的其他信息。尝试使用openresty pintsized lua-resty-http和 https 失败,目前没有办法让cert/key/ca双向 TLS 流正常工作。使用下面的答案,我能够配置对后端应用程序微服务的上游代理调用,以正确服务请求。我的 proxy.conf 文件片段使其正常工作,如下所示。 注意:上游服务器必须与证书中的 CN 相匹配,否则它将无法工作,或者至少我通过证书失败没有得到任何指示。
http {
# Upstream backend for services
upstream apimy {
server apimy:3003;
}
...
location /api/analytics/token-info {
internal; # Specifies that a given location can only be used for internal requests
set $bearerToken $arg_token;
set $args "";
proxy_pass_request_headers on;
proxy_ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
proxy_ssl_ciphers HIGH:!aNULL:!MD5;
proxy_ssl_certificate /etc/certs/analytics_proxy_client_public.cert.pem;
proxy_ssl_certificate_key /etc/certs/analytics_proxy_client_private.key.pem;
proxy_ssl_trusted_certificate /etc/certs/analytics_proxy_client_ca.cert.pem;
proxy_ssl_verify on;
proxy_ssl_verify_depth 2;
proxy_ssl_session_reuse off;
proxy_set_header Host apimy;
proxy_set_header Content-Type "application/json";
proxy_set_header Accept "application/json";
proxy_set_header Authorization "Bearer $bearerToken";
proxy_pass https://apimy/api/analytics/token-info; # trailing slash
}
Run Code Online (Sandbox Code Playgroud)
另一个注意事项,无论我无法让 bearerToken 工作,将其作为 lua 端的变量传递,我必须将其作为参数传递,然后清除之后的参数,以便任何其他参数都不会传递到称呼。我的lua代码调用路径。
-- Connect --
ngx.log(ngx.DEBUG, '**** Connect with TLS ****');
res = ngx.location.capture('/api/analytics/token-info',
{
method = ngx.HTTP_POST,
body = json.encode(query),
args = {
token = accessToken;
}
})
Run Code Online (Sandbox Code Playgroud)
忘记 luasec,你的最后一个代码片段没有意义。
LuaSec 与 LuaSocket 兼容,但与 nginx cosocket API 绝对不兼容。
使用通用的 Resty-httprequest_uri()接口:
local http = require "resty.http"
local httpc = http.new()
local res, err = httpc:request_uri("https://example.com:443/helloworld", {
method = "POST",
body = "a=1&b=2",
headers = {
["Content-Type"] = "application/x-www-form-urlencoded",
},
ssl_verify = true
})
Run Code Online (Sandbox Code Playgroud)
顺便说一句,request_uri() 源是如何使用通用 Resty-http API (connect/ssl_handshake/request) 的完美示例
更新:
如果您确实需要通过证书对客户端进行身份验证,您可以使用下一种方法:
创建upstream,配置客户端证书:
location /my_upstream/ {
internal; # Specifies that a given location can only be used for internal requests
proxy_pass_request_headers off;
proxy_set_header Host backend.example.com;
proxy_set_header Content-Type "application/json";
proxy_set_header Accept "application/json";
proxy_set_header Authorization "Bearer $bearerToken"
proxy_pass https://backend.example.com/; # trailing slash
proxy_ssl_certificate /etc/nginx/client.pem;
proxy_ssl_certificate_key /etc/nginx/client.key
proxy_ssl_verify on;
proxy_ssl_verify_depth 2; #just example
proxy_ssl_trusted_certificate /etc/nginx/ca.pem;
}
Run Code Online (Sandbox Code Playgroud)
使用ngx.location.capture API 发出同步但仍非阻塞的 Nginx 子请求:
local res = ngx.location.capture('/my_upstream/login',
{
method = ngx.HTTP_POST,
body = "some text",
vars = {
bearerToken = "12345"
}
})
Run Code Online (Sandbox Code Playgroud)
请不要泛滥 RTFM 有关ngx.location.capture API 和proxy_pass的所有详细信息
PS:查看您的最新更新 - 无法相信有人会同时要求客户端证书和令牌授权。通常,不带客户端证书的 HTTPS 用于安全传输,并使用带有令牌的授权标头进行身份验证。