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
441
WasmConfig::WasmConfig(const envoy::extensions::wasm::v3::PluginConfig& config) : config_(config) {
13
441
  for (auto& capability : config_.capability_restriction_config().allowed_capabilities()) {
14
    // TODO(rapilado): Set the SanitizationConfig fields once sanitization is implemented.
15
6
    allowed_capabilities_[capability.first] = proxy_wasm::SanitizationConfig();
16
6
  }
17

            
18
441
  if (config_.vm_config().has_environment_variables()) {
19
260
    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
260
    if (config.vm_config().runtime() == "envoy.wasm.runtime.null" && !envs.key_values().empty()) {
26
1
      throw EnvoyException("envoy.extensions.wasm.v3.VmConfig.EnvironmentVariables.key_values must "
27
1
                           "not be set for NullVm.");
28
1
    }
29

            
30
    // Check key duplication.
31
259
    absl::flat_hash_set<std::string> keys;
32
259
    for (const auto& env : envs.key_values()) {
33
5
      keys.insert(env.first);
34
5
    }
35
260
    for (const auto& key : envs.host_env_keys()) {
36
7
      if (!keys.insert(key).second) {
37
2
        throw EnvoyException(
38
2
            fmt::format("Key {} is duplicated in "
39
2
                        "envoy.extensions.wasm.v3.VmConfig.environment_variables for {}. "
40
2
                        "All the keys must be unique.",
41
2
                        key, config_.name()));
42
2
      }
43
7
    }
44

            
45
    // Construct merged key-value pairs.
46
257
    for (const auto& env : envs.key_values()) {
47
4
      envs_[env.first] = env.second;
48
4
    }
49
257
    for (const auto& key : envs.host_env_keys()) {
50
4
      if (auto value = std::getenv(key.data())) {
51
3
        envs_[key] = value;
52
3
      }
53
4
    }
54
257
  }
55
441
}
56

            
57
} // namespace Wasm
58
} // namespace Common
59
} // namespace Extensions
60
} // namespace Envoy