Skip to content

Commit 7a229d7

Browse files
committed
metadata: Eliminate separate 'ingress_source_identity'
Both debug logging and access logging are more intelligible when the original source identity is used, also in the case of the north/south L7 LB, where an "Ingress IP" is used as the source address in the upstream connections. In that case SO_MARK encodes the identity of the Ingress IP so that the source identity seen in the destination is the same when the destination is in the same node (source identity derived from SO_MARK) and when the destination is in a different node (source identity mapped from the source (Ingress) IP). Note that the (original) source identity is used for policy determination only for ingress policy, for which the original source identity was already used. Given this, the only visible change is the source identity as seen on debug/trace logs and (hubble) access logs. Access logs already show the original source address, so this change aligns the recorded source identity with it, so that instead of: Jun 18 12:37:20.940: default/ubuntu-deployment-6f7cc4b9fb-9gmnp:39430 (ingress) -> default/nginx-deployment-worker-7d99874b8b-dw4bt:80 (ID:53552) http-request FORWARDED (HTTP/1.1 GET http://10.96.154.80/) Hubble will show this: Signed-off-by: Jarno Rajahalme <[email protected]>
1 parent 010e0cd commit 7a229d7

File tree

7 files changed

+57
-71
lines changed

7 files changed

+57
-71
lines changed

cilium/bpf_metadata.cc

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -403,16 +403,18 @@ Config::extractSocketMetadata(Network::ConnectionSocket& socket) {
403403
// Use a pointer as we may need to change the policy in the case of "North/South L7 LB" below.
404404
const auto* policy = &getPolicy(pod_ip);
405405

406-
// Resolve the source security ID from conntrack map, or from ip cache
407-
uint32_t source_identity = resolveSourceIdentity(*policy, sip, dip, is_ingress_, is_l7lb_);
406+
// original_source_identity is set to the original source identity and is used in policy logs and
407+
// access logging. This is typically the same as 'source_identity', but for north/south L7 LB,
408+
// where the upstream source address is set to an Ingress IP, the 'source_identity' will be that
409+
// if the Ingress IP. This original source identity is still used in policy logs and access
410+
// logging even in that case.
411+
uint32_t original_source_identity =
412+
resolveSourceIdentity(*policy, sip, dip, is_ingress_, is_l7lb_);
413+
uint32_t source_identity = original_source_identity;
408414

409415
// Resolve the destination security ID for egress traffic
410416
uint32_t destination_identity = is_ingress_ ? 0 : resolvePolicyId(dip);
411417

412-
// ingress_source_identity is non-zero when the egress path l7 LB should also enforce
413-
// the ingress path policy using the original source identity.
414-
uint32_t ingress_source_identity = 0;
415-
416418
// Use the configured IPv4/IPv6 Ingress IPs as starting point for the sources addresses
417419
IpAddressPair source_addresses(ipv4_source_address_, ipv6_source_address_);
418420

@@ -463,7 +465,6 @@ Config::extractSocketMetadata(Network::ConnectionSocket& socket) {
463465

464466
// Enforce Ingress policy?
465467
if (enforce_policy_on_l7lb_) {
466-
ingress_source_identity = source_identity;
467468
ingress_policy_name =
468469
l7lb_policy_name_.empty() ? ingress_ip->addressAsString() : l7lb_policy_name_;
469470
}
@@ -519,15 +520,15 @@ Config::extractSocketMetadata(Network::ConnectionSocket& socket) {
519520
}
520521

521522
ENVOY_LOG(trace,
522-
"cilium.bpf_metadata: mark {}, ingress_source_identity {}, source_identity {}, "
523+
"cilium.bpf_metadata: mark {}, original_source_identity {}, source_identity {}, "
523524
"is_ingress {}, is_l7lb_ {}, ingress_policy_name {}, port {}, pod_ip {}",
524-
mark, ingress_source_identity, source_identity, is_ingress_, is_l7lb_,
525+
mark, original_source_identity, source_identity, is_ingress_, is_l7lb_,
525526
ingress_policy_name, dip->port(), pod_ip);
526527
return {Cilium::BpfMetadata::SocketMetadata(
527-
mark, ingress_source_identity, source_identity, is_ingress_, is_l7lb_, dip->port(),
528-
std::move(pod_ip), std::move(ingress_policy_name), std::move(src_address),
529-
std::move(source_addresses.ipv4_), std::move(source_addresses.ipv6_), std::move(dst_address),
530-
shared_from_this(), proxy_id_, std::move(proxylib_l7proto), sni)};
528+
mark, original_source_identity, is_ingress_, is_l7lb_, dip->port(), std::move(pod_ip),
529+
std::move(ingress_policy_name), std::move(src_address), std::move(source_addresses.ipv4_),
530+
std::move(source_addresses.ipv6_), std::move(dst_address), shared_from_this(), proxy_id_,
531+
std::move(proxylib_l7proto), sni)};
531532
}
532533

533534
Network::FilterStatus Instance::onAccept(Network::ListenerFilterCallbacks& cb) {

cilium/bpf_metadata.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,17 @@ namespace BpfMetadata {
3535
#define DEFAULT_CACHE_ENTRY_TTL_MS 3
3636

3737
struct SocketMetadata : public Logger::Loggable<Logger::Id::filter> {
38-
SocketMetadata(uint32_t mark, uint32_t ingress_source_identity, uint32_t source_identity,
39-
bool ingress, bool l7lb, uint16_t port, std::string&& pod_ip,
40-
std::string&& ingress_policy_name,
38+
SocketMetadata(uint32_t mark, uint32_t source_identity, bool ingress, bool l7lb, uint16_t port,
39+
std::string&& pod_ip, std::string&& ingress_policy_name,
4140
Network::Address::InstanceConstSharedPtr original_source_address,
4241
Network::Address::InstanceConstSharedPtr source_address_ipv4,
4342
Network::Address::InstanceConstSharedPtr source_address_ipv6,
4443
Network::Address::InstanceConstSharedPtr original_dest_address,
4544
const PolicyResolverSharedPtr& policy_resolver, uint32_t proxy_id,
4645
std::string&& proxylib_l7_proto, absl::string_view sni)
47-
: ingress_source_identity_(ingress_source_identity), source_identity_(source_identity),
48-
ingress_(ingress), is_l7lb_(l7lb), port_(port), pod_ip_(std::move(pod_ip)),
49-
ingress_policy_name_(std::move(ingress_policy_name)), proxy_id_(proxy_id),
50-
proxylib_l7_proto_(std::move(proxylib_l7_proto)), sni_(sni),
46+
: source_identity_(source_identity), ingress_(ingress), is_l7lb_(l7lb), port_(port),
47+
pod_ip_(std::move(pod_ip)), ingress_policy_name_(std::move(ingress_policy_name)),
48+
proxy_id_(proxy_id), proxylib_l7_proto_(std::move(proxylib_l7_proto)), sni_(sni),
5149
policy_resolver_(policy_resolver), mark_(mark),
5250
original_source_address_(std::move(original_source_address)),
5351
source_address_ipv4_(std::move(source_address_ipv4)),
@@ -56,7 +54,7 @@ struct SocketMetadata : public Logger::Loggable<Logger::Id::filter> {
5654

5755
std::shared_ptr<Envoy::Cilium::CiliumPolicyFilterState> buildCiliumPolicyFilterState() {
5856
return std::make_shared<Envoy::Cilium::CiliumPolicyFilterState>(
59-
ingress_source_identity_, source_identity_, ingress_, is_l7lb_, port_, std::move(pod_ip_),
57+
source_identity_, ingress_, is_l7lb_, port_, std::move(pod_ip_),
6058
std::move(ingress_policy_name_), policy_resolver_, proxy_id_, sni_);
6159
};
6260

@@ -111,7 +109,6 @@ struct SocketMetadata : public Logger::Loggable<Logger::Id::filter> {
111109
socket.connectionInfoProvider().restoreLocalAddress(original_dest_address_);
112110
}
113111

114-
uint32_t ingress_source_identity_;
115112
uint32_t source_identity_;
116113
bool ingress_;
117114
bool is_l7lb_;
@@ -165,8 +162,8 @@ class Config : public Cilium::PolicyResolver,
165162
bool enforce_policy_on_l7lb_;
166163
std::string l7lb_policy_name_;
167164
std::chrono::milliseconds ipcache_entry_ttl_;
168-
Random::RandomGenerator& random_;
169165

166+
Random::RandomGenerator& random_;
170167
std::shared_ptr<const Cilium::NetworkPolicyMap> npmap_{};
171168
Cilium::CtMapSharedPtr ct_maps_{};
172169
Cilium::IpCacheSharedPtr ipcache_{};

cilium/filter_state_cilium_policy.cc

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,13 @@ bool CiliumPolicyFilterState::enforceNetworkPolicy(const Network::Connection& co
5353
const auto& policy = policy_resolver_->getPolicy(ingress_policy_name_);
5454

5555
// Enforce ingress policy for Ingress, on the original destination port
56-
if (ingress_source_identity_ != 0) {
57-
auto ingress_port_policy = policy.findPortPolicy(true, port_);
58-
if (!ingress_port_policy.allowed(proxy_id_, ingress_source_identity_, sni)) {
59-
ENVOY_CONN_LOG(debug,
60-
"Ingress network policy {} DROP for source identity and destination "
61-
"reserved ingress identity: {} proxy_id: {} port: {} sni: \"{}\"",
62-
conn, ingress_policy_name_, ingress_source_identity_, proxy_id_, port_, sni);
63-
return false;
64-
}
56+
auto ingress_port_policy = policy.findPortPolicy(true, port_);
57+
if (!ingress_port_policy.allowed(proxy_id_, source_identity_, sni)) {
58+
ENVOY_CONN_LOG(debug,
59+
"Ingress network policy {} DROP for source identity and destination "
60+
"reserved ingress identity: {} proxy_id: {} port: {} sni: \"{}\"",
61+
conn, ingress_policy_name_, source_identity_, proxy_id_, port_, sni);
62+
return false;
6563
}
6664

6765
// Enforce egress policy for Ingress
@@ -122,36 +120,34 @@ bool CiliumPolicyFilterState::enforceIngressHTTPPolicy(
122120
const auto& policy = policy_resolver_->getPolicy(ingress_policy_name_);
123121

124122
// Enforce ingress policy for Ingress, on the original destination port
125-
if (ingress_source_identity_ != 0) {
126-
const auto port_policy = policy.findPortPolicy(true, port_);
127-
if (!port_policy.hasHttpRules()) {
128-
ENVOY_CONN_LOG(debug,
129-
"cilium.l7policy: Ingress {} HTTP ingress policy enforcement skipped (no HTTP "
130-
"rules) on proxy_id: {} id: {} port: {}",
131-
conn, ingress_policy_name_, proxy_id_, ingress_source_identity_, port_);
132-
return true;
133-
}
123+
const auto ingress_policy = policy.findPortPolicy(true, port_);
124+
if (!ingress_policy.hasHttpRules()) {
125+
ENVOY_CONN_LOG(debug,
126+
"cilium.l7policy: Ingress {} HTTP ingress policy enforcement skipped (no HTTP "
127+
"rules) on proxy_id: {} id: {} port: {}",
128+
conn, ingress_policy_name_, proxy_id_, source_identity_, port_);
129+
return true;
130+
}
134131

135-
if (!port_policy.allowed(proxy_id_, ingress_source_identity_, headers, log_entry)) {
136-
ENVOY_CONN_LOG(
137-
debug,
138-
"cilium.l7policy: Ingress {} HTTP ingress policy DROP on proxy_id: {} id: {} port: {}",
139-
conn, ingress_policy_name_, proxy_id_, ingress_source_identity_, port_);
140-
return false;
141-
}
132+
if (!ingress_policy.allowed(proxy_id_, source_identity_, headers, log_entry)) {
133+
ENVOY_CONN_LOG(
134+
debug,
135+
"cilium.l7policy: Ingress {} HTTP ingress policy DROP on proxy_id: {} id: {} port: {}",
136+
conn, ingress_policy_name_, proxy_id_, source_identity_, port_);
137+
return false;
142138
}
143139

144140
// Enforce egress policy for Ingress on the upstream destination identity and port
145-
const auto port_policy = policy.findPortPolicy(false, destination_port);
146-
if (!port_policy.hasHttpRules()) {
141+
const auto egress_policy = policy.findPortPolicy(false, destination_port);
142+
if (!egress_policy.hasHttpRules()) {
147143
ENVOY_CONN_LOG(debug,
148144
"cilium.l7policy: Ingress {} HTTP egress policy enforcement skipped (no HTTP "
149145
"rules) on proxy_id: {} id: {} port: {}",
150146
conn, ingress_policy_name_, proxy_id_, destination_identity, destination_port);
151147
return true;
152148
}
153149

154-
if (!port_policy.allowed(proxy_id_, destination_identity, headers, log_entry)) {
150+
if (!egress_policy.allowed(proxy_id_, destination_identity, headers, log_entry)) {
155151
ENVOY_CONN_LOG(
156152
debug,
157153
"cilium.l7policy: Ingress {} HTTP egress policy DROP on proxy_id: {} id: {} port: {}",

cilium/filter_state_cilium_policy.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,13 @@ using PolicyResolverSharedPtr = std::shared_ptr<PolicyResolver>;
3434
class CiliumPolicyFilterState : public StreamInfo::FilterState::Object,
3535
public Logger::Loggable<Logger::Id::filter> {
3636
public:
37-
CiliumPolicyFilterState(uint32_t ingress_source_identity, uint32_t source_identity, bool ingress,
38-
bool l7lb, uint16_t port, std::string&& pod_ip,
39-
std::string&& ingress_policy_name,
37+
CiliumPolicyFilterState(uint32_t source_identity, bool ingress, bool l7lb, uint16_t port,
38+
std::string&& pod_ip, std::string&& ingress_policy_name,
4039
const PolicyResolverSharedPtr& policy_resolver, uint32_t proxy_id,
4140
absl::string_view sni)
42-
: ingress_source_identity_(ingress_source_identity), source_identity_(source_identity),
43-
ingress_(ingress), is_l7lb_(l7lb), port_(port), pod_ip_(std::move(pod_ip)),
44-
ingress_policy_name_(std::move(ingress_policy_name)), proxy_id_(proxy_id), sni_(sni),
45-
policy_resolver_(policy_resolver) {
41+
: source_identity_(source_identity), ingress_(ingress), is_l7lb_(l7lb), port_(port),
42+
pod_ip_(std::move(pod_ip)), ingress_policy_name_(std::move(ingress_policy_name)),
43+
proxy_id_(proxy_id), sni_(sni), policy_resolver_(policy_resolver) {
4644
ENVOY_LOG(
4745
debug,
4846
"Cilium CiliumPolicyFilterState(): source_identity: {}, "
@@ -79,7 +77,6 @@ class CiliumPolicyFilterState : public StreamInfo::FilterState::Object,
7977
static const std::string& key();
8078

8179
// Additional ingress policy enforcement is performed if ingress_source_identity is non-zero
82-
uint32_t ingress_source_identity_;
8380
uint32_t source_identity_;
8481
bool ingress_;
8582
bool is_l7lb_;

cilium/l7policy.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ Http::FilterHeadersStatus AccessFilter::decodeHeaders(Http::RequestHeaderMap& he
269269
}
270270

271271
// Is there an Ingress policy?
272-
if (policy_fs->ingress_policy_name_.length() > 0) {
272+
if (!policy_fs->ingress_policy_name_.empty()) {
273273
allowed = policy_fs->enforceIngressHTTPPolicy(conn.ref(), destination_identity,
274274
destination_port, headers, *log_entry_);
275275

tests/bpf_metadata.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ TestConfig::extractSocketMetadata(Network::ConnectionSocket& socket) {
191191
port, l7proto);
192192

193193
return {Cilium::BpfMetadata::SocketMetadata(
194-
0, 0, source_identity, is_ingress_, is_l7lb_, port, std::move(pod_ip), "", nullptr, nullptr,
194+
0, source_identity, is_ingress_, is_l7lb_, port, std::move(pod_ip), "", nullptr, nullptr,
195195
nullptr, original_dst_address, shared_from_this(), 0, std::move(l7proto), "")};
196196
}
197197

tests/metadata_config_test.cc

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -300,13 +300,12 @@ TEST_F(MetadataConfigTest, NorthSouthL7LbMetadata) {
300300
auto policy_fs = socket_metadata->buildCiliumPolicyFilterState();
301301
EXPECT_NE(nullptr, policy_fs);
302302

303-
EXPECT_EQ(8, policy_fs->source_identity_);
303+
EXPECT_EQ(12345678, policy_fs->source_identity_);
304304
EXPECT_EQ(false, policy_fs->ingress_);
305305
EXPECT_EQ(true, policy_fs->is_l7lb_);
306306
EXPECT_EQ(80, policy_fs->port_);
307307
EXPECT_EQ("", policy_fs->pod_ip_);
308308
EXPECT_EQ("", policy_fs->ingress_policy_name_);
309-
EXPECT_EQ(0, policy_fs->ingress_source_identity_);
310309

311310
auto source_addresses_socket_option = socket_metadata->buildSourceAddressSocketOption(-1);
312311
EXPECT_NE(nullptr, source_addresses_socket_option);
@@ -341,13 +340,12 @@ TEST_F(MetadataConfigTest, NorthSouthL7LbIngressEnforcedMetadata) {
341340
auto policy_fs = socket_metadata->buildCiliumPolicyFilterState();
342341
EXPECT_NE(nullptr, policy_fs);
343342

344-
EXPECT_EQ(8, policy_fs->source_identity_);
343+
EXPECT_EQ(12345678, policy_fs->source_identity_);
345344
EXPECT_EQ(false, policy_fs->ingress_);
346345
EXPECT_EQ(true, policy_fs->is_l7lb_);
347346
EXPECT_EQ(80, policy_fs->port_);
348347
EXPECT_EQ("", policy_fs->pod_ip_);
349348
EXPECT_EQ("10.1.1.42", policy_fs->ingress_policy_name_);
350-
EXPECT_EQ(12345678, policy_fs->ingress_source_identity_);
351349

352350
AccessLog::Entry log_entry;
353351
log_entry.entry_.set_policy_name("pod");
@@ -411,13 +409,12 @@ TEST_F(MetadataConfigTest, NorthSouthL7LbPodAndIngressEnforcedMetadata) {
411409
auto policy_fs = socket_metadata->buildCiliumPolicyFilterState();
412410
EXPECT_NE(nullptr, policy_fs);
413411

414-
EXPECT_EQ(8, policy_fs->source_identity_);
412+
EXPECT_EQ(9999, policy_fs->source_identity_);
415413
EXPECT_EQ(false, policy_fs->ingress_);
416414
EXPECT_EQ(true, policy_fs->is_l7lb_);
417415
EXPECT_EQ(80, policy_fs->port_);
418416
EXPECT_EQ("192.168.1.1", policy_fs->pod_ip_);
419417
EXPECT_EQ("10.1.1.42", policy_fs->ingress_policy_name_);
420-
EXPECT_EQ(9999, policy_fs->ingress_source_identity_);
421418

422419
AccessLog::Entry log_entry;
423420
log_entry.entry_.set_policy_name("pod");
@@ -482,13 +479,12 @@ TEST_F(MetadataConfigTest, NorthSouthL7LbIngressEnforcedCIDRMetadata) {
482479
auto policy_fs = socket_metadata->buildCiliumPolicyFilterState();
483480
EXPECT_NE(nullptr, policy_fs);
484481

485-
EXPECT_EQ(8, policy_fs->source_identity_);
482+
EXPECT_EQ(2, policy_fs->source_identity_);
486483
EXPECT_EQ(false, policy_fs->ingress_);
487484
EXPECT_EQ(true, policy_fs->is_l7lb_);
488485
EXPECT_EQ(80, policy_fs->port_);
489486
EXPECT_EQ("", policy_fs->pod_ip_);
490487
EXPECT_EQ("10.1.1.42", policy_fs->ingress_policy_name_);
491-
EXPECT_EQ(2, policy_fs->ingress_source_identity_);
492488

493489
AccessLog::Entry log_entry;
494490
log_entry.entry_.set_policy_name("pod");
@@ -590,13 +586,12 @@ TEST_F(MetadataConfigTest, EastWestL7LbMetadataNoOriginalSource) {
590586
auto policy_fs = socket_metadata->buildCiliumPolicyFilterState();
591587
EXPECT_NE(nullptr, policy_fs);
592588

593-
EXPECT_EQ(8, policy_fs->source_identity_);
589+
EXPECT_EQ(111, policy_fs->source_identity_);
594590
EXPECT_EQ(false, policy_fs->ingress_);
595591
EXPECT_EQ(true, policy_fs->is_l7lb_);
596592
EXPECT_EQ(80, policy_fs->port_);
597593
EXPECT_EQ("10.1.1.1", policy_fs->pod_ip_);
598594
EXPECT_EQ("", policy_fs->ingress_policy_name_);
599-
EXPECT_EQ(0, policy_fs->ingress_source_identity_);
600595

601596
auto source_addresses_socket_option = socket_metadata->buildSourceAddressSocketOption(-1);
602597
EXPECT_NE(nullptr, source_addresses_socket_option);

0 commit comments

Comments
 (0)