First release of open-appsec source code

This commit is contained in:
roybarda
2022-10-26 19:33:19 +03:00
parent 3883109caf
commit a883352f79
1353 changed files with 276290 additions and 1 deletions

View File

@@ -0,0 +1,49 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __AGENT_DETAILS_REPORTER_H__
#define __AGENT_DETAILS_REPORTER_H__
#include "singleton.h"
#include "component.h"
#include "i_agent_details_reporter.h"
#include "i_messaging.h"
#include "i_environment.h"
#include "i_mainloop.h"
#include "i_rest_api.h"
#include "component.h"
class AgentDetailsReporter
:
public Component,
Singleton::Provide<I_AgentDetailsReporter>,
Singleton::Consume<I_Messaging>,
Singleton::Consume<I_MainLoop>,
Singleton::Consume<I_Environment>,
Singleton::Consume<I_RestApi>
{
public:
AgentDetailsReporter();
~AgentDetailsReporter();
void preload() override;
void init() override;
void fini() override;
private:
class Impl;
std::unique_ptr<Impl> pimpl;
};
#endif // __AGENT_DETAILS_REPORTER_H__

View File

@@ -0,0 +1,95 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __HTTP_ENCODER_H__
#define __HTTP_ENCODER_H__
#include <string>
#include "messaging/http_core.h"
class HTTPRequest
{
public:
HTTPRequest() = default;
HTTPRequest(const std::string &_method_statement);
HTTPRequest(const std::string &_method_statement, const std::string &_host, const bool to_proxy);
HTTPRequest & insertHeader(const std::string &header_key, const std::string &header_val);
HTTPRequest & insertHeader(const std::string &header);
HTTPRequest & insertHeaders(const std::string &rec_headers);
HTTPRequest & insertBody(const std::string &body);
std::string toString() const;
private:
std::string method_statement;
HTTPHeaders headers;
std::string body;
};
class ConnectRequest: public HTTPRequest
{
public:
ConnectRequest(const std::string &_host, const std::string &_port);
};
class PostRequest: public HTTPRequest
{
public:
PostRequest(const std::string &_post_path, const std::string &_host, bool to_proxy);
};
class PutRequest : public HTTPRequest
{
public:
PutRequest(const std::string &_put_path, const std::string &_host, bool to_proxy);
};
class GetRequest: public HTTPRequest
{
public:
GetRequest(const std::string &_get_path, const std::string &_host, bool to_proxy);
};
class PatchRequest: public HTTPRequest
{
public:
PatchRequest(const std::string &_patch_path, const std::string &_host, bool to_proxy);
};
class HTTPEncoder
{
public:
HTTPEncoder(const std::string &_host, const std::string &_port);
HTTPRequest & Connect();
HTTPRequest & Post(const std::string &post_path);
HTTPRequest & Put(const std::string &put_path);
HTTPRequest & Patch(const std::string &patch_path);
HTTPRequest & Get(const std::string &get_path);
HTTPEncoder & isOverProxy();
HTTPEncoder & isOverSSL();
std::string build() const { return request.toString(); }
private:
HTTPRequest request;
std::string host;
std::string port;
bool over_ssl = false;
bool over_proxy = false;
};
#endif // __HTTP_ENCODER_H__

View File

@@ -0,0 +1,35 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __INSTANCE_AWARENESS_H__
#define __INSTANCE_AWARENESS_H__
#include <string>
#include <memory>
#include "singleton.h"
#include "i_instance_awareness.h"
#include "component.h"
class InstanceAwareness : public Component, public Singleton::Provide<I_InstanceAwareness>
{
public:
InstanceAwareness();
~InstanceAwareness();
private:
class Impl;
std::unique_ptr<Impl> pimpl;
};
#endif // __INSTANCE_AWARENESS_H__

View File

@@ -0,0 +1,41 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __IOCTL_IS_H__
#define __IOCTL_IS_H__
#include <memory>
#include "singleton.h"
#include "i_ioctl.h"
#include "i_mainloop.h"
#include "i_shell_cmd.h"
#include "component.h"
class IoctlIS
:
public Component,
Singleton::Provide<I_Ioctl>,
Singleton::Consume<I_MainLoop>,
Singleton::Consume<I_ShellCmd>
{
public:
IoctlIS();
~IoctlIS();
private:
class Impl;
std::unique_ptr<Impl> pimpl;
};
#endif // __IOCTL_IS_H__

View File

@@ -0,0 +1,96 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __MAINLOOP_METRIC_H__
#define __MAINLOOP_METRIC_H__
#include "generic_metric.h"
class MainloopEvent : public Event<MainloopEvent>
{
public:
void
setTimeSlice(int value)
{
time_slice_used = value;
}
void
setSleepTime(uint64_t value)
{
sleep_time_used = value;
}
void
setStressValue(uint32_t value)
{
current_stress_used = value;
}
uint32_t
getTimeSlice() const
{
return time_slice_used;
}
uint32_t
getSleepTime() const
{
return sleep_time_used;
}
uint32_t
getStressValue() const
{
return current_stress_used;
}
private:
uint32_t time_slice_used = 0;
uint64_t sleep_time_used = 0;
uint32_t current_stress_used = 0;
};
class MainloopMetric
:
public GenericMetric,
public Listener<MainloopEvent>
{
public:
void
upon(const MainloopEvent &event) override
{
max_time_slice.report(event.getTimeSlice());
avg_time_slice.report(event.getTimeSlice());
last_report_time_slice.report(event.getTimeSlice());
max_sleep_time.report(event.getSleepTime());
avg_sleep_time.report(event.getSleepTime());
last_report_sleep_time.report(event.getSleepTime());
max_stress_value.report(event.getStressValue());
avg_stress_value.report(event.getStressValue());
last_report_stress_value.report(event.getStressValue());
}
private:
MetricCalculations::Max<uint32_t> max_time_slice{this, "mainloopMaxTimeSliceSample", 0};
MetricCalculations::Average<double> avg_time_slice{this, "mainloopAvgTimeSliceSample"};
MetricCalculations::LastReportedValue<uint32_t> last_report_time_slice{this, "mainloopLastTimeSliceSample"};
MetricCalculations::Max<uint64_t> max_sleep_time{this, "mainloopMaxSleepTimeSample", 0};
MetricCalculations::Average<double> avg_sleep_time{this, "mainloopAvgSleepTimeSample"};
MetricCalculations::LastReportedValue<uint32_t> last_report_sleep_time{this, "mainloopLastSleepTimeSample"};
MetricCalculations::Max<uint32_t> max_stress_value{this, "mainloopMaxStressValueSample", 0};
MetricCalculations::Average<double> avg_stress_value{this, "mainloopAvgStressValueSample"};
MetricCalculations::LastReportedValue<uint32_t> last_report_stress_value{this, "mainloopLastStressValueSample"};
};
#endif // __MAINLOOP_METRIC_H__

View File

@@ -0,0 +1,52 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __MESSAGING_BUFFER_H__
#define __MESSAGING_BUFFER_H__
#include <memory>
#include "i_messaging_buffer.h"
#include "i_time_get.h"
#include "i_instance_awareness.h"
#include "singleton.h"
#include "i_encryptor.h"
#include "i_environment.h"
#include "i_mainloop.h"
#include "component.h"
class MessagingBuffer
:
public Component,
Singleton::Provide<I_MessagingBuffer>,
Singleton::Consume<I_TimeGet>,
Singleton::Consume<I_InstanceAwareness>,
Singleton::Consume<I_Encryptor>,
Singleton::Consume<I_Environment>,
Singleton::Consume<I_MainLoop>
{
public:
MessagingBuffer();
~MessagingBuffer();
void preload();
void init();
void fini();
private:
class Impl;
std::unique_ptr<Impl> pimpl;
};
#endif // __MESSAGING_BUFFER_H__

View File

@@ -0,0 +1,58 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __BUCKET_MANAGER_H__
#define __BUCKET_MANAGER_H__
#include <unordered_map>
#include <iterator>
#include <string>
#include "event_queue.h"
#include "instance_awareness.h"
#include "i_time_get.h"
#include "i_encryptor.h"
using bucketName = std::string;
class BucketManager
{
public:
void init(const std::string &service_name);
void fini();
bool doesExist(const bucketName &);
void push(const bucketName &, std::string &&);
bool handleNextBucket();
bool hasValue();
EventQueue & peek();
void flush();
private:
std::string resolveFilesName(const std::string &file_name);
std::string buffer_directory = "";
std::string next_bucket = "";
std::string service_name = "";
std::string management_file_path = "";
uint buffer_max_size = 0; // in MB
uint max_buffer_files = 0;
EventQueue iterator;
std::unordered_map<bucketName, EventQueue> buckets;
I_InstanceAwareness *instance_awareness = nullptr;
I_Encryptor *encryptor = nullptr;
};
#endif // __BUCKET_MANAGER_H__

View File

@@ -0,0 +1,125 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __EVENT_QUEUE_H__
#define __EVENT_QUEUE_H__
#include <boost/range/iterator_range.hpp>
#include <cstring>
#include <iostream>
#include <fstream>
#include <memory>
#include <list>
#include <chrono>
#include "i_time_get.h"
#include "maybe_res.h"
USE_DEBUG_FLAG(D_EVENT_BUFFER);
class EventQueueFile
{
public:
EventQueueFile(const std::string &_file_path)
:
file_path(_file_path)
{}
EventQueueFile(
const std::string &file_location_path,
const std::string &file_extention,
bool is_file_compressed);
EventQueueFile(const EventQueueFile &other_event, int _num_of_events_in_file, uint64_t _size_of_file)
:
file_path(other_event.file_path),
suffix(other_event.suffix),
num_of_events_in_file(_num_of_events_in_file),
size_of_file(_size_of_file)
{}
static const std::string zip_file_suffix;
const std::string & getFilePath() const { return file_path; }
bool isCompressed() const { return is_compressed; }
int getSuffix() const { return suffix; }
int getNumOfEvents() const { return num_of_events_in_file; }
uint64_t getFileSizeInBytes() const { return size_of_file; }
void restoreNumberOfLines();
void incFileSize(uint64_t size_to_add);
void handleCompression(int size_of_files_list);
void decompress(const std::string &infilename, const std::string &outfilename, bool remove_old = true);
void compress();
private:
std::string file_path;
int suffix = -1;
bool is_compressed = false;
int num_of_events_in_file = 0;
size_t size_of_file = 0;
};
class EventQueue
{
public:
EventQueue() = default;
~EventQueue();
void init(const std::string &path, uint max_buff_size);
void fini();
bool isEmpty() const;
const std::string & peek();
void push(std::string &&event_data);
void reloadEventsIntoList(const std::string &path);
Maybe<void> refreshBufferFile();
void refreshReadBuff();
void trim();
void flush();
private:
void rotate();
void updateReadFile();
void setReaderFileAndOpen(const EventQueueFile &file);
void sortEventFilesBySuffix(std::vector<EventQueueFile> &tmp_vec);
void pushNewEventQueueFile(EventQueueFile &eventFile, std::vector<EventQueueFile> &tmp_vec);
double getSizeMB(double size_in_B) const;
Maybe<void> writeCachesToFile();
void enforceMaxNumberOfFiles();
// File management
std::list<EventQueueFile> files; //front is write, back is read
std::ifstream reader;
std::ofstream writer;
// Read & write management
double max_size; // in MB
uint64_t size_on_disk; // in B
uint64_t write_cache_size; // in B
uint64_t read_cache_size; // in B
std::list<std::string> write_cache_buff;
std::list<std::string> read_cache_buff;
unsigned int num_of_events_on_disk;
unsigned int read_events_on_disk;
// Timing management
std::chrono::microseconds next_sync_freq_in_sec;
I_TimeGet *timer = nullptr;
bool is_pending_rotate = false;
bool is_pending_write = false;
};
#endif // __EVENT_QUEUE_H__

View File

@@ -0,0 +1,120 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __HTTP_REQUEST_EVENT_H__
#define __HTTP_REQUEST_EVENT_H__
#include <string>
#include "cereal/archives/json.hpp"
class HTTPRequestSignature
{
public:
HTTPRequestSignature() = default;
HTTPRequestSignature(const std::string &_method, const std::string &_url, const std::string &_tag)
:
method(_method),
url(_url),
tag(_tag)
{
}
bool
operator<(const HTTPRequestSignature &other) const
{
return getSignature() < other.getSignature();
}
std::string getSignature() const { return method + url + tag; }
const std::string & getMethod() const { return method; }
const std::string & getURL() const { return url; }
const std::string & getTag() const { return tag; }
template<class Archive>
void load(Archive &ar)
{
try {
ar(cereal::make_nvp("tag", tag));
} catch(...) {
tag = "buffered messages";
ar.setNextName(nullptr);
}
ar(method, url);
}
template<class Archive>
void save(Archive &ar) const
{
ar(cereal::make_nvp("tag", tag));
ar(method, url);
}
private:
std::string method;
std::string url;
std::string tag;
};
class HTTPRequestEvent : public HTTPRequestSignature
{
public:
HTTPRequestEvent() = default;
HTTPRequestEvent(
const std::string &_method,
const std::string &_url,
const std::string &_headers,
const std::string &&_body,
const std::string &_tag = "buffered messages")
:
HTTPRequestSignature(_method, _url, _tag),
headers(_headers),
body(std::move(_body))
{
}
HTTPRequestEvent(
const HTTPRequestSignature &&sig,
const std::string &_headers,
const std::string &&_body)
:
HTTPRequestSignature(std::move(sig)),
headers(_headers),
body(std::move(_body))
{
}
template<class Archive>
void load(Archive &ar)
{
HTTPRequestSignature::load(ar);
ar(headers, body);
}
template<class Archive>
void save(Archive &ar) const
{
HTTPRequestSignature::save(ar);
ar(headers, body);
}
const std::string & getHeaders() const { return headers; }
const std::string & getBody() const { return body; }
private:
std::string headers;
std::string body;
};
#endif // __HTTP_REQUEST_EVENT_H__

View File

@@ -0,0 +1,57 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __PROTO_MESSAGE_COMP_H__
#define __PROTO_MESSAGE_COMP_H__
#include <memory>
#include <string>
#include "singleton.h"
#include "i_mainloop.h"
#include "i_encryptor.h"
#include "i_messaging.h"
#include "i_agent_details.h"
#include "i_environment.h"
#include "i_rest_api.h"
#include "i_messaging_buffer.h"
#include "i_shell_cmd.h"
#include "component.h"
class ProtoMessageComp
:
public Component,
Singleton::Provide<I_Messaging>,
Singleton::Consume<I_MainLoop>,
Singleton::Consume<I_TimeGet>,
Singleton::Consume<I_AgentDetails>,
Singleton::Consume<I_Encryptor>,
Singleton::Consume<I_Environment>,
Singleton::Consume<I_MessagingBuffer>,
Singleton::Consume<I_ShellCmd>
{
public:
ProtoMessageComp();
~ProtoMessageComp();
void init();
void fini();
void preload();
private:
class Impl;
std::unique_ptr<Impl> pimpl;
};
#endif // __PROTO_MESSAGE_COMP_H__

View File

@@ -0,0 +1,43 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __SHELL_CMD_H__
#define __SHELL_CMD_H__
#include "i_shell_cmd.h"
#include "i_mainloop.h"
#include "i_time_get.h"
#include "singleton.h"
#include "component.h"
class ShellCmd
:
public Component,
Singleton::Provide<I_ShellCmd>,
Singleton::Consume<I_MainLoop>,
Singleton::Consume<I_TimeGet>
{
public:
ShellCmd();
~ShellCmd();
void preload() override;
void init() override;
void fini() override;
private:
class Impl;
std::unique_ptr<Impl> pimpl;
};
#endif // __SHELL_CMD_H__

View File

@@ -0,0 +1,45 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __TRAP_HANDLER_H__
#define __TRAP_HANDLER_H__
#include <memory>
#include "singleton.h"
#include "i_trap_handler.h"
#include "i_ioctl.h"
#include "i_mainloop.h"
#include "component.h"
class TrapHandler
:
public Component,
Singleton::Provide<I_TrapHandler>,
Singleton::Consume<I_Ioctl>,
Singleton::Consume<I_MainLoop>,
Singleton::Consume<I_TrapHandler>
{
public:
TrapHandler();
~TrapHandler();
void init();
void fini();
private:
class Impl;
std::unique_ptr<Impl> pimpl;
};
#endif // __TRAP_HANDLER_H__