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

datasource: add environment variable support #18498

Merged
merged 17 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from 13 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
5 changes: 4 additions & 1 deletion api/envoy/config/core/v3/base.proto
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ message WatchedDirectory {
string path = 1 [(validate.rules).string = {min_len: 1}];
}

// Data source consisting of either a file or an inline value.
// Data source consisting of a file, an inline value, or an environment variable.
message DataSource {
option (udpa.annotations.versioning).previous_message_type = "envoy.api.v2.core.DataSource";

Expand All @@ -387,6 +387,9 @@ message DataSource {

// String inlined in the configuration.
string inline_string = 3;

// Environment variable data source.
string environment_variable = 4 [(validate.rules).string = {min_len: 1}];
}
}

Expand Down
1 change: 1 addition & 0 deletions docs/root/version_history/current.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Removed Config or Runtime
New Features
------------
* api: added support for *xds.type.v3.TypedStruct* in addition to the now-deprecated *udpa.type.v1.TypedStruct* proto message, which is a wrapper proto used to encode typed JSON data in a *google.protobuf.Any* field.
* config: added :ref:`environment_variable <envoy_v3_api_field_config.core.v3.datasource.environment_variable>` to the :ref:`DataSource <envoy_v3_api_msg_config.core.v3.datasource>`.
* ext_authz: added :ref:`query_parameters_to_set <envoy_v3_api_field_service.auth.v3.OkHttpResponse.query_parameters_to_set>` and :ref:`query_parameters_to_remove <envoy_v3_api_field_service.auth.v3.OkHttpResponse.query_parameters_to_remove>` for adding and removing query string parameters when using a gRPC authorization server.
* http: added support for :ref:`retriable health check status codes <envoy_v3_api_field_config.core.v3.HealthCheck.HttpHealthCheck.retriable_statuses>`.
* thrift_proxy: add upstream response zone metrics in the form ``cluster.cluster_name.zone.local_zone.upstream_zone.thrift.upstream_resp_success``.
Expand Down
9 changes: 9 additions & 0 deletions source/common/config/datasource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ std::string read(const envoy::config::core::v3::DataSource& source, bool allow_e
case envoy::config::core::v3::DataSource::SpecifierCase::kInlineString:
data = source.inline_string();
break;
case envoy::config::core::v3::DataSource::SpecifierCase::kEnvironmentVariable: {
const char* environment_variable = std::getenv(source.environment_variable().c_str());
if (environment_variable == nullptr) {
throw EnvoyException(
fmt::format("Environment variable doesn't exist: {}", source.environment_variable()));
}
data = environment_variable;
break;
}
default:
if (!allow_empty) {
throw EnvoyException(
Expand Down
68 changes: 68 additions & 0 deletions test/common/config/datasource_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "test/mocks/event/mocks.h"
#include "test/mocks/init/mocks.h"
#include "test/mocks/upstream/cluster_manager.h"
#include "test/test_common/environment.h"
#include "test/test_common/utility.h"

#include "gtest/gtest.h"
Expand Down Expand Up @@ -586,6 +587,73 @@ TEST_F(AsyncDataSourceTest, BaseIntervalTest) {
EXPECT_THROW(TestUtility::loadFromYamlAndValidate(yaml, config), EnvoyException);
}

TEST(DataSourceTest, WellKnownEnvironmentVariableTest) {
envoy::config::core::v3::DataSource config;

const std::string yaml = R"EOF(
environment_variable:
PATH
)EOF";
TestUtility::loadFromYamlAndValidate(yaml, config);

EXPECT_EQ(envoy::config::core::v3::DataSource::SpecifierCase::kEnvironmentVariable,
config.specifier_case());
EXPECT_EQ(config.environment_variable(), "PATH");
Api::ApiPtr api = Api::createApiForTest();
const auto path_data = DataSource::read(config, false, *api);
EXPECT_FALSE(path_data.empty());
}

TEST(DataSourceTest, MissingEnvironmentVariableTest) {
envoy::config::core::v3::DataSource config;

const std::string yaml = R"EOF(
environment_variable:
ThisVariableDoesntExist
)EOF";
TestUtility::loadFromYamlAndValidate(yaml, config);

EXPECT_EQ(envoy::config::core::v3::DataSource::SpecifierCase::kEnvironmentVariable,
config.specifier_case());
EXPECT_EQ(config.environment_variable(), "ThisVariableDoesntExist");
Api::ApiPtr api = Api::createApiForTest();
EXPECT_THROW(DataSource::read(config, false, *api), EnvoyException);
EXPECT_THROW(DataSource::read(config, true, *api), EnvoyException);
}

struct ScopedEnvironmentVariable {
ScopedEnvironmentVariable(const std::string& environment_variable, const std::string& value)
: environment_variable_(environment_variable) {
TestEnvironment::setEnvVar(environment_variable_, value, 1);
}
~ScopedEnvironmentVariable() { TestEnvironment::unsetEnvVar(environment_variable_); }
const std::string environment_variable_;
};

TEST(DataSourceTest, EmptyEnvironmentVariableTest) {
envoy::config::core::v3::DataSource config;
ScopedEnvironmentVariable test_variable("ThisVariableIsEmpty", "");

const std::string yaml = R"EOF(
environment_variable:
ThisVariableIsEmpty
)EOF";
TestUtility::loadFromYamlAndValidate(yaml, config);

EXPECT_EQ(envoy::config::core::v3::DataSource::SpecifierCase::kEnvironmentVariable,
config.specifier_case());
EXPECT_EQ(config.environment_variable(), "ThisVariableIsEmpty");
Api::ApiPtr api = Api::createApiForTest();
EXPECT_THROW(DataSource::read(config, false, *api), EnvoyException);
#ifdef WIN32
// Windows doesn't support empty environment variables.
EXPECT_THROW(DataSource::read(config, true, *api), EnvoyException);
#else
const auto environment_variable = DataSource::read(config, true, *api);
EXPECT_TRUE(environment_variable.empty());
#endif
}

} // namespace
} // namespace Config
} // namespace Envoy