Skip to content

Commit 2c2827e

Browse files
fix: add schema compatibility for consumer and credential resources (#13154)
1 parent c89f85e commit 2c2827e

File tree

2 files changed

+129
-1
lines changed

2 files changed

+129
-1
lines changed

apisix/schema_def.lua

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,7 @@ _M.consumer = {
728728
type = "object",
729729
properties = {
730730
-- metadata
731+
id = id_schema,
731732
username = {
732733
type = "string", minLength = 1, maxLength = rule_name_def.maxLength,
733734
pattern = [[^[a-zA-Z0-9_\-]+$]]
@@ -749,7 +750,17 @@ _M.credential = {
749750
type = "object",
750751
properties = {
751752
-- metadata
752-
id = id_schema,
753+
id = {
754+
oneOf = {
755+
id_schema,
756+
{
757+
type = "string",
758+
minLength = 15,
759+
maxLength = 128,
760+
pattern = [[^[a-zA-Z0-9-_]+/credentials/[a-zA-Z0-9-_.]+$]],
761+
}
762+
}
763+
},
753764
name = rule_name_def,
754765
desc = desc_def,
755766
labels = labels_def,

t/config-center-yaml/credentials.t

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
use t::APISIX 'no_plan';
18+
19+
repeat_each(1);
20+
log_level('info');
21+
no_root_location();
22+
no_shuffle();
23+
24+
add_block_preprocessor(sub {
25+
my ($block) = @_;
26+
27+
my $yaml_config = $block->yaml_config // <<_EOC_;
28+
apisix:
29+
node_listen: 1984
30+
deployment:
31+
role: data_plane
32+
role_data_plane:
33+
config_provider: yaml
34+
_EOC_
35+
36+
$block->set_value("yaml_config", $yaml_config);
37+
});
38+
39+
run_tests();
40+
41+
__DATA__
42+
43+
=== TEST 1: test with username in id field - invalid key
44+
--- apisix_yaml
45+
routes:
46+
-
47+
uri: /hello
48+
plugins:
49+
key-auth:
50+
upstream:
51+
nodes:
52+
"127.0.0.1:1980": 1
53+
type: roundrobin
54+
consumers:
55+
- id: rose/credentials/542f7414-87f6-4793-a68e-99983dde9913
56+
name: rose
57+
plugins:
58+
key-auth:
59+
key: csdt8UPw76/SG+WlHBoFeg==
60+
61+
- id: rose
62+
username: rose
63+
plugins:
64+
limit-count:
65+
time_window: 3
66+
count: 100000000
67+
key_type: var
68+
allow_degradation: false
69+
key: remote_addr
70+
rejected_code: 429
71+
show_limit_quota_header: true
72+
policy: local
73+
#END
74+
--- more_headers
75+
apikey: user-key
76+
--- error_code: 401
77+
--- request
78+
POST /hello
79+
80+
81+
82+
=== TEST 2: test with username in id field - valid key
83+
--- apisix_yaml
84+
routes:
85+
-
86+
uri: /hello
87+
plugins:
88+
key-auth:
89+
upstream:
90+
nodes:
91+
"127.0.0.1:1980": 1
92+
type: roundrobin
93+
consumers:
94+
- id: rose/credentials/542f7414-87f6-4793-a68e-99983dde9913
95+
name: rose
96+
plugins:
97+
key-auth:
98+
key: csdt8UPw76/SG+WlHBoFeg==
99+
100+
- id: rose
101+
username: rose
102+
plugins:
103+
limit-count:
104+
time_window: 3
105+
count: 100000000
106+
key_type: var
107+
allow_degradation: false
108+
key: remote_addr
109+
rejected_code: 429
110+
show_limit_quota_header: true
111+
policy: local
112+
#END
113+
--- more_headers
114+
apikey: csdt8UPw76/SG+WlHBoFeg==
115+
--- error_code: 200
116+
--- request
117+
POST /hello

0 commit comments

Comments
 (0)