PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/middleware_ip_whitelist_test.go

https://gitlab.com/jjae2124/tyk
Go | 382 lines | 338 code | 44 blank | 0 comment | 28 complexity | 58db410ca33b765c2f0fcc1fd64f9dfb MD5 | raw file
  1. package main
  2. import (
  3. "github.com/gorilla/mux"
  4. "net/http"
  5. "net/http/httptest"
  6. "net/url"
  7. "testing"
  8. )
  9. var ipMiddlewareTestDefinitionEnabledFail string = `
  10. {
  11. "name": "Tyk Test API - IPCONF Fail",
  12. "api_id": "1",
  13. "org_id": "default",
  14. "definition": {
  15. "location": "header",
  16. "key": "version"
  17. },
  18. "auth": {
  19. "auth_header_name": "authorization"
  20. },
  21. "version_data": {
  22. "not_versioned": true,
  23. "versions": {
  24. "v1": {
  25. "name": "v1",
  26. "expires": "2100-01-02 15:04",
  27. "use_extended_paths": true,
  28. "paths": {
  29. "ignored": [],
  30. "white_list": [],
  31. "black_list": []
  32. }
  33. }
  34. }
  35. },
  36. "proxy": {
  37. "listen_path": "/v1",
  38. "target_url": "http://lonelycode.com",
  39. "strip_listen_path": false
  40. },
  41. "enable_ip_whitelisting": true,
  42. "allowed_ips": ["12.12.12.12"]
  43. }
  44. `
  45. var ipMiddlewareTestDefinitionEnabledPass string = `
  46. {
  47. "name": "Tyk Test API",
  48. "api_id": "1",
  49. "org_id": "default",
  50. "definition": {
  51. "location": "header",
  52. "key": "version"
  53. },
  54. "auth": {
  55. "auth_header_name": "authorization"
  56. },
  57. "version_data": {
  58. "not_versioned": true,
  59. "versions": {
  60. "v1": {
  61. "name": "v1",
  62. "expires": "2100-01-02 15:04",
  63. "use_extended_paths": true,
  64. "paths": {
  65. "ignored": [],
  66. "white_list": [],
  67. "black_list": []
  68. }
  69. }
  70. }
  71. },
  72. "proxy": {
  73. "listen_path": "/v1",
  74. "target_url": "http://lonelycode.com",
  75. "strip_listen_path": false
  76. },
  77. "enable_ip_whitelisting": true,
  78. "allowed_ips": ["127.0.0.1", "127.0.0.1/24"]
  79. }
  80. `
  81. var ipMiddlewareTestDefinitionDisabled string = `
  82. {
  83. "name": "Tyk Test API",
  84. "api_id": "1",
  85. "org_id": "default",
  86. "definition": {
  87. "location": "header",
  88. "key": "version"
  89. },
  90. "auth": {
  91. "auth_header_name": "authorization"
  92. },
  93. "version_data": {
  94. "not_versioned": true,
  95. "versions": {
  96. "v1": {
  97. "name": "v1",
  98. "expires": "2100-01-02 15:04",
  99. "use_extended_paths": true,
  100. "paths": {
  101. "ignored": [],
  102. "white_list": [],
  103. "black_list": []
  104. }
  105. }
  106. }
  107. },
  108. "proxy": {
  109. "listen_path": "/v1",
  110. "target_url": "http://lonelycode.com",
  111. "strip_listen_path": false
  112. },
  113. "enable_ip_whitelisting": false,
  114. "allowed_ips": []
  115. }
  116. `
  117. var ipMiddlewareTestDefinitionMissing string = `
  118. {
  119. "name": "Tyk Test API",
  120. "api_id": "1",
  121. "org_id": "default",
  122. "definition": {
  123. "location": "header",
  124. "key": "version"
  125. },
  126. "auth": {
  127. "auth_header_name": "authorization"
  128. },
  129. "version_data": {
  130. "not_versioned": true,
  131. "versions": {
  132. "v1": {
  133. "name": "v1",
  134. "expires": "2100-01-02 15:04",
  135. "use_extended_paths": true,
  136. "paths": {
  137. "ignored": [],
  138. "white_list": [],
  139. "black_list": []
  140. }
  141. }
  142. }
  143. },
  144. "proxy": {
  145. "listen_path": "/v1",
  146. "target_url": "http://lonelycode.com",
  147. "strip_listen_path": false
  148. }
  149. }
  150. `
  151. func MakeIPSampleAPI(apiTestDef string) *APISpec {
  152. log.Debug("CREATING TEMPORARY API FOR IP WHITELIST")
  153. thisSpec := createDefinitionFromString(apiTestDef)
  154. redisStore := RedisStorageManager{KeyPrefix: "apikey-"}
  155. healthStore := &RedisStorageManager{KeyPrefix: "apihealth."}
  156. orgStore := &RedisStorageManager{KeyPrefix: "orgKey."}
  157. thisSpec.Init(&redisStore, &redisStore, healthStore, orgStore)
  158. specs := &[]*APISpec{&thisSpec}
  159. newMuxes := mux.NewRouter()
  160. loadAPIEndpoints(newMuxes)
  161. loadApps(specs, newMuxes)
  162. newHttpMuxer := http.NewServeMux()
  163. newHttpMuxer.Handle("/", newMuxes)
  164. http.DefaultServeMux = newHttpMuxer
  165. log.Debug("IP TEST Reload complete")
  166. return &thisSpec
  167. }
  168. func TestIpMiddlewareIPFail(t *testing.T) {
  169. spec := MakeIPSampleAPI(ipMiddlewareTestDefinitionEnabledFail)
  170. redisStore := RedisStorageManager{KeyPrefix: "apikey-"}
  171. healthStore := &RedisStorageManager{KeyPrefix: "apihealth."}
  172. orgStore := &RedisStorageManager{KeyPrefix: "orgKey."}
  173. spec.Init(&redisStore, &redisStore, healthStore, orgStore)
  174. thisSession := createNonThrottledSession()
  175. spec.SessionManager.UpdateSession("1234wer", thisSession, 60)
  176. uri := "/about-lonelycoder/"
  177. method := "GET"
  178. recorder := httptest.NewRecorder()
  179. param := make(url.Values)
  180. req, err := http.NewRequest(method, uri+param.Encode(), nil)
  181. req.RemoteAddr = "127.0.0.1"
  182. req.Header.Add("authorization", "1234wer")
  183. if err != nil {
  184. t.Fatal(err)
  185. }
  186. chain := getChain(*spec)
  187. chain.ServeHTTP(recorder, req)
  188. if recorder.Code != 403 {
  189. t.Error("Invalid response code, should be 403: \n", recorder.Code, recorder.Body)
  190. }
  191. }
  192. func TestIpMiddlewareIPPass(t *testing.T) {
  193. spec := MakeIPSampleAPI(ipMiddlewareTestDefinitionEnabledPass)
  194. redisStore := RedisStorageManager{KeyPrefix: "apikey-"}
  195. healthStore := &RedisStorageManager{KeyPrefix: "apihealth."}
  196. orgStore := &RedisStorageManager{KeyPrefix: "orgKey."}
  197. spec.Init(&redisStore, &redisStore, healthStore, orgStore)
  198. thisSession := createNonThrottledSession()
  199. spec.SessionManager.UpdateSession("gfgg1234", thisSession, 60)
  200. uri := "/about-lonelycoder/"
  201. method := "GET"
  202. recorder := httptest.NewRecorder()
  203. param := make(url.Values)
  204. req, err := http.NewRequest(method, uri+param.Encode(), nil)
  205. req.RemoteAddr = "127.0.0.1"
  206. req.Header.Add("authorization", "gfgg1234")
  207. if err != nil {
  208. t.Fatal(err)
  209. }
  210. chain := getChain(*spec)
  211. chain.ServeHTTP(recorder, req)
  212. if recorder.Code != 200 {
  213. t.Error("Invalid response code, should be 200: \n", recorder.Code, recorder.Body, req.RemoteAddr)
  214. }
  215. }
  216. func TestIpMiddlewareIPPassCIDR(t *testing.T) {
  217. spec := MakeIPSampleAPI(ipMiddlewareTestDefinitionEnabledPass)
  218. redisStore := RedisStorageManager{KeyPrefix: "apikey-"}
  219. healthStore := &RedisStorageManager{KeyPrefix: "apihealth."}
  220. orgStore := &RedisStorageManager{KeyPrefix: "orgKey."}
  221. spec.Init(&redisStore, &redisStore, healthStore, orgStore)
  222. thisSession := createNonThrottledSession()
  223. spec.SessionManager.UpdateSession("gfgg1234", thisSession, 60)
  224. uri := "/about-lonelycoder/"
  225. method := "GET"
  226. recorder := httptest.NewRecorder()
  227. param := make(url.Values)
  228. req, err := http.NewRequest(method, uri+param.Encode(), nil)
  229. req.RemoteAddr = "127.0.0.2"
  230. req.Header.Add("authorization", "gfgg1234")
  231. if err != nil {
  232. t.Fatal(err)
  233. }
  234. chain := getChain(*spec)
  235. chain.ServeHTTP(recorder, req)
  236. if recorder.Code != 200 {
  237. t.Error("Invalid response code, should be 200: \n", recorder.Code, recorder.Body, req.RemoteAddr)
  238. }
  239. }
  240. func TestIPMiddlewareIPFailXForwardedFor(t *testing.T) {
  241. spec := MakeIPSampleAPI(ipMiddlewareTestDefinitionEnabledPass)
  242. redisStore := RedisStorageManager{KeyPrefix: "apikey-"}
  243. healthStore := &RedisStorageManager{KeyPrefix: "apihealth."}
  244. orgStore := &RedisStorageManager{KeyPrefix: "orgKey."}
  245. spec.Init(&redisStore, &redisStore, healthStore, orgStore)
  246. thisSession := createNonThrottledSession()
  247. spec.SessionManager.UpdateSession("gfgg1234", thisSession, 60)
  248. uri := "/about-lonelycoder/"
  249. method := "GET"
  250. recorder := httptest.NewRecorder()
  251. param := make(url.Values)
  252. req, err := http.NewRequest(method, uri+param.Encode(), nil)
  253. req.RemoteAddr = "10.0.0.1"
  254. req.Header.Add("authorization", "gfgg1234")
  255. if err != nil {
  256. t.Fatal(err)
  257. }
  258. chain := getChain(*spec)
  259. chain.ServeHTTP(recorder, req)
  260. if recorder.Code != 403 {
  261. t.Error("Invalid response code, should be 403: \n", recorder.Code, recorder.Body, req.RemoteAddr)
  262. }
  263. }
  264. func TestIPMiddlewareIPPassXForwardedFor(t *testing.T) {
  265. spec := MakeIPSampleAPI(ipMiddlewareTestDefinitionEnabledPass)
  266. redisStore := RedisStorageManager{KeyPrefix: "apikey-"}
  267. healthStore := &RedisStorageManager{KeyPrefix: "apihealth."}
  268. orgStore := &RedisStorageManager{KeyPrefix: "orgKey."}
  269. spec.Init(&redisStore, &redisStore, healthStore, orgStore)
  270. thisSession := createNonThrottledSession()
  271. spec.SessionManager.UpdateSession("gfgg1234", thisSession, 60)
  272. uri := "/about-lonelycoder/"
  273. method := "GET"
  274. recorder := httptest.NewRecorder()
  275. param := make(url.Values)
  276. req, err := http.NewRequest(method, uri+param.Encode(), nil)
  277. req.RemoteAddr = "10.0.0.1"
  278. req.Header.Add("X-Forwarded-For", "127.0.0.1")
  279. req.Header.Add("authorization", "gfgg1234")
  280. if err != nil {
  281. t.Fatal(err)
  282. }
  283. chain := getChain(*spec)
  284. chain.ServeHTTP(recorder, req)
  285. if recorder.Code != 200 {
  286. t.Error("Invalid response code, should be 200: \n", recorder.Code, recorder.Body, req.RemoteAddr)
  287. }
  288. }
  289. func TestIpMiddlewareIPMissing(t *testing.T) {
  290. spec := MakeIPSampleAPI(ipMiddlewareTestDefinitionMissing)
  291. redisStore := RedisStorageManager{KeyPrefix: "apikey-"}
  292. healthStore := &RedisStorageManager{KeyPrefix: "apihealth."}
  293. orgStore := &RedisStorageManager{KeyPrefix: "orgKey."}
  294. spec.Init(&redisStore, &redisStore, healthStore, orgStore)
  295. thisSession := createNonThrottledSession()
  296. spec.SessionManager.UpdateSession("1234rtyrty", thisSession, 60)
  297. uri := "/about-lonelycoder/"
  298. method := "GET"
  299. recorder := httptest.NewRecorder()
  300. param := make(url.Values)
  301. req, err := http.NewRequest(method, uri+param.Encode(), nil)
  302. req.Header.Add("authorization", "1234rtyrty")
  303. if err != nil {
  304. t.Fatal(err)
  305. }
  306. chain := getChain(*spec)
  307. chain.ServeHTTP(recorder, req)
  308. if recorder.Code != 200 {
  309. t.Error("Invalid response code, should be 200: \n", recorder.Code, recorder.Body)
  310. }
  311. }
  312. func TestIpMiddlewareIPDisabled(t *testing.T) {
  313. spec := MakeIPSampleAPI(ipMiddlewareTestDefinitionDisabled)
  314. redisStore := RedisStorageManager{KeyPrefix: "apikey-"}
  315. healthStore := &RedisStorageManager{KeyPrefix: "apihealth."}
  316. orgStore := &RedisStorageManager{KeyPrefix: "orgKey."}
  317. spec.Init(&redisStore, &redisStore, healthStore, orgStore)
  318. thisSession := createNonThrottledSession()
  319. spec.SessionManager.UpdateSession("1234iuouio", thisSession, 60)
  320. uri := "/about-lonelycoder/"
  321. method := "GET"
  322. recorder := httptest.NewRecorder()
  323. param := make(url.Values)
  324. req, err := http.NewRequest(method, uri+param.Encode(), nil)
  325. req.Header.Add("authorization", "1234iuouio")
  326. if err != nil {
  327. t.Fatal(err)
  328. }
  329. chain := getChain(*spec)
  330. chain.ServeHTTP(recorder, req)
  331. if recorder.Code != 200 {
  332. t.Error("Invalid response code, should be 200: \n", recorder.Code, recorder.Body)
  333. }
  334. }