Skip to content

Commit 487881d

Browse files
committed
perf: avoid building entire policy chains when parsing response from portal
1 parent 947c9fb commit 487881d

1 file changed

Lines changed: 25 additions & 17 deletions

File tree

gateway/src/apicast/configuration_loader/remote_v2.lua

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -103,33 +103,37 @@ local function service_config_endpoint(portal_endpoint, service_id, env, version
103103
)
104104
end
105105

106+
-- Helper function to get OIDC issuer endpoint without parsing the service
107+
local function get_oidc_issuer_endpoint(proxy_content)
108+
return proxy_content.proxy and proxy_content.proxy.oidc_issuer_endpoint
109+
end
110+
106111
local function parse_proxy_configs(self, proxy_configs)
107112
local config = { services = array(), oidc = array() }
108113

109114
for i, proxy_conf in ipairs(proxy_configs) do
110115
local proxy_config = proxy_conf.proxy_config
116+
local content = proxy_config.content
117+
config.services[i] = content
111118

112-
-- Copy the config because parse_service have side-effects. It adds
113-
-- liquid templates in some policies and those cannot be encoded into a
114-
-- JSON. We should get rid of these side effects.
115-
local original_proxy_config = deepcopy(proxy_config)
119+
-- Get OIDC issuer endpoint directly without parsing the service
116120

117-
local service = configuration.parse_service(proxy_config.content)
121+
local issuer_endpoint = get_oidc_issuer_endpoint(content)
122+
local oidc
123+
if issuer_endpoint then
124+
oidc = self.oidc:call(issuer_endpoint, self.ttl)
125+
end
118126

119-
-- We always assign a oidc to the service, even an empty one with the
120-
-- service_id, if not on APICAST_SERVICES_LIST will fail on filtering
121-
local oidc = self:oidc_issuer_configuration(service)
122127
if not oidc then
123128
oidc = {}
124129
end
125130

126131
-- deepcopy because this can be cached, and we want to have a deepcopy to
127132
-- avoid issues with service_id
128133
local oidc_copy = deepcopy(oidc)
129-
oidc_copy.service_id = service.id
134+
oidc_copy.service_id = tostring(content.id)
130135

131136
config.oidc[i] = oidc_copy
132-
config.services[i] = original_proxy_config.content
133137
end
134138
return cjson.encode(config)
135139
end
@@ -480,20 +484,24 @@ function _M:config(service, environment, version, service_regexp_filter)
480484

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

489489
local config_service = configuration.parse_service(proxy_config.content)
490490
if service_regexp_filter and not config_service:match_host(service_regexp_filter) then
491491
return nil, "Service filtered out because APICAST_SERVICES_FILTER_BY_URL"
492492
end
493493

494-
original_proxy_config.oidc = self:oidc_issuer_configuration(config_service)
494+
-- Get OIDC issuer configuration directly without parsing the service
495+
local issuer_endpoint = get_oidc_issuer_endpoint(content)
496+
local oidc
497+
498+
if issuer_endpoint then
499+
oidc = self.oidc:call(issuer_endpoint, self.ttl)
500+
end
501+
502+
proxy_config.oidc = oidc
495503

496-
return original_proxy_config
504+
return proxy_config
497505
else
498506
return nil, status_code_error(res)
499507
end

0 commit comments

Comments
 (0)