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

[20274] Validate YAML tags on parsing #85

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Prev Previous commit
Apply suggested changes
Signed-off-by: Lucia Echevarria <luciaechevarria@eprosima.com>
LuciaEchevarria99 committed Sep 24, 2024
commit 2ad717f2f756d93fdc70339f68a9aae588c2e6b9
51 changes: 41 additions & 10 deletions ddspipe_yaml/src/cpp/YamlReader_features.cpp
Original file line number Diff line number Diff line change
@@ -130,13 +130,22 @@ void YamlReader::fill(
std::set<core::types::ParticipantId> dst;

// Required route source
src = get<core::types::ParticipantId>(route_yml, ROUTES_SRC_TAG, version);

if (object.routes.count(src) != 0)
if (is_tag_present(route_yml, ROUTES_SRC_TAG))
{
src = get<core::types::ParticipantId>(route_yml, ROUTES_SRC_TAG, version);

if (object.routes.count(src) != 0)
{
throw eprosima::utils::ConfigurationException(
utils::Formatter() <<
"Multiple routes defined for participant " << src << " : only one allowed.");
}
}
else
{
throw eprosima::utils::ConfigurationException(
utils::Formatter() <<
"Multiple routes defined for participant " << src << " : only one allowed.");
"Source participant required under tag " << ROUTES_SRC_TAG << " in route definition.");
}

// Optional route destination(s)
@@ -206,22 +215,44 @@ void YamlReader::fill(
{
YamlValidator::validate<core::TopicRoutesConfiguration>(topic_routes_yml, version);

// utils::Heritable<ddspipe::core::types::DistributedTopic> topic;
auto topic = utils::Heritable<ddspipe::core::types::DdsTopic>::make_heritable();
core::RoutesConfiguration routes;

// Required topic and type names
fill<eprosima::ddspipe::core::types::DdsTopic>(topic.get_reference(), topic_routes_yml, version);
if (!(is_tag_present(topic_routes_yml,
TOPIC_NAME_TAG) && is_tag_present(topic_routes_yml, TOPIC_TYPE_NAME_TAG)))
{
throw eprosima::utils::ConfigurationException(
utils::Formatter() <<
"Topic routes require topic and type names to be defined under tags " << TOPIC_NAME_TAG <<
" and " << TOPIC_TYPE_NAME_TAG << ", respectively.");
}
else
{
topic = get<utils::Heritable<ddspipe::core::types::DistributedTopic>>(topic_routes_yml, version);

if (object.topic_routes.count(topic) != 0)
{
throw eprosima::utils::ConfigurationException(
utils::Formatter() <<
"Multiple routes defined for topic " << topic << " : only one allowed.");
}
}

if (object.topic_routes.count(topic) != 0)
// Required routes
if (is_tag_present(topic_routes_yml, ROUTES_TAG))
{
routes = get<core::RoutesConfiguration>(topic_routes_yml, ROUTES_TAG, version);
}
else
{
throw eprosima::utils::ConfigurationException(
utils::Formatter() <<
"Multiple routes defined for topic " << topic << " : only one allowed.");
"No routes found under tag " << ROUTES_TAG << " for topic " << topic << " .");
}

// Required route
object.topic_routes[topic] = get<core::RoutesConfiguration>(topic_routes_yml, ROUTES_TAG, version);
// Insert routes
object.topic_routes[topic] = routes;
}
}

64 changes: 35 additions & 29 deletions ddspipe_yaml/src/cpp/YamlReader_generic.cpp
Original file line number Diff line number Diff line change
@@ -25,11 +25,6 @@
#include <ddspipe_core/types/dds/GuidPrefix.hpp>
#include <ddspipe_core/types/participant/ParticipantId.hpp>

#include <ddspipe_participants/configuration/DiscoveryServerParticipantConfiguration.hpp>
#include <ddspipe_participants/configuration/EchoParticipantConfiguration.hpp>
#include <ddspipe_participants/configuration/InitialPeersParticipantConfiguration.hpp>
#include <ddspipe_participants/configuration/ParticipantConfiguration.hpp>
#include <ddspipe_participants/configuration/SimpleParticipantConfiguration.hpp>
#include <ddspipe_participants/types/address/Address.hpp>
#include <ddspipe_participants/types/security/tls/TlsConfiguration.hpp>

@@ -290,30 +285,11 @@ std::string YamlReader::get<std::string>(

template <>
DDSPIPE_YAML_DllAPI
bool YamlValidator::validate<utils::Timestamp>(
const Yaml& yml,
const YamlReaderVersion& /* version */)
{
static const std::set<TagType> tags{
TIMESTAMP_DATETIME_FORMAT_TAG,
TIMESTAMP_LOCAL_TAG,
TIMESTAMP_DATETIME_TAG,
TIMESTAMP_MILLISECONDS_TAG,
TIMESTAMP_MICROSECONDS_TAG,
TIMESTAMP_NANOSECONDS_TAG};

return YamlValidator::validate_tags(yml, tags);
}

template <>
DDSPIPE_YAML_DllAPI
utils::Timestamp YamlReader::get<utils::Timestamp>(
void YamlReader::fill(
utils::Timestamp& object,
const Yaml& yml,
const YamlReaderVersion version /* version */)
const YamlReaderVersion version)
{
YamlValidator::validate<utils::Timestamp>(yml, version);

utils::Timestamp ret_timestamp;
std::string datetime_str;
std::string datetime_format("%Y-%m-%d_%H-%M-%S");
bool local = true;
@@ -339,7 +315,7 @@ utils::Timestamp YamlReader::get<utils::Timestamp>(
datetime_str = get<std::string>(yml, TIMESTAMP_DATETIME_TAG, version);
try
{
ret_timestamp = utils::string_to_timestamp(datetime_str, datetime_format, local);
object = utils::string_to_timestamp(datetime_str, datetime_format, local);
}
catch (const std::exception& e)
{
@@ -372,7 +348,37 @@ utils::Timestamp YamlReader::get<utils::Timestamp>(
ns = std::chrono::nanoseconds(get_nonnegative_int(yml, TIMESTAMP_NANOSECONDS_TAG));
}

return std::chrono::time_point_cast<utils::Timestamp::duration>(ret_timestamp + ms + us + ns);
object = std::chrono::time_point_cast<utils::Timestamp::duration>(object + ms + us + ns);
}

template <>
DDSPIPE_YAML_DllAPI
bool YamlValidator::validate<utils::Timestamp>(
const Yaml& yml,
const YamlReaderVersion& /* version */)
{
static const std::set<TagType> tags{
TIMESTAMP_DATETIME_FORMAT_TAG,
TIMESTAMP_LOCAL_TAG,
TIMESTAMP_DATETIME_TAG,
TIMESTAMP_MILLISECONDS_TAG,
TIMESTAMP_MICROSECONDS_TAG,
TIMESTAMP_NANOSECONDS_TAG};

return YamlValidator::validate_tags(yml, tags);
}

template <>
DDSPIPE_YAML_DllAPI
utils::Timestamp YamlReader::get<utils::Timestamp>(
const Yaml& yml,
const YamlReaderVersion version /* version */)
{
YamlValidator::validate<utils::Timestamp>(yml, version);

utils::Timestamp object;
fill<utils::Timestamp>(object, yml, version);
return object;
}

} /* namespace yaml */
2 changes: 1 addition & 1 deletion ddspipe_yaml/src/cpp/YamlReader_participants.cpp
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ namespace yaml {
using namespace eprosima::ddspipe::core::types;
using namespace eprosima::ddspipe::participants::types;

/************************
/***********************
* PARTICIPANTS *
************************/

6 changes: 3 additions & 3 deletions ddspipe_yaml/src/cpp/YamlReader_types.cpp
Original file line number Diff line number Diff line change
@@ -523,17 +523,17 @@ void YamlReader::fill(
{
if (is_tag_present(yml, LOG_FILTER_ERROR_TAG))
{
object[utils::VerbosityKind::Error] = get<std::string>(yml, LOG_FILTER_ERROR_TAG, version);
object.error = get<std::string>(yml, LOG_FILTER_ERROR_TAG, version);
}

if (is_tag_present(yml, LOG_FILTER_WARNING_TAG))
{
object[utils::VerbosityKind::Warning] = get<std::string>(yml, LOG_FILTER_WARNING_TAG, version);
object.warning = get<std::string>(yml, LOG_FILTER_WARNING_TAG, version);
}

if (is_tag_present(yml, LOG_FILTER_INFO_TAG))
{
object[utils::VerbosityKind::Info] = get<std::string>(yml, LOG_FILTER_INFO_TAG, version);
object.info = get<std::string>(yml, LOG_FILTER_INFO_TAG, version);
}
}