Skip to content

Commit ba9f654

Browse files
committed
TCP input TLS - Use TlsDecoder in DecoderFactory.
1 parent 74fb9b5 commit ba9f654

File tree

6 files changed

+63
-4
lines changed

6 files changed

+63
-4
lines changed

src/plugins/input/tcp/src/ClientManager.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ class ClientManager {
3333
*/
3434
ClientManager(ipx_ctx_t *ctx, DecoderFactory factory);
3535

36+
/**
37+
* @brief Initialize TLS. This is separately from constructor because it may prompt the user for
38+
* password for private key.
39+
* @param conf Configuration for the initialization.
40+
*/
41+
void initialize_tls(const Config &conf) { m_factory.initialize_tls(conf); }
42+
3643
/**
3744
* @brief Adds connection to the vector and epoll.
3845
* @param fd file descriptor of the new tcp connection.

src/plugins/input/tcp/src/DecoderFactory.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@
2626
#include "Decoder.hpp" // Decoder
2727
#include "Lz4Decoder.hpp" // LZ4_MAGIC, Lz4Decoder
2828
#include "IpfixDecoder.hpp" // IPFIX_MAGIC, IpfixDecoder
29+
#include "tls/TlsDecoder.hpp"
2930

3031
#include <iostream>
3132

3233
namespace tcp_in {
3334

34-
DecoderFactory::DecoderFactory() {};
35+
DecoderFactory::DecoderFactory(ipx_ctx_t *ctx) : m_ctx(ctx) {
36+
// TLS is initialized separately because it may prompt the user.
37+
};
3538

3639
std::unique_ptr<Decoder> DecoderFactory::detect_decoder(int fd) {
3740
// number of bytes neaded to detect the decoder
@@ -48,14 +51,26 @@ std::unique_ptr<Decoder> DecoderFactory::detect_decoder(int fd) {
4851
if (res == -1) {
4952
const char *err_msg;
5053
ipx_strerror(errno, err_msg);
51-
throw std::runtime_error("Failed to receive start of first message: " + std::string(err_msg));
54+
throw std::runtime_error(
55+
"Failed to receive start of first message: " + std::string(err_msg)
56+
);
5257
}
5358

5459
constexpr const char *not_enough_data_err =
5560
"Failed to read enough bytes to recognize the decoder";
5661

5762
// check decoders in order from shortest magic number to longest
5863

64+
if (res < 1) {
65+
throw std::runtime_error(not_enough_data_err);
66+
}
67+
68+
// TLS decoder
69+
auto magic_u8 = buf[0];
70+
if (magic_u8 == tls::TLS_MAGIC) {
71+
return create_tls_decoder(fd);
72+
}
73+
5974
if (res < 2) {
6075
throw std::runtime_error(not_enough_data_err);
6176
}
@@ -79,6 +94,15 @@ std::unique_ptr<Decoder> DecoderFactory::detect_decoder(int fd) {
7994
throw std::runtime_error("Failed to recognize the decoder.");
8095
}
8196

97+
void DecoderFactory::initialize_tls(const Config &conf) {
98+
if (!conf.certificate_file.empty()) {
99+
IPX_CTX_INFO(m_ctx, "Initializing TLS decoder.");
100+
m_tls_factory = std::unique_ptr<tls::DecoderFactory>(new tls::DecoderFactory(conf));
101+
} else {
102+
IPX_CTX_INFO(m_ctx, "TLS Decoder is disabled.");
103+
}
104+
}
105+
82106
std::unique_ptr<Decoder> DecoderFactory::create_ipfix_decoder(int fd) {
83107
return std::unique_ptr<Decoder>(new IpfixDecoder(fd));
84108
}
@@ -87,5 +111,12 @@ std::unique_ptr<Decoder> DecoderFactory::create_lz4_decoder(int fd) {
87111
return std::unique_ptr<Decoder>(new Lz4Decoder(fd));
88112
}
89113

114+
std::unique_ptr<Decoder> DecoderFactory::create_tls_decoder(int fd) {
115+
if (!m_tls_factory) {
116+
throw std::runtime_error("TLS decoder is not enabled.");
117+
}
118+
return m_tls_factory->create(fd);
119+
}
120+
90121
} // namespace tcp_in
91122

src/plugins/input/tcp/src/DecoderFactory.hpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@
1212

1313
#include <memory> // std::unique_ptr
1414

15+
#include "Config.hpp"
1516
#include "Decoder.hpp" // Decoder
17+
#include "tls/DecoderFactory.hpp"
1618

1719
namespace tcp_in {
1820

1921
/** Factory for TCP decoders. */
2022
class DecoderFactory {
2123
public:
22-
DecoderFactory();
24+
DecoderFactory(ipx_ctx_t *ctx);
2325

2426
/**
2527
* @brief Detects the type of decoder that should be used to decode the given stream and
@@ -31,9 +33,20 @@ class DecoderFactory {
3133
*/
3234
std::unique_ptr<Decoder> detect_decoder(int fd);
3335

36+
/**
37+
* @brief Initialize TLS. This is separately from constructor because it may prompt the user for
38+
* password for private key.
39+
* @param conf Configuration for the initialization.
40+
*/
41+
void initialize_tls(const Config &conf);
42+
3443
private:
3544
std::unique_ptr<Decoder> create_ipfix_decoder(int fd);
3645
std::unique_ptr<Decoder> create_lz4_decoder(int fd);
46+
std::unique_ptr<Decoder> create_tls_decoder(int fd);
47+
48+
ipx_ctx_t *m_ctx;
49+
std::unique_ptr<tls::DecoderFactory> m_tls_factory;
3750
};
3851

3952
} // namespace tcp_in

src/plugins/input/tcp/src/IpxPlugin.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ int ipx_plugin_init(ipx_ctx_t *ctx, const char *params) {
3838
try {
3939
Config conf(ctx, params);
4040
plugin = new Plugin(ctx, conf);
41+
plugin->initialize_tls(conf); // This may prompt the user for password for private key.
4142
} catch (std::exception &ex) {
4243
IPX_CTX_ERROR(ctx, "%s", ex.what());
4344
return IPX_ERR_DENIED;

src/plugins/input/tcp/src/Plugin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace tcp_in {
2424

2525
Plugin::Plugin(ipx_ctx_t *ctx, Config &config) :
2626
m_ctx(ctx),
27-
m_clients(ctx, DecoderFactory()),
27+
m_clients(ctx, DecoderFactory(ctx)),
2828
m_acceptor(m_clients, ctx)
2929
{
3030
m_acceptor.bind_addresses(config);

src/plugins/input/tcp/src/Plugin.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ class Plugin {
3030
*/
3131
Plugin(ipx_ctx_t *ctx, Config &config);
3232

33+
/**
34+
* @brief Initialize TLS. This is separately from constructor because it may prompt the user for
35+
* password for private key.
36+
* @param conf Configuration for the initialization.
37+
*/
38+
void initialize_tls(const Config &conf) { m_clients.initialize_tls(conf); }
39+
3340
// force that Plugin stays in its original memory (so that reference to `m_clients` in acceptor
3441
// stays valid)
3542
Plugin(const Plugin &) = delete;

0 commit comments

Comments
 (0)