Skip to content

Commit 7edeb7d

Browse files
samugieguzki
authored andcommitted
fixed service version configuration via environment variable
1 parent 06678b0 commit 7edeb7d

File tree

4 files changed

+104
-13
lines changed

4 files changed

+104
-13
lines changed

CHANGELOG.md

+12
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
6464
- Fixed issues with OIDC filters [PR #1304](https://github.com/3scale/APIcast/pull/1304) [PR #1306](https://github.com/3scale/APIcast/pull/1306) [THREESCALE-6042](https://issues.redhat.com/browse/THREESCALE-6042)
6565
- Fixed issues with OIDC filters [PR #1304](https://github.com/3scale/APIcast/pull/1304) [THREESCALE-6042](https://issues.redhat.com/browse/THREESCALE-6042)
6666
- Fixed issues with Upstream MTLS certs [PR #1307](https://github.com/3scale/APIcast/pull/1307) [THREESCALE-7508](https://issues.redhat.com/browse/THREESCALE-7508)
67+
- Fixed warning messages [PR #1318](https://github.com/3scale/APIcast/pull/1318) [THREESCALE-7906](https://issues.redhat.com/browse/THREESCALE-7906)
68+
- Fixed dirty context [PR #1328](https://github.com/3scale/APIcast/pull/1328) [THREESCALE-8000](https://issues.redhat.com/browse/THREESCALE-8000) [THREESCALE-8007](https://issues.redhat.com/browse/THREESCALE-8007)
69+
- Fixed jwk alg confusion [PR #1329](https://github.com/3scale/APIcast/pull/1329) [THREESCALE-8249](https://issues.redhat.com/browse/THREESCALE-8249)
70+
- Fixed issue with resolving target server hostnames to IP when using CONNECT method [PR #1323](https://github.com/3scale/APIcast/pull/1323) [THREESCALE-7967](https://issues.redhat.com/browse/THREESCALE-7967)
71+
- Fixed issue with resolving target server hostnames to IPs when forwarding requests through http/s proxy [PR #1323](https://github.com/3scale/APIcast/pull/1323) [THREESCALE-7967](https://issues.redhat.com/browse/THREESCALE-7967)
72+
- Fixed dirty context [PR #1328](https://github.com/3scale/APIcast/pull/1328) [THREESCALE-8000](https://issues.redhat.com/browse/THREESCALE-8000) [THREESCALE-8007](https://issues.redhat.com/browse/THREESCALE-8007) [THREESCALE-8252](https://issues.redhat.com/browse/THREESCALE-8252)
73+
- Fixed dirty context (part 2 of PR #1328) when tls termination policy is in the policy chain [PR #1333](https://github.com/3scale/APIcast/pull/1333)
74+
- Fixed NGINX filters policy error [PR #1339](https://github.com/3scale/APIcast/pull/1339) [THREESCALE-7349](https://issues.redhat.com/browse/THREESCALE-7349)
75+
- Fix to avoid uninitialized variables when request URI is too large [PR #1340](https://github.com/3scale/APIcast/pull/1340) [THREESCALE-7906](https://issues.redhat.com/browse/THREESCALE-7906)
76+
- Fixed issue where request path is stripped for proxied https requests [PR #1342](https://github.com/3scale/APIcast/pull/1342) [THREESCALE-8426](https://issues.redhat.com/browse/THREESCALE-8426)
77+
- Bumped liquid-lua to version 0.2.0-2 [PR #1369](https://github.com/3scale/APIcast/pull/1369) - includes: [THREESCALE-8483](https://issues.redhat.com/browse/THREESCALE-8483) and [THREESCALE-8484](https://issues.redhat.com/browse/THREESCALE-8484)
78+
- New /admin/api/account/proxy_configs endpoint for configuration loading [PR #1352](https://github.com/3scale/APIcast/pull/1352) [THREESCALE-8508](https://issues.redhat.com/browse/THREESCALE-8508)
6779

6880
### Added
6981

gateway/src/apicast/configuration_loader/remote_v2.lua

+27-13
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,19 @@ local function parse_resp_body(self, resp_body)
137137
return cjson.encode(config)
138138
end
139139

140+
local function use_service_version(portal_endpoint_has_path)
141+
local vars = resty_env.list()
142+
for n, v in pairs(vars) do
143+
if match(n, "APICAST_SERVICE_\\d+_CONFIGURATION_VERSION") and v and v ~= ''
144+
and portal_endpoint_has_path then
145+
ngx.log(ngx.INFO, "APICAST_SERVICE_${ID}_CONFIGURATION_VERSION is configured: ",
146+
"services will be loaded individually")
147+
return true
148+
end
149+
end
150+
return false
151+
end
152+
140153
-- When the APICAST_LOAD_SERVICES_WHEN_NEEDED is enabled, but the config loader
141154
-- is boot, APICAST_LOAD_SERVICES_WHEN_NEEDED is going to be ignored. But in
142155
-- that case, env refers to a host and we need to reset it to pick the env
@@ -150,12 +163,8 @@ end
150163
-- http://${THREESCALE_PORTAL_ENDPOINT}/<env>.json?host=host
151164
-- http://${THREESCALE_PORTAL_ENDPOINT}/admin/api/account/proxy_configs/<env>.json?host=host&version=version
152165
local function configuration_endpoint_params(env, host, portal_endpoint_path)
153-
local version = resty_env.get(
154-
format('APICAST_SERVICE_%s_CONFIGURATION_VERSION', service_id)
155-
) or "latest"
156-
157166
return portal_endpoint_path and {path = env, args = {host = host}}
158-
or {path = '/admin/api/account/proxy_configs/' .. env, args = {host = host, version = version} }
167+
or {path = '/admin/api/account/proxy_configs/' .. env, args = {host = host, version = "latest"} }
159168
end
160169

161170
function _M:index(host)
@@ -172,6 +181,13 @@ function _M:index(host)
172181
return nil, 'missing environment'
173182
end
174183

184+
-- Exit from index if a service version is configured because
185+
-- in that case we have to retrieve the services individually
186+
-- through the "old" services endpoint.
187+
if use_service_version(not self.path and true) then
188+
return nil, 'wrong endpoint url'
189+
end
190+
175191
local endpoint_params = configuration_endpoint_params(env, host, self.path)
176192
local base_url = resty_url.join(self.endpoint, endpoint_params.path .. '.json')
177193
local query_args = encode_args(endpoint_params.args) ~= '' and '?'..encode_args(endpoint_params.args)
@@ -211,16 +227,14 @@ function _M:call(environment)
211227
return ret, err
212228
end
213229

214-
if load_just_for_host then
215-
return m:call(host)
216-
else
217-
return m:call()
218-
end
230+
return m:call()
231+
219232
end
220233

221-
-- Everything past this point in `call()` is deprecated because
222-
-- now the configuration load is handled completely by `index()`.
223-
-- Service filtering is done in configuration.lua
234+
-- Unless APICAST_SERVICE_${ID}_CONFIGURATION_VERSION is set,
235+
-- everything past this point in `call()` is skipped because
236+
-- otherwise the configuration load is handled completely by
237+
-- `index()`. Service filtering is done in configuration.lua
224238
local http_client = self.http_client
225239

226240
if not http_client then

spec/configuration_loader/remote_v2_spec.lua

+17
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,23 @@ UwIDAQAB
455455

456456
describe(':index', function()
457457

458+
it('invalid status is handled', function()
459+
loader = _M.new('http://example.com/', { client = test_backend })
460+
env.set('THREESCALE_DEPLOYMENT_ENV', 'production')
461+
env.set('APICAST_CONFIGURATION_LOADER', 'lazy')
462+
test_backend.expect{ url = 'http://example.com/admin/api/account/proxy_configs/production.json?version=latest' }.
463+
respond_with{ status = 512, body = nil}
464+
assert.same({ nil, 'invalid status' }, { loader:index() })
465+
end)
466+
467+
it('returns "wrong endpoint url" when service version is configured', function()
468+
loader = _M.new('http://example.com/', { client = test_backend })
469+
env.set('THREESCALE_DEPLOYMENT_ENV', 'production')
470+
env.set('APICAST_SERVICE_2_CONFIGURATION_VERSION', 42)
471+
env.set('APICAST_CONFIGURATION_LOADER', 'lazy')
472+
assert.same({ nil, 'wrong endpoint url' }, { loader:index() })
473+
end)
474+
458475
it('returns configuration for all services (no path on endpoint)', function()
459476
loader = _M.new('http://example.com/', { client = test_backend })
460477
env.set('THREESCALE_DEPLOYMENT_ENV', 'production')

t/configuration-loading-lazy.t

+48
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,51 @@ using lazy configuration loader
155155
OIDC url is not valid, uri:
156156
--- no_error_log
157157
[error]
158+
159+
=== TEST 6: load configuration with APICAST_SERVICE_${ID}_CONFIGURATION_VERSION
160+
--- env eval
161+
(
162+
'APICAST_CONFIGURATION_LOADER' => 'lazy',
163+
'THREESCALE_PORTAL_ENDPOINT' => "http://test:$ENV{TEST_NGINX_SERVER_PORT}",
164+
'APICAST_SERVICE_2_CONFIGURATION_VERSION' => 42
165+
)
166+
--- upstream env
167+
location = /admin/api/services.json {
168+
echo '{ "services": [ { "service": { "id": 2 } } ] }';
169+
}
170+
location = /admin/api/services/2/proxy/configs/production/42.json {
171+
echo '
172+
{
173+
"proxy_config": {
174+
"content": {
175+
"id": 2,
176+
"backend_version": 1,
177+
"proxy": {
178+
"hosts": [ "localhost" ],
179+
"api_backend": "http://test:$TEST_NGINX_SERVER_PORT/",
180+
"proxy_rules": [
181+
{ "pattern": "/t", "http_method": "GET", "metric_system_name": "test","delta": 1 }
182+
]
183+
}
184+
}
185+
}
186+
}';
187+
}
188+
189+
location /t {
190+
echo "all ok";
191+
}
192+
193+
--- backend
194+
location /transactions/authrep.xml {
195+
content_by_lua_block {
196+
ngx.exit(200)
197+
}
198+
}
199+
--- request
200+
GET /t?user_key=fake
201+
--- error_code: 200
202+
--- error_log
203+
using lazy configuration loader
204+
--- no_error_log
205+
[error]

0 commit comments

Comments
 (0)