|
| 1 | +#include "source/extensions/filters/udp/dns_filter/dns_filter_access_log.h" |
| 2 | + |
| 3 | +#include "source/common/formatter/substitution_format_string.h" |
| 4 | +#include "source/common/protobuf/utility.h" |
| 5 | +#include "source/extensions/filters/udp/dns_filter/dns_parser.h" |
| 6 | + |
| 7 | +#include "absl/container/flat_hash_map.h" |
| 8 | +#include "absl/strings/str_cat.h" |
| 9 | + |
| 10 | +namespace Envoy { |
| 11 | +namespace Extensions { |
| 12 | +namespace UdpFilters { |
| 13 | +namespace DnsFilter { |
| 14 | + |
| 15 | +namespace { |
| 16 | + |
| 17 | +/** |
| 18 | + * FormatterProvider for DNS-specific fields from DnsQueryContext. |
| 19 | + */ |
| 20 | +class DnsFormatterProvider : public Formatter::FormatterProvider { |
| 21 | +public: |
| 22 | + using FieldExtractor = std::function<absl::optional<std::string>(const Formatter::Context&, |
| 23 | + const StreamInfo::StreamInfo&)>; |
| 24 | + |
| 25 | + DnsFormatterProvider(FieldExtractor field_extractor) |
| 26 | + : field_extractor_(std::move(field_extractor)) {} |
| 27 | + |
| 28 | + // FormatterProvider |
| 29 | + absl::optional<std::string> format(const Formatter::Context& context, |
| 30 | + const StreamInfo::StreamInfo& stream_info) const override { |
| 31 | + return field_extractor_(context, stream_info); |
| 32 | + } |
| 33 | + |
| 34 | + Protobuf::Value formatValue(const Formatter::Context& context, |
| 35 | + const StreamInfo::StreamInfo& stream_info) const override { |
| 36 | + const auto str = field_extractor_(context, stream_info); |
| 37 | + return str.has_value() ? ValueUtil::stringValue(str.value()) : ValueUtil::nullValue(); |
| 38 | + } |
| 39 | + |
| 40 | +private: |
| 41 | + const FieldExtractor field_extractor_; |
| 42 | +}; |
| 43 | + |
| 44 | +/** |
| 45 | + * Helper to create formatter provider for query-dependent fields (fields that require queries_[0]). |
| 46 | + */ |
| 47 | +template <typename FieldAccessor> |
| 48 | +Formatter::FormatterProviderPtr makeQueryFieldProvider(FieldAccessor accessor) { |
| 49 | + return std::make_unique<DnsFormatterProvider>( |
| 50 | + [accessor](const Formatter::Context& ctx, |
| 51 | + const StreamInfo::StreamInfo&) -> absl::optional<std::string> { |
| 52 | + const auto dns_ctx = ctx.typedExtension<DnsQueryContext>(); |
| 53 | + if (!dns_ctx.has_value() || dns_ctx->queries_.empty()) { |
| 54 | + return absl::nullopt; |
| 55 | + } |
| 56 | + return absl::StrCat(accessor(*dns_ctx)); |
| 57 | + }); |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Helper to create formatter provider for context-level fields (fields that don't require queries). |
| 62 | + */ |
| 63 | +template <typename FieldAccessor> |
| 64 | +Formatter::FormatterProviderPtr makeContextFieldProvider(FieldAccessor accessor) { |
| 65 | + return std::make_unique<DnsFormatterProvider>( |
| 66 | + [accessor](const Formatter::Context& ctx, |
| 67 | + const StreamInfo::StreamInfo&) -> absl::optional<std::string> { |
| 68 | + const auto dns_ctx = ctx.typedExtension<DnsQueryContext>(); |
| 69 | + if (!dns_ctx.has_value()) { |
| 70 | + return absl::nullopt; |
| 71 | + } |
| 72 | + return accessor(*dns_ctx); |
| 73 | + }); |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * DNS Filter command parser implementation. |
| 78 | + */ |
| 79 | +class DnsFilterCommandParser : public Formatter::CommandParser { |
| 80 | +public: |
| 81 | + using ProviderFunc = |
| 82 | + std::function<Formatter::FormatterProviderPtr(absl::string_view, absl::optional<size_t>)>; |
| 83 | + using ProviderFuncTable = absl::flat_hash_map<std::string, ProviderFunc>; |
| 84 | + |
| 85 | + // CommandParser |
| 86 | + Formatter::FormatterProviderPtr parse(absl::string_view command, absl::string_view command_arg, |
| 87 | + absl::optional<size_t> max_length) const override { |
| 88 | + const auto& provider_table = providerFuncTable(); |
| 89 | + const auto func_it = provider_table.find(std::string(command)); |
| 90 | + if (func_it == provider_table.end()) { |
| 91 | + return nullptr; |
| 92 | + } |
| 93 | + return func_it->second(command_arg, max_length); |
| 94 | + } |
| 95 | + |
| 96 | +private: |
| 97 | + static const ProviderFuncTable& providerFuncTable() { |
| 98 | + CONSTRUCT_ON_FIRST_USE( |
| 99 | + ProviderFuncTable, |
| 100 | + { |
| 101 | + {"QUERY_NAME", |
| 102 | + [](absl::string_view, absl::optional<size_t>) -> Formatter::FormatterProviderPtr { |
| 103 | + return makeQueryFieldProvider( |
| 104 | + [](const DnsQueryContext& ctx) { return ctx.queries_[0]->name_; }); |
| 105 | + }}, |
| 106 | + {"QUERY_TYPE", |
| 107 | + [](absl::string_view, absl::optional<size_t>) -> Formatter::FormatterProviderPtr { |
| 108 | + return makeQueryFieldProvider( |
| 109 | + [](const DnsQueryContext& ctx) { return ctx.queries_[0]->type_; }); |
| 110 | + }}, |
| 111 | + {"QUERY_CLASS", |
| 112 | + [](absl::string_view, absl::optional<size_t>) -> Formatter::FormatterProviderPtr { |
| 113 | + return makeQueryFieldProvider( |
| 114 | + [](const DnsQueryContext& ctx) { return ctx.queries_[0]->class_; }); |
| 115 | + }}, |
| 116 | + {"ANSWER_COUNT", |
| 117 | + [](absl::string_view, absl::optional<size_t>) -> Formatter::FormatterProviderPtr { |
| 118 | + return makeContextFieldProvider( |
| 119 | + [](const DnsQueryContext& ctx) { return absl::StrCat(ctx.answers_.size()); }); |
| 120 | + }}, |
| 121 | + {"RESPONSE_CODE", |
| 122 | + [](absl::string_view, absl::optional<size_t>) -> Formatter::FormatterProviderPtr { |
| 123 | + return makeContextFieldProvider( |
| 124 | + [](const DnsQueryContext& ctx) { return absl::StrCat(ctx.response_code_); }); |
| 125 | + }}, |
| 126 | + {"PARSE_STATUS", |
| 127 | + [](absl::string_view, absl::optional<size_t>) -> Formatter::FormatterProviderPtr { |
| 128 | + return makeContextFieldProvider([](const DnsQueryContext& ctx) -> std::string { |
| 129 | + return ctx.parse_status_ ? "true" : "false"; |
| 130 | + }); |
| 131 | + }}, |
| 132 | + }); |
| 133 | + } |
| 134 | +}; |
| 135 | + |
| 136 | +} // namespace |
| 137 | + |
| 138 | +Formatter::CommandParserPtr createDnsFilterCommandParser() { |
| 139 | + return std::make_unique<DnsFilterCommandParser>(); |
| 140 | +} |
| 141 | + |
| 142 | +} // namespace DnsFilter |
| 143 | +} // namespace UdpFilters |
| 144 | +} // namespace Extensions |
| 145 | +} // namespace Envoy |
0 commit comments