You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by me...@apache.org on 2020/02/18 07:54:35 UTC

[incubator-apisix] branch master updated: code style: use new `lj-releng` to catch all exceptions. (#1135)

This is an automated email from the ASF dual-hosted git repository.

membphis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 6d8907f  code style: use new `lj-releng` to catch all exceptions. (#1135)
6d8907f is described below

commit 6d8907f4ff30fc61e1a4348119bf35a1ca2338d8
Author: YuanSheng Wang <me...@gmail.com>
AuthorDate: Tue Feb 18 15:54:30 2020 +0800

    code style: use new `lj-releng` to catch all exceptions. (#1135)
---
 .travis/linux_openresty_runner.sh     |  3 +-
 .travis/linux_tengine_runner.sh       |  3 +-
 Makefile                              | 14 +------
 lua/apisix.lua                        | 16 ++++----
 lua/apisix/balancer.lua               |  3 +-
 lua/apisix/plugins/basic-auth.lua     |  6 ++-
 lua/apisix/plugins/grpc-transcode.lua |  3 +-
 lua/apisix/plugins/log-util.lua       |  3 +-
 lua/apisix/plugins/udp-logger.lua     | 13 ++++--
 lua/apisix/plugins/wolf-rbac.lua      | 76 ++++++++++++++++++++++++++---------
 utils/check-lua-code-style.sh         | 42 +++++++++++++++++++
 11 files changed, 130 insertions(+), 52 deletions(-)

diff --git a/.travis/linux_openresty_runner.sh b/.travis/linux_openresty_runner.sh
index 9035252..201e36d 100755
--- a/.travis/linux_openresty_runner.sh
+++ b/.travis/linux_openresty_runner.sh
@@ -65,8 +65,7 @@ do_install() {
     fi
 
     git clone https://github.com/iresty/test-nginx.git test-nginx
-    wget -P utils https://raw.githubusercontent.com/iresty/openresty-devel-utils/iresty/lj-releng
-    chmod a+x utils/lj-releng
+    make utils
 
     git clone https://github.com/apache/openwhisk-utilities.git .travis/openwhisk-utilities
     cp .travis/ASF* .travis/openwhisk-utilities/scancode/
diff --git a/.travis/linux_tengine_runner.sh b/.travis/linux_tengine_runner.sh
index 6ce1a27..9aeaf42 100755
--- a/.travis/linux_tengine_runner.sh
+++ b/.travis/linux_tengine_runner.sh
@@ -215,8 +215,7 @@ do_install() {
     fi
 
     git clone https://github.com/iresty/test-nginx.git test-nginx
-    wget -P utils https://raw.githubusercontent.com/iresty/openresty-devel-utils/iresty/lj-releng
-	chmod a+x utils/lj-releng
+    make utils
 
     git clone https://github.com/apache/openwhisk-utilities.git .travis/openwhisk-utilities
     cp .travis/ASF* .travis/openwhisk-utilities/scancode/
diff --git a/Makefile b/Makefile
index f56f378..f294efa 100644
--- a/Makefile
+++ b/Makefile
@@ -60,7 +60,7 @@ endif
 .PHONY: utils
 utils:
 ifeq ("$(wildcard utils/lj-releng)", "")
-	wget -O utils/lj-releng https://raw.githubusercontent.com/iresty/openresty-devel-utils/iresty/lj-releng
+	wget -O utils/lj-releng https://raw.githubusercontent.com/iresty/openresty-devel-utils/master/lj-releng
 	chmod a+x utils/lj-releng
 endif
 
@@ -68,17 +68,7 @@ endif
 ### lint:             Lint Lua source code
 .PHONY: lint
 lint: utils
-	luacheck -q lua
-	./utils/lj-releng lua/*.lua \
-		lua/apisix/*.lua \
-		lua/apisix/admin/*.lua \
-		lua/apisix/core/*.lua \
-		lua/apisix/http/*.lua \
-		lua/apisix/http/router/*.lua \
-		lua/apisix/plugins/*.lua \
-		lua/apisix/plugins/grpc-transcode/*.lua \
-		lua/apisix/plugins/limit-count/*.lua > \
-		/tmp/check.log 2>&1 || (cat /tmp/check.log && exit 1)
+	./utils/check-lua-code-style.sh
 
 
 ### init:             Initialize the runtime environment
diff --git a/lua/apisix.lua b/lua/apisix.lua
index 2f203b1..bee4376 100644
--- a/lua/apisix.lua
+++ b/lua/apisix.lua
@@ -268,7 +268,8 @@ function _M.http_access_phase()
 
     local route = api_ctx.matched_route
     if not route then
-        return core.response.exit(404, {error_msg = "failed to match any routes"})
+        return core.response.exit(404,
+                    {error_msg = "failed to match any routes"})
     end
 
     if route.value.service_protocol == "grpc" then
@@ -471,12 +472,13 @@ local function cors_admin()
     local method = get_method()
     if method == "OPTIONS" then
         core.response.set_header("Access-Control-Allow-Origin", "*",
-                                "Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE, PATCH",
-                                "Access-Control-Max-Age", "3600",
-                                "Access-Control-Allow-Headers", "*",
-                                "Access-Control-Allow-Credentials", "true",
-                                "Content-Length", "0",
-                                "Content-Type", "text/plain")
+            "Access-Control-Allow-Methods",
+            "POST, GET, PUT, OPTIONS, DELETE, PATCH",
+            "Access-Control-Max-Age", "3600",
+            "Access-Control-Allow-Headers", "*",
+            "Access-Control-Allow-Credentials", "true",
+            "Content-Length", "0",
+            "Content-Type", "text/plain")
         ngx_exit(200)
     end
 
diff --git a/lua/apisix/balancer.lua b/lua/apisix/balancer.lua
index 4b3f4bb..400aff5 100644
--- a/lua/apisix/balancer.lua
+++ b/lua/apisix/balancer.lua
@@ -139,7 +139,8 @@ local function fetch_chash_hash_key(ctx, upstream)
 
     if not chash_key then
         chash_key = ctx.var["remote_addr"]
-        core.log.warn("chash_key fetch is nil, use default chash_key remote_addr: ", chash_key)
+        core.log.warn("chash_key fetch is nil, use default chash_key ",
+                      "remote_addr: ", chash_key)
     end
     core.log.info("upstream key: ", key)
     core.log.info("hash_on: ", hash_on)
diff --git a/lua/apisix/plugins/basic-auth.lua b/lua/apisix/plugins/basic-auth.lua
index de9ccbf..7c88589 100644
--- a/lua/apisix/plugins/basic-auth.lua
+++ b/lua/apisix/plugins/basic-auth.lua
@@ -73,7 +73,8 @@ local function extract_auth_header(authorization)
 
         obj.username = ngx.re.gsub(res[1], "\\s+", "")
         obj.password = ngx.re.gsub(res[2], "\\s+", "")
-        core.log.info("plugin access phase, authorization: ", obj.username, ": ", obj.password)
+        core.log.info("plugin access phase, authorization: ",
+                      obj.username, ": ", obj.password)
 
         return obj, nil
     end
@@ -96,7 +97,8 @@ do
         core.table.clear(consumer_ids)
 
         for _, cur_consumer in ipairs(consumers.nodes) do
-            core.log.info("consumer node: ", core.json.delay_encode(cur_consumer))
+            core.log.info("consumer node: ",
+                          core.json.delay_encode(cur_consumer))
             consumer_ids[cur_consumer.auth_conf.username] = cur_consumer
         end
 
diff --git a/lua/apisix/plugins/grpc-transcode.lua b/lua/apisix/plugins/grpc-transcode.lua
index 4c8191f..d16bfd6 100644
--- a/lua/apisix/plugins/grpc-transcode.lua
+++ b/lua/apisix/plugins/grpc-transcode.lua
@@ -101,7 +101,8 @@ function _M.access(conf, ctx)
         return
     end
 
-    local ok, err = request(proto_obj, conf.service, conf.method, conf.pb_option)
+    local ok, err = request(proto_obj, conf.service,
+                            conf.method, conf.pb_option)
     if not ok then
         core.log.error("transform request error: ", err)
         return
diff --git a/lua/apisix/plugins/log-util.lua b/lua/apisix/plugins/log-util.lua
index 78892b5..6ee03b2 100644
--- a/lua/apisix/plugins/log-util.lua
+++ b/lua/apisix/plugins/log-util.lua
@@ -23,7 +23,8 @@ local function get_full_log(ngx)
     local var = ctx.var
     local service_id
     local route_id
-    local url = var.scheme .. "://" .. var.host .. ":" .. var.server_port .. var.request_uri
+    local url = var.scheme .. "://" .. var.host .. ":" .. var.server_port
+                .. var.request_uri
     local matched_route = ctx.matched_route and ctx.matched_route.value
 
     if matched_route then
diff --git a/lua/apisix/plugins/udp-logger.lua b/lua/apisix/plugins/udp-logger.lua
index 0012660..97e2e6a 100644
--- a/lua/apisix/plugins/udp-logger.lua
+++ b/lua/apisix/plugins/udp-logger.lua
@@ -27,7 +27,9 @@ local schema = {
     properties = {
         host = {type = "string"},
         port = {type = "integer", minimum = 0},
-        timeout = {type = "integer", minimum = 1, default= 1000} -- timeout in milliseconds
+        timeout = {   -- timeout in milliseconds
+            type = "integer", minimum = 1, default= 1000
+        }
     },
     required = {"host", "port"}
 }
@@ -54,18 +56,21 @@ local function log(premature, conf, log_message)
 
     local ok, err = sock:setpeername(conf.host, conf.port)
     if not ok then
-        core.log.error("failed to connect to UDP server: host[", conf.host, "] port[", conf.port, "] ", err)
+        core.log.error("failed to connect to UDP server: host[",
+                       conf.host, "] port[", conf.port, "] ", err)
         return
     end
 
     ok, err = sock:send(log_message)
     if not ok then
-        core.log.error("failed to send data to UDP server: host[", conf.host, "] port[", conf.port, "] ", err)
+        core.log.error("failed to send data to UDP server: host[",
+                       conf.host, "] port[", conf.port, "] ", err)
     end
 
     ok, err = sock:close()
     if not ok then
-        core.log.error("failed to close the UDP connection, host[", conf.host, "] port[", conf.port, "] ", err)
+        core.log.error("failed to close the UDP connection, host[",
+                       conf.host, "] port[", conf.port, "] ", err)
     end
 end
 
diff --git a/lua/apisix/plugins/wolf-rbac.lua b/lua/apisix/plugins/wolf-rbac.lua
index b38501e..efbc689 100644
--- a/lua/apisix/plugins/wolf-rbac.lua
+++ b/lua/apisix/plugins/wolf-rbac.lua
@@ -116,7 +116,8 @@ local function http_req(method, uri, body, myheaders, timeout)
         httpc:set_timeout(timeout)
     end
 
-    local params = {method = method, headers = myheaders, body = body, ssl_verify = false}
+    local params = {method = method, headers = myheaders, body = body,
+                    ssl_verify = false}
     local res, err = httpc:request_uri(uri, params)
     if err then
         core.log.error("FAIL REQUEST [ ",core.json.delay_encode(
@@ -200,11 +201,17 @@ local function check_url_permission(server, appid, action, resName, clientIP, wo
 
     if err then
         core.log.error("fail request: ", url, ", err:", err)
-        return {status = 500, err = "request to wolf-server failed, err:" .. tostring(err)}
+        return {
+            status = 500,
+            err = "request to wolf-server failed, err:" .. err
+        }
     end
 
     if res.status ~= 200 and res.status ~= 401 then
-        return {status = 500, err = 'request to wolf-server failed, status:' .. tostring(res.status)}
+        return {
+            status = 500,
+            err = 'request to wolf-server failed, status:' .. res.status
+        }
     end
 
     local body, err = json.decode(res.body)
@@ -231,12 +238,14 @@ function _M.rewrite(conf, ctx)
 
     local rbac_token = fetch_rbac_token(ctx)
     if rbac_token == nil then
-        core.log.info("no permission to access ", core.json.delay_encode(permItem), ", need login!")
+        core.log.info("no permission to access ",
+                      core.json.delay_encode(permItem), ", need login!")
         return 401, {message = "Missing rbac token in request"}
     end
 
     local tokenInfo, err = parse_rbac_token(rbac_token)
-    core.log.info("token info: ", core.json.delay_encode(tokenInfo), ", err: ", err)
+    core.log.info("token info: ", core.json.delay_encode(tokenInfo),
+                  ", err: ", err)
     if err then
         return 401, {message = 'invalid rbac token: parse failed'}
     end
@@ -267,10 +276,15 @@ function _M.rewrite(conf, ctx)
     local url = ctx.var.uri
     local action = ctx.var.request_method
     local clientIP = core.request.get_ip(ctx)
-    local permItem = {appid = appid, action = action, url = url, clientIP = clientIP, wolf_token = wolf_token}
+    local permItem = {
+        appid = appid, action = action, url = url,
+        clientIP = clientIP, wolf_token = wolf_token
+    }
 
-    local res = check_url_permission(server, appid, action, url, clientIP, wolf_token)
-    core.log.info(" check_url_permission(", core.json.delay_encode(permItem), ") res: ",core.json.delay_encode(res))
+    local res = check_url_permission(server, appid, action, url,
+                    clientIP, wolf_token)
+    core.log.info(" check_url_permission(", core.json.delay_encode(permItem),
+                  ") res: ",core.json.delay_encode(res))
 
     local username = nil
     local nickname = nil
@@ -278,7 +292,8 @@ function _M.rewrite(conf, ctx)
         local userInfo = res.userInfo
         core.response.set_header("X-UserId", userInfo.id)
         core.response.set_header("X-Username", userInfo.username)
-        core.response.set_header("X-Nickname", ngx.escape_uri(userInfo.nickname) or userInfo.username)
+        core.response.set_header("X-Nickname",
+            ngx.escape_uri(userInfo.nickname) or userInfo.username)
         ctx.userInfo = userInfo
         username = userInfo.username
         nickname = userInfo.nickname
@@ -286,9 +301,11 @@ function _M.rewrite(conf, ctx)
 
     if res.status ~= 200 then
         -- no permission.
-        core.log.error(" check_url_permission(", core.json.delay_encode(permItem),
+        core.log.error(" check_url_permission(",
+            core.json.delay_encode(permItem),
             ") failed, res: ",core.json.delay_encode(res))
-        return 401, {message = res.err, username = username, nickname = nickname}
+        return 401, {message = res.err, username = username,
+                     nickname = nickname}
     end
     core.log.info("wolf-rbac check permission passed")
 end
@@ -297,7 +314,8 @@ local function get_args()
     local ctx = ngx.ctx.api_ctx
     local args, err
     ngx.req.read_body()
-    if string.find(ctx.var.http_content_type or "","application/json", 1, true) then
+    if string.find(ctx.var.http_content_type or "","application/json",
+                   1, true) then
         args, err = json.decode(ngx.req.get_body_data())
         if err then
             core.log.error("json.decode(", ngx.req.get_body_data(), ") failed! ", err)
@@ -305,6 +323,7 @@ local function get_args()
     else
         args = ngx.req.get_post_args()
     end
+
     return args
 end
 
@@ -332,7 +351,9 @@ local function login()
     local consumer = consumers[appid]
     if not consumer then
         core.log.info("request appid [", appid, "] not found")
-        return core.response.exit(400, {message = "appid [" .. tostring(appid) .. "] not found"})
+        return core.response.exit(400,
+                {message = "appid [" .. tostring(appid) .. "] not found"}
+               )
     end
 
     core.log.info("consumer: ", core.json.delay_encode(consumer))
@@ -342,18 +363,31 @@ local function login()
     headers["Content-Type"] = "application/json; charset=utf-8"
     local timeout = 1000 * 5
     local request_debug = core.json.delay_encode(
-        {method = 'POST', uri = uri, body = args, headers = headers,timeout = timeout})
+        {
+            method = 'POST', uri = uri, body = args,
+            headers = headers,timeout = timeout
+        }
+    )
+
     core.log.info("login request [", request_debug, "] ....")
     local res, err = http_post(uri, core.json.encode(args), headers, timeout)
     if err or not res then
         core.log.error("login request [", request_debug, "] failed! err: ", err)
-        return core.response.exit(500, {message = "request to wolf-server failed! " .. tostring(err)})
+        return core.response.exit(500,
+                {message = "request to wolf-server failed! " .. tostring(err)})
     end
-    core.log.info("login request [", request_debug, "] status: ", res.status, ", body: ", res.body)
+    core.log.info("login request [", request_debug, "] status: ", res.status,
+                  ", body: ", res.body)
 
     if res.status ~= 200 then
-        core.log.error("login request [", request_debug, "] failed! status: ", res.status)
-        return core.response.exit(500, {message = "request to wolf-server failed! status:" .. tostring(res.status) })
+        core.log.error("login request [", request_debug, "] failed! status: ",
+                       res.status)
+        return core.response.exit(500,
+            {
+                message = "request to wolf-server failed! status:"
+                          .. tostring(res.status)
+            }
+        )
     end
     local body, err = json.decode(res.body)
     if err or not body then
@@ -361,10 +395,12 @@ local function login()
         return core.response.exit(500, {message = "request to wolf-server failed!"})
     end
     if not body.ok then
-        core.log.error("user login [", request_debug, "] failed! response body:", core.json.delay_encode(body))
+        core.log.error("user login [", request_debug, "] failed! response body:",
+                       core.json.delay_encode(body))
         return core.response.exit(200, {message = body.reason})
     end
-    core.log.info("user login [", request_debug, "] success! response body:", core.json.delay_encode(body))
+    core.log.info("user login [", request_debug, "] success! response body:",
+                  core.json.delay_encode(body))
 
     local userInfo = body.data.userInfo
     local wolf_token = body.data.token
diff --git a/utils/check-lua-code-style.sh b/utils/check-lua-code-style.sh
new file mode 100755
index 0000000..f8f0265
--- /dev/null
+++ b/utils/check-lua-code-style.sh
@@ -0,0 +1,42 @@
+#!/bin/sh
+
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+set -ex
+
+luacheck -q lua
+
+./utils/lj-releng lua/*.lua \
+    lua/apisix/*.lua \
+    lua/apisix/admin/*.lua \
+    lua/apisix/core/*.lua \
+    lua/apisix/http/*.lua \
+    lua/apisix/http/router/*.lua \
+    lua/apisix/plugins/*.lua \
+    lua/apisix/plugins/grpc-transcode/*.lua \
+    lua/apisix/plugins/limit-count/*.lua > \
+    /tmp/check.log 2>&1 || (cat /tmp/check.log && exit 1)
+
+sed -i 's/.*newproxy.*//g' /tmp/check.log
+count=`grep -E ".lua:[0-9]+:" /tmp/check.log -c || true`
+
+if [ $count -ne 0 ]
+then
+    cat /tmp/check.log
+    exit 1
+fi