Skip to content

Commit 03d784d

Browse files
committed
API key query - rest spec and yaml tests
1 parent 245ba38 commit 03d784d

File tree

2 files changed

+258
-0
lines changed

2 files changed

+258
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"security.query_api_key":{
3+
"documentation":{
4+
"url":"https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-query-api-key.html",
5+
"description":"Retrieves information for API keys using a subset of query DSL"
6+
},
7+
"stability":"stable",
8+
"visibility":"public",
9+
"headers":{
10+
"accept": [ "application/json"],
11+
"content_type": ["application/json"]
12+
},
13+
"url":{
14+
"paths":[
15+
{
16+
"path":"/_security/_query/api_key",
17+
"methods":[
18+
"GET",
19+
"POST"
20+
]
21+
}
22+
]
23+
},
24+
"params":{},
25+
"body":{
26+
"description":"From, size, query, sort and search_after",
27+
"required":false
28+
}
29+
}
30+
}
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
---
2+
setup:
3+
- skip:
4+
features: headers
5+
6+
- do:
7+
cluster.health:
8+
wait_for_status: yellow
9+
10+
- do:
11+
security.put_role:
12+
name: "admin_role"
13+
body: >
14+
{
15+
"cluster": ["manage_api_key"]
16+
}
17+
18+
- do:
19+
security.put_role:
20+
name: "user_role"
21+
body: >
22+
{
23+
"cluster": ["manage_own_api_key"]
24+
}
25+
26+
- do:
27+
security.put_user:
28+
username: "api_key_manager"
29+
body: >
30+
{
31+
"password" : "x-pack-test-password",
32+
"roles" : [ "admin_role" ],
33+
"full_name" : "API key manager"
34+
}
35+
36+
- do:
37+
security.put_user:
38+
username: "api_key_user_1"
39+
body: >
40+
{
41+
"password" : "x-pack-test-password",
42+
"roles" : [ "user_role" ],
43+
"full_name" : "API key user 1"
44+
}
45+
46+
- do:
47+
security.put_user:
48+
username: "api_key_user_2"
49+
body: >
50+
{
51+
"password" : "x-pack-test-password",
52+
"roles" : [ "user_role" ],
53+
"full_name" : "API key user 2"
54+
}
55+
56+
---
57+
teardown:
58+
- do:
59+
security.delete_role:
60+
name: "admin_role"
61+
ignore: 404
62+
63+
- do:
64+
security.delete_role:
65+
name: "use_role"
66+
ignore: 404
67+
68+
- do:
69+
security.delete_user:
70+
username: "api_key_user_1"
71+
ignore: 404
72+
73+
- do:
74+
security.delete_user:
75+
username: "api_key_user_2"
76+
ignore: 404
77+
- do:
78+
security.delete_user:
79+
username: "api_key_manager"
80+
ignore: 404
81+
82+
---
83+
"Test query api key":
84+
85+
- do:
86+
headers:
87+
Authorization: "Basic YXBpX2tleV9tYW5hZ2VyOngtcGFjay10ZXN0LXBhc3N3b3Jk" # api_key_manager
88+
security.create_api_key:
89+
body: >
90+
{
91+
"name": "manager-api-key",
92+
"expiration": "10d",
93+
"metadata": {
94+
"letter": "a",
95+
"number": 42
96+
}
97+
}
98+
- match: { name: "manager-api-key" }
99+
- set: { id: manager_key_id }
100+
101+
- do:
102+
headers:
103+
Authorization: "Basic YXBpX2tleV91c2VyXzE6eC1wYWNrLXRlc3QtcGFzc3dvcmQ=" # api_key_user_1
104+
security.create_api_key:
105+
body: >
106+
{
107+
"name": "user1-api-key",
108+
"expiration": "1d",
109+
"metadata": {
110+
"letter": "a",
111+
"number": 1
112+
}
113+
}
114+
- match: { name: "user1-api-key" }
115+
- set: { id: user1_key_id }
116+
117+
- do:
118+
headers:
119+
Authorization: "Basic YXBpX2tleV91c2VyXzI6eC1wYWNrLXRlc3QtcGFzc3dvcmQ=" # api_key_user_2
120+
security.create_api_key:
121+
body: >
122+
{
123+
"name": "user2-api-key",
124+
"expiration": "1d",
125+
"metadata": {
126+
"letter": "b",
127+
"number": 42
128+
}
129+
}
130+
- match: { name: "user2-api-key" }
131+
- set: { id: user2_key_id }
132+
133+
# empty body works just like match_all
134+
- do:
135+
headers:
136+
Authorization: "Basic YXBpX2tleV9tYW5hZ2VyOngtcGFjay10ZXN0LXBhc3N3b3Jk" # api_key_manager
137+
security.query_api_key:
138+
body: {}
139+
- match: { total: 3 }
140+
- match: { count: 3 }
141+
142+
# match_all
143+
- do:
144+
headers:
145+
Authorization: "Basic YXBpX2tleV91c2VyXzE6eC1wYWNrLXRlc3QtcGFzc3dvcmQ=" # api_key_user_1
146+
security.query_api_key:
147+
body: >
148+
{
149+
"query": { "match_all": {} }
150+
}
151+
- match: { total: 1 }
152+
- match: { count: 1 }
153+
- match: { api_keys.0.id: "${user1_key_id}" }
154+
155+
- do:
156+
headers:
157+
Authorization: "Basic YXBpX2tleV91c2VyXzI6eC1wYWNrLXRlc3QtcGFzc3dvcmQ=" # api_key_user_2
158+
security.query_api_key:
159+
body: >
160+
{
161+
"query": { "wildcard": {"name": "user*"} }
162+
}
163+
- match: { total: 1 }
164+
- match: { count: 1 }
165+
- match: { api_keys.0.id: "${user2_key_id}" }
166+
167+
- do:
168+
headers:
169+
Authorization: "Basic YXBpX2tleV9tYW5hZ2VyOngtcGFjay10ZXN0LXBhc3N3b3Jk" # api_key_manager
170+
security.query_api_key:
171+
body: >
172+
{
173+
"query": { "wildcard": {"name": "user*"} },
174+
"sort": [ {"creation": {"order": "desc"}} ],
175+
"from": 1,
176+
"size": 1
177+
}
178+
- match: { total: 2 }
179+
- match: { count: 1 }
180+
- match: { api_keys.0.id: "${user1_key_id}" }
181+
182+
- do:
183+
headers:
184+
Authorization: "Basic YXBpX2tleV9tYW5hZ2VyOngtcGFjay10ZXN0LXBhc3N3b3Jk" # api_key_manager
185+
security.query_api_key:
186+
body: >
187+
{
188+
"query": { "wildcard": {"name": "*key"} },
189+
"sort": [ "expiration", "username" ],
190+
"size": 1
191+
}
192+
- match: { total: 3 }
193+
- match: { count: 1 }
194+
- match: { api_keys.0.id: "${user1_key_id}" }
195+
- set: { api_keys.0.expiration: expiration0 }
196+
- set: { api_keys.0.username: username0 }
197+
198+
- do:
199+
headers:
200+
Authorization: "Basic YXBpX2tleV9tYW5hZ2VyOngtcGFjay10ZXN0LXBhc3N3b3Jk" # api_key_manager
201+
security.query_api_key:
202+
body: >
203+
{
204+
"query": { "wildcard": {"name": "*key"} },
205+
"sort": [ "expiration", "username" ],
206+
"size": 1,
207+
"search_after": [ "${expiration0}", "${username0}" ]
208+
}
209+
- match: { total: 3 }
210+
- match: { count: 1 }
211+
- match: { api_keys.0.id: "${user2_key_id}" }
212+
- set: { api_keys.0.expiration: expiration1 }
213+
- set: { api_keys.0.username: username1 }
214+
215+
- do:
216+
headers:
217+
Authorization: "Basic YXBpX2tleV9tYW5hZ2VyOngtcGFjay10ZXN0LXBhc3N3b3Jk" # api_key_manager
218+
security.query_api_key:
219+
body: >
220+
{
221+
"query": { "wildcard": {"name": "*key"} },
222+
"sort": [ "expiration", "username" ],
223+
"size": 1,
224+
"search_after": [ "${expiration1}", "${username1}" ]
225+
}
226+
- match: { total: 3 }
227+
- match: { count: 1 }
228+
- match: { api_keys.0.id: "${manager_key_id}" }

0 commit comments

Comments
 (0)