/x-pack/plugin/src/yamlRestTest/resources/rest-api-spec/test/api_key/10_basic.yml

https://github.com/imotov/elasticsearch · YAML · 381 lines · 351 code · 29 blank · 1 comment · 0 complexity · aa4cf29313ed99b3ff0cc39d556a227a MD5 · raw file

  1. ---
  2. setup:
  3. - skip:
  4. features: headers
  5. - do:
  6. cluster.health:
  7. wait_for_status: yellow
  8. - do:
  9. security.put_role:
  10. name: "admin_role"
  11. body: >
  12. {
  13. "cluster": ["manage_api_key"],
  14. "indices": [
  15. {
  16. "names": "*",
  17. "privileges": ["all"]
  18. }
  19. ],
  20. "applications": [
  21. {
  22. "application": "myapp",
  23. "privileges": ["*"],
  24. "resources": ["*"]
  25. }
  26. ]
  27. }
  28. - do:
  29. security.put_user:
  30. username: "api_key_user"
  31. body: >
  32. {
  33. "password" : "x-pack-test-password",
  34. "roles" : [ "admin_role" ],
  35. "full_name" : "API key user"
  36. }
  37. # Create app privileges
  38. - do:
  39. security.put_privileges:
  40. body: >
  41. {
  42. "myapp": {
  43. "read": {
  44. "application": "myapp",
  45. "name": "read",
  46. "actions": [ "data:read/*" ]
  47. },
  48. "write": {
  49. "application": "myapp",
  50. "name": "write",
  51. "actions": [ "data:write/*" ]
  52. }
  53. }
  54. }
  55. ---
  56. teardown:
  57. - do:
  58. security.delete_role:
  59. name: "admin_role"
  60. ignore: 404
  61. - do:
  62. security.delete_user:
  63. username: "api_key_user"
  64. ignore: 404
  65. - do:
  66. security.delete_privileges:
  67. application: myapp
  68. name: "read,write"
  69. ignore: 404
  70. ---
  71. "Test create api key":
  72. - do:
  73. headers:
  74. Authorization: "Basic YXBpX2tleV91c2VyOngtcGFjay10ZXN0LXBhc3N3b3Jk" # api_key_user
  75. security.create_api_key:
  76. body: >
  77. {
  78. "name": "my-api-key",
  79. "expiration": "1d",
  80. "role_descriptors": {
  81. "role-a": {
  82. "cluster": ["all"],
  83. "index": [
  84. {
  85. "names": ["index-a"],
  86. "privileges": ["read"]
  87. }
  88. ]
  89. },
  90. "role-b": {
  91. "cluster": ["manage"],
  92. "index": [
  93. {
  94. "names": ["index-b"],
  95. "privileges": ["all"]
  96. }
  97. ]
  98. }
  99. }
  100. }
  101. - match: { name: "my-api-key" }
  102. - is_true: id
  103. - is_true: api_key
  104. - is_true: expiration
  105. - set: { id: api_key_id }
  106. - transform_and_set: { login_creds: "#base64EncodeCredentials(id,api_key)" }
  107. - match: { encoded: $login_creds }
  108. - do:
  109. headers:
  110. Authorization: ApiKey ${login_creds}
  111. security.authenticate: {}
  112. - match: { username: "api_key_user" }
  113. - length: { roles: 0 }
  114. - match: { authentication_realm.name: "_es_api_key" }
  115. - match: { authentication_realm.type: "_es_api_key" }
  116. - match: { api_key.id: "${api_key_id}" }
  117. - match: { api_key.name: "my-api-key" }
  118. - do:
  119. security.clear_api_key_cache:
  120. ids: "${api_key_id}"
  121. - match: { _nodes.failed: 0 }
  122. ---
  123. "Test get api key":
  124. - skip:
  125. features: transform_and_set
  126. - do:
  127. headers:
  128. Authorization: "Basic YXBpX2tleV91c2VyOngtcGFjay10ZXN0LXBhc3N3b3Jk" # api_key_user
  129. security.create_api_key:
  130. body: >
  131. {
  132. "name": "my-api-key",
  133. "expiration": "1d",
  134. "role_descriptors": {
  135. "role-a": {
  136. "cluster": ["all"],
  137. "index": [
  138. {
  139. "names": ["index-a"],
  140. "privileges": ["read"]
  141. }
  142. ]
  143. },
  144. "role-b": {
  145. "cluster": ["manage"],
  146. "index": [
  147. {
  148. "names": ["index-b"],
  149. "privileges": ["all"]
  150. }
  151. ]
  152. }
  153. }
  154. }
  155. - match: { name: "my-api-key" }
  156. - is_true: id
  157. - is_true: api_key
  158. - is_true: expiration
  159. - set: { id: api_key_id }
  160. - set: { name: api_key_name }
  161. - do:
  162. headers:
  163. Authorization: "Basic YXBpX2tleV91c2VyOngtcGFjay10ZXN0LXBhc3N3b3Jk" # api_key_user
  164. security.get_api_key:
  165. id: "$api_key_id"
  166. - match: { "api_keys.0.id": "$api_key_id" }
  167. - match: { "api_keys.0.name": "$api_key_name" }
  168. - match: { "api_keys.0.username": "api_key_user" }
  169. - match: { "api_keys.0.invalidated": false }
  170. - is_true: "api_keys.0.creation"
  171. - do:
  172. headers:
  173. Authorization: "Basic YXBpX2tleV91c2VyOngtcGFjay10ZXN0LXBhc3N3b3Jk" # api_key_user
  174. security.get_api_key:
  175. owner: true
  176. - length: { "api_keys" : 1 }
  177. - match: { "api_keys.0.username": "api_key_user" }
  178. - match: { "api_keys.0.invalidated": false }
  179. - is_true: "api_keys.0.creation"
  180. - do:
  181. security.clear_api_key_cache:
  182. ids: "*"
  183. - match: { _nodes.failed: 0 }
  184. ---
  185. "Test invalidate api keys":
  186. - skip:
  187. features: transform_and_set
  188. - do:
  189. headers:
  190. Authorization: "Basic YXBpX2tleV91c2VyOngtcGFjay10ZXN0LXBhc3N3b3Jk" # api_key_user
  191. security.create_api_key:
  192. body: >
  193. {
  194. "name": "my-api-key-1",
  195. "expiration": "1d",
  196. "role_descriptors": {
  197. }
  198. }
  199. - match: { name: "my-api-key-1" }
  200. - is_true: id
  201. - is_true: api_key
  202. - is_true: expiration
  203. - set: { id: api_key_id_1 }
  204. - transform_and_set: { login_creds: "#base64EncodeCredentials(id,api_key)" }
  205. - match: { encoded: $login_creds }
  206. - do:
  207. headers:
  208. Authorization: "Basic YXBpX2tleV91c2VyOngtcGFjay10ZXN0LXBhc3N3b3Jk" # api_key_user
  209. security.create_api_key:
  210. body: >
  211. {
  212. "name": "my-api-key-2",
  213. "expiration": "1d",
  214. "role_descriptors": {
  215. }
  216. }
  217. - match: { name: "my-api-key-2" }
  218. - is_true: id
  219. - is_true: api_key
  220. - is_true: expiration
  221. - set: { id: api_key_id_2 }
  222. - do:
  223. headers:
  224. Authorization: "Basic YXBpX2tleV91c2VyOngtcGFjay10ZXN0LXBhc3N3b3Jk" # api_key_user
  225. security.create_api_key:
  226. body: >
  227. {
  228. "name": "my-api-key-3",
  229. "expiration": "1d",
  230. "role_descriptors": {
  231. }
  232. }
  233. - match: { name: "my-api-key-3" }
  234. - is_true: id
  235. - is_true: api_key
  236. - is_true: expiration
  237. - set: { id: api_key_id_3 }
  238. - do:
  239. headers:
  240. Authorization: "Basic YXBpX2tleV91c2VyOngtcGFjay10ZXN0LXBhc3N3b3Jk" # api_key_user
  241. security.invalidate_api_key:
  242. body: >
  243. {
  244. "ids": [ "${api_key_id_1}", "${api_key_id_2}", "${api_key_id_3}" ]
  245. }
  246. - length: { "invalidated_api_keys" : 3 }
  247. - match: { "invalidated_api_keys.0" : "/^(${api_key_id_1}|${api_key_id_2}|${api_key_id_3})$/" }
  248. - match: { "invalidated_api_keys.1" : "/^(${api_key_id_1}|${api_key_id_2}|${api_key_id_3})$/" }
  249. - match: { "invalidated_api_keys.2" : "/^(${api_key_id_1}|${api_key_id_2}|${api_key_id_3})$/" }
  250. - length: { "previously_invalidated_api_keys" : 0 }
  251. - match: { "error_count" : 0 }
  252. ---
  253. "Test has privileges API for api key":
  254. - skip:
  255. features: transform_and_set
  256. - do:
  257. headers:
  258. Authorization: "Basic YXBpX2tleV91c2VyOngtcGFjay10ZXN0LXBhc3N3b3Jk" # api_key_user
  259. security.create_api_key:
  260. body: >
  261. {
  262. "name": "my-api-key",
  263. "expiration": "1d",
  264. "role_descriptors": {
  265. "role-a": {
  266. "cluster": ["all"],
  267. "index": [
  268. {
  269. "names": ["index-a"],
  270. "privileges": ["read"]
  271. }
  272. ],
  273. "applications": [
  274. {
  275. "application": "myapp",
  276. "privileges": ["read"],
  277. "resources": ["*"]
  278. }
  279. ]
  280. },
  281. "role-b": {
  282. "cluster": ["manage"],
  283. "index": [
  284. {
  285. "names": ["index-b"],
  286. "privileges": ["all"]
  287. }
  288. ]
  289. }
  290. }
  291. }
  292. - match: { name: "my-api-key" }
  293. - is_true: id
  294. - is_true: api_key
  295. - is_true: expiration
  296. - transform_and_set: { login_creds: "#base64EncodeCredentials(id,api_key)" }
  297. - match: { encoded: $login_creds }
  298. - do:
  299. headers:
  300. Authorization: ApiKey ${login_creds}
  301. security.has_privileges:
  302. user: null
  303. body: >
  304. {
  305. "index": [
  306. {
  307. "names" :[ "*", "index-a" ],
  308. "privileges" : [ "read", "index", "write" ]
  309. },
  310. {
  311. "names" :[ "index-a", "index-b" ],
  312. "privileges" : [ "read", "write" ]
  313. }
  314. ],
  315. "application": [
  316. {
  317. "application" : "myapp",
  318. "resources" : [ "*", "some-other-res" ],
  319. "privileges" : [ "data:read/me", "data:write/me" ]
  320. }
  321. ]
  322. }
  323. - match: { "username" : "api_key_user" }
  324. - match: { "has_all_requested" : false }
  325. - match: { "index" : {
  326. "*" : {
  327. "read": false,
  328. "index": false,
  329. "write": false
  330. },
  331. "index-a" : {
  332. "read": true,
  333. "index": false,
  334. "write": false
  335. },
  336. "index-b" : {
  337. "read": true,
  338. "write": true
  339. }
  340. } }
  341. - match: { "application" : {
  342. "myapp" : {
  343. "*" : {
  344. "data:read/me" : true,
  345. "data:write/me" : false
  346. },
  347. "some-other-res" : {
  348. "data:read/me" : true,
  349. "data:write/me" : false
  350. }
  351. }
  352. } }