-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
04-access_spec.lua
1502 lines (1234 loc) · 43.8 KB
/
04-access_spec.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local helpers = require "spec.helpers"
local cjson = require "cjson"
local redis = require "resty.redis"
local version = require "version"
local REDIS_HOST = helpers.redis_host
local REDIS_PORT = helpers.redis_port
local REDIS_SSL_PORT = helpers.redis_ssl_port
local REDIS_SSL_SNI = helpers.redis_ssl_sni
local REDIS_PASSWORD = ""
local REDIS_DATABASE = 1
local UPSTREAM_HOST = "localhost"
local UPSTREAM_PORT = helpers.get_available_port()
local UPSTREAM_URL = string.format("http://%s:%d/always_200", UPSTREAM_HOST, UPSTREAM_PORT)
local fmt = string.format
local proxy_client = helpers.proxy_client
local table_insert = table.insert
local tonumber = tonumber
local ngx_sleep = ngx.sleep
local ngx_now = ngx.now
-- This performs the test up to two times (and no more than two).
-- We are **not** retrying to "give it another shot" in case of a flaky test.
-- The reason why we allow for a single retry in this test suite is because
-- tests are dependent on the value of the current minute. If the minute
-- flips during the test (i.e. going from 03:43:59 to 03:44:00), the result
-- will fail. Since each test takes less than a minute to run, running it
-- a second time right after that failure ensures that another flip will
-- not occur. If the second execution failed as well, this means that there
-- was an actual problem detected by the test.
local function retry(fn)
if not pcall(fn) then
ngx_sleep(61 - (ngx_now() % 60)) -- Wait for minute to expire
fn()
end
end
local function GET(url, opt)
local client = proxy_client()
local res, err = client:get(url, opt)
if not res then
client:close()
return nil, err
end
assert(res:read_body())
client:close()
return res
end
local function redis_connect()
local red = assert(redis:new())
red:set_timeout(2000)
assert(red:connect(REDIS_HOST, REDIS_PORT))
local red_version = string.match(red:info(), 'redis_version:([%g]+)\r\n')
return red, assert(version(red_version))
end
local function flush_redis()
local redis = require "resty.redis"
local red = assert(redis:new())
red:set_timeout(2000)
local ok, err = red:connect(REDIS_HOST, REDIS_PORT)
if not ok then
error("failed to connect to Redis: " .. err)
end
if REDIS_PASSWORD and REDIS_PASSWORD ~= "" then
local ok, err = red:auth(REDIS_PASSWORD)
if not ok then
error("failed to connect to Redis: " .. err)
end
end
local ok, err = red:select(REDIS_DATABASE)
if not ok then
error("failed to change Redis database: " .. err)
end
red:flushall()
red:close()
end
local function client_requests(n, proxy_fn)
local ret = {
minute_limit = {},
minute_remaining = {},
hour_limit = {},
hour_remaining = {},
limit = {},
remaining = {},
status = {},
reset = {},
}
for _ = 1, n do
local res = assert(proxy_fn())
table_insert(ret.reset, tonumber(res.headers["RateLimit-Reset"]))
table_insert(ret.status, res.status)
table_insert(ret.minute_limit, tonumber(res.headers["X-RateLimit-Limit-Minute"]))
table_insert(ret.minute_remaining, tonumber(res.headers["X-RateLimit-Remaining-Minute"]))
table_insert(ret.hour_limit, tonumber(res.headers["X-RateLimit-Limit-Hour"]))
table_insert(ret.hour_remaining, tonumber(res.headers["X-RateLimit-Remaining-Hour"]))
table_insert(ret.limit, tonumber(res.headers["RateLimit-Limit"]))
table_insert(ret.remaining, tonumber(res.headers["RateLimit-Remaining"]))
helpers.wait_timer("rate-limiting", true, "any-finish")
end
return ret
end
local function validate_headers(headers, check_minute, check_hour)
if check_minute then
assert.same({
6, 6, 6, 6, 6, 6, 6,
}, headers.minute_limit)
assert.same({
5, 4, 3, 2, 1, 0, 0,
}, headers.minute_remaining)
end
if check_hour then
for _, v in ipairs(headers.hour_limit) do
assert(v > 0)
end
for _, v in ipairs(headers.hour_remaining) do
assert(v >= 0)
end
end
assert.same({
6, 6, 6, 6, 6, 6, 6,
}, headers.limit)
assert.same({
5, 4, 3, 2, 1, 0, 0,
}, headers.remaining)
assert.same({
200, 200, 200, 200, 200, 200, 429,
}, headers.status)
for _, reset in ipairs(headers.reset) do
if check_hour then
assert.equal(true, reset <= 3600 and reset >= 0)
elseif check_minute then
assert.equal(true, reset <= 60 and reset >= 0)
else
error("check_hour or check_minute must be true")
end
end
end
local function setup_service(admin_client, url)
local service = assert(admin_client:send({
method = "POST",
path = "/services",
body = {
url = url,
},
headers = {
["Content-Type"] = "application/json",
},
}))
return cjson.decode(assert.res_status(201, service))
end
local function setup_route(admin_client, service, paths, protocol)
protocol = protocol or "http"
local route = assert(admin_client:send({
method = "POST",
path = "/routes",
body = {
protocols = { protocol },
service = { id = service.id, },
paths = paths,
},
headers = {
["Content-Type"] = "application/json",
},
}))
return cjson.decode(assert.res_status(201, route))
end
local function setup_rl_plugin(admin_client, conf, service, consumer)
local plugin
if service then
plugin = assert(admin_client:send({
method = "POST",
path = "/plugins",
body = {
name = "rate-limiting",
service = { id = service.id, },
config = conf,
},
headers = {
["Content-Type"] = "application/json",
},
}))
elseif consumer then
plugin = assert(admin_client:send({
method = "POST",
path = "/plugins",
body = {
name = "rate-limiting",
consumer = { id = consumer.id, },
config = conf,
},
headers = {
["Content-Type"] = "application/json",
},
}))
else
plugin = assert(admin_client:send({
method = "POST",
path = "/plugins",
body = {
name = "rate-limiting",
config = conf,
},
headers = {
["Content-Type"] = "application/json",
},
}))
end
return cjson.decode(assert.res_status(201, plugin))
end
local function setup_key_auth_plugin(admin_client, conf, service)
local plugin
if service then
plugin = assert(admin_client:send({
method = "POST",
path = "/plugins",
body = {
name = "key-auth",
service = { id = service.id, },
config = conf,
},
headers = {
["Content-Type"] = "application/json",
},
}))
else
plugin = assert(admin_client:send({
method = "POST",
path = "/plugins",
body = {
name = "key-auth",
config = conf,
},
headers = {
["Content-Type"] = "application/json",
},
}))
end
return cjson.decode(assert.res_status(201, plugin))
end
local function setup_consumer(admin_client, username)
local consumer = assert(admin_client:send({
method = "POST",
path = "/consumers",
body = {
username = username,
},
headers = {
["Content-Type"] = "application/json",
},
}))
return cjson.decode(assert.res_status(201, consumer))
end
local function setup_credential(admin_client, consumer, key)
local credential = assert(admin_client:send({
method = "POST",
path = "/consumers/" .. consumer.id .. "/key-auth",
body = {
key = key,
},
headers = {
["Content-Type"] = "application/json",
},
}))
return cjson.decode(assert.res_status(201, credential))
end
local function delete_service(admin_client, service)
local res = assert(admin_client:send({
method = "DELETE",
path = "/services/" .. service.id,
}))
assert.res_status(204, res)
end
local function delete_route(admin_client, route)
local res = assert(admin_client:send({
method = "DELETE",
path = "/routes/" .. route.id,
}))
assert.res_status(204, res)
end
local function delete_plugin(admin_client, plugin)
local res = assert(admin_client:send({
method = "DELETE",
path = "/plugins/" .. plugin.id,
}))
assert.res_status(204, res)
end
local function delete_consumer(admin_client, consumer)
local res = assert(admin_client:send({
method = "DELETE",
path = "/consumers/" .. consumer.id,
}))
assert.res_status(204, res)
end
local function delete_credential(admin_client, credential)
local res = assert(admin_client:send({
method = "DELETE",
path = "/consumers/" .. credential.consumer.id .. "/key-auth/" .. credential.id,
}))
assert.res_status(204, res)
end
local limit_by_confs = {
"ip",
"consumer",
"credential",
"service",
"header",
"path",
}
local ssl_confs = {
no_ssl = {
redis_port = REDIS_PORT,
},
ssl_verify = {
redis_ssl = true,
redis_ssl_verify = true,
redis_server_name = REDIS_SSL_SNI,
redis_port = REDIS_SSL_PORT,
},
ssl_no_verify = {
redis_ssl = true,
redis_ssl_verify = false,
redis_server_name = "really.really.really.does.not.exist.host.test",
redis_port = REDIS_SSL_PORT,
},
}
local desc
for _, strategy in helpers.each_strategy() do
for __, policy in ipairs({ "local", "cluster", "redis" }) do
for ___, limit_by in ipairs(limit_by_confs) do
for ssl_conf_name, ssl_conf in pairs(ssl_confs) do
if ssl_conf_name ~= "no_ssl" and policy ~= "redis" then
goto continue
end
desc = fmt("Plugin: rate-limiting #db (access) [strategy: %s] [policy: %s] [limit_by: %s] [redis: %s]",
strategy, policy, limit_by, ssl_conf_name)
describe(desc, function()
local db, https_server, admin_client
lazy_setup(function()
_, db = helpers.get_db_utils(strategy, nil, { "rate-limiting", "key-auth" })
if policy == "redis" then
flush_redis()
elseif policy == "cluster" then
db:truncate("ratelimiting_metrics")
end
https_server = helpers.https_server.new(UPSTREAM_PORT)
https_server:start()
helpers.start_kong({
database = strategy,
nginx_conf = "spec/fixtures/custom_nginx.template",
plugins = "bundled,rate-limiting,key-auth",
trusted_ips = "0.0.0.0/0,::/0",
lua_ssl_trusted_certificate = "spec/fixtures/redis/ca.crt",
})
end)
lazy_teardown(function()
assert(https_server, "unexpected error")
https_server:shutdown()
assert(helpers.stop_kong(), "failed to stop Kong")
end)
before_each(function ()
admin_client = helpers.admin_client()
if strategy == "cluster" then
db:truncate("ratelimiting_metrics")
end
if policy == "redis" then
flush_redis()
end
end)
after_each(function()
admin_client:close()
end)
it(fmt("blocks if exceeding limit (single %s)", limit_by), function()
local test_path = "/test"
local test_header = "test-header"
local test_key_name = "test-key"
local test_credential = "test_credential"
local service = setup_service(admin_client, UPSTREAM_URL)
local route = setup_route(admin_client, service, { test_path })
local rl_plugin = setup_rl_plugin(admin_client, {
minute = 6,
policy = policy,
limit_by = limit_by,
path = test_path, -- only for limit_by = "path"
header_name = test_header, -- only for limit_by = "header"
redis = {
host = REDIS_HOST,
port = ssl_conf.redis_port,
password = REDIS_PASSWORD,
database = REDIS_DATABASE,
ssl = ssl_conf.redis_ssl,
ssl_verify = ssl_conf.redis_ssl_verify,
server_name = ssl_conf.redis_server_name,
}
}, service)
local auth_plugin
local consumer
local credential
if limit_by == "consumer" or limit_by == "credential" then
auth_plugin = setup_key_auth_plugin(admin_client, {
key_names = { test_key_name },
}, service)
consumer = setup_consumer(admin_client, "Bob")
credential = setup_credential(admin_client, consumer, test_credential)
end
finally(function()
if limit_by == "consumer" or limit_by == "credential" then
delete_credential(admin_client, credential)
delete_consumer(admin_client, consumer)
delete_plugin(admin_client, auth_plugin)
end
delete_plugin(admin_client, rl_plugin)
delete_route(admin_client, route)
delete_service(admin_client, service)
end)
helpers.wait_for_all_config_update({
override_global_rate_limiting_plugin = true,
override_global_key_auth_plugin = true,
})
local function proxy_fn()
if limit_by == "ip" or
limit_by == "path" or
limit_by == "service" then
return GET(test_path)
end
if limit_by == "header" then
return GET(test_path, { [test_header] = "test" })
end
if limit_by == "consumer" or limit_by == "credential" then
return GET(test_path, { headers = { [test_key_name] = test_credential }})
end
error("unexpected limit_by: " .. limit_by)
end
retry(function ()
validate_headers(client_requests(7, proxy_fn), true)
end)
end)
if limit_by == "ip" then
it("blocks if exceeding limit (multiple ip)", function()
local test_path = "/test"
local service = setup_service(admin_client, UPSTREAM_URL)
local route = setup_route(admin_client, service, { test_path })
local rl_plugin = setup_rl_plugin(admin_client, {
minute = 6,
policy = policy,
limit_by = "ip",
redis = {
host = REDIS_HOST,
port = ssl_conf.redis_port,
password = REDIS_PASSWORD,
database = REDIS_DATABASE,
ssl = ssl_conf.redis_ssl,
ssl_verify = ssl_conf.redis_ssl_verify,
server_name = ssl_conf.redis_server_name,
}
}, service)
finally(function()
delete_plugin(admin_client, rl_plugin)
delete_route(admin_client, route)
delete_service(admin_client, service)
end)
helpers.wait_for_all_config_update({
override_global_rate_limiting_plugin = true,
override_global_key_auth_plugin = true,
})
retry(function ()
for _, ip in ipairs({ "127.0.0.1", "127.0.0.2" }) do
validate_headers(client_requests(7, function ()
return GET(test_path, { headers = { ["X-Real-IP"] = ip }})
end), true)
end -- for _, ip in ipairs({ "127.0.0.1", "127.0.0.2" }) do
end) -- retry(function ()
end) -- it("blocks if exceeding limit (multiple ip)", function()
it("blocks if exceeding limit #grpc (single ip)", function()
local test_path = "/hello.HelloService/"
local service = setup_service(admin_client, helpers.grpcbin_url)
local route = setup_route(admin_client, service, { test_path }, "grpc")
local rl_plugin = setup_rl_plugin(admin_client, {
minute = 6,
policy = policy,
limit_by = "ip",
redis = {
host = REDIS_HOST,
port = ssl_conf.redis_port,
password = REDIS_PASSWORD,
database = REDIS_DATABASE,
ssl = ssl_conf.redis_ssl,
ssl_verify = ssl_conf.redis_ssl_verify,
server_name = ssl_conf.redis_server_name,
}
}, service)
finally(function()
delete_plugin(admin_client, rl_plugin)
delete_route(admin_client, route)
delete_service(admin_client, service)
end)
helpers.wait_for_all_config_update({
override_global_rate_limiting_plugin = true,
override_global_key_auth_plugin = true,
})
retry(function ()
for i = 1, 6 do
local ok, res = helpers.proxy_client_grpc(){
service = "hello.HelloService.SayHello",
opts = {
["-v"] = true,
},
}
assert.is_true(ok, res)
assert.matches("x%-ratelimit%-limit%-minute: 6", res)
assert.matches("x%-ratelimit%-remaining%-minute: " .. (6 - i), res)
assert.matches("ratelimit%-limit: 6", res)
assert.matches("ratelimit%-remaining: " .. (6 - i), res)
local reset = tonumber(string.match(res, "ratelimit%-reset: (%d+)"))
assert.equal(true, reset <= 60 and reset >= 0)
-- wait for zero-delay timer
helpers.wait_timer("rate-limiting", true, "any-finish")
end
-- Additional request, while limit is 6/minute
local ok, res = helpers.proxy_client_grpc(){
service = "hello.HelloService.SayHello",
opts = {
["-v"] = true,
},
}
assert.falsy(ok)
-- Kong should return "ResourceExhausted", but for some reason here,
-- perhaps because of grpcurl, it gets translated to "Unavailable".
assert.matches("Code: Unavailable", res)
assert.matches("ratelimit%-limit: 6", res)
assert.matches("ratelimit%-remaining: 0", res)
local retry = tonumber(string.match(res, "retry%-after: (%d+)"))
assert.equal(true, retry <= 60 and retry > 0)
local reset = tonumber(string.match(res, "ratelimit%-reset: (%d+)"))
assert.equal(true, reset <= 60 and reset > 0)
end)
end)
it("hide_client_headers (single ip)", function ()
local test_path = "/test"
local service = setup_service(admin_client, UPSTREAM_URL)
local route = setup_route(admin_client, service, { test_path })
local rl_plugin = setup_rl_plugin(admin_client, {
minute = 6,
policy = policy,
limit_by = "ip",
hide_client_headers = true,
redis = {
host = REDIS_HOST,
port = ssl_conf.redis_port,
password = REDIS_PASSWORD,
database = REDIS_DATABASE,
ssl = ssl_conf.redis_ssl,
ssl_verify = ssl_conf.redis_ssl_verify,
server_name = ssl_conf.redis_server_name,
}
}, service)
finally(function()
delete_plugin(admin_client, rl_plugin)
delete_route(admin_client, route)
delete_service(admin_client, service)
end)
helpers.wait_for_all_config_update({
override_global_rate_limiting_plugin = true,
override_global_key_auth_plugin = true,
})
local res = assert(GET(test_path))
assert.is_nil(res.headers["X-Ratelimit-Limit-Minute"])
assert.is_nil(res.headers["X-Ratelimit-Remaining-Minute"])
assert.is_nil(res.headers["Ratelimit-Limit"])
assert.is_nil(res.headers["Ratelimit-Remaining"])
assert.is_nil(res.headers["Ratelimit-Reset"])
assert.is_nil(res.headers["Retry-After"])
end)
it("handles multiple limits (single ip)", function()
local test_path = "/test"
local test_header = "test-header"
local service = setup_service(admin_client, UPSTREAM_URL)
local route = setup_route(admin_client, service, { test_path })
local rl_plugin = setup_rl_plugin(admin_client, {
minute = 6,
hour = 99999,
policy = policy,
limit_by = limit_by,
path = test_path,
header_name = test_header,
redis = {
host = REDIS_HOST,
port = ssl_conf.redis_port,
password = REDIS_PASSWORD,
database = REDIS_DATABASE,
ssl = ssl_conf.redis_ssl,
ssl_verify = ssl_conf.redis_ssl_verify,
server_name = ssl_conf.redis_server_name,
}
}, service)
finally(function()
delete_plugin(admin_client, rl_plugin)
delete_route(admin_client, route)
delete_service(admin_client, service)
end)
helpers.wait_for_all_config_update({
override_global_rate_limiting_plugin = true,
override_global_key_auth_plugin = true,
})
local function proxy_fn()
return GET(test_path)
end
retry(function ()
validate_headers(client_requests(7, proxy_fn), true, true)
end)
end)
it("expire counter", function()
local test_path = "/test"
local service = setup_service(admin_client, UPSTREAM_URL)
local route = setup_route(admin_client, service, { test_path })
local rl_plugin = setup_rl_plugin(admin_client, {
second = 1,
policy = policy,
limit_by = "ip",
redis = {
host = REDIS_HOST,
port = ssl_conf.redis_port,
password = REDIS_PASSWORD,
database = REDIS_DATABASE,
ssl = ssl_conf.redis_ssl,
ssl_verify = ssl_conf.redis_ssl_verify,
server_name = ssl_conf.redis_server_name,
}
}, service)
finally(function()
delete_plugin(admin_client, rl_plugin)
delete_route(admin_client, route)
delete_service(admin_client, service)
end)
helpers.wait_for_all_config_update({
override_global_rate_limiting_plugin = true,
override_global_key_auth_plugin = true,
})
assert
.with_timeout(15)
.with_max_tries(10)
.with_step(0.5) -- the windows is 1 second, we wait 0.5 seconds between each retry,
-- that can avoid some unlucky case (we are at the end of the window)
.ignore_exceptions(false)
.eventually(function()
local res1 = GET(test_path, { headers = { ["X-Real-IP"] = "127.0.0.3" }})
assert.res_status(200, res1)
assert.are.same(1, tonumber(res1.headers["RateLimit-Limit"]))
assert.are.same(0, tonumber(res1.headers["RateLimit-Remaining"]))
assert.is_true(tonumber(res1.headers["ratelimit-reset"]) >= 0)
assert.are.same(1, tonumber(res1.headers["X-RateLimit-Limit-Second"]))
assert.are.same(0, tonumber(res1.headers["X-RateLimit-Remaining-Second"]))
local res2 = GET(test_path, { headers = { ["X-Real-IP"] = "127.0.0.3" }})
local body2 = assert.res_status(429, res2)
local json2 = cjson.decode(body2)
assert.not_nil(json2.message)
assert.matches("API rate limit exceeded", json2.message)
ngx_sleep(1)
local res3 = GET(test_path, { headers = { ["X-Real-IP"] = "127.0.0.3" }})
assert.res_status(200, res3)
end)
.has_no_error("counter should have been cleared after current window")
end)
it("blocks with a custom error code and message", function()
local test_path = "/test"
local service = setup_service(admin_client, UPSTREAM_URL)
local route = setup_route(admin_client, service, { test_path })
local rl_plugin = setup_rl_plugin(admin_client, {
minute = 1,
policy = policy,
limit_by = limit_by,
path = test_path,
redis = {
host = REDIS_HOST,
port = ssl_conf.redis_port,
password = REDIS_PASSWORD,
database = REDIS_DATABASE,
ssl = ssl_conf.redis_ssl,
ssl_verify = ssl_conf.redis_ssl_verify,
server_name = ssl_conf.redis_server_name,
},
error_code = 404,
error_message = "Fake Not Found",
}, service)
finally(function()
delete_plugin(admin_client, rl_plugin)
delete_route(admin_client, route)
delete_service(admin_client, service)
end)
helpers.wait_for_all_config_update({
override_global_rate_limiting_plugin = true,
override_global_key_auth_plugin = true,
})
local res = GET(test_path)
assert.res_status(200, res)
helpers.wait_timer("rate-limiting", true, "any-finish")
res = GET(test_path)
local json = cjson.decode(assert.res_status(404, res))
assert.matches("Fake Not Found", json.message)
end) -- it("blocks with a custom error code and message", function()
end -- if limit_by == "ip" then
if limit_by == "service" then
it("blocks if exceeding limit (multiple service)", function ()
local test_path_1, test_path_2 = "/1-test", "/2-test"
local service_1, service_2 = setup_service(admin_client, UPSTREAM_URL),
setup_service(admin_client, UPSTREAM_URL)
local route_1, route_2 = setup_route(admin_client, service_1, { test_path_1 }),
setup_route(admin_client, service_2, { test_path_2 })
local rl_plugin = setup_rl_plugin(admin_client, {
minute = 6,
policy = policy,
limit_by = "service",
redis = {
host = REDIS_HOST,
port = ssl_conf.redis_port,
password = REDIS_PASSWORD,
database = REDIS_DATABASE,
ssl = ssl_conf.redis_ssl,
ssl_verify = ssl_conf.redis_ssl_verify,
server_name = ssl_conf.redis_server_name,
}
})
finally(function()
delete_plugin(admin_client, rl_plugin)
delete_route(admin_client, route_1)
delete_route(admin_client, route_2)
delete_service(admin_client, service_1)
delete_service(admin_client, service_2)
end)
helpers.wait_for_all_config_update({
override_global_rate_limiting_plugin = true,
override_global_key_auth_plugin = true,
})
retry(function ()
for _, path in ipairs({ test_path_1, test_path_2 }) do
validate_headers(client_requests(7, function()
return GET(path)
end), true)
end -- for _, path in ipairs({ test_path_1, test_path_2 }) do
end) -- retry(function ()
end) -- it(fmt("blocks if exceeding limit (multiple %s)", limit_by), function ()
end -- if limit_by == "service" then
if limit_by == "path" then
it("blocks if exceeding limit (multiple path)", function()
local test_path_1, test_path_2 = "/1-test", "/2-test"
local service = setup_service(admin_client, UPSTREAM_URL)
local route_1, route_2 = setup_route(admin_client, service, { test_path_1 }),
setup_route(admin_client, service, { test_path_2 })
local rl_plugin = setup_rl_plugin(admin_client, {
minute = 6,
policy = policy,
limit_by = "path",
path = test_path_1,
redis = {
host = REDIS_HOST,
port = ssl_conf.redis_port,
password = REDIS_PASSWORD,
database = REDIS_DATABASE,
ssl = ssl_conf.redis_ssl,
ssl_verify = ssl_conf.redis_ssl_verify,
server_name = ssl_conf.redis_server_name,
}
}, service)
finally(function()
delete_plugin(admin_client, rl_plugin)
delete_route(admin_client, route_1)
delete_route(admin_client, route_2)
delete_service(admin_client, service)
end)
helpers.wait_for_all_config_update({
override_global_rate_limiting_plugin = true,
override_global_key_auth_plugin = true,
})
retry(function ()
for _, path in ipairs({ test_path_1, test_path_2 }) do
validate_headers(client_requests(7, function()
return GET(path)
end), true)
end -- for _, path in ipairs({ test_path_1, test_path_2 }) do
end) -- retry(function ()
end) -- it("blocks if exceeding limit (multiple path)", function()
end -- if limit_by == "path" then
if limit_by == "header" then
it("blocks if exceeding limit (multiple header)", function()
local test_path = "/test"
local test_header_1, test_header_2 = "test-header-1", "test-header-2"
local service = setup_service(admin_client, UPSTREAM_URL)
local route = setup_route(admin_client, service, { test_path })
local rl_plugin = setup_rl_plugin(admin_client, {
minute = 6,
policy = policy,
limit_by = "header",
header_name = test_header_1,
redis = {
host = REDIS_HOST,
port = ssl_conf.redis_port,
password = REDIS_PASSWORD,
database = REDIS_DATABASE,
ssl = ssl_conf.redis_ssl,
ssl_verify = ssl_conf.redis_ssl_verify,
server_name = ssl_conf.redis_server_name,
}
}, service)
finally(function()
delete_plugin(admin_client, rl_plugin)
delete_route(admin_client, route)
delete_service(admin_client, service)
end)
helpers.wait_for_all_config_update({
override_global_rate_limiting_plugin = true,
override_global_key_auth_plugin = true,
})
retry(function ()
for _, header_name in ipairs({ test_header_1, test_header_2 }) do
validate_headers(client_requests(7, function()
return GET(test_path, { headers = { [header_name] = "test" }})
end), true)
end -- for _, header_name in ipairs({ test_header_1, test_header_2 }) do
end) -- retry(function ()
end) -- it("blocks if exceeding limit (multiple header)", function()
end -- if limit_by == "header" then
if limit_by == "consumer" or limit_by == "credential" then
it(fmt("blocks if exceeding limit (multiple %s)", limit_by), function()
local test_path = "/test"
local test_key_name = "test-key"
local test_credential_1, test_credential_2 = "test_credential_1", "test_credential_2"
local service = setup_service(admin_client, UPSTREAM_URL)
local route = setup_route(admin_client, service, { test_path })
local rl_plugin = setup_rl_plugin(admin_client, {
minute = 6,
policy = policy,
limit_by = limit_by,
redis = {