membphis commented on a change in pull request #2192: URL: https://github.com/apache/apisix/pull/2192#discussion_r487736639
########## File path: apisix/plugins/hmac-auth.lua ########## @@ -0,0 +1,298 @@ +-- +-- 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. +-- +local ngx = ngx +local type = type +local select = select +local abs = math.abs +local ngx_time = ngx.time +local str_fmt = string.format +local ngx_re = require("ngx.re") +local ngx_req = ngx.req +local pairs = pairs +local ipairs = ipairs +local hmac_sha1 = ngx.hmac_sha1 +local escape_uri = ngx.escape_uri +local core = require("apisix.core") +local hmac = require("resty.hmac") +local consumer = require("apisix.consumer") +local ngx_decode_base64 = ngx.decode_base64 + +local SIGNATURE_KEY = "X-HMAC-SIGNATURE" +local ALGORITHM_KEY = "X-HMAC-ALGORITHM" +local TIMESTAMP_KEY = "X-HMAC-TIMESTAMP" +local ACCESS_KEY = "X-HMAC-ACCESS-KEY" +local plugin_name = "hmac-auth" + +local schema = { + type = "object", + oneOf = { + { + title = "work with consumer object", + properties = { + access_key = {type = "string", minLength = 1, maxLength = 256}, + secret_key = {type = "string", minLength = 1, maxLength = 256}, + algorithm = { + type = "string", + enum = {"hmac-sha1", "hmac-sha256", "hmac-sha512"}, + default = "hmac-sha256" + }, + clock_skew = { + type = "integer", + default = 300 + } + }, + required = {"access_key", "secret_key"}, + additionalProperties = false, + }, + { + title = "work with route or service object", + properties = {}, + additionalProperties = false, + } + } +} + +local _M = { + version = 0.1, + priority = 2530, + type = 'auth', + name = plugin_name, + schema = schema, +} + +local hmac_funcs = { + ["hmac-sha1"] = function(secret_key, message) + return hmac_sha1(secret_key, message) + end, + ["hmac-sha256"] = function(secret_key, message) + return hmac:new(secret_key, hmac.ALGOS.SHA256):final(message) + end, + ["hmac-sha512"] = function(secret_key, message) + return hmac:new(secret_key, hmac.ALGOS.SHA512):final(message) + end, +} + + +local function try_attr(t, ...) + local conf + local count = select('#', ...) + for i = 1, count do + local attr = select(i, ...) + conf = t[attr] + if type(conf) ~= "table" then + return false + end + end + + return true +end + + +local create_consumer_cache +do + local consumer_ids = {} + + function create_consumer_cache(consumers) + core.table.clear(consumer_ids) + + for _, consumer in ipairs(consumers.nodes) do + core.log.info("consumer node: ", core.json.delay_encode(consumer)) + consumer_ids[consumer.auth_conf.access_key] = consumer + end + + return consumer_ids + end + +end -- do + + +function _M.check_schema(conf) + core.log.info("input conf: ", core.json.delay_encode(conf)) + + return core.schema.check(schema, conf) +end + + +local function get_consumer(access_key) + if not access_key then + return nil, {message = "missing access key"} + end + + local consumer_conf = consumer.plugin(plugin_name) + if not consumer_conf then + return nil, {message = "Missing related consumer"} + end + + local consumers = core.lrucache.plugin(plugin_name, "consumers_key", + consumer_conf.conf_version, + create_consumer_cache, consumer_conf) + + local consumer = consumers[access_key] + if not consumer then + return nil, {message = "Invalid access key"} + end + core.log.info("consumer: ", core.json.delay_encode(consumer)) + + return consumer, nil Review comment: `, nil` is useless ########## File path: apisix/plugins/hmac-auth.lua ########## @@ -0,0 +1,298 @@ +-- +-- 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. +-- +local ngx = ngx +local type = type +local select = select +local abs = math.abs +local ngx_time = ngx.time +local str_fmt = string.format +local ngx_re = require("ngx.re") +local ngx_req = ngx.req +local pairs = pairs +local ipairs = ipairs +local hmac_sha1 = ngx.hmac_sha1 +local escape_uri = ngx.escape_uri +local core = require("apisix.core") +local hmac = require("resty.hmac") +local consumer = require("apisix.consumer") +local ngx_decode_base64 = ngx.decode_base64 + +local SIGNATURE_KEY = "X-HMAC-SIGNATURE" +local ALGORITHM_KEY = "X-HMAC-ALGORITHM" +local TIMESTAMP_KEY = "X-HMAC-TIMESTAMP" +local ACCESS_KEY = "X-HMAC-ACCESS-KEY" +local plugin_name = "hmac-auth" + +local schema = { + type = "object", + oneOf = { + { + title = "work with consumer object", + properties = { + access_key = {type = "string", minLength = 1, maxLength = 256}, + secret_key = {type = "string", minLength = 1, maxLength = 256}, + algorithm = { + type = "string", + enum = {"hmac-sha1", "hmac-sha256", "hmac-sha512"}, + default = "hmac-sha256" + }, + clock_skew = { + type = "integer", + default = 300 + } + }, + required = {"access_key", "secret_key"}, + additionalProperties = false, + }, + { + title = "work with route or service object", + properties = {}, + additionalProperties = false, + } + } +} + +local _M = { + version = 0.1, + priority = 2530, + type = 'auth', + name = plugin_name, + schema = schema, +} + +local hmac_funcs = { + ["hmac-sha1"] = function(secret_key, message) + return hmac_sha1(secret_key, message) + end, + ["hmac-sha256"] = function(secret_key, message) + return hmac:new(secret_key, hmac.ALGOS.SHA256):final(message) + end, + ["hmac-sha512"] = function(secret_key, message) + return hmac:new(secret_key, hmac.ALGOS.SHA512):final(message) + end, +} + + +local function try_attr(t, ...) + local conf + local count = select('#', ...) + for i = 1, count do + local attr = select(i, ...) + conf = t[attr] + if type(conf) ~= "table" then + return false + end + end + + return true +end + + +local create_consumer_cache +do + local consumer_ids = {} + + function create_consumer_cache(consumers) + core.table.clear(consumer_ids) + + for _, consumer in ipairs(consumers.nodes) do + core.log.info("consumer node: ", core.json.delay_encode(consumer)) + consumer_ids[consumer.auth_conf.access_key] = consumer + end + + return consumer_ids + end + +end -- do + + +function _M.check_schema(conf) + core.log.info("input conf: ", core.json.delay_encode(conf)) + + return core.schema.check(schema, conf) +end + + +local function get_consumer(access_key) + if not access_key then + return nil, {message = "missing access key"} + end + + local consumer_conf = consumer.plugin(plugin_name) + if not consumer_conf then + return nil, {message = "Missing related consumer"} + end + + local consumers = core.lrucache.plugin(plugin_name, "consumers_key", + consumer_conf.conf_version, + create_consumer_cache, consumer_conf) + + local consumer = consumers[access_key] + if not consumer then + return nil, {message = "Invalid access key"} + end + core.log.info("consumer: ", core.json.delay_encode(consumer)) + + return consumer, nil +end + + +local function generate_signature(ctx, secret_key, params) + local canonical_uri = ctx.var.uri + local canonical_query_string = "" + local request_method = ngx_req.get_method() + local args = ngx_req.get_uri_args() + + if canonical_uri == "" then + canonical_uri = "/" + end + + if type(args) == "table" then + local keys = {} + local query_tab = {} + + for k, v in pairs(args) do + core.table.insert(keys, k) + end + core.table.sort(keys) + + for _, key in pairs(keys) do + local param = args[key] + if type(param) == "table" then + for _, vval in pairs(param) do + core.table.insert(query_tab, escape_uri(key) .. "=" .. escape_uri(vval)) + end + else + core.table.insert(query_tab, escape_uri(key) .. "=" .. escape_uri(param)) + end + end + canonical_query_string = core.table.concat(query_tab, "&") + end + + local req_body = core.request.get_body() + req_body = req_body or "" + + local signing_string = request_method .. canonical_uri .. canonical_query_string .. + req_body .. params.access_key .. params.timestamp .. secret_key + + return hmac_funcs[params.algorithm](secret_key, signing_string) +end + + +local function validate(ctx, params) + if not params.access_key or not params.signature then + return nil, {message = "access key or signature missing"} + end + + local consumer, err = get_consumer(params.access_key) + if err then + return nil, err + end + + local conf = consumer.auth_conf + if conf.algorithm ~= params.algorithm then + return nil, {message = str_fmt("algorithm %s not supported", params.algorithm)} Review comment: use `..` to concat new string ########## File path: t/plugin/hmac-auth.t ########## @@ -0,0 +1,581 @@ +# +# 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. +# +BEGIN { + $ENV{"CUSTOM_HMAC_AUTH"} = "false" +} + +use t::APISIX 'no_plan'; + +repeat_each(2); +no_long_string(); +no_root_location(); +no_shuffle(); +run_tests; + +__DATA__ + +=== TEST 1: add consumer with username and plugins +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/consumers', + ngx.HTTP_PUT, + [[{ + "username": "jack", + "plugins": { + "hmac-auth": { + "access_key": "my-access-key", + "secret_key": "my-secret-key" + } + } + }]], + [[{ + "node": { + "value": { + "username": "jack", + "plugins": { + "hmac-auth": { + "access_key": "my-access-key", + "secret_key": "my-secret-key", + "algorithm": "hmac-sha256", + "clock_skew": 300 + } + } + } + }, + "action": "set" + }]] + ) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 2: add consumer with plugin hmac-auth - missing secret key +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/consumers', + ngx.HTTP_PUT, + [[{ + "username": "foo", + "plugins": { + "hmac-auth": { + "access_key": "user-key" + } + } + }]]) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- error_code: 400 +--- no_error_log +[error] + + + +=== TEST 3: add consumer with plugin hmac-auth - missing access key +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/consumers', + ngx.HTTP_PUT, + [[{ + "username": "bar", + "plugins": { + "hmac-auth": { + "secret_key": "skey" + } + } + }]]) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- error_code: 400 +--- no_error_log +[error] + + + +=== TEST 4: add consumer with plugin hmac-auth - access key exceeds the length limit +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/consumers', + ngx.HTTP_PUT, + [[{ + "username": "li", + "plugins": { + "hmac-auth": { + "access_key": "akeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakey", + "secret_key": "skey" + } + } + }]]) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- error_code: 400 Review comment: ditto ########## File path: t/plugin/hmac-auth.t ########## @@ -0,0 +1,581 @@ +# +# 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. +# +BEGIN { + $ENV{"CUSTOM_HMAC_AUTH"} = "false" +} + +use t::APISIX 'no_plan'; + +repeat_each(2); +no_long_string(); +no_root_location(); +no_shuffle(); +run_tests; + +__DATA__ + +=== TEST 1: add consumer with username and plugins +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/consumers', + ngx.HTTP_PUT, + [[{ + "username": "jack", + "plugins": { + "hmac-auth": { + "access_key": "my-access-key", + "secret_key": "my-secret-key" + } + } + }]], + [[{ + "node": { + "value": { + "username": "jack", + "plugins": { + "hmac-auth": { + "access_key": "my-access-key", + "secret_key": "my-secret-key", + "algorithm": "hmac-sha256", + "clock_skew": 300 + } + } + } + }, + "action": "set" + }]] + ) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 2: add consumer with plugin hmac-auth - missing secret key +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/consumers', + ngx.HTTP_PUT, + [[{ + "username": "foo", + "plugins": { + "hmac-auth": { + "access_key": "user-key" + } + } + }]]) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- error_code: 400 +--- no_error_log +[error] + + + +=== TEST 3: add consumer with plugin hmac-auth - missing access key +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/consumers', + ngx.HTTP_PUT, + [[{ + "username": "bar", + "plugins": { + "hmac-auth": { + "secret_key": "skey" + } + } + }]]) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- error_code: 400 +--- no_error_log +[error] + + + +=== TEST 4: add consumer with plugin hmac-auth - access key exceeds the length limit +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/consumers', + ngx.HTTP_PUT, + [[{ + "username": "li", + "plugins": { + "hmac-auth": { + "access_key": "akeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakey", + "secret_key": "skey" + } + } + }]]) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- error_code: 400 +--- no_error_log +[error] + + + +=== TEST 5: add consumer with plugin hmac-auth - access key exceeds the length limit +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/consumers', + ngx.HTTP_PUT, + [[{ + "username": "zhang", + "plugins": { + "hmac-auth": { + "access_key": "akey", + "secret_key": "skeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskey" + } + } + }]]) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- error_code: 400 +--- no_error_log +[error] + + + +=== TEST 6: enable hmac auth plugin using admin api +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "plugins": { + "hmac-auth": {} + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/hello" + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 7: verify, missing signature +--- request +GET /hello +--- error_code: 401 +--- response_body +{"message":"access key or signature missing"} +--- no_error_log +[error] + + + +=== TEST 8: verify: invalid access key +--- request +GET /hello +--- more_headers +X-HMAC-SIGNATURE: asdf +X-HMAC-ALGORITHM: hmac-sha256 +X-HMAC-TIMESTAMP: 112 +X-HMAC-ACCESS-KEY: sdf +--- error_code: 401 +--- response_body +{"message":"Invalid access key"} +--- no_error_log +[error] + + + +=== TEST 9: verify: invalid algorithm +--- request +GET /hello +--- more_headers +X-HMAC-SIGNATURE: asdf +X-HMAC-ALGORITHM: ljlj +X-HMAC-TIMESTAMP: 112 +X-HMAC-ACCESS-KEY: sdf +--- error_code: 401 +--- response_body +{"message":"Invalid access key"} +--- no_error_log +[error] + + + +=== TEST 10: verify: invalid timestamp +--- request +GET /hello +--- more_headers +X-HMAC-SIGNATURE: asdf +X-HMAC-ALGORITHM: hmac-sha256 +X-HMAC-TIMESTAMP: 112 +X-HMAC-ACCESS-KEY: my-access-key +--- error_code: 401 +--- response_body +{"message":"Invalid timestamp"} +--- no_error_log +[error] + + + +=== TEST 11: verify: ok +--- config +location /t { + content_by_lua_block { + local ngx_time = ngx.time + local core = require("apisix.core") + local t = require("lib.test_admin") + local hmac = require("resty.hmac") + local ngx_encode_base64 = ngx.encode_base64 + + local secret_key = "my-secret-key" + local timestamp = ngx_time() + local access_key = "my-access-key" + local signing_string = "GET" .. "/hello" .. "" .. + "" .. access_key .. timestamp .. secret_key Review comment: bad indentation ########## File path: t/plugin/hmac-auth.t ########## @@ -0,0 +1,581 @@ +# +# 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. +# +BEGIN { + $ENV{"CUSTOM_HMAC_AUTH"} = "false" +} + +use t::APISIX 'no_plan'; + +repeat_each(2); +no_long_string(); +no_root_location(); +no_shuffle(); +run_tests; + +__DATA__ + +=== TEST 1: add consumer with username and plugins +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/consumers', + ngx.HTTP_PUT, + [[{ + "username": "jack", + "plugins": { + "hmac-auth": { + "access_key": "my-access-key", + "secret_key": "my-secret-key" + } + } + }]], + [[{ + "node": { + "value": { + "username": "jack", + "plugins": { + "hmac-auth": { + "access_key": "my-access-key", + "secret_key": "my-secret-key", + "algorithm": "hmac-sha256", + "clock_skew": 300 + } + } + } + }, + "action": "set" + }]] + ) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 2: add consumer with plugin hmac-auth - missing secret key +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/consumers', + ngx.HTTP_PUT, + [[{ + "username": "foo", + "plugins": { + "hmac-auth": { + "access_key": "user-key" + } + } + }]]) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- error_code: 400 +--- no_error_log +[error] + + + +=== TEST 3: add consumer with plugin hmac-auth - missing access key +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/consumers', + ngx.HTTP_PUT, + [[{ + "username": "bar", + "plugins": { + "hmac-auth": { + "secret_key": "skey" + } + } + }]]) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- error_code: 400 +--- no_error_log +[error] + + + +=== TEST 4: add consumer with plugin hmac-auth - access key exceeds the length limit +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/consumers', + ngx.HTTP_PUT, + [[{ + "username": "li", + "plugins": { + "hmac-auth": { + "access_key": "akeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakey", + "secret_key": "skey" + } + } + }]]) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- error_code: 400 +--- no_error_log +[error] + + + +=== TEST 5: add consumer with plugin hmac-auth - access key exceeds the length limit +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/consumers', + ngx.HTTP_PUT, + [[{ + "username": "zhang", + "plugins": { + "hmac-auth": { + "access_key": "akey", + "secret_key": "skeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskey" + } + } + }]]) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- error_code: 400 +--- no_error_log +[error] + + + +=== TEST 6: enable hmac auth plugin using admin api +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "plugins": { + "hmac-auth": {} + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/hello" + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 7: verify, missing signature +--- request +GET /hello +--- error_code: 401 +--- response_body +{"message":"access key or signature missing"} +--- no_error_log +[error] + + + +=== TEST 8: verify: invalid access key +--- request +GET /hello +--- more_headers +X-HMAC-SIGNATURE: asdf +X-HMAC-ALGORITHM: hmac-sha256 +X-HMAC-TIMESTAMP: 112 +X-HMAC-ACCESS-KEY: sdf +--- error_code: 401 +--- response_body +{"message":"Invalid access key"} +--- no_error_log +[error] + + + +=== TEST 9: verify: invalid algorithm +--- request +GET /hello +--- more_headers +X-HMAC-SIGNATURE: asdf +X-HMAC-ALGORITHM: ljlj +X-HMAC-TIMESTAMP: 112 +X-HMAC-ACCESS-KEY: sdf +--- error_code: 401 +--- response_body +{"message":"Invalid access key"} +--- no_error_log +[error] + + + +=== TEST 10: verify: invalid timestamp +--- request +GET /hello +--- more_headers +X-HMAC-SIGNATURE: asdf +X-HMAC-ALGORITHM: hmac-sha256 +X-HMAC-TIMESTAMP: 112 +X-HMAC-ACCESS-KEY: my-access-key +--- error_code: 401 +--- response_body +{"message":"Invalid timestamp"} +--- no_error_log +[error] + + + +=== TEST 11: verify: ok +--- config +location /t { + content_by_lua_block { + local ngx_time = ngx.time + local core = require("apisix.core") + local t = require("lib.test_admin") + local hmac = require("resty.hmac") + local ngx_encode_base64 = ngx.encode_base64 + + local secret_key = "my-secret-key" + local timestamp = ngx_time() + local access_key = "my-access-key" + local signing_string = "GET" .. "/hello" .. "" .. + "" .. access_key .. timestamp .. secret_key + + local signature = hmac:new(secret_key, hmac.ALGOS.SHA256):final(signing_string) + core.log.info("signature:", ngx_encode_base64(signature)) + local headers = {} + headers["X-HMAC-SIGNATURE"] = ngx_encode_base64(signature) + headers["X-HMAC-ALGORITHM"] = "hmac-sha256" + headers["X-HMAC-TIMESTAMP"] = timestamp + headers["X-HMAC-ACCESS-KEY"] = access_key + + local code, body = t.test('/hello', + ngx.HTTP_GET, + "", + nil, + headers + ) + + ngx.status = code + ngx.say(body) + } +} +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 12: add consumer with 0 clock skew +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/consumers', + ngx.HTTP_PUT, + [[{ + "username": "robin", + "plugins": { + "hmac-auth": { + "access_key": "my-access-key3", + "secret_key": "my-secret-key3", + "clock_skew": 0 + } + } + }]], + [[{ + "node": { + "value": { + "username": "robin", + "plugins": { + "hmac-auth": { + "access_key": "my-access-key3", + "secret_key": "my-secret-key3", + "algorithm": "hmac-sha256", + "clock_skew": 0 + } + } + } + }, + "action": "set" + }]] + ) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 13: verify: invalid signature +--- request +GET /hello +--- more_headers +X-HMAC-SIGNATURE: asdf +X-HMAC-ALGORITHM: hmac-sha256 +X-HMAC-TIMESTAMP: 112 +X-HMAC-ACCESS-KEY: my-access-key3 +--- error_code: 401 +--- response_body +{"message":"Invalid signature"} +--- no_error_log +[error] + + + +=== TEST 14: add consumer with 1 clock skew +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/consumers', + ngx.HTTP_PUT, + [[{ + "username": "pony", + "plugins": { + "hmac-auth": { + "access_key": "my-access-key2", + "secret_key": "my-secret-key2", + "clock_skew": 1 + } + } + }]], + [[{ + "node": { + "value": { + "username": "pony", + "plugins": { + "hmac-auth": { + "access_key": "my-access-key2", + "secret_key": "my-secret-key2", + "algorithm": "hmac-sha256", + "clock_skew": 1 + } + } + } + }, + "action": "set" + }]] + ) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 15: verify: invalid timestamp +--- config +location /t { + content_by_lua_block { + local ngx_time = ngx.time + local core = require("apisix.core") + local t = require("lib.test_admin") + local hmac = require("resty.hmac") + local ngx_encode_base64 = ngx.encode_base64 + + local secret_key = "my-secret-key2" + local timestamp = ngx_time() + local access_key = "my-access-key2" + local signing_string = "GET" .. "/hello" .. "" .. + "" .. access_key .. timestamp .. secret_key + + ngx.sleep(2) + + local signature = hmac:new(secret_key, hmac.ALGOS.SHA256):final(signing_string) + core.log.info("signature:", ngx_encode_base64(signature)) + local headers = {} + headers["X-HMAC-SIGNATURE"] = ngx_encode_base64(signature) + headers["X-HMAC-ALGORITHM"] = "hmac-sha256" + headers["X-HMAC-TIMESTAMP"] = timestamp + headers["X-HMAC-ACCESS-KEY"] = access_key + + local code, body = t.test('/hello', + ngx.HTTP_GET, + core.json.encode(data), + nil, + headers + ) + + ngx.status = code + ngx.say(body) + } +} +--- request +GET /t +--- error_code: 401 +--- response_body eval +qr/\{"message":"Invalid timestamp"\}/ +--- no_error_log +[error] + + + +=== TEST 16: verify: put ok +--- config +location /t { + content_by_lua_block { + local ngx_time = ngx.time + local core = require("apisix.core") + local t = require("lib.test_admin") + local hmac = require("resty.hmac") + local ngx_encode_base64 = ngx.encode_base64 + + local data = {cert = ssl_cert, key = ssl_key, sni = "test.com"} Review comment: `ssl_cert `, global vairable? it should be `nil` always ########## File path: apisix/plugins/hmac-auth.lua ########## @@ -0,0 +1,298 @@ +-- +-- 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. +-- +local ngx = ngx +local type = type +local select = select +local abs = math.abs +local ngx_time = ngx.time +local str_fmt = string.format +local ngx_re = require("ngx.re") +local ngx_req = ngx.req +local pairs = pairs +local ipairs = ipairs +local hmac_sha1 = ngx.hmac_sha1 +local escape_uri = ngx.escape_uri +local core = require("apisix.core") +local hmac = require("resty.hmac") +local consumer = require("apisix.consumer") +local ngx_decode_base64 = ngx.decode_base64 + +local SIGNATURE_KEY = "X-HMAC-SIGNATURE" +local ALGORITHM_KEY = "X-HMAC-ALGORITHM" +local TIMESTAMP_KEY = "X-HMAC-TIMESTAMP" +local ACCESS_KEY = "X-HMAC-ACCESS-KEY" +local plugin_name = "hmac-auth" + +local schema = { + type = "object", + oneOf = { + { + title = "work with consumer object", + properties = { + access_key = {type = "string", minLength = 1, maxLength = 256}, + secret_key = {type = "string", minLength = 1, maxLength = 256}, + algorithm = { + type = "string", + enum = {"hmac-sha1", "hmac-sha256", "hmac-sha512"}, + default = "hmac-sha256" + }, + clock_skew = { + type = "integer", + default = 300 + } + }, + required = {"access_key", "secret_key"}, + additionalProperties = false, + }, + { + title = "work with route or service object", + properties = {}, + additionalProperties = false, + } + } +} + +local _M = { + version = 0.1, + priority = 2530, + type = 'auth', + name = plugin_name, + schema = schema, +} + +local hmac_funcs = { + ["hmac-sha1"] = function(secret_key, message) + return hmac_sha1(secret_key, message) + end, + ["hmac-sha256"] = function(secret_key, message) + return hmac:new(secret_key, hmac.ALGOS.SHA256):final(message) + end, + ["hmac-sha512"] = function(secret_key, message) + return hmac:new(secret_key, hmac.ALGOS.SHA512):final(message) + end, +} + + +local function try_attr(t, ...) + local conf + local count = select('#', ...) + for i = 1, count do + local attr = select(i, ...) + conf = t[attr] + if type(conf) ~= "table" then + return false + end + end + + return true +end + + +local create_consumer_cache +do + local consumer_ids = {} + + function create_consumer_cache(consumers) + core.table.clear(consumer_ids) + + for _, consumer in ipairs(consumers.nodes) do + core.log.info("consumer node: ", core.json.delay_encode(consumer)) + consumer_ids[consumer.auth_conf.access_key] = consumer + end + + return consumer_ids + end + +end -- do + + +function _M.check_schema(conf) + core.log.info("input conf: ", core.json.delay_encode(conf)) + + return core.schema.check(schema, conf) +end + + +local function get_consumer(access_key) + if not access_key then + return nil, {message = "missing access key"} + end + + local consumer_conf = consumer.plugin(plugin_name) + if not consumer_conf then + return nil, {message = "Missing related consumer"} + end + + local consumers = core.lrucache.plugin(plugin_name, "consumers_key", + consumer_conf.conf_version, + create_consumer_cache, consumer_conf) + + local consumer = consumers[access_key] + if not consumer then + return nil, {message = "Invalid access key"} + end + core.log.info("consumer: ", core.json.delay_encode(consumer)) + + return consumer, nil +end + + +local function generate_signature(ctx, secret_key, params) + local canonical_uri = ctx.var.uri + local canonical_query_string = "" + local request_method = ngx_req.get_method() + local args = ngx_req.get_uri_args() + + if canonical_uri == "" then + canonical_uri = "/" + end + + if type(args) == "table" then + local keys = {} + local query_tab = {} + + for k, v in pairs(args) do + core.table.insert(keys, k) + end + core.table.sort(keys) + + for _, key in pairs(keys) do + local param = args[key] + if type(param) == "table" then + for _, vval in pairs(param) do + core.table.insert(query_tab, escape_uri(key) .. "=" .. escape_uri(vval)) + end + else + core.table.insert(query_tab, escape_uri(key) .. "=" .. escape_uri(param)) + end + end + canonical_query_string = core.table.concat(query_tab, "&") + end + + local req_body = core.request.get_body() + req_body = req_body or "" + + local signing_string = request_method .. canonical_uri .. canonical_query_string .. + req_body .. params.access_key .. params.timestamp .. secret_key + + return hmac_funcs[params.algorithm](secret_key, signing_string) +end + + +local function validate(ctx, params) + if not params.access_key or not params.signature then + return nil, {message = "access key or signature missing"} + end + + local consumer, err = get_consumer(params.access_key) + if err then + return nil, err + end + + local conf = consumer.auth_conf + if conf.algorithm ~= params.algorithm then + return nil, {message = str_fmt("algorithm %s not supported", params.algorithm)} + end + + core.log.info("conf.clock_skew:", conf.clock_skew) + if conf.clock_skew and conf.clock_skew > 0 then + local diff = abs(ngx_time() - params.timestamp) + core.log.info("conf.diff:", diff) + if diff > conf.clock_skew then + return nil, {message = "Invalid timestamp"} + end + end + + local secret_key = conf and conf.secret_key + local request_signature = ngx_decode_base64(params.signature) + local generated_signature = generate_signature(ctx, secret_key, params) + + core.log.info("request_signature: ", request_signature, + " generated_signature: ", generated_signature) + + if request_signature ~= generated_signature then + return nil, {message = "Invalid signature"} + end + + return consumer +end + +local function get_params(ctx) + local params = {} + local local_conf = core.config.local_conf() + local signature_key = SIGNATURE_KEY + local algorithm_key = ALGORITHM_KEY + local timestamp_key = TIMESTAMP_KEY + local access_key = ACCESS_KEY + + if try_attr(local_conf, "plugin_attr", "hmac-auth") then + local attr = local_conf.plugin_attr["hmac-auth"] + signature_key = attr.signature_key or signature_key + algorithm_key = attr.algorithm_key or algorithm_key + timestamp_key = attr.timestamp_key or timestamp_key + access_key = attr.access_key or access_key + end + + local ak = core.request.header(ctx, access_key) + local signature = core.request.header(ctx, signature_key) + local algorithm = core.request.header(ctx, algorithm_key) + local timestamp = core.request.header(ctx, timestamp_key) + + -- get params from header `Authorization` + if not ak then + local auth_string = core.request.header(ctx, "Authorization") Review comment: it may be "nil", and we need to add a test case to check this case ########## File path: apisix/plugins/hmac-auth.lua ########## @@ -0,0 +1,298 @@ +-- +-- 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. +-- +local ngx = ngx +local type = type +local select = select +local abs = math.abs +local ngx_time = ngx.time +local str_fmt = string.format +local ngx_re = require("ngx.re") +local ngx_req = ngx.req +local pairs = pairs +local ipairs = ipairs +local hmac_sha1 = ngx.hmac_sha1 +local escape_uri = ngx.escape_uri +local core = require("apisix.core") +local hmac = require("resty.hmac") +local consumer = require("apisix.consumer") +local ngx_decode_base64 = ngx.decode_base64 + +local SIGNATURE_KEY = "X-HMAC-SIGNATURE" +local ALGORITHM_KEY = "X-HMAC-ALGORITHM" +local TIMESTAMP_KEY = "X-HMAC-TIMESTAMP" +local ACCESS_KEY = "X-HMAC-ACCESS-KEY" +local plugin_name = "hmac-auth" + +local schema = { + type = "object", + oneOf = { + { + title = "work with consumer object", + properties = { + access_key = {type = "string", minLength = 1, maxLength = 256}, + secret_key = {type = "string", minLength = 1, maxLength = 256}, + algorithm = { + type = "string", + enum = {"hmac-sha1", "hmac-sha256", "hmac-sha512"}, + default = "hmac-sha256" + }, + clock_skew = { + type = "integer", + default = 300 + } + }, + required = {"access_key", "secret_key"}, + additionalProperties = false, + }, + { + title = "work with route or service object", + properties = {}, + additionalProperties = false, + } + } +} + +local _M = { + version = 0.1, + priority = 2530, + type = 'auth', + name = plugin_name, + schema = schema, +} + +local hmac_funcs = { + ["hmac-sha1"] = function(secret_key, message) + return hmac_sha1(secret_key, message) + end, + ["hmac-sha256"] = function(secret_key, message) + return hmac:new(secret_key, hmac.ALGOS.SHA256):final(message) + end, + ["hmac-sha512"] = function(secret_key, message) + return hmac:new(secret_key, hmac.ALGOS.SHA512):final(message) + end, +} + + +local function try_attr(t, ...) + local conf + local count = select('#', ...) + for i = 1, count do + local attr = select(i, ...) + conf = t[attr] + if type(conf) ~= "table" then + return false + end + end + + return true +end + + +local create_consumer_cache +do + local consumer_ids = {} + + function create_consumer_cache(consumers) + core.table.clear(consumer_ids) + + for _, consumer in ipairs(consumers.nodes) do + core.log.info("consumer node: ", core.json.delay_encode(consumer)) + consumer_ids[consumer.auth_conf.access_key] = consumer + end + + return consumer_ids + end + +end -- do + + +function _M.check_schema(conf) + core.log.info("input conf: ", core.json.delay_encode(conf)) + + return core.schema.check(schema, conf) +end + + +local function get_consumer(access_key) + if not access_key then + return nil, {message = "missing access key"} + end + + local consumer_conf = consumer.plugin(plugin_name) + if not consumer_conf then + return nil, {message = "Missing related consumer"} + end + + local consumers = core.lrucache.plugin(plugin_name, "consumers_key", + consumer_conf.conf_version, + create_consumer_cache, consumer_conf) + + local consumer = consumers[access_key] + if not consumer then + return nil, {message = "Invalid access key"} + end + core.log.info("consumer: ", core.json.delay_encode(consumer)) + + return consumer, nil +end + + +local function generate_signature(ctx, secret_key, params) + local canonical_uri = ctx.var.uri + local canonical_query_string = "" + local request_method = ngx_req.get_method() + local args = ngx_req.get_uri_args() + + if canonical_uri == "" then + canonical_uri = "/" + end + + if type(args) == "table" then + local keys = {} + local query_tab = {} + + for k, v in pairs(args) do + core.table.insert(keys, k) + end + core.table.sort(keys) + + for _, key in pairs(keys) do + local param = args[key] + if type(param) == "table" then + for _, vval in pairs(param) do + core.table.insert(query_tab, escape_uri(key) .. "=" .. escape_uri(vval)) + end + else + core.table.insert(query_tab, escape_uri(key) .. "=" .. escape_uri(param)) + end + end + canonical_query_string = core.table.concat(query_tab, "&") + end + + local req_body = core.request.get_body() + req_body = req_body or "" + + local signing_string = request_method .. canonical_uri .. canonical_query_string .. Review comment: please take a look at this style: ```lua local signing_string = request_method .. canonical_uri .. canonical_query_string .. req_body .. params.access_key .. params.timestamp .. secret_key ``` ########## File path: t/plugin/hmac-auth.t ########## @@ -0,0 +1,581 @@ +# +# 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. +# +BEGIN { + $ENV{"CUSTOM_HMAC_AUTH"} = "false" +} + +use t::APISIX 'no_plan'; + +repeat_each(2); +no_long_string(); +no_root_location(); +no_shuffle(); +run_tests; + +__DATA__ + +=== TEST 1: add consumer with username and plugins +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/consumers', + ngx.HTTP_PUT, + [[{ + "username": "jack", + "plugins": { + "hmac-auth": { + "access_key": "my-access-key", + "secret_key": "my-secret-key" + } + } + }]], + [[{ + "node": { + "value": { + "username": "jack", + "plugins": { + "hmac-auth": { + "access_key": "my-access-key", + "secret_key": "my-secret-key", + "algorithm": "hmac-sha256", + "clock_skew": 300 + } + } + } + }, + "action": "set" + }]] + ) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 2: add consumer with plugin hmac-auth - missing secret key +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/consumers', + ngx.HTTP_PUT, + [[{ + "username": "foo", + "plugins": { + "hmac-auth": { + "access_key": "user-key" + } + } + }]]) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- error_code: 400 +--- no_error_log +[error] + + + +=== TEST 3: add consumer with plugin hmac-auth - missing access key +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/consumers', + ngx.HTTP_PUT, + [[{ + "username": "bar", + "plugins": { + "hmac-auth": { + "secret_key": "skey" + } + } + }]]) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- error_code: 400 Review comment: we'd better check the response body ########## File path: t/plugin/hmac-auth.t ########## @@ -0,0 +1,581 @@ +# +# 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. +# +BEGIN { Review comment: https://github.com/apache/apisix/pull/2192/files#diff-5628ae74c6204ec3a971a2b2668b9ce5R17-R19 I think we can drop them. That is the default action. ########## File path: apisix/plugins/hmac-auth.lua ########## @@ -0,0 +1,298 @@ +-- +-- 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. +-- +local ngx = ngx +local type = type +local select = select +local abs = math.abs +local ngx_time = ngx.time +local str_fmt = string.format +local ngx_re = require("ngx.re") +local ngx_req = ngx.req +local pairs = pairs +local ipairs = ipairs +local hmac_sha1 = ngx.hmac_sha1 +local escape_uri = ngx.escape_uri +local core = require("apisix.core") +local hmac = require("resty.hmac") +local consumer = require("apisix.consumer") +local ngx_decode_base64 = ngx.decode_base64 + +local SIGNATURE_KEY = "X-HMAC-SIGNATURE" +local ALGORITHM_KEY = "X-HMAC-ALGORITHM" +local TIMESTAMP_KEY = "X-HMAC-TIMESTAMP" +local ACCESS_KEY = "X-HMAC-ACCESS-KEY" +local plugin_name = "hmac-auth" + +local schema = { + type = "object", + oneOf = { + { + title = "work with consumer object", + properties = { + access_key = {type = "string", minLength = 1, maxLength = 256}, + secret_key = {type = "string", minLength = 1, maxLength = 256}, + algorithm = { + type = "string", + enum = {"hmac-sha1", "hmac-sha256", "hmac-sha512"}, + default = "hmac-sha256" + }, + clock_skew = { + type = "integer", + default = 300 + } + }, + required = {"access_key", "secret_key"}, + additionalProperties = false, + }, + { + title = "work with route or service object", + properties = {}, + additionalProperties = false, + } + } +} + +local _M = { + version = 0.1, + priority = 2530, + type = 'auth', + name = plugin_name, + schema = schema, +} + +local hmac_funcs = { + ["hmac-sha1"] = function(secret_key, message) + return hmac_sha1(secret_key, message) + end, + ["hmac-sha256"] = function(secret_key, message) + return hmac:new(secret_key, hmac.ALGOS.SHA256):final(message) + end, + ["hmac-sha512"] = function(secret_key, message) + return hmac:new(secret_key, hmac.ALGOS.SHA512):final(message) + end, +} + + +local function try_attr(t, ...) + local conf + local count = select('#', ...) + for i = 1, count do + local attr = select(i, ...) + conf = t[attr] + if type(conf) ~= "table" then + return false + end + end + + return true +end + + +local create_consumer_cache +do + local consumer_ids = {} + + function create_consumer_cache(consumers) + core.table.clear(consumer_ids) + + for _, consumer in ipairs(consumers.nodes) do + core.log.info("consumer node: ", core.json.delay_encode(consumer)) + consumer_ids[consumer.auth_conf.access_key] = consumer + end + + return consumer_ids + end + +end -- do + + +function _M.check_schema(conf) + core.log.info("input conf: ", core.json.delay_encode(conf)) + + return core.schema.check(schema, conf) +end + + +local function get_consumer(access_key) + if not access_key then + return nil, {message = "missing access key"} + end + + local consumer_conf = consumer.plugin(plugin_name) + if not consumer_conf then + return nil, {message = "Missing related consumer"} + end + + local consumers = core.lrucache.plugin(plugin_name, "consumers_key", + consumer_conf.conf_version, + create_consumer_cache, consumer_conf) + + local consumer = consumers[access_key] + if not consumer then + return nil, {message = "Invalid access key"} + end + core.log.info("consumer: ", core.json.delay_encode(consumer)) + + return consumer, nil +end + + +local function generate_signature(ctx, secret_key, params) + local canonical_uri = ctx.var.uri + local canonical_query_string = "" + local request_method = ngx_req.get_method() + local args = ngx_req.get_uri_args() + + if canonical_uri == "" then + canonical_uri = "/" + end + + if type(args) == "table" then + local keys = {} + local query_tab = {} + + for k, v in pairs(args) do + core.table.insert(keys, k) + end + core.table.sort(keys) + + for _, key in pairs(keys) do + local param = args[key] + if type(param) == "table" then + for _, vval in pairs(param) do + core.table.insert(query_tab, escape_uri(key) .. "=" .. escape_uri(vval)) + end + else + core.table.insert(query_tab, escape_uri(key) .. "=" .. escape_uri(param)) + end + end + canonical_query_string = core.table.concat(query_tab, "&") + end + + local req_body = core.request.get_body() + req_body = req_body or "" + + local signing_string = request_method .. canonical_uri .. canonical_query_string .. + req_body .. params.access_key .. params.timestamp .. secret_key + + return hmac_funcs[params.algorithm](secret_key, signing_string) +end + + +local function validate(ctx, params) + if not params.access_key or not params.signature then + return nil, {message = "access key or signature missing"} + end + + local consumer, err = get_consumer(params.access_key) + if err then + return nil, err + end + + local conf = consumer.auth_conf + if conf.algorithm ~= params.algorithm then + return nil, {message = str_fmt("algorithm %s not supported", params.algorithm)} + end + + core.log.info("conf.clock_skew:", conf.clock_skew) Review comment: missing one space after `:` ########## File path: t/plugin/hmac-auth.t ########## @@ -0,0 +1,581 @@ +# +# 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. +# +BEGIN { + $ENV{"CUSTOM_HMAC_AUTH"} = "false" +} + +use t::APISIX 'no_plan'; + +repeat_each(2); +no_long_string(); +no_root_location(); +no_shuffle(); +run_tests; + +__DATA__ + +=== TEST 1: add consumer with username and plugins +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/consumers', + ngx.HTTP_PUT, + [[{ + "username": "jack", + "plugins": { + "hmac-auth": { + "access_key": "my-access-key", + "secret_key": "my-secret-key" + } + } + }]], + [[{ + "node": { + "value": { + "username": "jack", + "plugins": { + "hmac-auth": { + "access_key": "my-access-key", + "secret_key": "my-secret-key", + "algorithm": "hmac-sha256", + "clock_skew": 300 + } + } + } + }, + "action": "set" + }]] + ) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 2: add consumer with plugin hmac-auth - missing secret key +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/consumers', + ngx.HTTP_PUT, + [[{ + "username": "foo", + "plugins": { + "hmac-auth": { + "access_key": "user-key" + } + } + }]]) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- error_code: 400 +--- no_error_log +[error] + + + +=== TEST 3: add consumer with plugin hmac-auth - missing access key +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/consumers', + ngx.HTTP_PUT, + [[{ + "username": "bar", + "plugins": { + "hmac-auth": { + "secret_key": "skey" + } + } + }]]) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- error_code: 400 +--- no_error_log +[error] + + + +=== TEST 4: add consumer with plugin hmac-auth - access key exceeds the length limit +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/consumers', + ngx.HTTP_PUT, + [[{ + "username": "li", + "plugins": { + "hmac-auth": { + "access_key": "akeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakey", + "secret_key": "skey" + } + } + }]]) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- error_code: 400 +--- no_error_log +[error] + + + +=== TEST 5: add consumer with plugin hmac-auth - access key exceeds the length limit +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/consumers', + ngx.HTTP_PUT, + [[{ + "username": "zhang", + "plugins": { + "hmac-auth": { + "access_key": "akey", + "secret_key": "skeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskey" + } + } + }]]) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- error_code: 400 +--- no_error_log +[error] + + + +=== TEST 6: enable hmac auth plugin using admin api +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "plugins": { + "hmac-auth": {} + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/hello" + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 7: verify, missing signature +--- request +GET /hello +--- error_code: 401 +--- response_body +{"message":"access key or signature missing"} +--- no_error_log +[error] + + + +=== TEST 8: verify: invalid access key +--- request +GET /hello +--- more_headers +X-HMAC-SIGNATURE: asdf +X-HMAC-ALGORITHM: hmac-sha256 +X-HMAC-TIMESTAMP: 112 +X-HMAC-ACCESS-KEY: sdf +--- error_code: 401 +--- response_body +{"message":"Invalid access key"} +--- no_error_log +[error] + + + +=== TEST 9: verify: invalid algorithm +--- request +GET /hello +--- more_headers +X-HMAC-SIGNATURE: asdf +X-HMAC-ALGORITHM: ljlj +X-HMAC-TIMESTAMP: 112 +X-HMAC-ACCESS-KEY: sdf +--- error_code: 401 +--- response_body +{"message":"Invalid access key"} +--- no_error_log +[error] + + + +=== TEST 10: verify: invalid timestamp +--- request +GET /hello +--- more_headers +X-HMAC-SIGNATURE: asdf +X-HMAC-ALGORITHM: hmac-sha256 +X-HMAC-TIMESTAMP: 112 +X-HMAC-ACCESS-KEY: my-access-key +--- error_code: 401 +--- response_body +{"message":"Invalid timestamp"} +--- no_error_log +[error] + + + +=== TEST 11: verify: ok +--- config +location /t { + content_by_lua_block { + local ngx_time = ngx.time + local core = require("apisix.core") + local t = require("lib.test_admin") + local hmac = require("resty.hmac") + local ngx_encode_base64 = ngx.encode_base64 + + local secret_key = "my-secret-key" + local timestamp = ngx_time() + local access_key = "my-access-key" + local signing_string = "GET" .. "/hello" .. "" .. Review comment: `.. ""` is useless? ########## File path: t/plugin/hmac-auth.t ########## @@ -0,0 +1,581 @@ +# +# 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. +# +BEGIN { + $ENV{"CUSTOM_HMAC_AUTH"} = "false" +} + +use t::APISIX 'no_plan'; + +repeat_each(2); +no_long_string(); +no_root_location(); +no_shuffle(); +run_tests; + +__DATA__ + +=== TEST 1: add consumer with username and plugins +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/consumers', + ngx.HTTP_PUT, + [[{ + "username": "jack", + "plugins": { + "hmac-auth": { + "access_key": "my-access-key", + "secret_key": "my-secret-key" + } + } + }]], + [[{ + "node": { + "value": { + "username": "jack", + "plugins": { + "hmac-auth": { + "access_key": "my-access-key", + "secret_key": "my-secret-key", + "algorithm": "hmac-sha256", + "clock_skew": 300 + } + } + } + }, + "action": "set" + }]] + ) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 2: add consumer with plugin hmac-auth - missing secret key +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/consumers', + ngx.HTTP_PUT, + [[{ + "username": "foo", + "plugins": { + "hmac-auth": { + "access_key": "user-key" + } + } + }]]) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- error_code: 400 +--- no_error_log +[error] + + + +=== TEST 3: add consumer with plugin hmac-auth - missing access key +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/consumers', + ngx.HTTP_PUT, + [[{ + "username": "bar", + "plugins": { + "hmac-auth": { + "secret_key": "skey" + } + } + }]]) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- error_code: 400 +--- no_error_log +[error] + + + +=== TEST 4: add consumer with plugin hmac-auth - access key exceeds the length limit +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/consumers', + ngx.HTTP_PUT, + [[{ + "username": "li", + "plugins": { + "hmac-auth": { + "access_key": "akeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakeyakey", + "secret_key": "skey" + } + } + }]]) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- error_code: 400 +--- no_error_log +[error] + + + +=== TEST 5: add consumer with plugin hmac-auth - access key exceeds the length limit +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/consumers', + ngx.HTTP_PUT, + [[{ + "username": "zhang", + "plugins": { + "hmac-auth": { + "access_key": "akey", + "secret_key": "skeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskeyskey" + } + } + }]]) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- error_code: 400 +--- no_error_log +[error] + + + +=== TEST 6: enable hmac auth plugin using admin api +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "plugins": { + "hmac-auth": {} + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/hello" + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 7: verify, missing signature +--- request +GET /hello +--- error_code: 401 +--- response_body +{"message":"access key or signature missing"} +--- no_error_log +[error] + + + +=== TEST 8: verify: invalid access key +--- request +GET /hello +--- more_headers +X-HMAC-SIGNATURE: asdf +X-HMAC-ALGORITHM: hmac-sha256 +X-HMAC-TIMESTAMP: 112 +X-HMAC-ACCESS-KEY: sdf +--- error_code: 401 +--- response_body +{"message":"Invalid access key"} +--- no_error_log +[error] + + + +=== TEST 9: verify: invalid algorithm +--- request +GET /hello +--- more_headers +X-HMAC-SIGNATURE: asdf +X-HMAC-ALGORITHM: ljlj +X-HMAC-TIMESTAMP: 112 +X-HMAC-ACCESS-KEY: sdf +--- error_code: 401 +--- response_body +{"message":"Invalid access key"} +--- no_error_log +[error] + + + +=== TEST 10: verify: invalid timestamp +--- request +GET /hello +--- more_headers +X-HMAC-SIGNATURE: asdf +X-HMAC-ALGORITHM: hmac-sha256 +X-HMAC-TIMESTAMP: 112 +X-HMAC-ACCESS-KEY: my-access-key +--- error_code: 401 +--- response_body +{"message":"Invalid timestamp"} +--- no_error_log +[error] + + + +=== TEST 11: verify: ok +--- config +location /t { + content_by_lua_block { + local ngx_time = ngx.time + local core = require("apisix.core") + local t = require("lib.test_admin") + local hmac = require("resty.hmac") + local ngx_encode_base64 = ngx.encode_base64 + + local secret_key = "my-secret-key" + local timestamp = ngx_time() + local access_key = "my-access-key" + local signing_string = "GET" .. "/hello" .. "" .. + "" .. access_key .. timestamp .. secret_key + + local signature = hmac:new(secret_key, hmac.ALGOS.SHA256):final(signing_string) + core.log.info("signature:", ngx_encode_base64(signature)) + local headers = {} + headers["X-HMAC-SIGNATURE"] = ngx_encode_base64(signature) + headers["X-HMAC-ALGORITHM"] = "hmac-sha256" + headers["X-HMAC-TIMESTAMP"] = timestamp + headers["X-HMAC-ACCESS-KEY"] = access_key + + local code, body = t.test('/hello', + ngx.HTTP_GET, + "", + nil, + headers + ) + + ngx.status = code + ngx.say(body) + } +} +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 12: add consumer with 0 clock skew +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/consumers', + ngx.HTTP_PUT, + [[{ + "username": "robin", + "plugins": { + "hmac-auth": { + "access_key": "my-access-key3", + "secret_key": "my-secret-key3", + "clock_skew": 0 + } + } + }]], + [[{ + "node": { + "value": { + "username": "robin", + "plugins": { + "hmac-auth": { + "access_key": "my-access-key3", + "secret_key": "my-secret-key3", + "algorithm": "hmac-sha256", + "clock_skew": 0 + } + } + } + }, + "action": "set" + }]] + ) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 13: verify: invalid signature +--- request +GET /hello +--- more_headers +X-HMAC-SIGNATURE: asdf +X-HMAC-ALGORITHM: hmac-sha256 +X-HMAC-TIMESTAMP: 112 +X-HMAC-ACCESS-KEY: my-access-key3 +--- error_code: 401 +--- response_body +{"message":"Invalid signature"} +--- no_error_log +[error] + + + +=== TEST 14: add consumer with 1 clock skew +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/consumers', + ngx.HTTP_PUT, + [[{ + "username": "pony", + "plugins": { + "hmac-auth": { + "access_key": "my-access-key2", + "secret_key": "my-secret-key2", + "clock_skew": 1 + } + } + }]], + [[{ + "node": { + "value": { + "username": "pony", + "plugins": { + "hmac-auth": { + "access_key": "my-access-key2", + "secret_key": "my-secret-key2", + "algorithm": "hmac-sha256", + "clock_skew": 1 + } + } + } + }, + "action": "set" + }]] + ) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 15: verify: invalid timestamp +--- config +location /t { + content_by_lua_block { + local ngx_time = ngx.time + local core = require("apisix.core") + local t = require("lib.test_admin") + local hmac = require("resty.hmac") + local ngx_encode_base64 = ngx.encode_base64 + + local secret_key = "my-secret-key2" + local timestamp = ngx_time() + local access_key = "my-access-key2" + local signing_string = "GET" .. "/hello" .. "" .. + "" .. access_key .. timestamp .. secret_key + + ngx.sleep(2) + + local signature = hmac:new(secret_key, hmac.ALGOS.SHA256):final(signing_string) + core.log.info("signature:", ngx_encode_base64(signature)) + local headers = {} + headers["X-HMAC-SIGNATURE"] = ngx_encode_base64(signature) + headers["X-HMAC-ALGORITHM"] = "hmac-sha256" + headers["X-HMAC-TIMESTAMP"] = timestamp + headers["X-HMAC-ACCESS-KEY"] = access_key + + local code, body = t.test('/hello', + ngx.HTTP_GET, + core.json.encode(data), + nil, + headers + ) + + ngx.status = code + ngx.say(body) + } +} +--- request +GET /t +--- error_code: 401 +--- response_body eval +qr/\{"message":"Invalid timestamp"\}/ +--- no_error_log +[error] + + + +=== TEST 16: verify: put ok +--- config +location /t { + content_by_lua_block { + local ngx_time = ngx.time + local core = require("apisix.core") + local t = require("lib.test_admin") + local hmac = require("resty.hmac") + local ngx_encode_base64 = ngx.encode_base64 + + local data = {cert = ssl_cert, key = ssl_key, sni = "test.com"} + local req_body = core.json.encode(data) + req_body = req_body or "" + + local secret_key = "my-secret-key" + local timestamp = ngx_time() + local access_key = "my-access-key" + local signing_string = "PUT" .. "/hello" .. "" .. + req_body .. access_key .. timestamp .. secret_key + + local signature = hmac:new(secret_key, hmac.ALGOS.SHA256):final(signing_string) + core.log.info("signature:", ngx_encode_base64(signature)) + local headers = {} + headers["X-HMAC-SIGNATURE"] = ngx_encode_base64(signature) + headers["X-HMAC-ALGORITHM"] = "hmac-sha256" + headers["X-HMAC-TIMESTAMP"] = timestamp + headers["X-HMAC-ACCESS-KEY"] = access_key + + local code, body = t.test('/hello', + ngx.HTTP_PUT, + req_body, + nil, + headers + ) + + ngx.status = code + ngx.say(body) + } +} +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 17: verify: put ok (pass auth data by header `Authorization`) +--- config +location /t { + content_by_lua_block { + local ngx_time = ngx.time + local core = require("apisix.core") + local t = require("lib.test_admin") + local hmac = require("resty.hmac") + local ngx_encode_base64 = ngx.encode_base64 + + local data = {cert = ssl_cert, key = ssl_key, sni = "test.com"} Review comment: `ssl_cert`? global variable? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
