route_with_plugin_jwt_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. package route_test
  18. import (
  19. "io/ioutil"
  20. "net/http"
  21. "time"
  22. . "github.com/onsi/ginkgo/v2"
  23. . "github.com/onsi/gomega"
  24. "github.com/apisix/manager-api/test/e2e/base"
  25. )
  26. var _ = Describe("route with jwt plugin", func() {
  27. var jwtToken string
  28. DescribeTable("create route and consumer with jwt plugin",
  29. func(tc base.HttpTestCase) {
  30. base.RunTestCase(tc)
  31. },
  32. Entry("make sure the route is not created ", base.HttpTestCase{
  33. Object: base.APISIXExpect(),
  34. Method: http.MethodGet,
  35. Path: "/hello",
  36. ExpectStatus: http.StatusNotFound,
  37. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  38. }),
  39. Entry("create route", base.HttpTestCase{
  40. Object: base.ManagerApiExpect(),
  41. Method: http.MethodPut,
  42. Path: "/apisix/admin/routes/r1",
  43. Body: `{
  44. "name": "route1",
  45. "uri": "/hello",
  46. "plugins": {
  47. "jwt-auth": {}
  48. },
  49. "upstream": {
  50. "type": "roundrobin",
  51. "nodes": {
  52. "` + base.UpstreamIp + `:1981": 1
  53. }
  54. }
  55. }`,
  56. Headers: map[string]string{"Authorization": base.GetToken()},
  57. ExpectStatus: http.StatusOK,
  58. ExpectBody: []string{`"code":0`, `"id":"r1"`, `"uri":"/hello"`, `"name":"route1"`, `"jwt-auth":{}`},
  59. }),
  60. Entry("make sure the consumer is not created", base.HttpTestCase{
  61. Object: base.ManagerApiExpect(),
  62. Method: http.MethodGet,
  63. Path: "/apisix/admin/consumers/jack",
  64. Headers: map[string]string{"Authorization": base.GetToken()},
  65. ExpectStatus: http.StatusNotFound,
  66. }),
  67. Entry("create consumer", base.HttpTestCase{
  68. Object: base.ManagerApiExpect(),
  69. Path: "/apisix/admin/consumers",
  70. Method: http.MethodPut,
  71. Body: `{
  72. "username": "jack",
  73. "plugins": {
  74. "jwt-auth": {
  75. "key": "user-key",
  76. "secret": "my-secret-key",
  77. "algorithm": "HS256"
  78. }
  79. },
  80. "desc": "test description"
  81. }`,
  82. Headers: map[string]string{"Authorization": base.GetToken()},
  83. ExpectStatus: http.StatusOK,
  84. ExpectBody: []string{`"code":0`, `"username":"jack"`, `"key":"user-key"`, `"secret":"my-secret-key"`},
  85. }),
  86. )
  87. It("create public api for JWT sign", func() {
  88. base.RunTestCase(base.HttpTestCase{
  89. Object: base.ManagerApiExpect(),
  90. Method: http.MethodPut,
  91. Path: "/apisix/admin/routes/jwt-sign",
  92. Body: `{
  93. "name": "jwt-auth",
  94. "uri": "/apisix/plugin/jwt/sign",
  95. "plugins": {
  96. "public-api": {}
  97. },
  98. "upstream": {
  99. "type": "roundrobin",
  100. "nodes": [{
  101. "host": "` + base.UpstreamIp + `",
  102. "port": 1980,
  103. "weight": 1
  104. }]
  105. }
  106. }`,
  107. Headers: map[string]string{"Authorization": base.GetToken()},
  108. ExpectStatus: http.StatusOK,
  109. ExpectBody: `"code":0`,
  110. })
  111. })
  112. It("sign jwt token", func() {
  113. time.Sleep(base.SleepTime)
  114. // sign jwt token
  115. body, status, err := base.HttpGet(base.APISIXHost+"/apisix/plugin/jwt/sign?key=user-key", nil)
  116. Expect(err).To(BeNil())
  117. Expect(status).To(Equal(http.StatusOK))
  118. jwtToken = string(body)
  119. // sign jwt token with not exists key
  120. body, status, err = base.HttpGet(base.APISIXHost+"/apisix/plugin/jwt/sign?key=not-exist-key", nil)
  121. Expect(err).To(BeNil())
  122. Expect(status).To(Equal(http.StatusNotFound))
  123. })
  124. It("verify route with correct jwt token", func() {
  125. base.RunTestCase(base.HttpTestCase{
  126. Object: base.APISIXExpect(),
  127. Method: http.MethodGet,
  128. Path: "/hello",
  129. Headers: map[string]string{"Authorization": jwtToken},
  130. ExpectStatus: http.StatusOK,
  131. ExpectBody: "hello world",
  132. })
  133. })
  134. DescribeTable("verify token and clean consumer",
  135. func(tc base.HttpTestCase) {
  136. base.RunTestCase(tc)
  137. },
  138. Entry("verify route without jwt token", base.HttpTestCase{
  139. Object: base.APISIXExpect(),
  140. Method: http.MethodGet,
  141. Path: "/hello",
  142. ExpectStatus: http.StatusUnauthorized,
  143. ExpectBody: `{"message":"Missing JWT token in request"}`,
  144. Sleep: base.SleepTime,
  145. }),
  146. Entry("verify route with incorrect jwt token", base.HttpTestCase{
  147. Object: base.APISIXExpect(),
  148. Method: http.MethodGet,
  149. Path: "/hello",
  150. Headers: map[string]string{"Authorization": "invalid-token"},
  151. ExpectStatus: http.StatusUnauthorized,
  152. ExpectBody: `{"message":"JWT token invalid"}`,
  153. }),
  154. Entry("delete consumer", base.HttpTestCase{
  155. Object: base.ManagerApiExpect(),
  156. Method: http.MethodDelete,
  157. Path: "/apisix/admin/consumers/jack",
  158. Headers: map[string]string{"Authorization": base.GetToken()},
  159. ExpectStatus: http.StatusOK,
  160. }),
  161. )
  162. It("verify route with the jwt token from just deleted consumer", func() {
  163. base.RunTestCase(base.HttpTestCase{
  164. Object: base.APISIXExpect(),
  165. Method: http.MethodGet,
  166. Path: "/hello",
  167. Headers: map[string]string{"Authorization": jwtToken},
  168. ExpectStatus: http.StatusUnauthorized,
  169. ExpectBody: `{"message":"Missing related consumer"}`,
  170. Sleep: base.SleepTime,
  171. })
  172. })
  173. DescribeTable("cleanup route and verify",
  174. func(tc base.HttpTestCase) {
  175. base.RunTestCase(tc)
  176. },
  177. Entry("delete route", base.HttpTestCase{
  178. Object: base.ManagerApiExpect(),
  179. Method: http.MethodDelete,
  180. Path: "/apisix/admin/routes/r1",
  181. Headers: map[string]string{"Authorization": base.GetToken()},
  182. ExpectStatus: http.StatusOK,
  183. }),
  184. Entry("verify the deleted route", base.HttpTestCase{
  185. Object: base.APISIXExpect(),
  186. Method: http.MethodGet,
  187. Path: "/hello",
  188. ExpectStatus: http.StatusNotFound,
  189. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  190. Sleep: base.SleepTime,
  191. }),
  192. )
  193. DescribeTable("create route and consumer with jwt (no algorithm)",
  194. func(tc base.HttpTestCase) {
  195. base.RunTestCase(tc)
  196. },
  197. Entry("create consumer with jwt (no algorithm)", base.HttpTestCase{
  198. Object: base.ManagerApiExpect(),
  199. Path: "/apisix/admin/consumers",
  200. Method: http.MethodPut,
  201. Body: `{
  202. "username":"consumer_1",
  203. "desc": "test description",
  204. "plugins":{
  205. "jwt-auth":{
  206. "exp":86400,
  207. "key":"user-key",
  208. "secret":"my-secret-key"
  209. }
  210. }
  211. }`,
  212. Headers: map[string]string{"Authorization": base.GetToken()},
  213. ExpectStatus: http.StatusOK,
  214. ExpectBody: []string{`"code":0`, `"username":"consumer_1"`,
  215. `"jwt-auth":{"exp":86400,"key":"user-key","secret":"my-secret-key"}`},
  216. }),
  217. Entry("get the consumer", base.HttpTestCase{
  218. Object: base.ManagerApiExpect(),
  219. Path: "/apisix/admin/consumers/consumer_1",
  220. Method: http.MethodGet,
  221. Headers: map[string]string{"Authorization": base.GetToken()},
  222. ExpectStatus: http.StatusOK,
  223. ExpectBody: `"username":"consumer_1"`,
  224. Sleep: base.SleepTime,
  225. }),
  226. Entry("create the route", base.HttpTestCase{
  227. Object: base.ManagerApiExpect(),
  228. Method: http.MethodPut,
  229. Path: "/apisix/admin/routes/r1",
  230. Body: `{
  231. "name": "route1",
  232. "uri": "/hello",
  233. "plugins": {
  234. "jwt-auth": {}
  235. },
  236. "upstream": {
  237. "type": "roundrobin",
  238. "nodes": {
  239. "` + base.UpstreamIp + `:1980": 1
  240. }
  241. }
  242. }`,
  243. Headers: map[string]string{"Authorization": base.GetToken()},
  244. ExpectBody: []string{`"code":0`, `"id":"r1"`, `"uri":"/hello"`, `"name":"route1"`, `"jwt-auth":{}`},
  245. ExpectStatus: http.StatusOK,
  246. }),
  247. )
  248. It("get the jwt token", func() {
  249. time.Sleep(base.SleepTime)
  250. request, _ := http.NewRequest("GET", base.APISIXHost+"/apisix/plugin/jwt/sign?key=user-key", nil)
  251. resp, err := http.DefaultClient.Do(request)
  252. Expect(err).To(BeNil())
  253. defer resp.Body.Close()
  254. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  255. jwtTokenBytes, _ := ioutil.ReadAll(resp.Body)
  256. jwtToken = string(jwtTokenBytes)
  257. })
  258. It("hit route with jwt token", func() {
  259. base.RunTestCase(base.HttpTestCase{
  260. Object: base.APISIXExpect(),
  261. Method: http.MethodGet,
  262. Path: "/hello",
  263. Headers: map[string]string{"Authorization": jwtToken},
  264. ExpectStatus: http.StatusOK,
  265. ExpectBody: "hello world",
  266. Sleep: base.SleepTime,
  267. })
  268. })
  269. DescribeTable("cleanup consumer and route",
  270. func(tc base.HttpTestCase) {
  271. base.RunTestCase(tc)
  272. },
  273. Entry("delete consumer", base.HttpTestCase{
  274. Object: base.ManagerApiExpect(),
  275. Path: "/apisix/admin/consumers/consumer_1",
  276. Method: http.MethodDelete,
  277. Headers: map[string]string{"Authorization": base.GetToken()},
  278. ExpectStatus: http.StatusOK,
  279. ExpectBody: `"code":0`,
  280. }),
  281. Entry("after delete consumer verify it again", base.HttpTestCase{
  282. Object: base.ManagerApiExpect(),
  283. Method: http.MethodGet,
  284. Path: "/apisix/admin/consumers/jack",
  285. Headers: map[string]string{"Authorization": base.GetToken()},
  286. ExpectStatus: http.StatusNotFound,
  287. ExpectBody: `"message":"data not found"`,
  288. Sleep: base.SleepTime,
  289. }),
  290. Entry("delete the route", base.HttpTestCase{
  291. Object: base.ManagerApiExpect(),
  292. Method: http.MethodDelete,
  293. Path: "/apisix/admin/routes/r1",
  294. Headers: map[string]string{"Authorization": base.GetToken()},
  295. ExpectStatus: http.StatusOK,
  296. }),
  297. Entry("delete the route", base.HttpTestCase{
  298. Object: base.ManagerApiExpect(),
  299. Method: http.MethodDelete,
  300. Path: "/apisix/admin/routes/jwt-sign",
  301. Headers: map[string]string{"Authorization": base.GetToken()},
  302. ExpectStatus: http.StatusOK,
  303. }),
  304. Entry("verify the deleted route", base.HttpTestCase{
  305. Object: base.APISIXExpect(),
  306. Method: http.MethodGet,
  307. Path: "/hello",
  308. ExpectStatus: http.StatusNotFound,
  309. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  310. Sleep: base.SleepTime,
  311. }),
  312. )
  313. })