从openresty lua制作https tls请求

我正在尝试找到一种在我的nginx/lua模块中从服务器中进行安全GET请求的方法,以检查呼叫的身份验证。这方面似乎很少有相关资料。我的当前尝试集中在使用resty.http和以下内容。

-- 创建http客户端
local httpc = http.new();
httpc:set_timeout(500)

ngx.log(ngx.DEBUG, '**** 使用TLS连接 ****');
ok, err = httpc:connect(my_server, port);

然而,我的_server需要输入证书、CA和密钥,但是使用["ca"] = myca.pem等方法并不起作用。如果我设置

lua_ssl_trusted_certificate=myca.pem

该请求将失败,并显示以下内容:

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

我查看了https://github.com/brunoos/luasec/archive/luasec-0.6,但是实际上无法在我的alpine Linux容器上编译干净。 不确定要使用哪个模块或如何继续,有任何想法吗?

更新 根据评论和答案的其他信息,尝试使用_OpenResty小型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
        }

另一个注意事项是,无论如何我都无法让bearerToken通过lua侧作为vars而工作,我必须将其作为arg传递,然后清除args,这样任何其他的就不会被传递到调用中。 我的调用该路径lua代码。

--连接--
ngx.log(ngx.DEBUG, '**** 使用TLS连接 ****');

res = ngx.location.capture('/api/analytics/token-info',
{
    method = ngx.HTTP_POST,
    body = json.encode(query),
    args = {
        token = accessToken;
    }
})

原文链接 https://stackoverflow.com/questions/48955685

点赞
stackoverflow用户2060502
stackoverflow用户2060502

忘掉luasec,你的最后一段代码片段没有意义。

LuaSec与LuaSocket兼容,但与nginx cosocket API不兼容。

使用通用resty-http request_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
  })

顺便说一句,request_uri()源代码是如何使用通用resty-http API(连接/ssl handshake/request)的完美示例。

更新:

如果你一定需要通过证书认证客户端,可以使用以下方法:

创建上游,配置客户端证书:

  location /my_upstream/ {
      internal; #指定给定位置仅用于内部请求
      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/; #尾随斜杆
      proxy_ssl_certificate     /etc/nginx/client.pem;
      proxy_ssl_certificate_key /etc/nginx/client.key
      proxy_ssl_verify on;
      proxy_ssl_verify_depth 2; #仅是例子
      proxy_ssl_trusted_certificate /etc/nginx/ca.pem;
  }

使用ngx.location.capture API发出同步但仍非阻塞的Nginx子请求:

local res = ngx.location.capture('/my_upstream/login',
    {
        method = ngx.HTTP_POST,
        body = "some text",
        vars = {
                    bearerToken = "12345"
               }
    })

请不要刷屏,RTFM关于ngx.location.capture API和proxy_pass的所有细节。

PS:看到你的最后一个更新——不能相信有人会同时要求客户端证书和token验证。 通常,HTTPS不需要客户端证书用于安全传输,而使用将token放在Authorization头部进行身份认证。

2018-02-26 21:34:22