mirror of
https://github.com/openappsec/openappsec.git
synced 2025-09-29 19:24:26 +03:00
Aug_23_2023-Dev
This commit is contained in:
@@ -22,8 +22,8 @@
|
||||
#include <memory>
|
||||
#include <iostream>
|
||||
|
||||
#include "debug.h"
|
||||
#include "time_print.h"
|
||||
#include "debug.h"
|
||||
#include "singleton.h"
|
||||
#include "context.h"
|
||||
#include "table/table_helpers.h"
|
||||
|
@@ -25,8 +25,13 @@ public:
|
||||
:
|
||||
tenant_id(_tenant_id),
|
||||
profile_id(_profile_id)
|
||||
{
|
||||
}
|
||||
{}
|
||||
|
||||
TenantProfilePair(const std::pair<std::string, std::string> &tenant_profile_pair)
|
||||
:
|
||||
tenant_id(tenant_profile_pair.first),
|
||||
profile_id(tenant_profile_pair.second)
|
||||
{}
|
||||
|
||||
size_t
|
||||
hash() const
|
||||
|
@@ -26,6 +26,7 @@
|
||||
#include "i_rest_api.h"
|
||||
#include "i_messaging_buffer.h"
|
||||
#include "i_shell_cmd.h"
|
||||
#include "i_proxy_configuration.h"
|
||||
#include "component.h"
|
||||
|
||||
class ProtoMessageComp
|
||||
@@ -38,7 +39,8 @@ class ProtoMessageComp
|
||||
Singleton::Consume<I_Encryptor>,
|
||||
Singleton::Consume<I_Environment>,
|
||||
Singleton::Consume<I_MessagingBuffer>,
|
||||
Singleton::Consume<I_ShellCmd>
|
||||
Singleton::Consume<I_ShellCmd>,
|
||||
Singleton::Consume<I_ProxyConfiguration>
|
||||
{
|
||||
public:
|
||||
ProtoMessageComp();
|
||||
|
@@ -38,12 +38,14 @@ public:
|
||||
virtual std::string getProfileId() const = 0;
|
||||
|
||||
// Agent Details
|
||||
virtual Maybe<std::string> getProxy() const = 0;
|
||||
virtual void setProxy(const std::string &_proxy) = 0;
|
||||
virtual void setAgentId(const std::string &_agent_id) = 0;
|
||||
virtual std::string getAgentId() const = 0;
|
||||
virtual Maybe<std::string> getProxy() const = 0;
|
||||
virtual void setProxy(const std::string &_proxy) = 0;
|
||||
virtual void setAgentId(const std::string &_agent_id) = 0;
|
||||
virtual std::string getAgentId() const = 0;
|
||||
virtual void setOrchestrationMode(OrchestrationMode _orchstration_mode) = 0;
|
||||
virtual OrchestrationMode getOrchestrationMode() const = 0;
|
||||
virtual OrchestrationMode getOrchestrationMode() const = 0;
|
||||
virtual std::string getAccessToken() const = 0;
|
||||
virtual void loadAccessToken() = 0;
|
||||
|
||||
// OpenSSL
|
||||
virtual void setOpenSSLDir(const std::string &openssl_dir) = 0;
|
||||
|
@@ -31,12 +31,6 @@
|
||||
|
||||
USE_DEBUG_FLAG(D_COMMUNICATION);
|
||||
|
||||
enum class ProxyProtocol
|
||||
{
|
||||
HTTP,
|
||||
HTTPS
|
||||
};
|
||||
|
||||
enum class MessageTypeTag
|
||||
{
|
||||
GENERIC,
|
||||
@@ -141,16 +135,8 @@ public:
|
||||
return genError("Failed to download file. Error: " + response.getErr());
|
||||
}
|
||||
|
||||
virtual Maybe<std::string> getProxyDomain(ProxyProtocol protocol) const = 0;
|
||||
virtual Maybe<std::string> getProxyCredentials(ProxyProtocol protocol) const = 0;
|
||||
virtual Maybe<uint16_t> getProxyPort(ProxyProtocol protocol) const = 0;
|
||||
virtual bool getProxyExists(ProxyProtocol protocol) const = 0;
|
||||
virtual Maybe<std::string> getProxyAddress(ProxyProtocol protocol) const = 0;
|
||||
virtual Maybe<void> loadProxy() = 0;
|
||||
virtual bool setActiveFog(MessageTypeTag tag) = 0;
|
||||
virtual void loadAccessToken() = 0;
|
||||
virtual bool setActiveFog(const string &host, const uint16_t port, bool is_secure, MessageTypeTag tag) = 0;
|
||||
virtual std::string getAccessToken() = 0;
|
||||
|
||||
protected:
|
||||
~I_Messaging() {}
|
||||
|
38
core/include/services_sdk/interfaces/i_proxy_configuration.h
Normal file
38
core/include/services_sdk/interfaces/i_proxy_configuration.h
Normal file
@@ -0,0 +1,38 @@
|
||||
// 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_PROXY_CONFIGURATION_H__
|
||||
#define __I_PROXY_CONFIGURATION_H__
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "maybe_res.h"
|
||||
|
||||
enum class ProxyProtocol
|
||||
{
|
||||
HTTP,
|
||||
HTTPS
|
||||
};
|
||||
|
||||
class I_ProxyConfiguration
|
||||
{
|
||||
public:
|
||||
virtual Maybe<std::string> getProxyDomain(ProxyProtocol protocol) const = 0;
|
||||
virtual Maybe<std::string> getProxyCredentials(ProxyProtocol protocol) const = 0;
|
||||
virtual Maybe<uint16_t> getProxyPort(ProxyProtocol protocol) const = 0;
|
||||
virtual bool getProxyExists(ProxyProtocol protocol) const = 0;
|
||||
virtual Maybe<std::string> getProxyAddress(ProxyProtocol protocol) const = 0;
|
||||
virtual Maybe<void> loadProxy() = 0;
|
||||
};
|
||||
|
||||
#endif // __I_PROXY_CONFIGURATION_H__
|
@@ -26,6 +26,8 @@ public:
|
||||
MOCK_METHOD1(setProxy, void(const std::string&));
|
||||
MOCK_METHOD1(setAgentId, void(const std::string&));
|
||||
MOCK_CONST_METHOD0(getAgentId, std::string());
|
||||
MOCK_METHOD0(loadAccessToken, void());
|
||||
MOCK_CONST_METHOD0(getAccessToken, std::string());
|
||||
|
||||
// OpenSSL
|
||||
MOCK_METHOD1(setOpenSSLDir, void(const std::string&));
|
||||
|
@@ -57,20 +57,12 @@ public:
|
||||
)
|
||||
);
|
||||
|
||||
MOCK_METHOD0(loadAccessToken, void());
|
||||
MOCK_METHOD0(setActiveFog, bool());
|
||||
MOCK_METHOD1(setActiveFog, bool(MessageTypeTag));
|
||||
MOCK_METHOD0(unsetFogProxy, void());
|
||||
MOCK_METHOD0(loadFogProxy, void());
|
||||
MOCK_METHOD4(setActiveFog, bool(const string &, const uint16_t, const bool, MessageTypeTag));
|
||||
MOCK_METHOD0(getAccessToken, string());
|
||||
|
||||
MOCK_CONST_METHOD1(getProxyDomain, Maybe<std::string>(ProxyProtocol protocol));
|
||||
MOCK_CONST_METHOD1(getProxyCredentials, Maybe<std::string>(ProxyProtocol protocol));
|
||||
MOCK_CONST_METHOD1(getProxyPort, Maybe<uint16_t>(ProxyProtocol protocol));
|
||||
MOCK_CONST_METHOD1(getProxyExists, bool(ProxyProtocol protocol));
|
||||
MOCK_CONST_METHOD1(getProxyAddress, Maybe<std::string>(ProxyProtocol protocol));
|
||||
MOCK_METHOD0(loadProxy, Maybe<void>());
|
||||
};
|
||||
|
||||
#endif // __MOCK_MESSAGING_H__
|
||||
|
@@ -20,17 +20,42 @@
|
||||
#include "i_encryptor.h"
|
||||
#include "i_shell_cmd.h"
|
||||
#include "i_environment.h"
|
||||
#include "i_mainloop.h"
|
||||
#include "i_proxy_configuration.h"
|
||||
#include "singleton.h"
|
||||
#include "component.h"
|
||||
#include "enum_array.h"
|
||||
#include "agent_core_utilities.h"
|
||||
|
||||
class ProxyData
|
||||
{
|
||||
public:
|
||||
bool
|
||||
operator==(const ProxyData &other) const
|
||||
{
|
||||
return protocol==other.protocol &&
|
||||
domain==other.domain &&
|
||||
is_exists==other.is_exists &&
|
||||
port==other.port &&
|
||||
auth==other.auth;
|
||||
}
|
||||
|
||||
std::string protocol = "";
|
||||
std::string domain = "";
|
||||
std::string auth = "";
|
||||
bool is_exists = false;
|
||||
uint16_t port = 0;
|
||||
};
|
||||
|
||||
class AgentDetails
|
||||
:
|
||||
public Component,
|
||||
Singleton::Provide<I_AgentDetails>::SelfInterface,
|
||||
Singleton::Provide<I_ProxyConfiguration>::SelfInterface,
|
||||
Singleton::Consume<I_Encryptor>,
|
||||
Singleton::Consume<I_ShellCmd>,
|
||||
Singleton::Consume<I_Environment>
|
||||
Singleton::Consume<I_Environment>,
|
||||
Singleton::Consume<I_MainLoop>
|
||||
{
|
||||
public:
|
||||
AgentDetails() : Component("AgentDetails") {}
|
||||
@@ -39,15 +64,17 @@ public:
|
||||
|
||||
void init();
|
||||
|
||||
Maybe<std::string> getProxy() const;
|
||||
Maybe<std::string> getFogDomain() const;
|
||||
Maybe<uint16_t> getFogPort() const;
|
||||
std::string getAgentId() const;
|
||||
std::string getTenantId() const;
|
||||
std::string getProfileId() const;
|
||||
Maybe<std::string> getOpenSSLDir() const;
|
||||
std::string getClusterId() const;
|
||||
OrchestrationMode getOrchestrationMode() const;
|
||||
Maybe<std::string> getProxy() const;
|
||||
Maybe<std::string> getFogDomain() const;
|
||||
Maybe<uint16_t> getFogPort() const;
|
||||
std::string getAgentId() const;
|
||||
std::string getTenantId() const;
|
||||
std::string getProfileId() const;
|
||||
Maybe<std::string> getOpenSSLDir() const;
|
||||
std::string getClusterId() const;
|
||||
OrchestrationMode getOrchestrationMode() const;
|
||||
std::string getAccessToken() const;
|
||||
void loadAccessToken();
|
||||
|
||||
void setFogDomain(const std::string &_fog_domain) { fog_domain = _fog_domain; }
|
||||
void setFogPort(const uint16_t _fog_port) { fog_port = _fog_port; }
|
||||
@@ -67,6 +94,14 @@ public:
|
||||
void serialize(cereal::JSONInputArchive &ar);
|
||||
|
||||
void setClusterId(const std::string &_cluster_id);
|
||||
|
||||
Maybe<std::string> getProxyDomain(ProxyProtocol protocol) const;
|
||||
Maybe<std::string> getProxyCredentials(ProxyProtocol protocol) const;
|
||||
Maybe<uint16_t> getProxyPort(ProxyProtocol protocol) const;
|
||||
bool getProxyExists(ProxyProtocol protocol) const;
|
||||
Maybe<std::string> getProxyAddress(ProxyProtocol protocol) const;
|
||||
Maybe<void> loadProxy();
|
||||
|
||||
private:
|
||||
std::string fog_domain = "";
|
||||
std::string agent_id = "";
|
||||
@@ -77,13 +112,27 @@ private:
|
||||
std::string cluster_id = "";
|
||||
std::string filesystem_path = "/etc/cp";
|
||||
std::string log_files_path = "/var/log";
|
||||
std::string access_token = "";
|
||||
uint16_t fog_port = 0;
|
||||
bool encrypted_connection = false;
|
||||
OrchestrationMode orchestration_mode = OrchestrationMode::ONLINE;
|
||||
bool is_proxy_configured_via_settings = false;
|
||||
std::map<ProxyProtocol, ProxyData> proxies;
|
||||
|
||||
static const std::map<std::string, I_AgentDetails::MachineType> machineTypes;
|
||||
void registerMachineType();
|
||||
Maybe<I_AgentDetails::MachineType> getMachineTypeFromDmiTable();
|
||||
|
||||
std::string convertProxyProtocolToString(ProxyProtocol proto) const;
|
||||
Maybe<void> verifyProxySyntax(
|
||||
const std::string &protocol,
|
||||
const std::string &auth,
|
||||
const std::string &domain,
|
||||
const std::string &port,
|
||||
const std::string &env_proxy
|
||||
);
|
||||
Maybe<std::string> loadProxyType(const std::string &proxy_type);
|
||||
Maybe<void> loadProxyType(ProxyProtocol protocol);
|
||||
};
|
||||
|
||||
#endif // __AGENT_DETAILS_H__
|
||||
|
@@ -22,6 +22,7 @@
|
||||
|
||||
#include "component.h"
|
||||
#include "time_proxy.h"
|
||||
#include "time_print.h"
|
||||
#include "debug.h"
|
||||
#include "config_component.h"
|
||||
#include "mainloop.h"
|
||||
|
@@ -90,10 +90,12 @@ DEFINE_FLAG(D_COMPONENT, D_ALL)
|
||||
DEFINE_FLAG(D_WAAP_PARSER_RAW, D_WAAP_PARSER)
|
||||
DEFINE_FLAG(D_WAAP_PARSER_URLENCODE, D_WAAP_PARSER)
|
||||
DEFINE_FLAG(D_WAAP_PARSER_PHPSERIALIZE, D_WAAP_PARSER)
|
||||
DEFINE_FLAG(D_WAAP_PARSER_PERCENT, D_WAAP_PARSER)
|
||||
DEFINE_FLAG(D_WAAP_OVERRIDE, D_WAAP)
|
||||
|
||||
DEFINE_FLAG(D_IPS, D_COMPONENT)
|
||||
DEFINE_FLAG(D_FILE_UPLOAD, D_COMPONENT)
|
||||
DEFINE_FLAG(D_RATE_LIMIT, D_COMPONENT)
|
||||
|
||||
DEFINE_FLAG(D_PARSER, D_COMPONENT)
|
||||
DEFINE_FLAG(D_WS, D_COMPONENT)
|
||||
|
@@ -28,7 +28,7 @@ namespace Intelligence
|
||||
{
|
||||
|
||||
enum class ClassifierType { CLASS, CATEGORY, FAMILY, GROUP, ORDER, KIND };
|
||||
enum class ObjectType { ASSET, ZONE, POLICY_PACKAGE, CONFIGURATION, SESSION };
|
||||
enum class ObjectType { ASSET, ZONE, POLICY_PACKAGE, CONFIGURATION, SESSION, SHORTLIVED };
|
||||
|
||||
class Invalidation
|
||||
{
|
||||
|
@@ -11,8 +11,15 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
/// @file rest.h
|
||||
/// @brief Header file for RESTful communication functionalities.
|
||||
///
|
||||
/// This file defines classes and utilities for RESTful communication, including input/output handling,
|
||||
/// schema generation, and client-server interactions.
|
||||
|
||||
#ifndef __REST_H__
|
||||
#define __REST_H__
|
||||
|
||||
#include "cereal/archives/json.hpp"
|
||||
#include "cereal/types/common.hpp"
|
||||
#include "cereal/types/string.hpp"
|
||||
@@ -24,19 +31,32 @@
|
||||
|
||||
#include "debug.h"
|
||||
#include "maybe_res.h"
|
||||
|
||||
#include "rest/schema_printer.h"
|
||||
|
||||
/// @class JsonError
|
||||
/// @brief Class representing JSON parsing errors.
|
||||
///
|
||||
/// This class is used to represent errors that occur during JSON parsing.
|
||||
class JsonError
|
||||
{
|
||||
public:
|
||||
/// @brief Constructor for JsonError class.
|
||||
/// @param e The error message to be stored.
|
||||
JsonError(const std::string &e) : err(e) {}
|
||||
const std::string & getMsg() const { return err; }
|
||||
|
||||
/// @brief Retrieves the error message.
|
||||
/// @return The error message as a constant reference to a string.
|
||||
const std::string &getMsg() const { return err; }
|
||||
|
||||
private:
|
||||
std::string err;
|
||||
std::string err; ///< Error message.
|
||||
};
|
||||
|
||||
/// @class BasicRest
|
||||
/// @brief Base class for RESTful communication handling.
|
||||
///
|
||||
/// The BasicRest class provides basic functionalities for handling RESTful communication,
|
||||
/// including input/output handling, schema generation, and client-server interactions.
|
||||
class BasicRest
|
||||
{
|
||||
using InputFunc = std::function<void(cereal::JSONInputArchive &)>;
|
||||
@@ -44,16 +64,32 @@ class BasicRest
|
||||
using SchemaFunc = std::function<void(std::ostream &, int)>;
|
||||
|
||||
public:
|
||||
/// @brief Enumeration representing the direction of communication (Client to Server, Server to Client, or Both).
|
||||
enum class Direction { C2S, S2C, BOTH };
|
||||
|
||||
/// @brief Enumeration representing the type of parameter (Mandatory, Optional, or Default).
|
||||
enum class ParamType { MANDATORY, OPTIONAL, DEFAULT };
|
||||
|
||||
/// @brief Destructor for the BasicRest class.
|
||||
virtual ~BasicRest() = default;
|
||||
|
||||
void load(cereal::JSONInputArchive &ar) { for (auto it : input_funcs) it(ar); }
|
||||
/// @brief Loads data from the JSON input archive.
|
||||
/// @param ar The JSON input archive.
|
||||
void load(cereal::JSONInputArchive &ar) { for (auto it : input_funcs) it(ar); }
|
||||
|
||||
/// @brief Saves data to the JSON output archive.
|
||||
/// @param ar The JSON output archive.
|
||||
void save(cereal::JSONOutputArchive &ar) const { for (auto it : output_funcs) it(ar); }
|
||||
|
||||
/// @brief Outputs the schema to an output stream.
|
||||
/// @param out The output stream to write the schema to.
|
||||
/// @param level The indentation level for the schema.
|
||||
void performOutputingSchema(std::ostream &out, int level = 0);
|
||||
|
||||
/// @brief Adds a schema for the given REST parameter type.
|
||||
/// @tparam RestParamType The type of the REST parameter.
|
||||
/// @param label The label for the parameter in the schema.
|
||||
/// @param is_mandatory A boolean indicating whether the parameter is mandatory in the schema.
|
||||
template <typename RestParamType>
|
||||
void
|
||||
addSchema(const std::string &label, bool is_mandatory)
|
||||
@@ -67,6 +103,12 @@ public:
|
||||
if (is_mandatory) required.push_back(label);
|
||||
};
|
||||
|
||||
/// @brief Adds an input parameter of the given REST parameter type.
|
||||
/// @tparam RestParamType The type of the REST parameter.
|
||||
/// @param param The REST parameter to add as an input.
|
||||
/// @param label The label for the parameter in the input.
|
||||
/// @param type The parameter type (Mandatory, Optional, or Default).
|
||||
/// @param val The default value for the parameter (used for default parameters).
|
||||
template <typename RestParamType>
|
||||
void
|
||||
addInput(RestParam<RestParamType> ¶m, const std::string &label, ParamType type, const RestParamType &val)
|
||||
@@ -86,6 +128,12 @@ public:
|
||||
input_funcs.push_back(func);
|
||||
}
|
||||
|
||||
/// @brief Adds an output parameter of the given REST parameter type.
|
||||
/// @tparam RestParamType The type of the REST parameter.
|
||||
/// @param param The REST parameter to add as an output.
|
||||
/// @param label The label for the parameter in the output.
|
||||
/// @param type The parameter type (Mandatory, Optional, or Default).
|
||||
/// @param val The default value for the parameter (used for default parameters).
|
||||
template <typename RestParamType>
|
||||
void
|
||||
addOutput(RestParam<RestParamType> ¶m, const std::string &label, ParamType type, const RestParamType &val)
|
||||
@@ -111,37 +159,78 @@ private:
|
||||
void outputSchema(std::ostream &os, int level);
|
||||
void outputRequired(std::ostream &os, int level);
|
||||
|
||||
std::vector<InputFunc> input_funcs;
|
||||
std::vector<OutputFunc> output_funcs;
|
||||
std::vector<SchemaFunc> schema_func;
|
||||
std::vector<std::string> required;
|
||||
std::vector<InputFunc> input_funcs; ///< Vector storing input functions.
|
||||
std::vector<OutputFunc> output_funcs; ///< Vector storing output functions.
|
||||
std::vector<SchemaFunc> schema_func; ///< Vector storing schema functions.
|
||||
std::vector<std::string> required; ///< Vector storing the names of required parameters.
|
||||
};
|
||||
|
||||
/// @class ServerRest
|
||||
/// @brief Class representing a server-side RESTful communication handler.
|
||||
///
|
||||
/// The ServerRest class is used for server-side RESTful communication and provides
|
||||
/// functionality for handling REST calls.
|
||||
class ServerRest : public BasicRest
|
||||
{
|
||||
public:
|
||||
/// @brief Virtual function for handling a REST call.
|
||||
virtual void doCall() = 0;
|
||||
|
||||
/// @brief Performs the REST call using the input stream.
|
||||
/// @param in The input stream containing the JSON data for the REST call.
|
||||
/// @return A Maybe object containing the result of the REST call (either the JSON data or an error message).
|
||||
Maybe<std::string> performRestCall(std::istream &in);
|
||||
|
||||
protected:
|
||||
static constexpr bool isInput(BasicRest::Direction dir) { return dir != BasicRest::Direction::S2C; }
|
||||
/// @brief Determines if the direction is for input.
|
||||
/// @param dir The direction of the communication.
|
||||
/// @return True if the direction is for input, false otherwise.
|
||||
static constexpr bool isInput(BasicRest::Direction dir) { return dir != BasicRest::Direction::S2C; }
|
||||
|
||||
/// @brief Determines if the direction is for output.
|
||||
/// @param dir The direction of the communication.
|
||||
/// @return True if the direction is for output, false otherwise.
|
||||
static constexpr bool isOutput(BasicRest::Direction dir) { return dir != BasicRest::Direction::C2S; }
|
||||
|
||||
/// @brief Determines if the direction is for schema.
|
||||
/// @param dir The direction of the communication.
|
||||
/// @return True if the direction is for schema, false otherwise.
|
||||
static constexpr bool isSchema(BasicRest::Direction dir) { return dir != BasicRest::Direction::S2C; }
|
||||
};
|
||||
|
||||
/// @class ClientRest
|
||||
/// @brief Class representing a client-side RESTful communication handler.
|
||||
///
|
||||
/// The ClientRest class is used for client-side RESTful communication and provides
|
||||
/// functionality for generating and loading JSON data.
|
||||
class ClientRest : public BasicRest
|
||||
{
|
||||
public:
|
||||
/// @brief Generates JSON data from the object's state.
|
||||
/// @return A Maybe object containing the JSON data, or an error message if serialization fails.
|
||||
Maybe<std::string> genJson() const;
|
||||
|
||||
/// @brief Loads JSON data into the object's state.
|
||||
/// @param json The JSON data to be loaded.
|
||||
/// @return True if the JSON data is successfully loaded, false otherwise.
|
||||
bool loadJson(const std::string &json);
|
||||
|
||||
protected:
|
||||
static constexpr bool isInput(BasicRest::Direction dir) { return dir != BasicRest::Direction::C2S; }
|
||||
static constexpr bool isOutput(BasicRest::Direction dir) { return dir != BasicRest::Direction::S2C; }
|
||||
static constexpr bool isSchema(BasicRest::Direction) { return false; }
|
||||
};
|
||||
/// @brief Determines if the direction is for input.
|
||||
/// @param dir The direction of the communication.
|
||||
/// @return True if the direction is for input, false otherwise.
|
||||
static constexpr bool isInput(BasicRest::Direction dir) { return dir != BasicRest::Direction::C2S; }
|
||||
|
||||
/// @brief Determines if the direction is for output.
|
||||
/// @param dir The direction of the communication.
|
||||
/// @return True if the direction is for output, false otherwise.
|
||||
static constexpr bool isOutput(BasicRest::Direction dir) { return dir != BasicRest::Direction::S2C; }
|
||||
|
||||
/// @brief Determines if the direction is for schema.
|
||||
/// @param dir The direction of the communication.
|
||||
/// @return True if the direction is for schema, false otherwise.
|
||||
static constexpr bool isSchema(BasicRest::Direction) { return false; }
|
||||
};
|
||||
|
||||
template <bool is_input>
|
||||
class InputAdder
|
||||
@@ -303,34 +392,217 @@ private:
|
||||
std::map<std::string, N> obj;
|
||||
};
|
||||
|
||||
/// @def C2S_LABEL_PARAM(type, name, label)
|
||||
/// @brief Add a mandatory parameter for the Client-to-Server (C2S) direction.
|
||||
///
|
||||
/// This macro is used to add a mandatory parameter to a REST request sent from
|
||||
/// the client to the server.
|
||||
///
|
||||
/// @param type The data type of the parameter.
|
||||
/// @param name The variable name of the parameter.
|
||||
/// @param label The label or name of the parameter as it appears in the JSON data.
|
||||
#define C2S_LABEL_PARAM(type, name, label) \
|
||||
ADD_MANDATORY_PARAM(BasicRest::Direction::C2S, type, name, label)
|
||||
|
||||
/// @def S2C_LABEL_PARAM(type, name, label)
|
||||
/// @brief Add a mandatory parameter for the Server-to-Client (S2C) direction.
|
||||
///
|
||||
/// This macro is used to add a mandatory parameter to a REST response sent from
|
||||
/// the server to the client.
|
||||
///
|
||||
/// @param type The data type of the parameter.
|
||||
/// @param name The variable name of the parameter.
|
||||
/// @param label The label or name of the parameter as it appears in the JSON data.
|
||||
#define S2C_LABEL_PARAM(type, name, label) \
|
||||
ADD_MANDATORY_PARAM(BasicRest::Direction::S2C, type, name, label)
|
||||
|
||||
/// @def BOTH_LABEL_PARAM(type, name, label)
|
||||
/// @brief Add a mandatory parameter for both Client-to-Server (C2S) and Server-to-Client (S2C) directions.
|
||||
///
|
||||
/// This macro is used to add a mandatory parameter that is used in both the request
|
||||
/// and response of a REST communication.
|
||||
///
|
||||
/// @param type The data type of the parameter.
|
||||
/// @param name The variable name of the parameter.
|
||||
/// @param label The label or name of the parameter as it appears in the JSON data.
|
||||
#define BOTH_LABEL_PARAM(type, name, label) \
|
||||
ADD_MANDATORY_PARAM(BasicRest::Direction::BOTH, type, name, label)
|
||||
|
||||
/// @def C2S_PARAM(type, name)
|
||||
/// @brief Add a mandatory parameter for the Client-to-Server (C2S) direction with the parameter
|
||||
/// label being the same as the parameter name.
|
||||
///
|
||||
/// This macro is a shorthand for adding a mandatory parameter to a REST request with
|
||||
/// the same label as the parameter name.
|
||||
///
|
||||
/// @param type The data type of the parameter.
|
||||
/// @param name The variable name of the parameter.
|
||||
#define C2S_PARAM(type, name) C2S_LABEL_PARAM(type, name, #name)
|
||||
|
||||
/// @def S2C_PARAM(type, name)
|
||||
/// @brief Add a mandatory parameter for the Server-to-Client (S2C) direction with the parameter
|
||||
/// label being the same as the parameter name.
|
||||
///
|
||||
/// This macro is a shorthand for adding a mandatory parameter to a REST response with
|
||||
/// the same label as the parameter name.
|
||||
///
|
||||
/// @param type The data type of the parameter.
|
||||
/// @param name The variable name of the parameter.
|
||||
#define S2C_PARAM(type, name) S2C_LABEL_PARAM(type, name, #name)
|
||||
|
||||
/// @def BOTH_PARAM(type, name)
|
||||
/// @brief Add a mandatory parameter for both Client-to-Server (C2S) and Server-to-Client (S2C) directions
|
||||
/// with the parameter label being the same as the parameter name.
|
||||
///
|
||||
/// This macro is a shorthand for adding a mandatory parameter that is used in both the
|
||||
/// request and response of a REST communication with the same label as the parameter name.
|
||||
///
|
||||
/// @param type The data type of the parameter.
|
||||
/// @param name The variable name of the parameter.
|
||||
#define BOTH_PARAM(type, name) BOTH_LABEL_PARAM(type, name, #name)
|
||||
|
||||
/// @def C2S_LABEL_OPTIONAL_PARAM(type, name, label)
|
||||
/// @brief Add an optional parameter for the Client-to-Server (C2S) direction.
|
||||
///
|
||||
/// This macro is used to add an optional parameter to a REST request sent from the client
|
||||
/// to the server.
|
||||
///
|
||||
/// @param type The data type of the parameter.
|
||||
/// @param name The variable name of the parameter.
|
||||
/// @param label The label or name of the parameter as it appears in the JSON data.
|
||||
#define C2S_LABEL_OPTIONAL_PARAM(type, name, label) \
|
||||
ADD_OPTIONAL_PARAM(BasicRest::Direction::C2S, type, name, label)
|
||||
|
||||
/// @def S2C_LABEL_OPTIONAL_PARAM(type, name, label)
|
||||
/// @brief Add an optional parameter for the Server-to-Client (S2C) direction.
|
||||
///
|
||||
/// This macro is used to add an optional parameter to a REST response sent from the server
|
||||
/// to the client.
|
||||
///
|
||||
/// @param type The data type of the parameter.
|
||||
/// @param name The variable name of the parameter.
|
||||
/// @param label The label or name of the parameter as it appears in the JSON data.
|
||||
#define S2C_LABEL_OPTIONAL_PARAM(type, name, label) \
|
||||
ADD_OPTIONAL_PARAM(BasicRest::Direction::S2C, type, name, label)
|
||||
|
||||
/// @def BOTH_LABEL_OPTIONAL_PARAM(type, name, label)
|
||||
/// @brief Add an optional parameter for both Client-to-Server (C2S) and Server-to-Client (S2C) directions.
|
||||
///
|
||||
/// This macro is used to add an optional parameter that can be used in both the request
|
||||
/// and response of a REST communication.
|
||||
///
|
||||
/// @param type The data type of the parameter.
|
||||
/// @param name The variable name of the parameter.
|
||||
/// @param label The label or name of the parameter as it appears in the JSON data.
|
||||
#define BOTH_LABEL_OPTIONAL_PARAM(type, name, label) \
|
||||
ADD_OPTIONAL_PARAM(BasicRest::Direction::BOTH, type, name, label)
|
||||
|
||||
/// @def C2S_OPTIONAL_PARAM(type, name)
|
||||
/// @brief Add an optional parameter for the Client-to-Server (C2S) direction with the parameter label
|
||||
/// being the same as the parameter name.
|
||||
///
|
||||
/// This macro is a shorthand for adding an optional parameter to a REST request with
|
||||
/// the same label as the parameter name.
|
||||
///
|
||||
/// @param type The data type of the parameter.
|
||||
/// @param name The variable name of the parameter.
|
||||
#define C2S_OPTIONAL_PARAM(type, name) C2S_LABEL_OPTIONAL_PARAM(type, name, #name)
|
||||
|
||||
/// @def S2C_OPTIONAL_PARAM(type, name)
|
||||
/// @brief Add an optional parameter for the Server-to-Client (S2C) direction with the parameter label
|
||||
/// being the same as the parameter name.
|
||||
///
|
||||
/// This macro is a shorthand for adding an optional parameter to a REST response with
|
||||
/// the same label as the parameter name.
|
||||
///
|
||||
/// @param type The data type of the parameter.
|
||||
/// @param name The variable name of the parameter.
|
||||
#define S2C_OPTIONAL_PARAM(type, name) S2C_LABEL_OPTIONAL_PARAM(type, name, #name)
|
||||
|
||||
/// @def BOTH_OPTIONAL_PARAM(type, name)
|
||||
/// @brief Add an optional parameter for both Client-to-Server (C2S) and Server-to-Client (S2C) directions
|
||||
/// with the parameter label being the same as the parameter name.
|
||||
///
|
||||
/// This macro is a shorthand for adding an optional parameter that can be used in both the
|
||||
/// request and response of a REST communication with the same label as the parameter name.
|
||||
///
|
||||
/// @param type The data type of the parameter.
|
||||
/// @param name The variable name of the parameter.
|
||||
#define BOTH_OPTIONAL_PARAM(type, name) BOTH_LABEL_OPTIONAL_PARAM(type, name, #name)
|
||||
|
||||
/// @def C2S_LABEL_DEAFULT_PARAM(type, name, label, val)
|
||||
/// @brief Add a parameter with a default value for the Client-to-Server (C2S) direction.
|
||||
///
|
||||
/// This macro is used to add a parameter to a REST request with a default value. If the
|
||||
/// parameter is not provided in the request, the default value will be used.
|
||||
///
|
||||
/// @param type The data type of the parameter.
|
||||
/// @param name The variable name of the parameter.
|
||||
/// @param label The label or name of the parameter as it appears in the JSON data.
|
||||
/// @param val The default value of the parameter.
|
||||
#define C2S_LABEL_DEAFULT_PARAM(type, name, label, val) \
|
||||
ADD_DEFAULT_PARAM(BasicRest::Direction::C2S, type, name, label, val)
|
||||
|
||||
/// @def S2C_LABEL_DEAFULT_PARAM(type, name, label, val)
|
||||
/// @brief Add a parameter with a default value for the Server-to-Client (S2C) direction.
|
||||
///
|
||||
/// This macro is used to add a parameter to a REST response with a default value. If the
|
||||
/// parameter is not provided in the response, the default value will be used.
|
||||
///
|
||||
/// @param type The data type of the parameter.
|
||||
/// @param name The variable name of the parameter.
|
||||
/// @param label The label or name of the parameter as it appears in the JSON data.
|
||||
/// @param val The default value of the parameter.
|
||||
#define S2C_LABEL_DEAFULT_PARAM(type, name, label, val) \
|
||||
ADD_DEFAULT_PARAM(BasicRest::Direction::S2C, type, name, label, val)
|
||||
|
||||
/// @def BOTH_LABEL_DEAFULT_PARAM(type, name, label, val)
|
||||
/// @brief Add a parameter with a default value for both Client-to-Server (C2S) and Server-to-Client (S2C) directions.
|
||||
///
|
||||
/// This macro is used to add a parameter to a REST response with a default value. If the
|
||||
/// parameter is not provided in the response, the default value will be used.
|
||||
///
|
||||
/// @param type The data type of the parameter.
|
||||
/// @param name The variable name of the parameter.
|
||||
/// @param label The label or name of the parameter as it appears in the JSON data.
|
||||
/// @param val The default value of the parameter.
|
||||
#define BOTH_LABEL_DEAFULT_PARAM(type, name, label, val) \
|
||||
ADD_DEFAULT_PARAM(BasicRest::Direction::BOTH, type, name, label, val)
|
||||
|
||||
/// @def C2S_DEAFULT_PARAM(type, name, val)
|
||||
/// @brief Add a parameter with a default value for the Client-to-Server (C2S) direction with the parameter label
|
||||
/// being the same as the parameter name.
|
||||
///
|
||||
/// This macro is used to add a parameter to a REST request with
|
||||
/// the same label as the parameter name and a default value. If the
|
||||
/// parameter is not provided in the request, the default value will be used.
|
||||
/// @param type The data type of the parameter.
|
||||
/// @param name The variable name of the parameter.
|
||||
/// @param val The default value of the parameter.
|
||||
#define C2S_DEAFULT_PARAM(type, name, val) C2S_LABEL_DEAFULT_PARAM(type, name, #name, val)
|
||||
|
||||
/// @def C2S_DEAFULT_PARAM(type, name, val)
|
||||
/// @brief Add a parameter with a default value for the Server-to-Client (S2C) direction with the parameter
|
||||
/// label being the same as the parameter name.
|
||||
///
|
||||
/// This macro is used to add a parameter to a REST request with
|
||||
/// the same label as the parameter name and a default value. If the
|
||||
/// parameter is not provided in the request, the default value will be used.
|
||||
/// @param type The data type of the parameter.
|
||||
/// @param name The variable name of the parameter.
|
||||
/// @param val The default value of the parameter.
|
||||
#define S2C_DEAFULT_PARAM(type, name, val) S2C_LABEL_DEAFULT_PARAM(type, name, #name, val)
|
||||
|
||||
/// @def C2S_DEAFULT_PARAM(type, name, val)
|
||||
/// @brief Add a parameter with a default value for the Client-to-Server (C2S) and Server-to-Client (S2C) directions
|
||||
/// with the parameter label being the same as the parameter name.
|
||||
///
|
||||
/// This macro is used to add a parameter to a REST request with
|
||||
/// the same label as the parameter name and a default value. If the
|
||||
/// parameter is not provided in the request, the default value will be used.
|
||||
/// @param type The data type of the parameter.
|
||||
/// @param name The variable name of the parameter.
|
||||
/// @param val The default value of the parameter.
|
||||
#define BOTH_DEAFULT_PARAM(type, name, val) BOTH_LABEL_DEAFULT_PARAM(type, name, #name, val)
|
||||
|
||||
#endif // __REST_H__
|
||||
|
@@ -119,6 +119,10 @@ public:
|
||||
return !(*this > other);
|
||||
}
|
||||
|
||||
Maybe<std::string> calculateSubnetStart(int subnet_value);
|
||||
|
||||
Maybe<std::string> calculateSubnetEnd(int subnet_value);
|
||||
|
||||
// Checks if the IP address is in the range [left, right] inclusive.
|
||||
// All IPAddrs must be of the same kind, or the result is false.
|
||||
bool isInRange(const IPAddr &left, const IPAddr &right) const;
|
||||
@@ -149,6 +153,14 @@ private:
|
||||
// Additional fields to be used by ConnKey class and placed here to save space, IPAddr class should ignore them.
|
||||
IPProto proto;
|
||||
PortNumber port;
|
||||
|
||||
Maybe<std::string> calculateSubnetStartV4(int subnet_value);
|
||||
|
||||
Maybe<std::string> calculateSubnetEndV4(int subnet_value);
|
||||
|
||||
Maybe<std::string> calculateSubnetStartV6(int subnet_value);
|
||||
|
||||
Maybe<std::string> calculateSubnetEndV6(int subnet_value);
|
||||
};
|
||||
|
||||
namespace ConnKeyUtil {
|
||||
@@ -171,12 +183,30 @@ public:
|
||||
static Maybe<CustomRange<RangeType>>
|
||||
createRange(const std::string &maybe_range)
|
||||
{
|
||||
std::string start_range = maybe_range.substr(0, maybe_range.find("-"));
|
||||
std::string end_range = maybe_range.substr(maybe_range.find("-") + 1);
|
||||
if (end_range.empty()) {
|
||||
end_range = start_range;
|
||||
}
|
||||
std::string start_range;
|
||||
std::string end_range;
|
||||
size_t delimiter_position;
|
||||
|
||||
if ((delimiter_position = maybe_range.find("-")) != std::string::npos) {
|
||||
// If it's a range.
|
||||
start_range = maybe_range.substr(0, delimiter_position);
|
||||
end_range = maybe_range.substr(delimiter_position + 1);
|
||||
if (end_range.empty()) {
|
||||
end_range = start_range;
|
||||
}
|
||||
} else if ((delimiter_position = maybe_range.find("/")) != std::string::npos) {
|
||||
// If it's a subnet.
|
||||
IPAddr ip;
|
||||
ConnKeyUtil::fromString((maybe_range.substr(0, delimiter_position)), ip);
|
||||
std::string subnet = maybe_range.substr(delimiter_position + 1);
|
||||
int subnet_value = std::stoi(subnet);
|
||||
start_range = ip.calculateSubnetStart(subnet_value).unpack();
|
||||
end_range = ip.calculateSubnetEnd(subnet_value).unpack();
|
||||
} else {
|
||||
// If it's a single IP.
|
||||
start_range = maybe_range;
|
||||
end_range = maybe_range;
|
||||
}
|
||||
RangeType _start;
|
||||
if (!ConnKeyUtil::fromString(start_range, _start)) {
|
||||
return genError("Error in start value of custom range, value: " + start_range);
|
||||
@@ -188,7 +218,7 @@ public:
|
||||
}
|
||||
|
||||
if (_start > _end) {
|
||||
return genError("Error in creating custom range, invalid range: " + maybe_range);
|
||||
return genError("Error in creating custom range, invalid range: " + maybe_range);
|
||||
}
|
||||
|
||||
return CustomRange(_start, _end);
|
||||
|
Reference in New Issue
Block a user