Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new loading mechanism for GeoIP context #4158

Merged
merged 29 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
da48c7c
Add new loader to allow for later loading
balavinaithirthan Apr 25, 2024
5af7c14
Modify save() and versioning for new load
balavinaithirthan Apr 25, 2024
3c98e6e
Link tenzir/plugin submodule changes to main
balavinaithirthan Apr 26, 2024
c626dc2
Create new commit hash for tenzir plugins downstream of main
balavinaithirthan Apr 26, 2024
89a0440
Modify source.json for nix change of tenzir/plugins
balavinaithirthan Apr 26, 2024
7d4e0a9
Allow for automatic version 2 conversion
balavinaithirthan Apr 26, 2024
e3bfecd
Link nix build to tenzir plugin
balavinaithirthan Apr 26, 2024
f812bde
Change temp dir path for load
balavinaithirthan Apr 26, 2024
f088784
Update source and plugins links
balavinaithirthan Apr 26, 2024
be87c21
Update submodule linkage
balavinaithirthan Apr 28, 2024
979ae05
Merge remote-tracking branch 'origin/main' into topic/newGeoIpInputType
balavinaithirthan Apr 28, 2024
d5345d1
Create cache dir before tmp file
balavinaithirthan May 2, 2024
516b73c
Update tenzir-plugin linkage
balavinaithirthan May 2, 2024
765b410
Merge remote-tracking branch 'origin/main' into topic/newGeoIpInputType
balavinaithirthan May 2, 2024
d999e76
Add more thorough error message for tmp file writing
balavinaithirthan May 2, 2024
83b2905
Change flags for tmp file
balavinaithirthan May 2, 2024
661aed4
Remove dp_path from context and add real unique identifier
balavinaithirthan May 8, 2024
fa36752
Add new tenzir/plugin link
balavinaithirthan May 8, 2024
41148c4
Modify geoip header formats
balavinaithirthan May 8, 2024
0bc90b8
Merge remote-tracking branch 'origin/main' into topic/newGeoIpInputType
balavinaithirthan May 8, 2024
1beab8b
Add tenzir/plugin link
balavinaithirthan May 8, 2024
705f067
Remove debug statements
balavinaithirthan May 8, 2024
ae4b1c4
Link source.json
balavinaithirthan May 8, 2024
53287f0
Modify tmp dir creation for specifity
balavinaithirthan May 8, 2024
a0a95d0
Modify error handling of directory creation
balavinaithirthan May 8, 2024
6455e6a
Improve spacing and formatting
balavinaithirthan May 8, 2024
a1c4153
Add change log entry
balavinaithirthan May 8, 2024
622b4cc
Update change log format
balavinaithirthan May 8, 2024
65e9de2
Merge remote-tracking branch 'origin/main' into topic/newGeoIpInputType
balavinaithirthan May 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion contrib/tenzir-plugins
91 changes: 80 additions & 11 deletions libtenzir/builtins/contexts/geoip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
// SPDX-FileCopyrightText: (c) 2023 The VAST Contributors
// SPDX-License-Identifier: BSD-3-Clause

#include "tenzir/detail/posix.hpp"
#include "tenzir/logger.hpp"

balavinaithirthan marked this conversation as resolved.
Show resolved Hide resolved
#include <tenzir/arrow_table_slice.hpp>
#include <tenzir/data.hpp>
#include <tenzir/error.hpp>
Expand All @@ -22,9 +25,11 @@

#include <chrono>
#include <cstdint>
#include <cstdio>
#include <maxminddb.h>
#include <memory>
#include <string>
#include <utility>

namespace tenzir::plugins::geoip {

Expand All @@ -44,6 +49,11 @@ struct mmdb_deleter final {
using mmdb_ptr = std::unique_ptr<MMDB_s, mmdb_deleter>;

auto make_mmdb(const std::string& path) -> caf::expected<mmdb_ptr> {
if (!std::filesystem::exists(path)) {
return diagnostic::error("")
.note("failed to find path `{}`", path)
.to_error();
}
auto ptr = new MMDB_s;
const auto status = MMDB_open(path.c_str(), MMDB_MODE_MMAP, ptr);
if (status != MMDB_SUCCESS) {
Expand Down Expand Up @@ -302,6 +312,11 @@ class ctx final : public virtual context {
/// Emits context information for every event in `slice` in order.
auto apply(series array, bool replace) const
-> caf::expected<std::vector<series>> override {
if (!mmdb_) {
return caf::make_error(ec::lookup_error,
fmt::format("no GeoIP data currently exists for "
"this context"));
}
auto status = 0;
MMDB_entry_data_list_s* entry_data_list = nullptr;
auto builder = series_builder{};
Expand Down Expand Up @@ -475,6 +490,11 @@ class ctx final : public virtual context {
}

auto reset() -> caf::expected<void> override {
if (!mmdb_) {
return caf::make_error(ec::lookup_error,
fmt::format("no GeoIP data currently exists for "
"this context"));
balavinaithirthan marked this conversation as resolved.
Show resolved Hide resolved
}
auto mmdb = make_mmdb(db_path_);
if (not mmdb) {
return mmdb.error();
Expand All @@ -490,18 +510,19 @@ class ctx final : public virtual context {
}

auto save() const -> caf::expected<save_result> override {
auto builder = flatbuffers::FlatBufferBuilder{};
auto path = builder.CreateString(db_path_);
fbs::context::geoip::GeoIPDataBuilder geoip_builder(builder);
geoip_builder.add_url(path);
auto geoip_data = geoip_builder.Finish();
fbs::context::geoip::FinishGeoIPDataBuffer(builder, geoip_data);
return save_result{.data = tenzir::fbs::release(builder), .version = 1};
if (!mmdb_) {
return caf::make_error(ec::lookup_error,
fmt::format("no GeoIP data currently exists for "
"this context"));
}
return save_result{.data = std::move(chunk::mmap(db_path_).value()),
.version = latest_version};
balavinaithirthan marked this conversation as resolved.
Show resolved Hide resolved
}

private:
std::string db_path_;
balavinaithirthan marked this conversation as resolved.
Show resolved Hide resolved
mmdb_ptr mmdb_;
int latest_version = 2;
};

struct v1_loader : public context_loader {
Expand Down Expand Up @@ -531,9 +552,59 @@ struct v1_loader : public context_loader {
}
};

struct v2_loader : public context_loader {
explicit v2_loader(record global_config)
: global_config_{std::move(global_config)} {
}

auto version() const -> int {
return 2;
}

auto load(chunk_ptr serialized) const
-> caf::expected<std::unique_ptr<context>> {
const auto* cache_dir
= get_if<std::string>(&global_config_, "tenzir.cache-directory");
balavinaithirthan marked this conversation as resolved.
Show resolved Hide resolved
std::filesystem::create_directories(*cache_dir);
const auto current_time = std::time(nullptr);
std::string temp_file_name = *cache_dir + std::to_string(current_time);
auto temp_file = std::fstream(temp_file_name, std::ios_base::out);
dominiklohmann marked this conversation as resolved.
Show resolved Hide resolved
if (!temp_file) {
return caf::make_error(ec::filesystem_error,
fmt::format("failed to open temp file on "
"data load: {}",
detail::describe_errno()));
}
temp_file.write(reinterpret_cast<const char*>(serialized->data()),
static_cast<std::streamsize>(serialized->size()));
if (!temp_file) {
return caf::make_error(ec::filesystem_error,
fmt::format("failed write the temp file "
"on data load: {}",
detail::describe_errno()));
}
temp_file.close();
if (!temp_file) {
return caf::make_error(ec::filesystem_error,
fmt::format("failed close the temp file: {}",
detail::describe_errno()));
}
auto mmdb = make_mmdb(temp_file_name);
balavinaithirthan marked this conversation as resolved.
Show resolved Hide resolved
if (not mmdb) {
return mmdb.error();
}
return std::make_unique<ctx>(std::move(temp_file_name), std::move(*mmdb));
}

private:
const record global_config_;
};

class plugin : public virtual context_plugin {
auto initialize(const record&, const record&) -> caf::error override {
auto initialize(const record&, const record& global_config)
-> caf::error override {
register_loader(std::make_unique<v1_loader>());
register_loader(std::make_unique<v2_loader>(global_config));
return caf::none;
}

Expand All @@ -559,9 +630,7 @@ class plugin : public virtual context_plugin {
.to_error();
}
if (db_path.empty()) {
return diagnostic::error("missing required db-path option")
.usage("context create <name> geoip --db-path <path>")
.to_error();
return std::make_unique<ctx>(std::move(db_path), nullptr);
}
auto mmdb = make_mmdb(db_path);
if (not mmdb) {
Expand Down
5 changes: 3 additions & 2 deletions nix/tenzir/plugins/source.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "tenzir-plugins",
"url": "git@github.com:tenzir/tenzir-plugins",
"ref": "main",
"rev": "135363991f09bb9bb3682bbd81d145f7a2b2e60c",
"rev": "14ceb6f3f509c6ef99928004e3e6b51e6379757d",
"submodules": true,
"shallow": true
"shallow": true,
"allRefs": true
}