-
-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathbpf_metadata.cc
More file actions
241 lines (205 loc) · 9.68 KB
/
bpf_metadata.cc
File metadata and controls
241 lines (205 loc) · 9.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include "tests/bpf_metadata.h"
#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "envoy/common/exception.h"
#include "envoy/config/core/v3/config_source.pb.h"
#include "envoy/config/subscription.h"
#include "envoy/network/address.h"
#include "envoy/network/filter.h"
#include "envoy/network/listen_socket.h"
#include "envoy/registry/registry.h"
#include "envoy/server/factory_context.h"
#include "envoy/server/filter_config.h"
#include "source/common/common/logger.h"
#include "source/common/config/utility.h"
#include "source/common/protobuf/message_validator_impl.h"
#include "source/common/protobuf/protobuf.h" // IWYU pragma: keep
#include "source/common/protobuf/utility.h"
#include "source/extensions/config_subscription/filesystem/filesystem_subscription_impl.h"
#include "test/test_common/environment.h"
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "cilium/api/bpf_metadata.pb.h"
#include "cilium/bpf_metadata.h"
#include "cilium/host_map.h"
#include "cilium/network_policy.h"
#include "cilium/secret_watcher.h"
#include "fmt/printf.h"
#include "tests/bpf_metadata.pb.h"
#include "tests/bpf_metadata.pb.validate.h" // IWYU pragma: keep
namespace Envoy {
std::string host_map_config = "version_info: \"0\"";
std::shared_ptr<const Cilium::PolicyHostMap> hostmap{nullptr}; // Keep reference to singleton
Network::Address::InstanceConstSharedPtr original_dst_address;
std::shared_ptr<const Cilium::NetworkPolicyMap> npmap{nullptr}; // Keep reference to singleton
std::string policy_config = "version_info: \"0\"";
std::string policy_path = "";
std::vector<std::pair<std::string, std::string>> sds_configs{};
namespace {
std::shared_ptr<const Cilium::PolicyHostMap>
createHostMap(const std::string& config, Server::Configuration::ListenerFactoryContext& context) {
return context.serverFactoryContext().singletonManager().getTyped<const Cilium::PolicyHostMap>(
"cilium_host_map_singleton", [&config, &context] {
std::string path = TestEnvironment::writeStringToFileForTest("host_map.yaml", config);
ENVOY_LOG_MISC(debug, "Loading Cilium Host Map from file \'{}\' instead of using gRPC",
path);
THROW_IF_NOT_OK(Envoy::Config::Utility::checkFilesystemSubscriptionBackingPath(
path, context.serverFactoryContext().api()));
Envoy::Config::SubscriptionStats stats =
Envoy::Config::Utility::generateStats(context.scope());
auto map = std::make_shared<Cilium::PolicyHostMap>(context.serverFactoryContext());
auto subscription = std::make_unique<Envoy::Config::FilesystemSubscriptionImpl>(
context.serverFactoryContext().mainThreadDispatcher(),
Envoy::Config::makePathConfigSource(path), *map,
std::make_shared<Cilium::PolicyHostDecoder>(), stats,
ProtobufMessage::getNullValidationVisitor(), context.serverFactoryContext().api());
map->startSubscription(std::move(subscription));
return map;
});
}
std::shared_ptr<const Cilium::NetworkPolicyMap>
createPolicyMap(const std::string& config,
const std::vector<std::pair<std::string, std::string>>& secret_configs,
Server::Configuration::FactoryContext& context) {
return context.serverFactoryContext().singletonManager().getTyped<const Cilium::NetworkPolicyMap>(
"cilium_network_policy_singleton", [&config, &secret_configs, &context] {
if (!secret_configs.empty()) {
for (const auto& sds_pair : secret_configs) {
auto& name = sds_pair.first;
auto& sds_config = sds_pair.second;
std::string sds_path = TestEnvironment::writeStringToFileForTest(
fmt::sprintf("secret-%s.yaml", name), sds_config);
THROW_IF_NOT_OK(Envoy::Config::Utility::checkFilesystemSubscriptionBackingPath(
sds_path, context.serverFactoryContext().api()));
}
Cilium::setSDSConfigFunc(
[](const std::string& name) -> envoy::config::core::v3::ConfigSource {
auto file_config = envoy::config::core::v3::ConfigSource();
/* initial_fetch_timeout left at default 15 seconds. */
file_config.set_resource_api_version(envoy::config::core::v3::ApiVersion::V3);
auto sds_path =
TestEnvironment::temporaryPath(fmt::sprintf("secret-%s.yaml", name));
*file_config.mutable_path_config_source() =
Envoy::Config::makePathConfigSource(sds_path);
return file_config;
});
}
// File subscription.
policy_path = TestEnvironment::writeStringToFileForTest("network_policy.yaml", config);
ENVOY_LOG_MISC(debug,
"Loading Cilium Network Policy from file \'{}\' instead "
"of using gRPC",
policy_path);
THROW_IF_NOT_OK(Envoy::Config::Utility::checkFilesystemSubscriptionBackingPath(
policy_path, context.serverFactoryContext().api()));
Envoy::Config::SubscriptionStats stats =
Envoy::Config::Utility::generateStats(context.scope());
auto map = std::make_shared<Cilium::NetworkPolicyMap>(context, absl::nullopt);
auto subscription = std::make_unique<Envoy::Config::FilesystemSubscriptionImpl>(
context.serverFactoryContext().mainThreadDispatcher(),
Envoy::Config::makePathConfigSource(policy_path), map->getImpl(),
std::make_shared<Cilium::NetworkPolicyDecoder>(), stats,
ProtobufMessage::getNullValidationVisitor(), context.serverFactoryContext().api());
map->startSubscription(std::move(subscription));
return map;
});
}
} // namespace
void initTestMaps(Server::Configuration::ListenerFactoryContext& context) {
// Create the file-based policy map before the filter is created, so that the
// singleton is set before the gRPC subscription is attempted.
hostmap = createHostMap(host_map_config, context);
// Create the file-based policy map before the filter is created, so that the
// singleton is set before the gRPC subscription is attempted.
npmap = createPolicyMap(policy_config, sds_configs, context);
}
namespace Cilium {
namespace BpfMetadata {
namespace {
::cilium::BpfMetadata getTestConfig(const ::cilium::TestBpfMetadata& config) {
::cilium::BpfMetadata test_config;
test_config.set_is_ingress(config.is_ingress());
test_config.set_is_l7lb(config.is_l7lb());
return test_config;
}
} // namespace
TestConfig::TestConfig(const ::cilium::TestBpfMetadata& config,
Server::Configuration::ListenerFactoryContext& context)
: Config(getTestConfig(config), context) {
hosts_ = hostmap;
npmap_ = npmap;
}
TestConfig::~TestConfig() {
hostmap.reset();
npmap.reset();
}
absl::optional<Cilium::BpfMetadata::SocketMetadata>
TestConfig::extractSocketMetadata(Network::ConnectionSocket& socket) {
// TLS filter chain matches this, make namespace part of this (e.g.,
// "default")?
socket.setDetectedTransportProtocol("cilium:default");
// This must be the full domain name
socket.setRequestedServerName("localhost");
std::string pod_ip;
std::uint64_t source_identity;
std::uint64_t destination_identity;
if (is_ingress_) {
source_identity = 1;
destination_identity = 173;
pod_ip = original_dst_address->ip()->addressAsString();
ENVOY_LOG_MISC(debug, "INGRESS POD_IP: {}", pod_ip);
} else {
source_identity = 173;
auto ip = socket.connectionInfoProvider().localAddress()->ip();
destination_identity = hosts_->resolve(ip);
pod_ip = ip->addressAsString();
ENVOY_LOG_MISC(debug, "EGRESS POD_IP: {}", pod_ip);
}
const auto& policy = getPolicy(pod_ip);
auto port = original_dst_address->ip()->port();
// Set metadata for policy based listener filter chain matching
// Note: tls_inspector may overwrite this value, if it executes after us!
std::string l7proto;
policy.useProxylib(is_ingress_, proxy_id_, is_ingress_ ? source_identity : destination_identity,
port, l7proto);
return {Cilium::BpfMetadata::SocketMetadata(
0, 0, source_identity, is_ingress_, is_l7lb_, port, std::move(pod_ip), "", nullptr, nullptr,
nullptr, original_dst_address, shared_from_this(), 0, std::move(l7proto), "")};
}
} // namespace BpfMetadata
} // namespace Cilium
namespace Server {
namespace Configuration {
class TestBpfMetadataConfigFactory : public NamedListenerFilterConfigFactory {
public:
// NamedListenerFilterConfigFactory
Network::ListenerFilterFactoryCb createListenerFilterFactoryFromProto(
const Protobuf::Message& proto_config,
const Network::ListenerFilterMatcherSharedPtr& listener_filter_matcher,
ListenerFactoryContext& context) override {
initTestMaps(context);
auto config = std::make_shared<Cilium::BpfMetadata::TestConfig>(
MessageUtil::downcastAndValidate<const ::cilium::TestBpfMetadata&>(
proto_config, context.messageValidationVisitor()),
context);
return [listener_filter_matcher,
config](Network::ListenerFilterManager& filter_manager) mutable -> void {
filter_manager.addAcceptFilter(listener_filter_matcher,
std::make_unique<Cilium::BpfMetadata::Instance>(config));
};
}
ProtobufTypes::MessagePtr createEmptyConfigProto() override {
return std::make_unique<::cilium::TestBpfMetadata>();
}
std::string name() const override { return "test_bpf_metadata"; }
};
/**
* Static registration for the bpf metadata filter. @see RegisterFactory.
*/
REGISTER_FACTORY(TestBpfMetadataConfigFactory, NamedListenerFilterConfigFactory);
} // namespace Configuration
} // namespace Server
} // namespace Envoy