Skip to content

Commit b61a897

Browse files
committed
Merge branch 'master' of github.com:apache/apisix
2 parents df432f1 + 251b135 commit b61a897

24 files changed

+612
-230
lines changed

apisix/cli/config.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ local _M = {
9595
meta = {
9696
lua_shared_dict = {
9797
["prometheus-metrics"] = "15m",
98+
["prometheus-cache"] = "10m",
9899
["standalone-config"] = "10m",
99100
["status-report"] = "1m",
100101
}
@@ -323,7 +324,8 @@ local _M = {
323324
export_addr = {
324325
ip = "127.0.0.1",
325326
port = 9091
326-
}
327+
},
328+
refresh_interval = 15
327329
},
328330
["server-info"] = {
329331
report_ttl = 60

apisix/cli/ngx_tpl.lua

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ lua {
6767
{% if enabled_stream_plugins["prometheus"] then %}
6868
lua_shared_dict prometheus-metrics {* meta.lua_shared_dict["prometheus-metrics"] *};
6969
{% end %}
70+
{% if enabled_plugins["prometheus"] or enabled_stream_plugins["prometheus"] then %}
71+
lua_shared_dict prometheus-cache {* meta.lua_shared_dict["prometheus-cache"] *};
72+
{% end %}
7073
{% if standalone_with_admin_api then %}
7174
lua_shared_dict standalone-config {* meta.lua_shared_dict["standalone-config"] *};
7275
{% end %}
@@ -96,22 +99,20 @@ http {
9699
}
97100
98101
init_worker_by_lua_block {
99-
require("apisix.plugins.prometheus.exporter").http_init(true)
102+
local prometheus = require("apisix.plugins.prometheus.exporter")
103+
prometheus.http_init(true)
104+
prometheus.init_exporter_timer()
100105
}
101106
102107
server {
103-
{% if use_apisix_base then %}
104-
listen {* prometheus_server_addr *} enable_process=privileged_agent;
105-
{% else %}
106-
listen {* prometheus_server_addr *};
107-
{% end %}
108+
listen {* prometheus_server_addr *};
108109
109110
access_log off;
110111
111112
location / {
112113
content_by_lua_block {
113114
local prometheus = require("apisix.plugins.prometheus.exporter")
114-
prometheus.export_metrics(true)
115+
prometheus.export_metrics()
115116
}
116117
}
117118
@@ -577,11 +578,7 @@ http {
577578
578579
{% if enabled_plugins["prometheus"] and prometheus_server_addr then %}
579580
server {
580-
{% if use_apisix_base then %}
581-
listen {* prometheus_server_addr *} enable_process=privileged_agent;
582-
{% else %}
583-
listen {* prometheus_server_addr *};
584-
{% end %}
581+
listen {* prometheus_server_addr *};
585582
586583
access_log off;
587584

apisix/core/config_etcd.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ function _M.new(key, opts)
10011001
sync_times = 0,
10021002
running = true,
10031003
conf_version = 0,
1004-
values = nil,
1004+
values = {},
10051005
need_reload = true,
10061006
watching_stream = nil,
10071007
routes_hash = nil,

apisix/init.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ function _M.http_init_worker()
155155
if local_conf.apisix and local_conf.apisix.enable_server_tokens == false then
156156
ver_header = "APISIX"
157157
end
158+
159+
-- To ensure that all workers related to Prometheus metrics are initialized,
160+
-- we need to put the initialization of the Prometheus plugin here.
161+
plugin.init_prometheus()
158162
end
159163

160164

apisix/plugin.lua

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,6 @@ function _M.load(config)
341341
return local_plugins
342342
end
343343

344-
local exporter = require("apisix.plugins.prometheus.exporter")
345-
346344
if ngx.config.subsystem == "http" then
347345
if not http_plugin_names then
348346
core.log.error("failed to read plugin list from local file")
@@ -356,15 +354,6 @@ function _M.load(config)
356354
if not ok then
357355
core.log.error("failed to load plugins: ", err)
358356
end
359-
360-
local enabled = core.table.array_find(http_plugin_names, "prometheus") ~= nil
361-
local active = exporter.get_prometheus() ~= nil
362-
if not enabled then
363-
exporter.destroy()
364-
end
365-
if enabled and not active then
366-
exporter.http_init()
367-
end
368357
end
369358
end
370359

@@ -808,18 +797,21 @@ do
808797
end
809798

810799

811-
function _M.init_worker()
800+
function _M.init_prometheus()
812801
local _, http_plugin_names, stream_plugin_names = get_plugin_names()
802+
local enabled_in_http = core.table.array_find(http_plugin_names, "prometheus")
803+
local enabled_in_stream = core.table.array_find(stream_plugin_names, "prometheus")
813804

814-
-- some plugins need to be initialized in init* phases
815-
if is_http and core.table.array_find(http_plugin_names, "prometheus") then
816-
local prometheus_enabled_in_stream =
817-
core.table.array_find(stream_plugin_names, "prometheus")
818-
require("apisix.plugins.prometheus.exporter").http_init(prometheus_enabled_in_stream)
819-
elseif not is_http and core.table.array_find(stream_plugin_names, "prometheus") then
820-
require("apisix.plugins.prometheus.exporter").stream_init()
805+
-- For stream-only mode, there are separate calls in ngx_tpl.lua.
806+
-- And for other modes, whether in stream or http plugins,
807+
-- the prometheus exporter needs to be initialized.
808+
if is_http and (enabled_in_http or enabled_in_stream) then
809+
require("apisix.plugins.prometheus.exporter").init_exporter_timer()
821810
end
811+
end
822812

813+
814+
function _M.init_worker()
823815
-- someone's plugin needs to be initialized after prometheus
824816
-- see https://github.com/apache/apisix/issues/3286
825817
_M.load()

apisix/plugins/api-breaker.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ function _M.access(conf, ctx)
166166
return
167167
end
168168

169-
local failure_times = math.ceil(unhealthy_count / conf.unhealthy.failures)
169+
local failure_times = math.floor(unhealthy_count / conf.unhealthy.failures)
170170
if failure_times < 1 then
171171
failure_times = 1
172172
end

apisix/plugins/chaitin-waf.lua

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ local plugin_schema = {
5454
properties = {
5555
mode = {
5656
type = "string",
57-
enum = { "off", "monitor", "block", nil },
58-
default = nil,
57+
enum = { "off", "monitor", "block" }
5958
},
6059
match = match_schema,
6160
append_waf_resp_header = {
@@ -100,8 +99,7 @@ local metadata_schema = {
10099
properties = {
101100
mode = {
102101
type = "string",
103-
enum = { "off", "monitor", "block", nil },
104-
default = nil,
102+
enum = { "off", "monitor", "block" }
105103
},
106104
nodes = {
107105
type = "array",

apisix/plugins/openid-connect.lua

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,22 @@
1515
-- limitations under the License.
1616
--
1717

18-
local core = require("apisix.core")
19-
local ngx_re = require("ngx.re")
20-
local openidc = require("resty.openidc")
21-
local random = require("resty.random")
22-
local string = string
23-
local ngx = ngx
24-
local ipairs = ipairs
25-
local type = type
26-
local concat = table.concat
18+
local core = require("apisix.core")
19+
local ngx_re = require("ngx.re")
20+
local openidc = require("resty.openidc")
21+
local random = require("resty.random")
22+
local jsonschema = require('jsonschema')
23+
local string = string
24+
local ngx = ngx
25+
local ipairs = ipairs
26+
local type = type
27+
local tostring = tostring
28+
local pcall = pcall
29+
local concat = table.concat
2730

2831
local ngx_encode_base64 = ngx.encode_base64
2932

30-
local plugin_name = "openid-connect"
33+
local plugin_name = "openid-connect"
3134

3235

3336
local schema = {
@@ -317,6 +320,11 @@ local schema = {
317320
items = {
318321
type = "string"
319322
}
323+
},
324+
claim_schema = {
325+
description = "JSON schema of OIDC response claim",
326+
type = "object",
327+
default = nil,
320328
}
321329
},
322330
encrypt_fields = {"client_secret", "client_rsa_private_key"},
@@ -331,7 +339,6 @@ local _M = {
331339
schema = schema,
332340
}
333341

334-
335342
function _M.check_schema(conf)
336343
if conf.ssl_verify == "no" then
337344
-- we used to set 'ssl_verify' to "no"
@@ -357,10 +364,16 @@ function _M.check_schema(conf)
357364
return false, err
358365
end
359366

367+
if conf.claim_schema then
368+
local ok, res = pcall(jsonschema.generate_validator, conf.claim_schema)
369+
if not ok then
370+
return false, "check claim_schema failed: " .. tostring(res)
371+
end
372+
end
373+
360374
return true
361375
end
362376

363-
364377
local function get_bearer_access_token(ctx)
365378
-- Get Authorization header, maybe.
366379
local auth_header = core.request.header(ctx, "Authorization")
@@ -528,6 +541,18 @@ local function required_scopes_present(required_scopes, http_scopes)
528541
return true
529542
end
530543

544+
local function validate_claims_in_oidcauth_response(resp, conf)
545+
if not conf.claim_schema then
546+
return true
547+
end
548+
local data = {
549+
user = resp.user,
550+
access_token = resp.access_token,
551+
id_token = resp.id_token,
552+
}
553+
return core.schema.check(conf.claim_schema, data)
554+
end
555+
531556
function _M.rewrite(plugin_conf, ctx)
532557
local conf = core.table.clone(plugin_conf)
533558

@@ -682,6 +707,13 @@ function _M.rewrite(plugin_conf, ctx)
682707
end
683708

684709
if response then
710+
local ok, err = validate_claims_in_oidcauth_response(response, conf)
711+
if not ok then
712+
core.log.error("OIDC claim validation failed: ", err)
713+
ngx.header["WWW-Authenticate"] = 'Bearer realm="' .. conf.realm ..
714+
'", error="invalid_token", error_description="' .. err .. '"'
715+
return ngx.HTTP_UNAUTHORIZED
716+
end
685717
-- If the openidc module has returned a response, it may contain,
686718
-- respectively, the access token, the ID token, the refresh token,
687719
-- and the userinfo.

apisix/plugins/prometheus.lua

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
local core = require("apisix.core")
1818
local exporter = require("apisix.plugins.prometheus.exporter")
1919

20-
2120
local plugin_name = "prometheus"
2221
local schema = {
2322
type = "object",
@@ -35,6 +34,7 @@ local _M = {
3534
priority = 500,
3635
name = plugin_name,
3736
log = exporter.http_log,
37+
destroy = exporter.destroy,
3838
schema = schema,
3939
run_policy = "prefer_route",
4040
}
@@ -55,4 +55,11 @@ function _M.api()
5555
end
5656

5757

58+
function _M.init()
59+
local local_conf = core.config.local_conf()
60+
local enabled_in_stream = core.table.array_find(local_conf.stream_plugins, "prometheus")
61+
exporter.http_init(enabled_in_stream)
62+
end
63+
64+
5865
return _M

0 commit comments

Comments
 (0)