Last active
August 29, 2015 14:04
-
-
Save vdel26/1e12be083a594a45b0a5 to your computer and use it in GitHub Desktop.
Use prebuilt cache in case 3scale cannot be reached
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local whitelist = require "whitelist" | |
| function authrep(params, service) | |
| ngx.var.cached_key = ngx.var.cached_key .. ":" .. ngx.var.usage | |
| local api_keys = ngx.shared.api_keys | |
| local is_known = api_keys:get(ngx.var.cached_key) | |
| if is_known ~= 200 then | |
| local res = ngx.location.capture("/threescale_authrep", { share_all_vars = true }) | |
| if res.status == 504 then | |
| -- 3scale could not be reached. Fall back to fixed cache | |
| log("DEBUG: TIMEOUT WHILE TRYING TO REACH 3SCALE") | |
| if whitelist[params.app_id] then | |
| log("DEBUG: application in cache") | |
| ngx.status = 200 | |
| return | |
| else | |
| log("DEBUG: application not in cache") | |
| error_authorization_failed(service) | |
| end | |
| end | |
| if res.status ~= 200 then | |
| -- remove the key, if it's not 200 let's go the slow route, to 3scale's backend | |
| api_keys:delete(ngx.var.cached_key) | |
| ngx.status = res.status | |
| ngx.header.content_type = "application/json" | |
| error_authorization_failed(service) | |
| else | |
| api_keys:set(ngx.var.cached_key,200) | |
| end | |
| ngx.var.cached_key = nil | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Set (list) | |
| local set = {} | |
| for _, l in ipairs(list) do set[l] = true end | |
| return set | |
| end | |
| ------------------------------------------------------- | |
| -- Whitelist with the applications identified by app_id | |
| -- that should be allowed to go through in case 3scale | |
| -- is not reachable | |
| ------------------------------------------------------- | |
| local whitelist = Set{ | |
| 'ababab', | |
| 'neinnein', | |
| 'test', | |
| 'e14b4b8d' | |
| } | |
| ------------------------------------------------------- | |
| return whitelist |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment