1
#include "source/extensions/matching/common_inputs/environment_variable/config.h"
2

            
3
#include <memory>
4

            
5
#include "envoy/matcher/matcher.h"
6

            
7
namespace Envoy {
8
namespace Extensions {
9
namespace Matching {
10
namespace CommonInputs {
11
namespace EnvironmentVariable {
12

            
13
Envoy::Matcher::CommonProtocolInputFactoryCb
14
Config::createCommonProtocolInputFactoryCb(const Protobuf::Message& config,
15
2
                                           ProtobufMessage::ValidationVisitor& validation_visitor) {
16
2
  const auto& environment_config = MessageUtil::downcastAndValidate<
17
2
      const envoy::extensions::matching::common_inputs::environment_variable::v3::Config&>(
18
2
      config, validation_visitor);
19

            
20
  // We read the env variable at construction time to avoid repeat lookups.
21
  // This assumes that the environment remains stable during the process lifetime.
22
2
  auto* value = getenv(environment_config.name().data());
23
2
  if (value != nullptr) {
24
1
    return [s = std::string(value)]() {
25
1
      return std::make_unique<Input>(absl::make_optional<absl::string_view>(s));
26
1
    };
27
1
  }
28

            
29
1
  return []() { return std::make_unique<Input>(absl::nullopt); };
30
2
}
31

            
32
/**
33
 * Static registration for the environment data input. @see RegisterFactory.
34
 */
35
REGISTER_FACTORY(Config, Envoy::Matcher::CommonProtocolInputFactory);
36

            
37
} // namespace EnvironmentVariable
38
} // namespace CommonInputs
39
} // namespace Matching
40
} // namespace Extensions
41
} // namespace Envoy