api7 / lua-resty-etcd

Nonblocking Lua etcd driver library for OpenResty

Home Page:https://api7.ai/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

bug: etcd token refresh

GoneGo1ng opened this issue · comments

Summary

I found a bug with token refresh. When lua-resty-etcd refreshes the token, the request failed. And ETCD reports Token is expired.

I think the error is caused by the first piece of code. When ignore_auth is true, the request header should not be carried jwt_token.

So I changed the code to the second piece. Then the error won't happen again.

I don't know if I'm doing it right, please fix this bug.

/lib/resty/etcd/v3.lua
...
    local headers = {}
    local keepalive = true
    if self.is_auth then
        if not ignore_auth then
            -- authentication reqeust not need auth request
            local _, err = refresh_jwt_token(self, timeout)
            if err then
                return nil, err
            end
        else
            keepalive = false   -- jwt_token not keepalive
        end
        headers.Authorization = self.jwt_token
    end
...
/lib/resty/etcd/v3.lua
...
    local headers = {}
    local keepalive = true
    if self.is_auth then
        if not ignore_auth then
            -- authentication reqeust not need auth request
            local _, err = refresh_jwt_token(self, timeout)
            if err then
                return nil, err
            end
            headers.Authorization = self.jwt_token
        else
            keepalive = false   -- jwt_token not keepalive
        end
        -- headers.Authorization = self.jwt_token
    end
...

ENV

  • lua-resty-etcd version: v1.4.4
  • etcd version: 3.4.13 v3
  • openresty version: 1.17.8.2

ETCD Error Log

{"level":"warn","ts":"2021-03-08T16:53:35.930+0800","caller":"auth/jwt.go:64","msg":"failed to parse a JWT token","token":"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MTUxOTMzMjYsInJldmlzaW9uIjozLCJ1c2VybmFtZSI6InJvb3QifQ.EzS4qwla069CDJloKXvwZYmH37a9Bz9sP7m77Cj_qIfyuDluiV7u6nnebAsWqiq8R97H45N2FzvsulNnV8w71wh6sxY9wfmo81WQypY_jjjCKzIegza4VRQ9nLvFvlSOyz_0Oiq2liiI7NeKan61JDHZYm7fK9o-QWsCBPIsIMk","error":"Token is expired"}

LGTM.
@nic-chen @Yiyiyimu
What about your opinions?

Nice catch @GoneGo1ng ! Welcome submit a PR 😁