/proc/self/cwd/source/extensions/common/wasm/plugin.cc
Line | Count | Source (jump to first uncovered line) |
1 | | #include "source/extensions/common/wasm/plugin.h" |
2 | | |
3 | | #include "envoy/common/exception.h" |
4 | | |
5 | | #include "include/proxy-wasm/wasm.h" |
6 | | |
7 | | namespace Envoy { |
8 | | namespace Extensions { |
9 | | namespace Common { |
10 | | namespace Wasm { |
11 | | |
12 | 105 | WasmConfig::WasmConfig(const envoy::extensions::wasm::v3::PluginConfig& config) : config_(config) { |
13 | 533 | for (auto& capability : config_.capability_restriction_config().allowed_capabilities()) { |
14 | | // TODO(rapilado): Set the SanitizationConfig fields once sanitization is implemented. |
15 | 533 | allowed_capabilities_[capability.first] = proxy_wasm::SanitizationConfig(); |
16 | 533 | } |
17 | | |
18 | 105 | if (config_.vm_config().has_environment_variables()) { |
19 | 58 | const auto& envs = config_.vm_config().environment_variables(); |
20 | | |
21 | | // We reject NullVm with key_values configuration |
22 | | // since it directly accesses Envoy's env vars and we should not modify Envoy's env vars here. |
23 | | // TODO(mathetake): Once proxy_get_map_values(type::EnvironmentVariables, ..) call is supported, |
24 | | // then remove this restriction. |
25 | 58 | if (config.vm_config().runtime() == "envoy.wasm.runtime.null" && !envs.key_values().empty()) { |
26 | 0 | throw EnvoyException("envoy.extensions.wasm.v3.VmConfig.EnvironmentVariables.key_values must " |
27 | 0 | "not be set for NullVm."); |
28 | 0 | } |
29 | | |
30 | | // Check key duplication. |
31 | 58 | absl::flat_hash_set<std::string> keys; |
32 | 317 | for (const auto& env : envs.key_values()) { |
33 | 317 | keys.insert(env.first); |
34 | 317 | } |
35 | 58 | for (const auto& key : envs.host_env_keys()) { |
36 | 50 | if (!keys.insert(key).second) { |
37 | 4 | throw EnvoyException( |
38 | 4 | fmt::format("Key {} is duplicated in " |
39 | 4 | "envoy.extensions.wasm.v3.VmConfig.environment_variables for {}. " |
40 | 4 | "All the keys must be unique.", |
41 | 4 | key, config_.name())); |
42 | 4 | } |
43 | 50 | } |
44 | | |
45 | | // Construct merged key-value pairs. |
46 | 312 | for (const auto& env : envs.key_values()) { |
47 | 312 | envs_[env.first] = env.second; |
48 | 312 | } |
49 | 54 | for (const auto& key : envs.host_env_keys()) { |
50 | 43 | if (auto value = std::getenv(key.data())) { |
51 | 25 | envs_[key] = value; |
52 | 25 | } |
53 | 43 | } |
54 | 54 | } |
55 | 105 | } |
56 | | |
57 | | } // namespace Wasm |
58 | | } // namespace Common |
59 | | } // namespace Extensions |
60 | | } // namespace Envoy |