Jan_31_2024-Dev

This commit is contained in:
Ned Wright
2024-01-31 17:34:53 +00:00
parent 752a5785f0
commit 6d67818a94
376 changed files with 8101 additions and 7064 deletions

View File

@@ -0,0 +1,63 @@
// 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 __BUFFERED_MESSAGE_H__
#define __BUFFERED_MESSAGE_H__
#include <iostream>
#include <sstream>
#include <string>
#include "i_messaging.h"
#include "cereal/archives/json.hpp"
#include "cereal/types/string.hpp"
#include "maybe_res.h"
class BufferedMessage
{
public:
BufferedMessage() {}
BufferedMessage(
std::string _body,
HTTPMethod _method,
std::string _uri,
MessageCategory _category,
MessageMetadata _message_metadata
) :
body(_body), method(_method), uri(_uri), category(_category), message_metadata(_message_metadata)
{}
void save(cereal::JSONOutputArchive &out_ar) const;
void load(cereal::JSONInputArchive &archive_in);
std::string toString() const;
bool operator==(const BufferedMessage &other) const;
const std::string &getBody() const;
const std::string &getURI() const;
HTTPMethod getMethod() const;
MessageCategory getCategory() const;
const MessageMetadata &getMessageMetadata() const;
private:
std::string body;
HTTPMethod method;
std::string uri;
MessageCategory category;
MessageMetadata message_metadata;
uint16_t retries_number = 0;
};
#endif // __BUFFERED_MESSAGE_H__

View File

@@ -0,0 +1,75 @@
// 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 __CONNECTION_H__
#define __CONNECTION_H__
#include <map>
#include <memory>
#include <string>
#include "i_agent_details.h"
#include "i_environment.h"
#include "i_mainloop.h"
#include "i_time_get.h"
#include "messaging/http_response.h"
#include "messaging/messaging_metadata.h"
#include "maybe_res.h"
class MessageConnectionKey
{
public:
MessageConnectionKey() {}
MessageConnectionKey(const std::string &_host_name, uint16_t _port, MessageCategory _category) :
host_name(_host_name), port(_port), category(_category)
{}
const std::string & getHostName() const;
uint16_t getPort() const;
const MessageCategory & getCategory() const;
bool operator<(const MessageConnectionKey &other) const;
private:
std::string host_name;
uint16_t port;
MessageCategory category;
};
class Connection
{
public:
Connection(const MessageConnectionKey &conn_key, const MessageMetadata &metadata);
~Connection();
Maybe<void> setProxySettings(const MessageProxySettings &settings);
void setExternalCertificate(const std::string &certificate);
const MessageProxySettings & getProxySettings() const;
const std::string & getExternalCertificate() const;
const MessageConnectionKey &getConnKey() const;
bool isOverProxy() const;
bool isUnsecure() const;
bool isSuspended();
Maybe<void> establishConnection();
Maybe<HTTPResponse, HTTPResponse> sendRequest(const std::string &request);
private:
class Impl;
std::shared_ptr<Impl> pimpl;
};
#endif // __CONNECTION_H__

View File

@@ -0,0 +1,42 @@
// 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 __CONNECTION_COMPONENT_H__
#define __CONNECTION_COMPONENT_H__
#include <map>
#include <string>
#include "i_agent_details.h"
#include "i_encryptor.h"
#include "i_environment.h"
#include "i_mainloop.h"
#include "i_messaging.h"
#include "i_time_get.h"
#include "interfaces/i_messaging_connection.h"
#include "maybe_res.h"
class ConnectionComponent : Singleton::Provide<I_MessagingConnection>
{
public:
ConnectionComponent();
~ConnectionComponent();
void init();
private:
class Impl;
std::unique_ptr<Impl> pimpl;
};
#endif // __CONNECTION_COMPONENT_H__

View File

@@ -0,0 +1,68 @@
// 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_H__
#define __HTTP_REQUEST_H__
#include <map>
#include <string>
#include "connection.h"
#include "i_agent_details.h"
#include "i_encryptor.h"
#include "i_messaging.h"
class HTTPRequest
{
public:
static Maybe<HTTPRequest> prepareRequest(
const Connection &conn,
HTTPMethod method,
const std::string &uri,
const std::map<std::string, std::string> &headers,
const std::string &body
);
Maybe<void> setConnectionHeaders(const Connection &conn, bool is_access_token_needed);
bool
isConnect() const
{
return method == HTTPMethod::CONNECT;
}
std::string toString() const;
private:
HTTPRequest(
HTTPMethod _method,
const std::string &_uri,
const std::map<std::string, std::string> &_headers,
const std::string &_body
) :
body(_body), uri(_uri), headers(_headers), method(_method)
{}
bool setConnectionHeaders(const Connection &conn);
void insertHeader(const std::string &header_key, const std::string &header_val);
Maybe<void> addAccessToken(const Connection &conn, bool is_registration);
Maybe<void> addProxyAuthorization(const Connection &conn);
std::string body;
std::string uri;
std::string method_statement;
std::map<std::string, std::string> headers;
HTTPMethod method;
};
#endif // __HTTP_REQUEST_H__

View File

@@ -0,0 +1,124 @@
// 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"
// LCOV_EXCL_START
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;
};
// LCOV_EXCL_STOP
#endif // __HTTP_REQUEST_EVENT_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 __I_MESSAGING_BUFFER_H__
#define __I_MESSAGING_BUFFER_H__
#include <iostream>
#include <sstream>
#include <string>
#include "i_messaging.h"
#include "../buffered_message.h"
#include "cereal/archives/json.hpp"
#include "cereal/types/string.hpp"
#include "maybe_res.h"
class I_MessageBuffer
{
public:
virtual void pushNewBufferedMessage(
const std::string &body,
HTTPMethod method,
const std::string &uri,
MessageCategory category,
MessageMetadata message_metadata,
bool force_immediate_writing
) = 0;
virtual Maybe<BufferedMessage> peekMessage() = 0;
virtual void popMessage() = 0;
virtual void cleanBuffer() = 0;
};
#endif // __I_MESSAGING_BUFFER_H__

View File

@@ -0,0 +1,42 @@
// 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 __I_MESSAGING_CONNECTION_H__
#define __I_MESSAGING_CONNECTION_H__
#include <map>
#include <string>
#include "http_request.h"
#include "i_messaging.h"
#include "connection.h"
#include "maybe_res.h"
class I_MessagingConnection
{
public:
virtual Maybe<Connection> establishConnection(const MessageMetadata &metadata, MessageCategory category) = 0;
virtual Maybe<Connection> getPersistentConnection(
const std::string &host_name, uint16_t port, MessageCategory category
) = 0;
virtual Maybe<Connection> getFogConnectionByCategory(MessageCategory category) = 0;
virtual Maybe<HTTPResponse, HTTPResponse> sendRequest(Connection &connection, HTTPRequest request) = 0;
protected:
virtual ~I_MessagingConnection() {}
};
#endif // __I_MESSAGING_CONNECTION_H__

View File

@@ -0,0 +1,37 @@
// 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 __MESSAGNIG_BUFFER_H__
#define __MESSAGNIG_BUFFER_H__
#include <map>
#include <string>
#include "i_environment.h"
#include "interfaces/i_messaging_buffer.h"
#include "singleton.h"
class MessagingBufferComponent : Singleton::Provide<I_MessageBuffer>
{
public:
MessagingBufferComponent();
~MessagingBufferComponent();
void init();
private:
class Impl;
std::unique_ptr<Impl> pimpl;
};
#endif // __MESSAGNIG_BUFFER_H__

View File

@@ -0,0 +1,101 @@
// 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 __MESSAGNIG_COMP_H__
#define __MESSAGNIG_COMP_H__
#include "messaging.h"
#include <fstream>
#include <map>
#include <ostream>
#include <sstream>
#include <string>
#include "cache.h"
#include "connection.h"
#include "connection_comp.h"
#include "flags.h"
#include "interfaces/i_messaging_buffer.h"
#include "interfaces/i_messaging_connection.h"
#include "maybe_res.h"
#include "messaging_buffer.h"
#include "singleton.h"
class MessagingComp
{
public:
void init();
Maybe<HTTPResponse, HTTPResponse> sendSyncMessage(
HTTPMethod method,
const std::string &uri,
const std::string &body,
MessageCategory category,
const MessageMetadata &message_metadata
);
void sendAsyncMessage(
HTTPMethod method,
const std::string &uri,
const std::string &body,
MessageCategory category,
const MessageMetadata &message_metadata
);
Maybe<HTTPStatusCode, HTTPResponse> downloadFile(
HTTPMethod method,
const std::string &uri,
const std::string &download_file_path,
MessageCategory category = MessageCategory::GENERIC,
const MessageMetadata &message_metadata = MessageMetadata()
);
Maybe<HTTPStatusCode, HTTPResponse> uploadFile(
const std::string &uri,
const std::string &upload_file_path,
MessageCategory category,
const MessageMetadata &message_metadata
);
bool setFogConnection(const std::string &host, uint16_t port, bool is_secure, MessageCategory category);
bool setFogConnection(MessageCategory category);
private:
Maybe<Connection> getConnection(MessageCategory category, const MessageMetadata &message_metadata);
Maybe<Connection> getPersistentConnection(const MessageMetadata &metadata, MessageCategory category) const;
Maybe<HTTPResponse, HTTPResponse> sendMessage(
HTTPMethod method,
const std::string &uri,
const std::string &body,
MessageCategory category,
const MessageMetadata &message_metadata
);
Maybe<HTTPResponse, HTTPResponse> suspendMessage(
const std::string &body,
HTTPMethod method,
const std::string &uri,
MessageCategory category,
const MessageMetadata &message_metadata
) const;
I_MessagingConnection *i_conn;
I_MessageBuffer *i_messaging_buffer;
I_AgentDetails *agent_details;
bool should_buffer_failed_messages;
TemporaryCache<std::string, HTTPResponse> fog_get_requests_cache;
};
#endif //__MESSAGNIG_COMP_H__

View File

@@ -0,0 +1,25 @@
#ifndef __MOCK_MESSAGING_BUFFER_H__
#define __MOCK_MESSAGING_BUFFER_H__
#include "cptest.h"
#include "interfaces/i_messaging_buffer.h"
// LCOV_EXCL_START Reason: No need to test mocks
class MockMessagingBuffer : public Singleton::Provide<I_MessageBuffer>::From<MockProvider<I_MessageBuffer>>
{
public:
using string = std::string;
MOCK_METHOD6(
pushNewBufferedMessage,
void(const std::string &, HTTPMethod, const std::string &, MessageCategory, MessageMetadata, bool)
);
MOCK_METHOD0(peekMessage, Maybe<BufferedMessage>());
MOCK_METHOD0(popMessage, void());
MOCK_METHOD0(cleanBuffer, void());
};
// LCOV_EXCL_STOP
#endif // __MOCK_MESSAGING_BUFFER_H__

View File

@@ -0,0 +1,38 @@
#ifndef __MOCK_MESSAGING_CONNECTION_H__
#define __MOCK_MESSAGING_CONNECTION_H__
#include "cptest.h"
#include "interfaces/i_messaging_connection.h"
// LCOV_EXCL_START Reason: No need to test mocks
class MockMessagingConnection :
public Singleton::Provide<I_MessagingConnection>::From<MockProvider<I_MessagingConnection>>
{
public:
using string = std::string;
MOCK_METHOD2(establishConnection, Maybe<Connection>(const MessageMetadata &, MessageCategory));
MOCK_METHOD3(
establishNewConnection,
Maybe<Connection>(MessageConnectionKey, Flags<MessageConnectionConfig>, const string &)
);
MOCK_METHOD2(establishNewProxyConnection, Maybe<Connection>(Flags<MessageConnectionConfig>, MessageProxySettings));
Maybe<HTTPResponse, HTTPResponse>
sendRequest(Connection &conn, HTTPRequest req)
{
return mockSendRequest(conn, req, false);
}
MOCK_METHOD3(mockSendRequest, Maybe<HTTPResponse, HTTPResponse>(Connection &, HTTPRequest, bool));
MOCK_METHOD3(getPersistentConnection, Maybe<Connection>(const string &, uint16_t, MessageCategory));
MOCK_METHOD1(getFogConnectionByCategory, Maybe<Connection>(MessageCategory));
};
// LCOV_EXCL_STOP
#endif // __MOCK_MESSAGING_CONNECTION_H__

View File

@@ -0,0 +1,34 @@
#ifndef __RESPONSE_PARSER_H__
#define __RESPONSE_PARSER_H__
#include "i_messaging.h"
class HTTPResponseParser
{
public:
Maybe<HTTPResponse> parseData(const std::string &data, bool is_connect);
bool
hasReachedError() const
{
return error;
}
private:
bool parseStatusLine();
bool handleHeaders();
bool handleBody(bool is_connect);
Maybe<std::string> getHeaderVal(const std::string &header_key);
bool getChunkedResponse();
bool isLegalChunkedResponse(const std::string &res);
Maybe<HTTPStatusCode> status_code = genError("Not received");
Maybe<std::map<std::string, std::string>> headers = genError("Not received");
std::string body;
std::string raw_response;
bool error = false;
};
#endif // __RESPONSE_PARSER_H__

View File

@@ -0,0 +1,75 @@
// 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 __SMART_BIO_H__
#define __SMART_BIO_H__
#include "openssl/bio.h"
#include "openssl/err.h"
#include "openssl/ssl.h"
#include "openssl/x509v3.h"
namespace smartBIO
{
template <class T>
struct Destrctor;
template <>
struct Destrctor<BIO>
{
void
operator()(BIO *pointer) const
{
if (pointer != nullptr) BIO_free_all(pointer);
}
};
// LCOV_EXCL_START Reason: No ssl ut
template <>
struct Destrctor<SSL_CTX>
{
void
operator()(SSL_CTX *pointer) const
{
if (pointer != nullptr) SSL_CTX_free(pointer);
}
};
template <>
struct Destrctor<X509>
{
void
operator()(X509 *pointer) const
{
if (pointer != nullptr) X509_free(pointer);
}
};
template <>
struct Destrctor<EVP_PKEY>
{
void
operator()(EVP_PKEY *pointer) const
{
if (pointer != nullptr) EVP_PKEY_free(pointer);
}
};
// LCOV_EXCL_STOP
template <class OpenSSLType>
using BioUniquePtr = std::unique_ptr<OpenSSLType, smartBIO::Destrctor<OpenSSLType>>;
} // namespace smartBIO
#endif // __SMART_BIO_H__