/proc/self/cwd/source/extensions/common/wasm/plugin.h
Line | Count | Source (jump to first uncovered line) |
1 | | #pragma once |
2 | | |
3 | | #include <memory> |
4 | | |
5 | | #include "envoy/config/core/v3/base.pb.h" |
6 | | #include "envoy/extensions/wasm/v3/wasm.pb.validate.h" |
7 | | #include "envoy/local_info/local_info.h" |
8 | | |
9 | | #include "source/common/protobuf/protobuf.h" |
10 | | #include "source/common/protobuf/utility.h" |
11 | | |
12 | | #include "include/proxy-wasm/wasm.h" |
13 | | |
14 | | namespace Envoy { |
15 | | namespace Extensions { |
16 | | namespace Common { |
17 | | namespace Wasm { |
18 | | |
19 | | // clang-format off |
20 | | using EnvironmentVariableMap = std::unordered_map<std::string, std::string>; |
21 | | // clang-format on |
22 | | |
23 | | class WasmConfig { |
24 | | public: |
25 | | WasmConfig(const envoy::extensions::wasm::v3::PluginConfig& config); |
26 | 19 | const envoy::extensions::wasm::v3::PluginConfig& config() { return config_; } |
27 | 1 | proxy_wasm::AllowedCapabilitiesMap& allowedCapabilities() { return allowed_capabilities_; } |
28 | 1 | EnvironmentVariableMap& environmentVariables() { return envs_; } |
29 | | |
30 | | private: |
31 | | const envoy::extensions::wasm::v3::PluginConfig config_; |
32 | | proxy_wasm::AllowedCapabilitiesMap allowed_capabilities_{}; |
33 | | EnvironmentVariableMap envs_; |
34 | | }; |
35 | | |
36 | | using WasmConfigPtr = std::unique_ptr<WasmConfig>; |
37 | | |
38 | | // Plugin contains the information for a filter/service. |
39 | | class Plugin : public proxy_wasm::PluginBase { |
40 | | public: |
41 | | Plugin(const envoy::extensions::wasm::v3::PluginConfig& config, |
42 | | envoy::config::core::v3::TrafficDirection direction, |
43 | | const LocalInfo::LocalInfo& local_info, |
44 | | const envoy::config::core::v3::Metadata* listener_metadata) |
45 | | : PluginBase(config.name(), config.root_id(), config.vm_config().vm_id(), |
46 | | config.vm_config().runtime(), MessageUtil::anyToBytes(config.configuration()), |
47 | | config.fail_open(), createPluginKey(config, direction, listener_metadata)), |
48 | | direction_(direction), local_info_(local_info), listener_metadata_(listener_metadata), |
49 | 16 | wasm_config_(std::make_unique<WasmConfig>(config)) {} |
50 | | |
51 | 0 | envoy::config::core::v3::TrafficDirection& direction() { return direction_; } |
52 | 0 | const LocalInfo::LocalInfo& localInfo() { return local_info_; } |
53 | 0 | const envoy::config::core::v3::Metadata* listenerMetadata() { return listener_metadata_; } |
54 | 15 | WasmConfig& wasmConfig() { return *wasm_config_; } |
55 | | |
56 | | private: |
57 | | static std::string createPluginKey(const envoy::extensions::wasm::v3::PluginConfig& config, |
58 | | envoy::config::core::v3::TrafficDirection direction, |
59 | 16 | const envoy::config::core::v3::Metadata* listener_metadata) { |
60 | 16 | return config.name() + "||" + envoy::config::core::v3::TrafficDirection_Name(direction) + |
61 | 16 | (listener_metadata ? "||" + std::to_string(MessageUtil::hash(*listener_metadata)) : ""); |
62 | 16 | } |
63 | | |
64 | | private: |
65 | | envoy::config::core::v3::TrafficDirection direction_; |
66 | | const LocalInfo::LocalInfo& local_info_; |
67 | | const envoy::config::core::v3::Metadata* listener_metadata_; |
68 | | WasmConfigPtr wasm_config_; |
69 | | }; |
70 | | |
71 | | using PluginSharedPtr = std::shared_ptr<Plugin>; |
72 | | |
73 | | } // namespace Wasm |
74 | | } // namespace Common |
75 | | } // namespace Extensions |
76 | | } // namespace Envoy |