【问题标题】:Making an https tls request from openresty lua从 openresty lua 发出 https tls 请求
【发布时间】:2018-02-23 20:20:39
【问题描述】:

我正在尝试找到一种方法,从我的 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);

my_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 容器上编译干净。 目前不确定要使用什么模块或如何进行,有什么想法吗?

更新 基于 cmets 的附加信息和我收到的答案。尝试通过 https 使用 openresty pintsized lua-resty-http 失败,目前无法让 cert/key/ca 双向 TLS 流正常工作。使用下面的答案,我能够为我的后端应用程序微服务配置上游代理调用,以正确处理请求。 我的 proxy.conf 文件 sn-ps 让它工作看起来像下面这样。 注意:上游服务器必须与证书中的 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 代码。

-- 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;
    }
})

【问题讨论】:

  • Luasec 很痛苦……很遗憾。我一直在使用 curl 或 libcurl - 例如。 luarocks.org/modules/luarocks/luacurl(虽然我不知道我实际用的是哪一个)
  • @AlexanderAltshuler 我看了那个和 luasec(看起来很痛苦),如果我使用 ssl_handshake,我应该为包含 cert/ca/key 的 json 有效负载使用哪个参数?
  • @ErickGriffin 你有一个选择——是否验证服务器证书。当设置为 true 时,服务器证书将根据 lua_ssl_trusted_certificate 指令指定的 CA 证书进行验证。您可能还需要调整 lua_ssl_verify_depth。如果您的服务器需要客户端证书(很少用例 IMO),我不知道使用 resty.http 的方法。
  • @AlexanderAltshuler,我尝试了握手,但似乎不起作用。我检查了我的 lua_ssl_protocols 并且它没有设置,所以我无法解释它抛出的错误,这表明协议问题。我正在更新上述问题,以获取您的任何其他 cmets/想法。

标签: nginx lua openresty


【解决方案1】:

忘记 luasec,你最后的代码 sn-p 没有意义。

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() source 是如何使用通用 resty-http API (connect/ssl_handshake/request) 的完美示例

更新:

如果您确实需要通过证书对客户端进行身份验证,您可以使用下一种方法:

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

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

使用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:查看您的最新更新 - 无法相信有人会同时要求客户端证书和令牌授权。 通常使用不带客户端证书的 HTTPS 进行安全传输,使用带有令牌的 Authorization 标头进行身份验证。

【讨论】:

  • 我同意并希望使用上述方法,但正如我在上面的更新中提到的,连接不仅需要 CA,还需要证书、密钥和密码文件。在上述电话中如何提供它们?
  • 我已经使用了上述实现,并且位置/服务器必须与 CN 匹配,它才能工作。我会为其他人发布我的官方修复。
猜你喜欢
  • 1970-01-01
  • 2018-01-31
  • 2019-08-16
  • 2020-05-18
  • 2015-09-28
  • 2021-08-06
  • 2016-04-29
  • 2023-03-26
相关资源
最近更新 更多