Line data Source code
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 4 : WasmConfig::WasmConfig(const envoy::extensions::wasm::v3::PluginConfig& config) : config_(config) { 13 4 : for (auto& capability : config_.capability_restriction_config().allowed_capabilities()) { 14 : // TODO(rapilado): Set the SanitizationConfig fields once sanitization is implemented. 15 0 : allowed_capabilities_[capability.first] = proxy_wasm::SanitizationConfig(); 16 0 : } 17 : 18 4 : if (config_.vm_config().has_environment_variables()) { 19 0 : 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 0 : 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 0 : absl::flat_hash_set<std::string> keys; 32 0 : for (const auto& env : envs.key_values()) { 33 0 : keys.insert(env.first); 34 0 : } 35 0 : for (const auto& key : envs.host_env_keys()) { 36 0 : if (!keys.insert(key).second) { 37 0 : throw EnvoyException( 38 0 : fmt::format("Key {} is duplicated in " 39 0 : "envoy.extensions.wasm.v3.VmConfig.environment_variables for {}. " 40 0 : "All the keys must be unique.", 41 0 : key, config_.name())); 42 0 : } 43 0 : } 44 : 45 : // Construct merged key-value pairs. 46 0 : for (const auto& env : envs.key_values()) { 47 0 : envs_[env.first] = env.second; 48 0 : } 49 0 : for (const auto& key : envs.host_env_keys()) { 50 0 : if (auto value = std::getenv(key.data())) { 51 0 : envs_[key] = value; 52 0 : } 53 0 : } 54 0 : } 55 4 : } 56 : 57 : } // namespace Wasm 58 : } // namespace Common 59 : } // namespace Extensions 60 : } // namespace Envoy