1
#include "tests/cilium_tcp_integration.h"
2

            
3
#include <fmt/base.h>
4
#include <fmt/format.h>
5
#include <spdlog/common.h>
6

            
7
#include <memory>
8
#include <string>
9

            
10
#include "envoy/network/address.h"
11

            
12
#include "source/common/common/base_logger.h"
13
#include "source/common/common/logger.h"
14
#include "source/common/network/address_impl.h"
15

            
16
#include "test/integration/base_integration_test.h"
17
#include "test/test_common/environment.h"
18
#include "test/test_common/network_utility.h"
19

            
20
#include "tests/bpf_metadata.h"
21

            
22
namespace Envoy {
23

            
24
const std::string TCP_POLICY_fmt = R"EOF(version_info: "0"
25
resources:
26
- "@type": type.googleapis.com/cilium.NetworkPolicy
27
  endpoint_ips:
28
  - '{{ ntop_ip_loopback_address }}'
29
  policy: 3
30
  ingress_per_port_policies:
31
  - port: {0}
32
    rules:
33
    - remote_policies: [ 1 ]
34
      l7_proto: "test.passer"
35
  egress_per_port_policies:
36
  - port: {0}
37
    rules:
38
    - remote_policies: [ 1 ]
39
      l7_proto: "test.passer"
40
)EOF";
41

            
42
CiliumTcpIntegrationTest::CiliumTcpIntegrationTest(const std::string& config)
43
40
    : BaseIntegrationTest(GetParam(), config),
44
40
      accessLogServer_(TestEnvironment::unixDomainSocketPath("access_log.sock")) {
45
40
  enableHalfClose(true);
46
40
#if 1
47
2640
  for (Logger::Logger& logger : Logger::Registry::loggers()) {
48
2640
    logger.setLevel(spdlog::level::trace);
49
2640
  }
50
40
#endif
51
40
}
52

            
53
7
std::string CiliumTcpIntegrationTest::testPolicyFmt() {
54
7
  return TestEnvironment::substitute(TCP_POLICY_fmt, GetParam());
55
7
}
56

            
57
40
void CiliumTcpIntegrationTest::createEnvoy() {
58
  // fake upstreams have been created by now, use the port from the 1st upstream
59
  // in policy.
60
40
  auto port = fake_upstreams_[0]->localAddress()->ip()->port();
61
40
  policy_config = fmt::format(fmt::runtime(testPolicyFmt()), port);
62
  // Pass the fake upstream address to the cilium bpf filter that will set it as
63
  // an "original destination address".
64
40
  if (GetParam() == Network::Address::IpVersion::v4) {
65
40
    original_dst_address = std::make_shared<Network::Address::Ipv4Instance>(
66
40
        Network::Test::getLoopbackAddressString(GetParam()), port);
67
40
  } else {
68
    original_dst_address = std::make_shared<Network::Address::Ipv6Instance>(
69
        Network::Test::getLoopbackAddressString(GetParam()), port);
70
  }
71
40
  BaseIntegrationTest::createEnvoy();
72
40
}
73

            
74
40
void CiliumTcpIntegrationTest::initialize() {
75
40
  config_helper_.renameListener("tcp_proxy");
76
40
  BaseIntegrationTest::initialize();
77
40
}
78

            
79
} // namespace Envoy