Skip to content

Commit ef8dd2d

Browse files
committed
perf: avoid construct the full policy chain when parsing response from portal
1 parent f5a1002 commit ef8dd2d

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

gateway/src/apicast/configuration_loader/remote_v2.lua

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -102,33 +102,34 @@ local function service_config_endpoint(portal_endpoint, service_id, env, version
102102
)
103103
end
104104

105+
local function get_oidc_issuer_endpoint(proxy_content)
106+
return proxy_content.proxy and proxy_content.proxy.oidc_issuer_endpoint
107+
end
108+
105109
local function parse_proxy_configs(self, proxy_configs)
106110
local config = { services = array(), oidc = array() }
107111

108112
for i, proxy_conf in ipairs(proxy_configs) do
109113
local proxy_config = proxy_conf.proxy_config
114+
local content = proxy_config.content
110115

111-
-- Copy the config because parse_service have side-effects. It adds
112-
-- liquid templates in some policies and those cannot be encoded into a
113-
-- JSON. We should get rid of these side effects.
114-
local original_proxy_config = deepcopy(proxy_config)
116+
config.services[i] = content
115117

116-
local service = configuration.parse_service(proxy_config.content)
117-
118-
-- We always assign a oidc to the service, even an empty one with the
119-
-- service_id, if not on APICAST_SERVICES_LIST will fail on filtering
120-
local oidc = self:oidc_issuer_configuration(service)
118+
local issuer_endpoint = get_oidc_issuer_endpoint(content)
119+
local oidc
120+
if issuer_endpoint then
121+
oidc = self.oidc:call(issuer_endpoint, self.ttl)
122+
end
121123
if not oidc then
122124
oidc = {}
123125
end
124126

125127
-- deepcopy because this can be cached, and we want to have a deepcopy to
126128
-- avoid issues with service_id
127129
local oidc_copy = deepcopy(oidc)
128-
oidc_copy.service_id = service.id
130+
oidc_copy.service_id = tostring(content.id)
129131

130132
config.oidc[i] = oidc_copy
131-
config.services[i] = original_proxy_config.content
132133
end
133134
return cjson.encode(config)
134135
end
@@ -482,20 +483,22 @@ function _M:config(service, environment, version, service_regexp_filter)
482483

483484
if res.status == 200 then
484485
local proxy_config = cjson.decode(res.body).proxy_config
485-
486-
-- Copy the config because parse_service have side-effects. It adds
487-
-- liquid templates in some policies and those cannot be encoded into a
488-
-- JSON. We should get rid of these side effects.
489-
local original_proxy_config = deepcopy(proxy_config)
486+
local content = proxy_config.content
490487

491488
local config_service = configuration.parse_service(proxy_config.content)
492489
if service_regexp_filter and not config_service:match_host(service_regexp_filter) then
493490
return nil, "Service filtered out because APICAST_SERVICES_FILTER_BY_URL"
494491
end
495492

496-
original_proxy_config.oidc = self:oidc_issuer_configuration(config_service)
493+
local issuer_endpoint = get_oidc_issuer_endpoint(content)
494+
local oidc
495+
496+
if issuer_endpoint then
497+
oidc = self.oidc:call(issuer_endpoint, self.ttl)
498+
end
497499

498-
return original_proxy_config
500+
proxy_config.oidc = oidc
501+
return proxy_config
499502
else
500503
return nil, status_code_error(res)
501504
end

0 commit comments

Comments
 (0)