Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: confusion when substituting ENV in config file #11545

Merged
merged 4 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apisix/cli/ops.lua
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ Please modify "admin_key" in conf/config.yaml .

for name, value in pairs(exported_vars) do
if value then
table_insert(sys_conf["envs"], name .. "=" .. value)
table_insert(sys_conf["envs"], name)
end
end
end
Expand Down
111 changes: 111 additions & 0 deletions t/cli/cli_envsubst_confusion.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

use t::APISIX 'no_plan';

repeat_each(1);

$ENV{SOME_STRING_VALUE_BUT_DIFFERENT} = 'astringvaluebutdifferent';
$ENV{SOME_STRING_VALUE} = 'astringvalue';

our $yaml_config = <<_EOC_;
apisix:
node_listen: 1984
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
_EOC_

our $apisix_yaml = <<_EOC_;
upstreams:
- id: 1
nodes:
- host: 127.0.0.1
port: 1980
weight: 1
routes:
- uri: /hello
upstream_id: 1
plugins:
response-rewrite:
headers:
set:
X-Some-String-Value-But-Different: Different \${{SOME_STRING_VALUE_BUT_DIFFERENT}}
X-Some-String-Value: \${{SOME_STRING_VALUE}}
#END
_EOC_

our $response_headers_correct = <<_EOC_;
X-Some-String-Value-But-Different: Different astringvaluebutdifferent
X-Some-String-Value: astringvalue
_EOC_

our $response_headers_INCORRECT = <<_EOC_;
X-Some-String-Value-But-Different: Different astringvalue
X-Some-String-Value: astringvalue
_EOC_

add_block_preprocessor(sub {
my ($block) = @_;

if (!$block->request) {
$block->set_value("request", "GET /hello");
}
});

run_tests();

__DATA__

=== TEST 1: assignment style, the PREFIX 1st - incorrect
--- main_config
env SOME_STRING_VALUE=astringvalue;
env SOME_STRING_VALUE_BUT_DIFFERENT=astringvaluebutdifferent;
--- yaml_config eval: $::yaml_config
--- apisix_yaml eval: $::apisix_yaml
--- response_headers eval: $::response_headers_INCORRECT



=== TEST 2: assignment style, the DIFF 1st - correct
--- main_config
env SOME_STRING_VALUE_BUT_DIFFERENT=astringvaluebutdifferent;
env SOME_STRING_VALUE=astringvalue;
--- yaml_config eval: $::yaml_config
--- apisix_yaml eval: $::apisix_yaml
--- response_headers eval: $::response_headers_correct



=== TEST 3: declaration style, the PREFIX 1st - correct
--- main_config
env SOME_STRING_VALUE;
env SOME_STRING_VALUE_BUT_DIFFERENT;
--- yaml_config eval: $::yaml_config
--- apisix_yaml eval: $::apisix_yaml
--- response_headers eval: $::response_headers_correct



=== TEST 4: declaration style, the DIFF 1st - also correct
--- main_config
env SOME_STRING_VALUE_BUT_DIFFERENT;
env SOME_STRING_VALUE;
--- yaml_config eval: $::yaml_config
--- apisix_yaml eval: $::apisix_yaml
--- response_headers eval: $::response_headers_correct
12 changes: 6 additions & 6 deletions t/cli/test_main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ deployment:

ETCD_HOST=127.0.0.1 ETCD_PORT=2379 make init

if ! grep "env ETCD_HOST=127.0.0.1;" conf/nginx.conf > /dev/null; then
if ! grep "env ETCD_HOST;" conf/nginx.conf > /dev/null; then
echo "failed: support environment variables in local_conf"
exit 1
fi
Expand All @@ -369,7 +369,7 @@ nginx_config:

ETCD_HOST=127.0.0.1 ETCD_PORT=2379 make init

if grep "env ETCD_HOST=127.0.0.1;" conf/nginx.conf > /dev/null; then
if grep "env ETCD_HOST=.*;" conf/nginx.conf > /dev/null; then
echo "failed: support environment variables in local_conf"
exit 1
fi
Expand All @@ -394,7 +394,7 @@ nginx_config:

ETCD_HOST=127.0.0.1 ETCD_PORT=2379 make init

if grep "env ETCD_HOST=127.0.0.1;" conf/nginx.conf > /dev/null; then
if grep "env ETCD_HOST;" conf/nginx.conf > /dev/null; then
echo "failed: support environment variables in local_conf"
exit 1
fi
Expand All @@ -414,7 +414,7 @@ tests:

make init

if ! grep "env TEST_ENV=1.1.1.1;" conf/nginx.conf > /dev/null; then
if ! grep "env TEST_ENV;" conf/nginx.conf > /dev/null; then
echo "failed: should use default value when environment not set"
exit 1
fi
Expand All @@ -426,7 +426,7 @@ tests:

make init

if ! grep "env TEST_ENV=very-long-domain-with-many-symbols.absolutely-non-exists-123ss.com:1234/path?param1=value1;" conf/nginx.conf > /dev/null; then
if ! grep "env TEST_ENV;" conf/nginx.conf > /dev/null; then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could lead to a breaking change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because when rendering nginx.conf, apisix cli decides to insert the relevant env directives based on whether they exist or are valid. The declarative method is sufficient. The final reading still needs to use os.getenv, env FOO=bar; this method is not necessary and can be replaced by the declarative method to ensure compatibility.

echo "failed: should use default value when environment not set"
exit 1
fi
Expand All @@ -438,7 +438,7 @@ tests:

TEST_ENV=127.0.0.1 make init

if ! grep "env TEST_ENV=127.0.0.1;" conf/nginx.conf > /dev/null; then
if ! grep "env TEST_ENV;" conf/nginx.conf > /dev/null; then
echo "failed: should use environment variable when environment is set"
exit 1
fi
Expand Down
2 changes: 1 addition & 1 deletion t/cli/test_standalone.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ routes:
# check for resolve variables
var_test_path=/test make init

if ! grep "env var_test_path=/test;" conf/nginx.conf > /dev/null; then
if ! grep "env var_test_path;" conf/nginx.conf > /dev/null; then
echo "failed: failed to resolve variables"
exit 1
fi
Expand Down
Loading